-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.py
489 lines (352 loc) · 11.4 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
from enum import Enum
import json
import os
import random
import gevent
import redis
from flask import Flask, send_from_directory
from flask_sockets import Sockets
from random_emoji import random_emoji
from logging.config import dictConfig
dictConfig({
'version': 1,
'formatters': {'default': {
'format': '[%(asctime)s] %(levelname)s in %(module)s: %(message)s',
}},
'handlers': {'wsgi': {
'class': 'logging.StreamHandler',
'stream': 'ext://flask.logging.wsgi_errors_stream',
'formatter': 'default'
}},
'root': {
'level': 'INFO',
'handlers': ['wsgi']
}
})
REDIS_URL = os.environ.get('REDIS_URL', '')
REDIS_CHAN = 'emoji'
app = Flask(
__name__,
static_url_path='/static',
static_folder='frontend/build/static'
)
app.debug = 'DEBUG' in os.environ
sockets = Sockets(app)
redis = redis.from_url(REDIS_URL)
FAST_TIMES = {
'LOBBY': -1,
'MESSENGER': 10,
'SCRAMBLER': 10,
'VOTER': 10,
'REVEALER': 10,
}
NORMAL_TIMES = {
'LOBBY': -1,
'MESSENGER': 50,
'SCRAMBLER': 90,
'VOTER': 50,
'REVEALER': 20,
}
TIMES = NORMAL_TIMES if 'FAST' not in os.environ else FAST_TIMES
# Game Stages
class Stages(Enum):
LOBBY = 'LOBBY'
MESSENGER = 'MESSENGER'
SCRAMBLER = 'SCRAMBLER'
VOTER = 'VOTER'
REVEALER = 'REVEALER'
# Server State
clients = []
sleeper = None
# do not mutate!
START_STATE = {
'games': [],
'players': {},
'current_stage': Stages.LOBBY.name,
'current_vote': 0
}
pubsub = redis.pubsub()
pubsub.subscribe(REDIS_CHAN)
# Accessors for state across servers which uses redis
def get_state():
return json.loads(redis.get('state'))
def set_state(state):
redis.set('state', json.dumps(state))
def get_next_stage(stage_name):
if stage_name == Stages.LOBBY.name:
return Stages.MESSENGER.name
elif stage_name == Stages.MESSENGER.name:
return Stages.SCRAMBLER.name
elif stage_name == Stages.SCRAMBLER.name:
return Stages.VOTER.name
elif stage_name == Stages.VOTER.name:
return Stages.REVEALER.name
elif stage_name == Stages.REVEALER.name:
return Stages.VOTER.name
def delete_client(client):
client_id = str(id(client))
print(f"deleting client {client_id}")
state = get_state()
if state['current_stage'] == Stages.LOBBY.name and client_id in state['players']:
del state['players'][str(client_id)]
if client in clients:
clients.remove(client)
set_state(state)
broadcast_state()
def send(client, raw_data):
try:
client.send(raw_data)
except Exception as e:
app.logger.exception('Failed to send to client, removing from pool')
delete_client(client)
def ping_clients():
while True:
for client in clients:
notify_user(client, {'type': 'ping'})
gevent.sleep(10)
def publish_redis_messages_to_clients():
for message in pubsub.listen():
app.logger.info(f'Pubsub: {message}')
if message['type'] == 'message':
data = json.loads(message['data'])
if '_user_id' in data:
print(f'Sending to {data["_user_id"]}')
try:
user = next(user for user in clients if str(id(user)) == data['_user_id'])
except StopIteration:
print(f'{data["_user_id"]} not on this server')
continue
gevent.spawn(send, user, json.dumps(data).encode())
else:
print(f'publishing to all {len(clients)} clients')
for client in clients:
gevent.spawn(send, client, message['data'])
else:
print(f'Redis gave informative message {message}')
def create_game(messenger_id, scrambler_id):
n_emoji = 10
goal_index = random.randint(0, n_emoji - 1)
return {
'messenger_id': messenger_id,
'scrambler_id': scrambler_id,
'emoji_board': make_emoji_list(n_emoji),
'goal_index': goal_index,
# Creates an anti_goal that excludes the regular goal
'anti_goal': random.choice([n for n in range(n_emoji) if n != goal_index]),
'message': '',
'scrambled_message': '',
'votes': {}
}
def is_all_skip(players):
"""determines if everyone's skip is True"""
return all(
player_data['skip']
for player_data in players.values()
)
# sets all skip to 0 unless specified
def reset_skips(players, *ids_to_set_skip):
for player in players:
if player in ids_to_set_skip:
players[player]['skip'] = True
else:
players[player]['skip'] = False
return players
def stop_sleep():
if sleeper:
sleeper.kill()
app.logger.info('Killed sleeper')
# unused
def send_hint_to_scrambler(client):
"""The client is the messenger for this round"""
games = get_state()['games']
game = next(game for game in games if game['messenger_id'] == str(id(client)))
scrambler = next(user for user in clients if game['scrambler_id'] == str(id(user)))
notify_user(scrambler, {
'type': 'unscrambled_hint',
'unscrambled_hint': game['message'],
})
def reset_game():
set_state(START_STATE)
broadcast_state()
def start_game_timer():
global sleeper
state = get_state()
# Stage = MESSENGER
state['current_stage'] = get_next_stage(state['current_stage'])
state['players'] = reset_skips(state['players'])
set_state(state)
broadcast_state()
sleeper = gevent.spawn(gevent.sleep, TIMES[state['current_stage']])
sleeper.join()
state = get_state()
# Stage = SCRAMBLER
state['current_stage'] = get_next_stage(state['current_stage'])
state['players'] = reset_skips(state['players'])
set_state(state)
broadcast_state()
sleeper = gevent.spawn(gevent.sleep, TIMES[state['current_stage']])
sleeper.join()
state = get_state()
games = state['games']
for game_id in games:
state = get_state()
# Stage = VOTER
state['current_stage'] = get_next_stage(state['current_stage'])
# Initialize skip with messenger and scrambler true
state['players'] = reset_skips(
state['players'],
game_id['messenger_id'],
game_id['scrambler_id']
)
set_state(state)
broadcast_state()
sleeper = gevent.spawn(gevent.sleep, TIMES[state['current_stage']])
sleeper.join()
state = get_state()
# Stage = REVEALER
state['current_stage'] = get_next_stage(state['current_stage'])
state['players'] = reset_skips(state['players'])
set_state(state)
broadcast_state()
sleeper = gevent.spawn(gevent.sleep, TIMES[state['current_stage']])
sleeper.join()
state['current_vote'] = state['current_vote'] + 1
set_state(state)
reset_game()
@app.route('/')
def index():
return send_from_directory('frontend/build/', 'index.html')
def make_emoji_list(n):
# ensure distinct emoji
emojis = set()
while len(emojis) < n:
emojis.add(random_emoji()[0])
return list(emojis)
def broadcast_state():
state = get_state()
print(f'Client ids: {[id(client) for client in clients]}')
games = state['games']
players = state['players']
current_stage = state['current_stage']
current_vote = state['current_vote']
notify_all({
'current_stage': current_stage,
'times': TIMES,
'current_vote': current_vote,
'type': 'state_update',
'games': games,
'users': players,
})
def handle_message(client, data):
client_id = str(id(client))
print(f'handle_message from {client_id} with data {data}')
if data['type'] == 'join':
state = get_state()
players = state['players']
games = state['games']
player = {
'username': data['username'],
'score': 0,
'skip': False
}
players[client_id] = player
set_state(state)
broadcast_state()
elif data['type'] == 'start':
state = get_state()
players = state['players']
games = state['games']
if len(players) < 3:
raise Exception('Not enough players')
player_ids = list(players.keys())
derangement = list(player_ids)
while any(x == y for x, y in zip(player_ids, derangement)):
random.shuffle(derangement)
for messenger_id, scrambler_id in zip(player_ids, derangement):
game = create_game(messenger_id, scrambler_id)
games.append(game)
print(f'Starting game {game}')
set_state(state)
broadcast_state()
gevent.spawn(start_game_timer)
# The clue that the hinter suggests
elif data['type'] == 'hint':
state = get_state()
players = state['players']
games = state['games']
hint = data['hint']
game = next(
game
for game in games
if game['messenger_id'] == client_id
)
game['message'] = hint
# Set same hint as scrambled in case of disconnect
game['scrambled_message'] = hint
set_state(state)
broadcast_state()
# The scrambled hint
elif data['type'] == 'scrambled_hint':
state = get_state()
games = state['games']
scrambled_hint = data['scrambled_hint']
game = next(game for game in games if game['scrambler_id'] == client_id)
game['scrambled_message'] = scrambled_hint
set_state(state)
broadcast_state()
elif data['type'] == 'vote':
state = get_state()
vote = data['vote']
games = state['games']
game = games[state['current_vote']]
game['votes'][client_id] = vote
set_state(state)
broadcast_state()
elif data['type'] == 'skip':
state = get_state()
players = state['players']
players[client_id]['skip'] = True
if is_all_skip(players):
app.logger.info('All players voted to skip')
stop_sleep()
set_state(state)
broadcast_state()
else:
raise Exception('Unknown event {}'.format(data))
def notify_all(data):
redis.publish(REDIS_CHAN, json.dumps(data))
def notify_user(client, data):
# Uses _user_id as a hidden field to restrict only send to single user
data['_user_id'] = str(id(client))
redis.publish(REDIS_CHAN, json.dumps(data))
@sockets.route('/socket')
def handle_websocket(client):
client_id = str(id(client))
print(f'Got connection from {client_id}')
clients.append(client)
message = {
'type': 'welcome',
'_user_id': client_id,
}
try:
broadcast_state()
except Exception:
app.logger.exception('Failed to broadcast state to new user')
notify_user(client, message)
while not client.closed:
message = client.receive()
if message is None:
print(f'Got none message, closing {client_id}')
delete_client(client)
else:
data = json.loads(message)
try:
handle_message(client, data)
except Exception:
app.logger.exception('Failed to handle user message')
gevent.sleep(.1)
# Reset server state on server reload
if redis.get('state') is None:
redis.set('state', json.dumps(START_STATE))
gevent.spawn(publish_redis_messages_to_clients)
gevent.spawn(ping_clients)