-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathengine.py
445 lines (407 loc) · 17.8 KB
/
engine.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
#!/usr/bin/env python
#~ from __future__ import print_function
import time
import traceback
import os
import random
import sys
import json
import io
if sys.version_info >= (3,):
def unicode(s):
return s
import logging
#~ from sandbox import get_sandbox
#~ from tcpserver import get_sandbox
class HeadTail(object):
'Capture first part of file write and discard remainder'
def __init__(self, file, max_capture=510):
self.file = file
self.max_capture = max_capture
self.capture_head_len = 0
self.capture_head = unicode('')
self.capture_tail = unicode('')
def write(self, data):
if self.file:
self.file.write(data)
capture_head_left = self.max_capture - self.capture_head_len
if capture_head_left > 0:
data_len = len(data)
if data_len <= capture_head_left:
self.capture_head += data
self.capture_head_len += data_len
else:
self.capture_head += data[:capture_head_left]
self.capture_head_len = self.max_capture
self.capture_tail += data[capture_head_left:]
self.capture_tail = self.capture_tail[-self.max_capture:]
else:
self.capture_tail += data
self.capture_tail = self.capture_tail[-self.max_capture:]
def flush(self):
if self.file:
self.file.flush()
def close(self):
if self.file:
self.file.close()
def head(self):
return self.capture_head
def tail(self):
return self.capture_tail
def headtail(self):
if self.capture_head != '' and self.capture_tail != '':
sep = unicode('\n..\n')
else:
sep = unicode('')
return self.capture_head + sep + self.capture_tail
def run_game(game, bots, options):
# file descriptors for replay and streaming formats
replay_log = options.get('replay_log', None)
stream_log = options.get('stream_log', None)
verbose_log = options.get('verbose_log', None )
# file descriptors for bots, should be list matching # of bots
input_logs = options.get('input_logs', [None]*len(bots))
output_logs = options.get('output_logs', [None]*len(bots))
error_logs = options.get('error_logs', [None]*len(bots))
#~ error_logs = options.get('error_logs', bots)
capture_errors = options.get('capture_errors', False)
turns = int(options['turns'])
loadtime = float(options['loadtime']) / 1000
turntime = float(options['turntime']) / 1000
strict = options.get('strict', False)
end_wait = options.get('end_wait', 0.0)
location = options.get('location', 'localhost')
game_id = options.get('game_id', 0)
error = ''
#~ bots = []
bot_status = []
bot_turns = []
if capture_errors:
error_logs = [HeadTail(log) for log in error_logs]
try:
#~ # create bot sandboxes
for b, bot in enumerate(bots):
#~ bot_cwd, bot_cmd = bot
#~ sandbox = get_sandbox(bot_cwd,
#~ secure=options.get('secure_jail', None))
#~ sandbox.start(bot_cmd)
#~ bots.append(sandbox)
bot_status.append('survived')
bot_turns.append(0)
# ensure it started
if not bot.sock:
bot_status[-1] = 'crashed'
if verbose_log:
verbose_log.write('bot %s did not start\n' % b)
print('ERR : bot %s did not start\n' % b)
game.kill_player(b)
#~ sandbox.pause()
#~ bot_status = ['survived']*len(bots)
if stream_log:
stream_log.write(game.get_player_start())
stream_log.flush()
if verbose_log:
verbose_log.write('running for %s turns\n' % turns)
for turn in range(turns+1):
#~ print turn, bots
if turn == 0:
game.start_game()
# send game state to each player
for b, bot in enumerate(bots):
if game.is_alive(b):
#~ print "bot " , b
if turn == 0:
start = game.get_player_start(b) + 'ready\n'
#~ print start
bot.write(start)
if input_logs and input_logs[b]:
input_logs[b].write(start)
input_logs[b].flush()
else:
state = 'turn ' + str(turn) + '\n' + game.get_player_state(b) + 'go\n'
bot.write(state)
#~ print state
if input_logs and input_logs[b]:
input_logs[b].write(state)
input_logs[b].flush()
bot_turns[b] = turn
if turn > 0:
if stream_log:
stream_log.write('turn %s\n' % turn)
stream_log.write('score %s\n' % ' '.join([str(s) for s in game.get_scores()]))
stream_log.write(game.get_state())
stream_log.flush()
game.start_turn()
# get moves from each player
if turn == 0:
time_limit = loadtime
else:
time_limit = turntime
if options.get('serial', False):
simul_num = int(options['serial']) # int(True) is 1
else:
simul_num = len(bots)
bot_moves = [[] for b in bots]
error_lines = [[] for b in bots]
statuses = [None for b in bots]
bot_list = [(b, bot) for b, bot in enumerate(bots)
if game.is_alive(b)]
#~ print simul_num, bot_list
random.shuffle(bot_list)
for group_num in range(0, len(bot_list), simul_num):
pnums, pbots = zip(*bot_list[group_num:group_num + simul_num])
moves, errors, status = get_moves(game, pbots, pnums,
time_limit, turn)
for p, b in enumerate(pnums):
bot_moves[b] = moves[p]
error_lines[b] = errors[p]
statuses[b] = status[p]
# handle any logs that get_moves produced
for b, errors in enumerate(error_lines):
if errors:
#~ print "ERRORS", bots[b].name, bots[b].game_id, errors, game.is_alive(b), statuses[b], bot_status[b]
if error_logs and error_logs[b]:
error_logs[b].write(unicode('\n').join(errors)+unicode('\n'))
# set status for timeouts and crashes
for b, status in enumerate(statuses):
if status != None:
bot_status[b] = status
bot_turns[b] = turn
# process all moves
bot_alive = [game.is_alive(b) for b in range(len(bots))]
if turn > 0 and not game.game_over():
for b, moves in enumerate(bot_moves):
if game.is_alive(b):
#~ bots[b].write( "INFO: %d %s game:%d\n" % (b,bots[b].name,bots[b].game_id) )
valid, ignored, invalid = game.do_moves(b, moves)
if output_logs and output_logs[b]:
output_logs[b].write('# turn %s\n' % turn)
if valid:
if output_logs and output_logs[b]:
output_logs[b].write('\n'.join(valid)+'\n')
output_logs[b].flush()
if ignored:
for inv in ignored:
bots[b].write('INFO: ignored ' + str(inv) +'\n')
if error_logs and error_logs[b]:
error_logs[b].write('turn %4d bot %s ignored actions:\n' % (turn, b))
error_logs[b].write('\n'.join(ignored)+'\n')
error_logs[b].flush()
if output_logs and output_logs[b]:
output_logs[b].write('\n'.join(ignored)+'\n')
output_logs[b].flush()
if invalid:
for inv in invalid:
bots[b].write('INFO: invalid ' + str(inv) +'\n')
if strict:
game.kill_player(b)
bot_status[b] = 'invalid'
bot_turns[b] = turn
if error_logs and error_logs[b]:
error_logs[b].write('turn %4d bot %s invalid actions:\n' % (turn, b))
error_logs[b].write('\n'.join(invalid)+'\n')
error_logs[b].flush()
if output_logs and output_logs[b]:
output_logs[b].write('\n'.join(invalid)+'\n')
output_logs[b].flush()
if turn > 0:
game.finish_turn()
# send ending info to eliminated bots
bots_eliminated = []
for b, alive in enumerate(bot_alive):
if alive and not game.is_alive(b):
bots_eliminated.append(b)
for b in bots_eliminated:
if verbose_log:
verbose_log.write('turn %4d bot %s eliminated\n' % (turn, b))
if bot_status[b] == 'survived': # could be invalid move
bot_status[b] = 'eliminated'
bot_turns[b] = turn
#~ score_line ='score %s\n' % ' '.join([str(s) for s in game.get_scores(b)])
#~ status_line = 'status %s\n' % ' '.join(map(str, game.order_for_player(b, bot_status)))
#~ end_line = 'end\nplayers %s\n' % len(bots) + score_line + status_line
#~ state = end_line + game.get_player_state(b) + 'go\n'
state = 'end\ngame ' + str(bots[b].game_id) + ": " + str(bot_status[b]) + " score: " + str(game.get_scores(b)[0]) + " turn: " + str(turn) + "\ngo\n"
bots[b].write(state)
if input_logs and input_logs[b]:
input_logs[b].write(state)
input_logs[b].flush()
if end_wait:
bots[b].resume()
if bots_eliminated and end_wait:
if verbose_log:
verbose_log.write('waiting {0} seconds for bots to process end turn\n'.format(end_wait))
time.sleep(end_wait)
for b in bots_eliminated:
bots[b].kill()
if verbose_log:
stats = game.get_stats()
stat_keys = sorted(stats.keys())
s = 'turn %4d stats: ' % turn
if turn % 50 == 0:
verbose_log.write(' '*len(s))
for key, values in stats.items():
verbose_log.write(' {0:^{1}}'.format(key, max(len(key), len(str(values)))))
verbose_log.write('\n')
verbose_log.write(s)
for key, values in stats.items():
verbose_log.write(' {0:^{1}}'.format(values, max(len(key), len(str(values)))))
verbose_log.write('\n')
#alive = [game.is_alive(b) for b in range(len(bots))]
#if sum(alive) <= 1:
if game.game_over():
break
# send bots final state and score, output to replay file
game.finish_game()
score_line ='score %s\n' % ' '.join(map(str, game.get_scores()))
status_line = 'status %s\n' % ' '.join(map(str,bot_status))
end_line = 'end\nplayers %s\n' % len(bots) + score_line + status_line
#~ print end_line
if stream_log:
stream_log.write(end_line)
stream_log.write(game.get_state())
stream_log.flush()
if verbose_log:
verbose_log.write(score_line)
verbose_log.write(status_line)
verbose_log.flush()
for b, bot in enumerate(bots):
if (game.is_alive(b)) and (bots[b].sock!=None):
#~ score_line ='score %s\n' % ' '.join([str(s) for s in game.get_scores(b)])
#~ status_line = 'status %s\n' % ' '.join(map(str, game.order_for_player(b, bot_status)))
#~ status_line += 'playerturns %s\n' % ' '.join(map(str, bot_turns))
#~ end_line = 'end\nplayers %s\n' % len(bots) + score_line + status_line
state = 'end\ngame ' + str(bots[b].game_id) + ": " + str(bot_status[b]) + " score: " + str(game.get_scores(b)[0]) + " turn: " + str(turn) + "\ngo\n"
#~ state = end_line + game.get_player_state(b) + 'go\n'
bot.write(state)
if input_logs and input_logs[b]:
input_logs[b].write(state)
input_logs[b].flush()
except Exception as e:
# TODO: sanitize error output, tracebacks shouldn't be sent to workers
error = traceback.format_exc()
if verbose_log:
verbose_log.write(traceback.format_exc())
#error = str(e)
finally:
if end_wait:
for bot in bots:
bot.resume()
if verbose_log:
verbose_log.write('waiting {0} seconds for bots to process end turn\n'.format(end_wait))
time.sleep(end_wait)
for bot in bots:
if bot.is_alive:
bot.kill()
bot.release()
if error:
game_result = { 'error': error }
else:
scores = game.get_scores()
game_result = {
'challenge': game.__class__.__name__.lower(),
'location': location,
'game_id': game_id,
'status': bot_status,
'playerturns': bot_turns,
'score': scores,
'rank': [sorted(scores, reverse=True).index(x) for x in scores],
'replayformat': 'json',
'replaydata': game.get_replay(),
'game_length': turn
}
if capture_errors:
game_result['errors'] = [head.headtail() for head in error_logs]
if replay_log:
json.dump(game_result, replay_log, sort_keys=True)
return game_result
def get_moves(game, bots, bot_nums, time_limit, turn):
bot_finished = [not game.is_alive(bot_nums[b]) for b in range(len(bots))]
bot_moves = [[] for b in bots]
error_lines = [[] for b in bots]
statuses = [None for b in bots]
start_time = time.time()
# resume all bots
for bot in bots:
if bot.is_alive:
bot.resume()
# loop until received all bots send moves or are dead
# or when time is up
while (sum(bot_finished) < len(bot_finished) and
time.time() - start_time < time_limit):
time.sleep(0.01)
for b, bot in enumerate(bots):
if bot_finished[b]:
continue # already got bot moves
if not bot.is_alive:
error_lines[b].append( unicode('turn %4d bot %s crashed') % (turn, bot_nums[b]))
statuses[b] = 'crashed'
line = bot.read_error()
while line != None:
error_lines[b].append(line)
line = bot.read_error()
bot_finished[b] = True
game.kill_player(bot_nums[b])
continue # bot is dead
# read a maximum of 100 lines per iteration
for x in range(100):
line = bot.read_line()
if line is None:
# stil waiting for more data
break
line = line.strip()
if line.lower() == 'go':
bot_finished[b] = True
# bot finished sending data for this turn
break
bot_moves[b].append(line)
for x in range(100):
line = bot.read_error()
if line is None:
break
error_lines[b].append(line)
# pause all bots again
for bot in bots:
if bot.is_alive:
bot.pause()
# check for any final output from bots
for b, bot in enumerate(bots):
if bot_finished[b]:
continue # already got bot moves
if not bot.is_alive:
error_lines[b].append(unicode('turn %4d bot %s crashed') % (turn, bot_nums[b]))
statuses[b] = 'crashed'
line = bot.read_error()
while line != None:
error_lines[b].append(line)
line = bot.read_error()
bot_finished[b] = True
game.kill_player(bot_nums[b])
continue # bot is dead
line = bot.read_line()
while line is not None and len(bot_moves[b]) < 40000:
line = line.strip()
if line.lower() == 'go':
bot_finished[b] = True
# bot finished sending data for this turn
break
bot_moves[b].append(line)
line = bot.read_line()
line = bot.read_error()
while line is not None and len(error_lines[b]) < 1000:
error_lines[b].append(line)
line = bot.read_error()
# kill timed out bots
for b, finished in enumerate(bot_finished):
if not finished:
error_lines[b].append(unicode('turn %4d bot %s timed out') % (turn, bot_nums[b]))
statuses[b] = 'timeout'
bot = bots[b]
for x in range(100):
line = bot.read_error()
if line is None:
break
error_lines[b].append(line)
game.kill_player(bot_nums[b])
bots[b].kill()
return bot_moves, error_lines, statuses