-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
62 lines (52 loc) · 1.79 KB
/
game.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
import logging
import json
from time import time
class NullHandler(logging.Handler):
def emit(self, record):
pass
h = NullHandler()
class Game():
log = logging.getLogger("game")
log.addHandler(h)
game_info = {'player_count': 2}
def __init__(self):
self.players = {}
self.time = time()
# self.alive = None
# self.waiting_on = None
# self.time = time()
#
# def start(self):
# self.alive = [x for x in range(1, Game.game_info['player_count']+1)]
# self.waiting_on = self.alive[:]
# self.time = time()
def update_timeout(self):
self.time = time()
Game.log.debug('timeout updated to %s' % self.time)
def timeout(self):
return time() - self.time > Game.game_info['timeout']
class Player():
log = logging.getLogger("player")
log.addHandler(h)
def __init__(self, sock, name, protocol=0):
self.sock = sock
self.name = name
self.protocol = protocol
#self.player_id = None
def __repr__(self):
#return 'Player(%d): %s' % (self.sock.fileno(), self.name)
return 'Player: %s' % self.name
def send(self, msg):
Player.log.debug('Sending to player %s: %s' % (self, msg))
if self.protocol == 0:
self.sock.sendall(str(msg))
if type(msg) == str and not msg.startswith('INFO'):
self.sock.sendall('go\n')
elif self.protocol == 1:
self.sock.sendall(json.dumps(msg))
def send_errors(self, errors):
if self.protocol == 0:
for error in errors:
self.sock.sendall('INFO %s\n' % error)
elif self.protocol == 1:
self.sock.sendall(json.dumps({'errors': errors}) + '\n')