-
Notifications
You must be signed in to change notification settings - Fork 0
/
host.py
executable file
·153 lines (128 loc) · 4.63 KB
/
host.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
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/env python
import os
import sys
import signal
import time
from mqtt_wrapper import MqttWrapperFactory
from GameStarter import GameStarter
from functools import wraps
from threading import Event, Thread
from services import Service
class HostFactory:
def mqtt_factory(config):
return MqttWrapperFactory(topic_prefix=config['root_topic'])
def game_starter(config):
return GameStarter(config.get('game_start_delay', 5.0), config.get('game_join_delay', 1.0), config.get('game_leave_delay', 0.5))
def game_runner(config, game_subtopic, consoles):
return GameRunner(HostFactory.mqtt_factory(config), game_subtopic, consoles)
class Lobby(Service):
def __init__(self, config, mqtt_factory):
super(Lobby, self).__init__()
self.mqtt = mqtt_factory.new()
self.config = config
self.next_game_id = 0
def get_next_game_id(self):
this_id = "game_%d" % (self.next_game_id)
self.next_game_id += 1
return this_id
def handle_join(self, topic, payload):
_, console, *_ = topic
player = self.gamestarter.player(console)
if payload == '1':
player.push()
elif payload == '0':
player.release()
else:
print('Unknown payload "%s" on "%s"' %(payload, topic))
def init(self):
self.mqtt.connect()
self.gamestarter = HostFactory.game_starter(self.config)
self.mqtt.sub('+/join', self.handle_join)
def run(self):
while(not self.stopped.isSet()):
self.sleep(0.05)
self.gamestarter.step_time(0.05)
if(self.gamestarter.should_start):
game_id = self.get_next_game_id()
self.start_subservice(HostFactory.game_runner(self.config, game_id, self.gamestarter.joined_players))
self.mqtt.pub('start', ','.join([game_id, *self.gamestarter.joined_players]))
self.gamestarter.reset()
def cleanup(self):
self.mqtt.stop()
class GameRunner(Service):
def __init__(self, mqtt_factory, game_subtopic, consoles):
super(GameRunner, self).__init__()
self.mqtt_factory = mqtt_factory
self.game_subtopic = game_subtopic
self.mqtt = mqtt_factory.new(game_subtopic)
self.consoles = consoles
self.ready_consoles = []
self.all_ready = Event()
self.game_func = None
self.next_round = 0
def get_next_round_id(self):
round_id = 'round_%d' % (self.next_round)
self.next_round += 1
return round_id
def interrupt(self):
self.all_ready.set()
def handle_console_ready(self, topic, payload):
_, _, console, _ = topic
if payload != 'ready':
return
if console not in self.ready_consoles:
self.ready_consoles.append(console)
if all(c in self.ready_consoles for c in self.consoles):
self.all_ready.set()
def init(self):
self.mqtt.connect()
self.mqtt.sub('+/ready', self.handle_console_ready)
def start_next_round(self):
# Replace mqtt instance with new one for this round
self.mqtt.stop()
self.mqtt = self.mqtt_factory.new('/'.join([self.game_subtopic, self.get_next_round_id()]))
self.mqtt.connect()
# TODO sub to round-specific topics
#self.mqtt.sub(thingy, handle_thingy)
self.game_func = self.game_round
def game_splash(self):
self.mqtt.pub('splash/text', '***** SPACEHACK *****')
self.sleep(4.0)
self.mqtt.pub('splash/text', 'Wheeeeeeeeeeeeee')
self.sleep(10.0)
self.start_next_round()
def game_round(self):
# TODO
self.sleep(10)
def run(self):
self.all_ready.wait()
self.game_func = self.game_splash
while not self.stopped.is_set():
self.game_func()
def cleanup(self):
self.mqtt.stop()
class SpacehackHost:
def __init__(self, config, mqtt_factory=None):
self.mqtt_factory=mqtt_factory
if self.mqtt_factory is None:
self.mqtt_factory = HostFactory.mqtt_factory(config)
self.config = config
self.lobby = Lobby(self.config, self.mqtt_factory)
def stop(self):
Service.stop_all()
Service.wait_all()
def start(self):
self.lobby.start()
def wait(self):
Service.wait_all()
if __name__ == '__main__':
config = config.load('host_config.toml')
sh = SpacehackHost(config)
def signal_handler(sig, frame):
print('Exit')
sh.stop()
sh.wait()
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
sh.start()
sh.wait()