-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcontroller.py
98 lines (81 loc) · 3.59 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
import os
import sys
import threading
import traceback
from queue import Queue
from threading import Thread
from AI import AI
from model import Message
from model import ServerConstants
from network import Network
from world import World
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
# change with switcher
def handle_message(self, message):
if message[ServerConstants.KEY_TYPE] == ServerConstants.MESSAGE_TYPE_INIT:
self.world._handle_init_message(message[ServerConstants.KEY_INFO])
new_world = World(world=self.world)
threading.Thread(target=self.launch_on_thread, args=(self.client.pick, new_world)).start()
elif message[ServerConstants.KEY_TYPE] == ServerConstants.MESSAGE_TYPE_TURN:
new_world = World(world=self.world)
new_world._handle_turn_message(message[ServerConstants.KEY_INFO])
threading.Thread(target=self.launch_on_thread, args=(self.client.turn, new_world)).start()
elif message[ServerConstants.KEY_TYPE] == ServerConstants.MESSAGE_TYPE_SHUTDOWN:
new_world = World(world=self.world)
new_world._handle_turn_message(message[ServerConstants.KEY_INFO]["turnMessage"])
scores_map = new_world._handle_end_message(message[ServerConstants.KEY_INFO]["scores"])
self.client.end(new_world, scores_map)
self.terminate()
def launch_on_thread(self, action, world):
try:
action(world)
except Exception as e:
print("Error in client:")
traceback.print_exc()
# print(e)
world._queue.put(Message(type=ServerConstants.MESSAGE_TYPE_END_TURN, turn=world.get_current_turn(), info={}))
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:
message = self.queue.get()
self.queue.task_done()
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 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 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
if __name__ == '__main__':
c = Controller()
if len(sys.argv) > 1 and sys.argv[1] == '--verbose':
World.DEBUGGING_MODE = True
c.start()