| 1 | """ |
|---|
| 2 | usage: client.py <hostname> <port> <table_name> <user_id> <client_id> |
|---|
| 3 | """ |
|---|
| 4 | import sys |
|---|
| 5 | import cjson |
|---|
| 6 | from twisted.internet.protocol import ClientFactory |
|---|
| 7 | from twisted.internet import reactor |
|---|
| 8 | from twisted.protocols.basic import NetstringReceiver |
|---|
| 9 | from pprint import PrettyPrinter |
|---|
| 10 | from string import split |
|---|
| 11 | |
|---|
| 12 | pprinter = PrettyPrinter().pprint |
|---|
| 13 | |
|---|
| 14 | def makeAction( action, value): |
|---|
| 15 | msg = { "method":"action", |
|---|
| 16 | "action":action, |
|---|
| 17 | "value":value } |
|---|
| 18 | return msg |
|---|
| 19 | |
|---|
| 20 | class PokerClientProtocol(NetstringReceiver): |
|---|
| 21 | def dealWithData(self,data): |
|---|
| 22 | print "---------------" |
|---|
| 23 | pprinter(data) |
|---|
| 24 | if data["stat"] == "fail": |
|---|
| 25 | print "error?" |
|---|
| 26 | return |
|---|
| 27 | if data["you"]["required_action"]: |
|---|
| 28 | print "---------------" |
|---|
| 29 | print "We need some input. Possible actions:" |
|---|
| 30 | p_acts = data["you"]["possible_actions"] |
|---|
| 31 | vals = [None,None] |
|---|
| 32 | while not vals[0] in p_acts: |
|---|
| 33 | pprinter(p_acts) |
|---|
| 34 | str = raw_input('Action? format: <action> OR <action>,<value>:\n') |
|---|
| 35 | vals = split(str,",") |
|---|
| 36 | vals.append(None) |
|---|
| 37 | str = cjson.encode(makeAction(vals[0], vals[1])) |
|---|
| 38 | print "Sending %s" % str |
|---|
| 39 | self.sendString( str ) |
|---|
| 40 | |
|---|
| 41 | def __init__(self,table_name, user_id, client_id ): |
|---|
| 42 | self.table_name = table_name |
|---|
| 43 | self.user_id = user_id |
|---|
| 44 | self.client_id = client_id |
|---|
| 45 | |
|---|
| 46 | def connectionMade(self): |
|---|
| 47 | login = {"method":"connect", |
|---|
| 48 | "user_id":self.user_id, |
|---|
| 49 | "client_id":self.client_id, |
|---|
| 50 | "table_name":self.table_name} |
|---|
| 51 | self.sendString(cjson.encode(login)) |
|---|
| 52 | |
|---|
| 53 | def stringReceived(self, line): |
|---|
| 54 | |
|---|
| 55 | decoded = cjson.decode(line) |
|---|
| 56 | self.dealWithData(decoded) |
|---|
| 57 | |
|---|
| 58 | def connectionLost(self, reason): |
|---|
| 59 | pass |
|---|
| 60 | |
|---|
| 61 | class PokerClientFactory(ClientFactory): |
|---|
| 62 | protocol = PokerClientProtocol |
|---|
| 63 | |
|---|
| 64 | def buildProtocol( self, addr ): |
|---|
| 65 | p = self.protocol(self.table_name, |
|---|
| 66 | self.user_id, |
|---|
| 67 | self.client_id) |
|---|
| 68 | p.factory = self |
|---|
| 69 | return p |
|---|
| 70 | def __init__( self, table_name, user_id, client_id ): |
|---|
| 71 | self.table_name = table_name |
|---|
| 72 | self.user_id = user_id |
|---|
| 73 | self.client_id = client_id |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | def clientConnectionFailed(self, connector, reason): |
|---|
| 77 | print 'connection failed:', reason.getErrorMessage() |
|---|
| 78 | reactor.stop() |
|---|
| 79 | |
|---|
| 80 | def clientConnectionLost(self, connector, reason): |
|---|
| 81 | print 'connection lost:', reason.getErrorMessage() |
|---|
| 82 | reactor.stop() |
|---|
| 83 | def main(): |
|---|
| 84 | if len(sys.argv) != 6: |
|---|
| 85 | print __doc__ |
|---|
| 86 | sys.exit(0) |
|---|
| 87 | |
|---|
| 88 | hostname = sys.argv[1] |
|---|
| 89 | port = sys.argv[2] |
|---|
| 90 | table_name = sys.argv[3] |
|---|
| 91 | user_id = sys.argv[4] |
|---|
| 92 | client_id = sys.argv[5] |
|---|
| 93 | |
|---|
| 94 | factory = PokerClientFactory(table_name, user_id, client_id ) |
|---|
| 95 | |
|---|
| 96 | # 8007 is the port you want to run under. Choose something >1024 |
|---|
| 97 | reactor.connectTCP(hostname,int(port),factory) |
|---|
| 98 | reactor.run() |
|---|
| 99 | |
|---|
| 100 | if __name__ == "__main__": |
|---|
| 101 | main() |
|---|
| 102 | |
|---|