-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcpserver.py
434 lines (376 loc) · 16.1 KB
/
tcpserver.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
#!/usr/bin/env python
import select
import socket
import sys
import os
import signal
import platform
from PlanetWars.PlanetWars import PlanetWars
from game import Player
from time import time
import logging
import json
import threading
# create console handler and set level to debug
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
# create formatter
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
# add formatter to ch
ch.setFormatter(formatter)
# create logger
log = logging.getLogger('tcp')
log.setLevel(logging.INFO)
# add ch to logger
log.addHandler(ch)
gamelog = logging.getLogger('game')
gamelog.setLevel(logging.DEBUG)
gamelog.addHandler(ch)
gamedatalog = logging.getLogger('gamedata')
gamedatalog.setLevel(logging.DEBUG)
gamedatalog.addHandler(ch)
ServerGame = PlanetWars
server_player_count = ServerGame.game_info['player_count']
BUFSIZ = 4096
from game import Game
from math import ceil, sqrt
from time import time
import json
import logging
from collections import Counter, deque, defaultdict
log = logging.getLogger("game.PlanetWars")
class TCPGame():
def __init__(self, game):
self.game = game
pass
def send_moves(self, player, moves):
pass
def in_progress(self):
return True
def run_game(self, botcmds, timeoutms, loadtimeoutms, num_turns=1000,
output_file="testout.txt", verbose=False, serial=False):
try:
f = open('bot1.txt', 'w')
# create bot sandboxes
bots = [Sandbox(*bot) for bot in botcmds]
for b, bot in enumerate(bots):
if not bot.is_alive:
print('bot %s did not start' % botcmds[b])
self.game.kill_player(b)
if output_file:
of = open(output_file, "w")
of.write(self.game.get_state())
of.flush()
print('running for %s turns' % num_turns)
for turn in range(num_turns+1):
print('turn %s' % turn)
try:
if turn == 1:
self.game.start_game()
# send game state to each player
for b, bot in enumerate(bots):
if self.game.is_alive(b):
if turn == 0:
bot.write(self.game.get_player_start(b) + 'ready\n')
if b == 0:
f.write(self.game.get_player_start(b))
f.flush()
else:
bot.write(self.game.get_player_state(b) + 'go\n')
if b == 0:
f.write(self.game.get_player_state(b))
f.flush()
if turn > 0:
self.game.start_turn()
# get moves from each player
if turn == 0:
time_limit = float(loadtimeoutms) / 1000
else:
time_limit = float(timeoutms) / 1000
start_time = time.time()
bot_finished = [not self.game.is_alive(b) for b in range(len(bots))]
bot_moves = ['' for b in bots]
# 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):
for b, bot in enumerate(bots):
if bot_finished[b]:
continue # already got bot moves
if not bot.is_alive:
print('bot died')
bot_finished[b] = True
self.game.kill_player(b)
continue # bot is dead
line = bot.read_line()
if line is None:
continue
line = line.strip()
if line.lower() == 'go':
bot_finished[b] = True
else:
bot_moves[b] += line + '\n'
# process all moves
if turn > 0 and not self.game.game_over():
self.game.do_all_moves(bot_moves)
self.game.finish_turn()
except:
traceback.print_exc()
print("Got an error running the bots.")
raise
if output_file:
of.write(self.game.get_state())
of.flush()
if verbose:
stats = self.game.get_stats()
s = 'turn %4d stats: '
for key, values in stats:
s += '%s: %s' % (key, values)
sys.stderr.write("\r%-50s" % s)
alive = [self.game.is_alive(b) for b in range(len(bots))]
#print('alive %s' % alive)
if sum(alive) <= 1:
break
self.game.finish_game()
#print(game.get_state())
finally:
for bot in bots:
if bot.is_alive:
bot.kill()
if output_file:
of.close()
return "Game Over, %s" % self.game.get_scores()
f.close()
class TCPGameServer(object):
def __init__(self, game_data, game_data_lock, port=2010, backlog=5, main_thread=False):
self.game_data = game_data
self.game_data_lock = game_data_lock
self.clients = []
self.clientmap = {}
self.waiting = 0
self.players_waiting = []
log.info('game id starting at %s' % (self.game_data.last_game_id + 1))
self.games = {}
self.gamemap = {}
# tcp binding options
self.port = port
self.backlog = backlog
self.running = False
self.force_shutdown = False
self.main_thread = main_thread
self.bind()
def bind(self):
self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.server.bind(('',self.port))
log.info('Listening to port %d ...' % self.port)
self.server.listen(self.backlog)
# Trap keyboard interrupts
if self.main_thread:
signal.signal(signal.SIGINT, self.sighandler)
def sighandler(self, signum, frame):
self.running = False
self.force_shutdown = False
def shutdown(self):
# Close the server
log.info('Shutting down server...')
# Close existing client sockets
for o in self.clients:
o.close()
self.server.close()
def select_map(self):
map_name = os.path.join('PlanetWars','boards','original','map7.txt')
data = []
f = open(map_name, 'r')
for line in f:
data.append(line)
f.close()
return map_name, data
def select_players(self):
# select players and remove from waiting list
players = []
for i in range(server_player_count):
players.append(self.players_waiting.pop(0))
return players
def create_game(self):
# create a new game
game_number = self.game_data.get_next_game_id()
game = ServerGame()
map_name, map_data = self.select_map()
game.parse_map_old(map_data)
self.games[game_number] = game
players = self.select_players()
player_id = 0
for player in players:
player_id += 1
game.players[player_id] = player
self.gamemap[player] = (player_id, game, game_number)
self.waiting -= server_player_count
log.info('created game %d with map %s with players %s' % (game_number, map_name, players))
self.send_game_state(game)
def process_player_data(self, player, data):
# process an individual player's moves for a game
player_id, game, game_id = self.gamemap[player]
info = game.parse_player_data(data, player.protocol)
log.debug('parsed player data: %s' % info)
if info.has_key('crashed') and info['crashed'] == True:
log.info('%d reported crash' % player)
game.kick_player(player_id)
player.sock.shutdown(socket.SHUT_RD)
elif info.has_key('errors') and len(info['errors']) > 0:
log.info('kicking %d in game %d because of move parse errors' % (player_id, game_id))
game.kick_player(player_id)
player.send_errors(info['errors'])
player.sock.shutdown(socket.SHUT_RD)
else:
errors = game.input_moves(player_id, info['moves'])
if errors != None and len(errors) > 0:
player.send_errors(errors)
if ServerGame.game_info['kick_invalid']:
log.info('kicking %d in game %d because of an invalid move' % (player_id, game_id))
game.kick_player(player_id)
player.sock.shutdown(socket.SHUT_RD)
def send_game_state(self, game):
for player_id, player in game.players.items():
game_state = game.get_game_state_old(player_id)
player.send(game_state)
log.debug('sending game state:\n\n%s\n' % game_state)
log.debug('timeout set to %d' % ServerGame.game_info['timeout'])
def send_results(self, game):
try:
results = game.get_results()
for player_id, player in game.players.items():
player.send(results)
player.sock.close()
self.clients.remove(player.sock)
del self.clientmap[player.sock]
del self.gamemap[player]
log.debug('sending game results:\n\n%s\n' % results)
except:
quit()
def check_games(self):
for game_id, game in self.games.items():
if game.ready_to_process():
game.process_turn()
if not game.finished():
self.send_game_state(game)
game.update_timeout()
elif game.timeout():
for player_id, player in game.players.items():
if player.sock != None and player_id in game.waiting_on:
log.info('kicking player %d in game %d because of timeout' % (player_id, game_id))
game.kick_player(player_id)
player.send('INFO TIMEOUT')
player.sock.shutdown(socket.SHUT_RD)
remove_games = []
for game_id, game in self.games.items():
if game.finished():
log.info('game %d is finished' % game_id)
self.send_results(game)
remove_games.append(game_id)
try:
f = open(os.path.join('games','%d.game' % game_id), 'w')
f.write(game.get_history_string())
f.close
except Exception, e:
log.exception(e)
quit()
self.game_data_lock.acquire()
self.game_data.save_game(game_id,
game.get_results(),
game.get_history_string(),
{}) #p_id for p_id, p in game.players.items()})
self.game_data_lock.release()
for game_id in remove_games:
log.debug("removing game %d from system" % game_id)
del self.games[game_id]
def serve(self):
inputs = [self.server]
self.running = True
last_update = time()
while self.running or (len(self.clients) > 0 and not self.force_shutdown):
if (time() - last_update) >= 10.0:
log.info('%d connections, %d games' % (len(self.clients), len(self.games)))
last_update = time()
try:
self.check_games()
except Exception, e:
log.exception(e)
try:
inputready,outputready,exceptready = select.select([self.server] + self.clients, [], [], 0.1)
except select.error, e:
log.exception(e)
break
except socket.error, e:
log.exception(e)
break
for s in inputready:
if s == self.server:
# handle the server socket
client, address = self.server.accept()
self.clients.append(client)
log.info('tcpgameserver: got connection %d from %s' % (client.fileno(), address))
# Read the login name
player_data = client.recv(4096)
log.debug("received data from client: %s" % (player_data))
if type(player_data) == dict:
player = Player(client, **player_data)
elif type(player_data) == list:
player = Player(client, *player_data)
elif type(player_data) == str:
data = player_data.split()
player = Player(client, data[1])
self.clientmap[client] = player
log.debug('created player: %s' % player)
self.waiting += 1
self.players_waiting.append(player)
# create new game if needed
if self.waiting >= server_player_count:
self.create_game()
else:
# handle all other sockets
player = self.clientmap[s]
try:
data = s.recv(BUFSIZ)
if data:
log.debug("received data from %s:\n %s" % (player, data))
self.process_player_data(player, data)
else:
log.info('tcpgameserver: %d hung up' % s.fileno())
s.close()
inputs.remove(s)
self.outputs.remove(s)
except socket.error, e:
# Remove
log.warning('client socket error: %s' % e)
if s in inputs:
inputs.remove(s)
if s in self.outputs:
self.outputs.remove(s)
if s in self.clients_waiting:
self.players_waiting.remove(s)
self.waiting -= 1
if s in self.gamemap:
game_id, game = self.gamemap[s]
game.kick_player(player.player_id)
self.shutdown()
def main(game_data, game_data_lock, main_thread=False):
import serveroptions
(options, args) = serveroptions.get_options(sys.argv)
log.debug(options)
tcp = TCPGameServer(game_data, game_data_lock, main_thread=main_thread)
if options.test_game:
game = ServerGame()
map_name, map_data = tcp.select_map()
log.info('testing map %s' % map_name)
game.parse_map_old(map_data)
else:
try:
tcp.serve()
except KeyboardInterrupt:
pass
if __name__ == "__main__":
from gamedata import GameData
game_data = GameData('PlanetWars')
game_data_lock = threading.Lock()
main(game_data, game_data_lock, True)