-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
141 lines (114 loc) · 4.45 KB
/
server.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import tornado.ioloop
import tornado.web
import tornado.websocket
from GameState import GameState
from time import sleep
from SoundManager import SoundManager
from QuestRoom import QuestRoom
import platform
if platform.system() == 'Windows':
from KeyboardListener import KeyboardListener
from tornado.options import define, options, parse_command_line
import json
BUTTONS_NUM = 4
define("port", default=8888, help="run on the given port", type=int)
clients = dict()
quest_room = None
sound_manager = None
keyboard_listener = None
all_buttons = [
{'title': 'Глюкало', 'id': 0},
{'title': 'Трюндель', 'id': 1},
{'title': 'Пресло', 'id': 2},
{'title': 'Коковник', 'id': 3},
{'title': 'Бедодька', 'id': 4},
{'title': 'Сорвалец', 'id': 5},
{'title': 'Глевло', 'id': 6},
{'title': 'Плевло', 'id': 7},
]
used_buttons = []
class IndexHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
def get(self):
self.render('index.html')
class DashboardHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
def get(self):
self.render('dashboard.html')
class WebSocketHandler(tornado.websocket.WebSocketHandler):
def open(self, *args):
self.id = self.get_argument("Id")
self.stream.set_nodelay(True)
if self.id in clients:
del clients[self.id]
clients[self.id] = { "id": self.id, "object": self }
id_str = str(self.id)
# print("quest last_sended_messages: {}".format(quest_room.last_sended_messages))
init_data = {'init': 'True'}
self.write_message(init_data)
if id_str in quest_room.last_sended_messages:
last_data = quest_room.last_sended_messages[id_str]
self.write_message(last_data)
quest_room.send_state(None)
def on_message(self, jsonMessage):
message = json.loads(jsonMessage)
print(message)
print(message['message'])
if "Time end" in message['message']:
# pass
print("We receive: {} and {}".format(message['message'], message['id']))
quest_room.progress_bar_zero(message['id'])
if "play_sound" == message['message']:
sound_id = message['sound']
sound_manager.play_sound(sound_id)
if "door" == message['message']:
door_id = int(message['door_id'])
door_state = message['state']
quest_room.set_door_state(door_id, door_state)
if "box" == message['message']:
box_id = int(message['box_id'])
box_state = message['state']
quest_room.set_box_state(box_id, box_state)
if "get_state" == message['message']:
quest_room.send_state()
if "skip_task" in message['message']:
if message['task_id'].isdigit():
task_id = int(message['task_id'])
quest_room.toggle_skiped_task(task_id)
if "light" == message['message']:
quest_room.turn_light(message['light_id'])
if "set_room_light" == message['message']:
room_led = message['room_led_id']
rgb_color_str = message['color']
rgb_color = [int(char_h + char_l, 16) for char_h, char_l in zip(rgb_color_str[0::2], rgb_color_str[1::2])]
# print("We receive set_room_light with room_led_id: {} and color {} = {}".format(room_led, rgb_color_str, rgb_color))
quest_room.set_room_light(room_led, rgb_color)
if "radio" == message['message']:
radio_action = int(message['radioAction'])
quest_room.turn_radio(radio_action)
def on_close(self):
if self.id not in clients: return
del clients[self.id]
def get_buttons(self, device_id):
return all_buttons[(device_id-1)*BUTTONS_NUM:device_id*BUTTONS_NUM]
app = tornado.web.Application([
(r'/', IndexHandler),
(r'/dashboard', DashboardHandler),
(r'/socket', WebSocketHandler),
],
static_path=os.path.join(os.path.dirname(__file__), "static"),
autoreload=True,
)
if __name__ == '__main__':
parse_command_line()
app.listen(options.port)
sound_manager = SoundManager()
sound_manager.daemon = True
sound_manager.start()
quest_room = QuestRoom(clients)
quest_room.sound_manager = sound_manager
quest_room.start()
tornado.ioloop.IOLoop.instance().start()