-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.py
55 lines (40 loc) · 1.18 KB
/
controller.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
from abc import ABCMeta
import pickle
class Controller(object, metaclass=ABCMeta):
_ctrl = None
players = None
active_player = None
force_exit = False
active_connections = None
COMMANDS = {
'NO_ACTION': '0',
'GET_INPUT': '1',
'KILL_CONN': '2',
}
def __init__(self):
assert not self._ctrl
self.active_connections = []
@classmethod
def get(cls):
if not Controller._ctrl:
Controller._ctrl = cls()
return Controller._ctrl
def current_player(self):
return self.players[self.active_player]
def next_turn(self):
self.active_player = (self.active_player + 1) % len(self.players)
def main_loop(self):
while True:
self.current_player().process_input()
if self.force_exit:
break
self.exit_game()
def exit_game(self):
for conn in self.active_connections:
data_dict = {
'data': '',
'command': self.COMMANDS['KILL_CONN'],
}
data = pickle.dumps(data_dict)
conn.sendall(data)
conn.close()