/
warconomy_cl.py
executable file
·90 lines (71 loc) · 2.57 KB
/
warconomy_cl.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env python
import sys
from twisted.internet import stdio, reactor
from twisted.internet.protocol import ClientFactory
from twisted.protocols.basic import LineReceiver
class ConsoleClient(LineReceiver):
delimiter = '\n'
def connectionMade(self):
self.sendLine("co client conn made (cl)")
def lineReceived(self, line):
if not line: return
#self.sendLine("co client line recvd: '%s'" % line)
if line == 'q':
reactor.stop()
server_client.send_to_server(line)
def connectionLost(self, reason):
#self.sendLine('client conn lost: %s' % reason.getErrorMessage())
reactor.stop()
def connectionFailed(self, reason):
#self.sendLine('client conn failed: %s' % reason.getErrorMessage())
reactor.stop()
player_id = 0
class ServerClient(LineReceiver):
def connectionMade(self):
print 'sv client conn made'
print 'sv client logging in as player_id %i' % player_id
self.sendLine('login %i' % player_id)
self.sendLine('ui')
def connectionLost(self, reason):
print 'sv client conn lost: %s' % reason
#reactor.stop()
def lineReceived(self, line):
#print "sv client line recvd: '%s'" % line
print line.strip()
def send_to_server(self, msg):
self.sendLine(msg)
server_client = None
class ServerClientFactory(ClientFactory):
protocol = ServerClient
def startedConnecting(self, connector):
print 'scf started connecting (%s)' % connector
def clientConnectionLost(self, connector, reason):
print 'scf client conn lost: %s' % reason.getErrorMessage()
def clientConnectionFailed(self, connector, reason):
print 'scf client conn failed: %s' % reason.getErrorMessage()
def buildProtocol(self, addr):
global server_client
server_client = self.protocol()
return server_client
def main():
global player_id
print 'Warconomy client'
ip = 'localhost'
port = 9876
if len(sys.argv) > 1:
args = sys.argv[1:]
for arg in args:
if arg.startswith('host='):
addr = arg.split('=')[1]
addr_pcs = addr.split(':')
ip = addr_pcs[0]
port = int(addr_pcs[1])
if arg.startswith('id='):
player_id = int(arg.split('=')[1])
stdio.StandardIO(ConsoleClient())
print 'connecting to host server at %s:%i' % (ip,port)
reactor.connectTCP(ip,port,ServerClientFactory())
reactor.run()
print 'exiting main()'
if __name__ == "__main__":
main()