-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcpclient.py
180 lines (146 loc) · 4.8 KB
/
tcpclient.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/usr/bin/env python
import time
import sys
import threading
import re
import string
import random
import subprocess
import shlex
from socket import socket, AF_INET, SOCK_STREAM
USAGE="""
tcpclient.py host_or_ip port botpath player_nick password [num_rounds]
if running on windows or if the botpath contains spaces,
you have to wrap it with "", eg.: "java /x/y/MyBot", "e:\\ai\\bots\\mybot.exe"
player_nick and password may only contain ascii_letters, '_', and numbers
"""
def readline(sock):
s=""
while(sock):
c = sock.recv(1)
if ( not c ):
break
elif ( c=='\r' ):
continue
elif ( c=='\n' ):
break
else:
s += c
return s
time_out = 1.0
def tcp(host, port, bot_command, user, password, options):
global time_out
# spread out if in batch mode, to allow more random ordering on the server
time.sleep(time_out + 5.0 * random.random())
# Start up the network connection
sock = socket(AF_INET, SOCK_STREAM)
sock.settimeout(240)
sock.connect((host, port))
if sock:
sys.stderr.write("\n\nconnected to %s:%d as %s\n" % (host,port,user))
else:
sys.stderr.write("\n\nfailed to connect to %s:%d as %s\n" % (host,port,user))
return
# send greetz
sock.sendall("USER %s %s\n" % (user, password) )
# start bot
try:
import os
args = shlex.split(bot_command)
bot = subprocess.Popen(args,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
cwd=".")
except:
print( 'your bot ('+str(bot_command)+') failed to start!' )
return;
try:
while sock:
end_reached = False
# get input, send it to bot
bot_input = ""
while sock:
try:
line = readline(sock)
except: return
if not line:
if end_reached:
sock.close()
sock = None
bot.kill()
return
continue # bad connection, keep on trying
print( line )
if line.startswith("INFO:"): # not meant for the bot
if (line.find("already running")>0) or (line.find("already queued")>0):
## penalty for getting eliminated, but still trying to be first in the upcoming game.
time_out += 10.0 + 10.0*random.random()
continue
bot_input += line + "\n"
if line.startswith("end"):
end_reached = True
if line.startswith("ready"):
time_out = 1.0
break
if line.startswith("go"):
if end_reached:
sock.close()
sock = None
bot.kill()
return
break
if not sock:
break
bot.stdin.write(bot_input)
bot.stdin.flush()
# get bot output, send to serv
client_mess=""
while 1:
answer = bot.stdout.readline()
if not answer: break
client_mess += answer
if answer.startswith("go"): break
# if there's no orders, send at least an empty line
if (client_mess==""):
client_mess="\r\n"
print( client_mess )
sock.sendall( client_mess )
finally:
try:
bot.kill()
except:
pass
def check_string( pname, use ):
""" check for invalid chars since json won't like them. """
for l in pname:
if l in string.letters: continue
if l in string.digits : continue
if l =='_' : continue
print( "your "+use+" (" + pname + ") contains invalid characters, please choose another one!" )
return False
return True
def main():
if len(sys.argv) < 6:
print USAGE
return
host = sys.argv[1]
port = int(sys.argv[2])
botpath = sys.argv[3]
pname = sys.argv[4]
password = sys.argv[5]
if not check_string(pname, "botname"):
return
if not check_string(password, "password"):
return
try:
rounds = int(sys.argv[6])
except:
rounds = -1
if rounds == -1:
while True:
tcp(host, port, botpath, pname, password, {})
else:
for i in range(rounds):
tcp(host, port, botpath, pname, password, {})
if __name__ == "__main__":
main()