-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtakai_wrapper.py
346 lines (302 loc) · 8.09 KB
/
takai_wrapper.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# NOTES:
# This is an almost exact copy of Chaitanya Vadrevu's ShlktBot wrapper!
# I have made only very minor modifications to hook this up to my AI.
# As a result, I can take no credit for this code.
#
# Chaitanya's original code can be found here:
# https://github.com/chaitu236/ShlktBot-wrapper
#
# Also, make sure to check out Chaitanya's PlayTak.com website!
#
# - Josh Achiam
import subprocess
import socket
import re
import argparse
import sys
import sched, time
from threading import Thread
from time import sleep
from socket import error as socket_error
sock=None
gameno=''
args=''
sc=None
connected=False
debug = True
def read_line():
data=''
global sock
while True:
c = sock.recv(1)
if len(c) == 0:
sock = None
connected = False
print 'Connection closed by server. Waiting 30 seconds before re-connecting'
time.sleep(30)
thread.exit()
if c=='\n' or c=='':
break
else:
data += c
if debug and data!="OK":
print '= '+data
return data
def send(msg):
global sock
if debug and msg!="PING":
print '* '+msg
try:
if sock != None and connected:
sock.sendall(msg+'\n')
else:
print 'Not sending since sock=None or not connected ', sock, connected
except socket_error as e:
print 'Socket error when trying to send ', e, sock, connected
def post_seek(size, time):
send('Seek '+str(size)+' '+str(time) + ' 0')
def bot_to_server(move):
move = move.strip()
#[T0, X]: ai move => c3
move = move.split(' ')[2][:-1]
match = re.match(r'f?[a-h][1-8]$', move)
if match:
return 'P '+move[1:].upper()
match = re.match(r'c([a-h][1-8])$', move)
if match:
return 'P '+match.group(1).upper()+' C'
match = re.match(r's([a-h][1-8])$', move)
if match:
return 'P '+match.group(1).upper()+' W'
match = re.match(r'([1-8])?([a-h])([1-8])([><\-+])$', move)
if match:
fl = ord(match.group(2))
rw = int(match.group(3))
sym = match.group(4)
print 'sym='+sym
fadd=0
radd=0
if sym=='<':
fadd=-1
elif sym =='>':
fadd=1
elif sym =='+':
radd = 1
elif sym=='-':
radd = -1
return 'M '+chr(fl).upper()+str(rw)+' '+chr(fl+fadd).upper()+''+str(rw+radd)+' 1'
match = re.match(r'([1-8])([a-h])([1-8])([><\-+])([1-8]+)$', move)
if match:
stsz = int(match.group(1))
fl = ord(match.group(2))
rw = int(match.group(3))
sym = match.group(4)
stk = match.group(5)
fadd=0
radd=0
if sym=='<':
fadd=-1*len(stk)
elif sym =='>':
fadd=1*len(stk)
elif sym =='+':
radd = 1*len(stk)
elif sym=='-':
radd = -1*len(stk)
msg = 'M '+chr(fl).upper()+str(rw)+' '+chr(fl+fadd).upper()+''+str(rw+radd)
for i in stk:
msg = msg+' '+i
return msg
return 'Not match'
#ai move => Cb4
print 'not implemented!!'
def wait_for_response(resp):
k=read_line()
while (resp not in k and "NOK" not in k):
k=read_line()
return k
def server_to_bot(move):
#if 'RequestUndo' in move:
# return 'undo'
spl = move.split(' ')
#Game#1 P A4 (C|W)
if spl[1] == 'P':
stone='f'
if len(spl) == 4:
if spl[3]=='W':
stone='s'
else:
stone='c'
return stone+spl[2].lower()
#Game#1 M A2 A5 2 1
elif spl[1] == 'M':
fl1 = ord(spl[2][0])
rw1 = int(spl[2][1])
fl2 = ord(spl[3][0])
rw2 = int(spl[3][1])
dir=''
if fl2==fl1:
if rw2>rw1:
dir='+'
else:
dir='-'
else:
if fl2>fl1:
dir='>'
else:
dir='<'
lst=''
liftsize=0
for i in range(4, len(spl)):
lst = lst+spl[i]
liftsize = liftsize+int(spl[i])
##there's an ambiguity here.. is the start sq. empty??.. lets find out
#send('Game#'+gameno+' Show '+spl[2])
#msg = wait_for_response('Game#'+gameno+' Show Sq')
#if 'NOK' in msg:
# return 'Over'
#Game#1 Show Sq [f]
#origsq = len(msg.split(' ')[3])-2
prefix=liftsize
return str(prefix)+spl[2].lower()+dir+lst
return ''
def is_white_turn(move_no):
return (move_no%2)==0
def read_game_move(game_no):
gm = 'Game#'+game_no
while(True):
msg = read_line()
for move in ['M', 'P', 'Abandoned', 'Over', 'Show']:#, 'RequestUndo']:
if(msg.startswith(gm+' '+move)):
return msg
def read_bot_move(p):
while(True):
move = p.stdout.readline()
print move
if 'AI move' in move:
return move
elif 'Game over' in move:
return ''
print 'something wrong!'
"""def check_for_undo_request(game_no):
gm = 'Game#'+game_no
flag = True
while(flag):
msg = read_line()
print 'i live here now'
if msg.startswith(gm+' RequestUndo'):
return msg
if msg == '':
return ''
"""
def bot(no, is_bot_white, size, opponent):
p = subprocess.Popen('exec th run_AI.lua ' + str(is_bot_white) + ' ' + str(size) + ' ' + opponent,
shell=True, bufsize=0, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
print 'color', no, 'iswhite?', is_bot_white
breakflag = False
move_no = 0
while(True):
#read from bot, write to server
if is_white_turn(move_no) == is_bot_white:
move=read_bot_move(p)
if move=='':
break;
"""msgs = check_for_undo_request(no)
if 'RequestUndo' in msgs:
send('Game#'+no+' RequestUndo')
p.stdin.write('undo2\n')
p.stdin.flush()
move_no = move_no - 2
else:"""
send('Game#'+no+' '+bot_to_server(move))
#read from server, write to bot
else:
print 'reading game move'
msg = read_game_move(no)
"""if 'RequestUndo' in msg:
send('Game#'+no+' RequestUndo')"""
if 'Abandoned' in msg or 'Over' in msg:
break;
msg = server_to_bot(msg)
if 'Over' in msg:
break;
print '> '+msg
p.stdin.write(msg+'\n')
p.stdin.flush()
move_no = move_no+1
p.kill()
def run():
send('Client TakaiBot0')
send('Login '+args.user+' '+args.password)
line = read_line()
if(line.startswith("Welcome")==False and line.startswith("You're already")==False):
return #sys.exit()
while(True):
post_seek(args.size, args.time)
msg=read_line()
while(msg.startswith("Game Start")!=True):
msg=read_line()
if msg.startswith("Login or Register"):
sys.exit()
#if msg.startswith("Seek new"):
# spl = msg.split(' ')
# if spl[3] == 'TakticianBot':
# send('Accept ' + spl[2])
#Game Start no. size player_white vs player_black yourcolor
print 'game started!'+msg
spl = msg.split(' ')
if spl[4] == 'dove_queen' or spl[6] == 'dove_queen':
send('Shout hi patsy :D')
send('Shout hey everyone, this is my best friend patsy!')
opponent = ''
if spl[7]=="white":
opponent = spl[6]
else:
opponent = spl[4]
#send('Shout lets do our best to play a beautiful game!')
global gameno
gameno = spl[2]
print 'gameno='+gameno
bot(gameno, spl[7]=="white", args.size, opponent)
send('Shout gg')
send('quit')
def args():
parser = argparse.ArgumentParser(description='This is a demo script by nixCraft.')
parser.add_argument('-u','--user', help='User',required=True)
parser.add_argument('-p','--password', help='Password',required=True)
parser.add_argument('-s','--size', help='Board Size',required=True)
parser.add_argument('-t','--time', help='Time in seconds per player',required=True)
global args
args = parser.parse_args()
def pinger():
send("PING")
sc.enter(30, 1, pinger, ())
def _startpinger():
global sc
sc = sched.scheduler(time.time, time.sleep)
sc.enter(10, 1, pinger, ())
sc.run()
def startpinger():
thread = Thread(target = _startpinger, args=())
thread.start()
if __name__ == "__main__":
global sock
args()
server_addr = ('playtak.com', 10000)
startpinger()
while(True):
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(server_addr)
connected = True
read_line()
read_line()
try:
run()
finally:
sock.close()
print 'Sleep it off.'
time.sleep(5)
pass
except socket_error:
print 'Socket error. Retrying in 10 seconds'
time.sleep(10)