-
Notifications
You must be signed in to change notification settings - Fork 3
/
Controller.py
99 lines (86 loc) · 4.06 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
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
import os
import sys
import threading
__author__ = 'pezzati'
from Network import *
from AI import AI
from threading import Thread
from queue import Queue
class Controller:
def __init__(self):
self.sending_flag = True
self.conf = {}
self.network = None
self.queue = Queue()
self.world = World(queue=self.queue)
self.client = AI()
self.argNames = ["AICHostIP", "AICHostPort", "AICToken", "AICRetryDelay"]
self.argDefaults = ["127.0.0.1", 7099, "00000000000000000000000000000000", "1000"]
self.turn_num = 0
self.preprocess_flag = False
def start(self):
self.read_settings()
self.network = Network(ip=self.conf[self.argNames[0]],
port=int(self.conf[self.argNames[1]]),
token=self.conf[self.argNames[2]],
message_handler=self.handle_message)
self.network.connect()
def run():
while self.sending_flag:
event = self.queue.get()
self.queue.task_done()
message = {
'name': Event.EVENT,
'args': [{'type': event.type, 'args': event.args}]
}
if World.DEBUGGING_MODE and World.LOG_FILE_POINTER is not None:
World.LOG_FILE_POINTER.write('------send message to server-----\n ' + message.__str__())
self.network.send(message)
Thread(target=run, daemon=True).start()
def terminate(self):
if World.LOG_FILE_POINTER is not None:
World.LOG_FILE_POINTER.write('finished')
World.LOG_FILE_POINTER.flush()
World.LOG_FILE_POINTER.close()
print("finished!")
self.network.close()
self.sending_flag = False
def read_settings(self):
if os.environ.get(self.argNames[0]) is None:
for i in range(len(self.argNames)):
self.conf[self.argNames[i]] = self.argDefaults[i]
else:
for i in range(len(self.argNames)):
self.conf[self.argNames[i]] = os.environ.get(self.argNames[i])
def handle_message(self, message):
if message[ServerConstants.KEY_NAME] == ServerConstants.MESSAGE_TYPE_INIT:
self.world._handle_init_message(message)
threading.Thread(target=self.launch_on_thread, args=(self.client.preprocess, 'init', self.world, [])).start()
elif message[ServerConstants.KEY_NAME] == ServerConstants.MESSAGE_TYPE_PICK:
new_world = World(world=self.world)
new_world._handle_pick_message(message)
threading.Thread(target=self.launch_on_thread, args=(self.client.pick, 'pick', new_world,
[new_world.current_turn])).start()
elif message[ServerConstants.KEY_NAME] == ServerConstants.MESSAGE_TYPE_TURN:
new_world = World(world=self.world)
new_world._handle_turn_message(message)
if new_world.current_phase == Phase.MOVE:
threading.Thread(target=self.launch_on_thread, args=(self.client.move, 'move', new_world,
[new_world.current_turn,
new_world.move_phase_num])).start()
elif new_world.current_phase == Phase.ACTION:
threading.Thread(target=self.launch_on_thread, args=(self.client.action, 'action', new_world,
[new_world.current_turn])).start()
elif message[ServerConstants.KEY_NAME] == ServerConstants.MESSAGE_TYPE_SHUTDOWN:
self.terminate()
def launch_on_thread(self, action, name, new_world, args):
try:
action(new_world)
except Exception as e:
print(e)
new_world.queue.put(Event(name + '-end', args))
if __name__ == '__main__':
c = Controller()
if len(sys.argv) > 1 and sys.argv[1] == '--verbose':
World.DEBUGGING_MODE = True
c.start()