-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
442 lines (398 loc) · 14.2 KB
/
app.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
from flask import Flask, render_template, request, redirect, url_for
from flask_socketio import SocketIO, emit
from threading import Thread
import os
from time import time, sleep
last_submission = time()
current_delay = 0 #seconds
cipher_discovered = False
rosetta_discovered = False
phrases_discovered = False
incorrect_answers = 0
number_of_clients = 0
number_of_players = 0
answered_logic_correctly = False
client_id_map = {}
CRYPTO_SOCCER = "a well-oiled soccer team | lost-teammate"
CRYPTO_RUSSIA = "a russian nesting can of worms | where is todd"
CRYPTO_GEEKBOT = "what did you do since yesterday? did you find any notable insights? | todd_is_lost"
HIVE_SCRATCH = "A bee goes home because it has an itch"
SLACK_CHANNEL = "A loose rope suspended between England and France"
GOOGLE_HANGOUTS = "A hundred zeros at a casual gathering"
CIPHER = "make commerce better for everyone"
CODED_MESSAGE = """
Hello, data team :wave:. Great work in getting this far. You proved that you work well together
and are able to solve problems together :rocket_animated:. You are quite the well-oiled soccer
team as it were. But this next game is a real Russian nesting can-of-worms. You will have to
prove once more that you can work together one more time. Head over to /river for the final
challenge. Go save Todd!!! :partyblob:
"""
HEIGHT = 1080
WIDTH = 1920
ENCODING = {
'a':'t',
'b':'u',
'c':'v',
'd':'w',
'e':'x',
'f':'y',
'g':'z',
'h':'a',
'i':'b',
'j':'c',
'k':'d',
'l':'e',
'm':'f',
'n':'g',
'o':'h',
'p':'i',
'q':'j',
'r':'k',
's':'l',
't':'m',
'u':'n',
'v':'o',
'w':'p',
'x':'q',
'y':'r',
'z':'s'
}
DECODING = {value:key for key,value in ENCODING.items()}
app = Flask(__name__)
# app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app)
def decode_form(form):
return dict(map(lambda x: x.split("="), form.split("&")))
def vigenere_encode(message, cipher):
ABC = "abcdefghijklmnopqrstuvwxyz"
cipher_length = len(cipher)
encoded = []
for i in range(len(message)):
#characters not found in ABC from cipher will not change the index
#characters not found in ABC from message will not be modified
if ABC.find(message[i]) < 0:
encoded.append(message[i])
else:
index = ABC.find(message[i]) + ABC.find(cipher[i%cipher_length]) + 1
encoded.append(ABC[index%26])
return "".join(encoded)
def vigenere_decode(message, cipher):
ABC = "abcdefghijklmnopqrstuvwxyz"
cipher_length = len(cipher)
decoded = []
for i in range(len(message)):
#characters not found in ABC from cipher will not change the index
#characters not found in ABC from message will not be modified
if ABC.find(message[i]) < 0:
decoded.append(message[i])
else:
index = ABC.find(message[i]) - ABC.find(cipher[i%cipher_length]) - 1
decoded.append(ABC[index%26])
return "".join(decoded)
class Player(object):
def __init__(self, player_id):
self.player_id = player_id
self.x = WIDTH * 0.9
self.y = HEIGHT * 0.5
self.name = None
self.left = False
self.right = False
self.up = False
self.down = False
class Engine(Thread):
def __init__(self):
super(Engine, self).__init__()
self.players = {}
self.running = True
def tick(self):
update = []
for player in self.players.values():
player.x += 10 * (player.right - player.left)
player.y += 10 * (player.down - player.up)
player.x = min(max(0,player.x), WIDTH)
player.y = min(max(0,player.y), HEIGHT)
update.append({
"id": player.player_id,
"x": player.x,
"y": player.y,
"name": player.name
})
with app.app_context():
socketio.emit("update", update, broadcast = True)
def run(self):
FPS = 60.
t0 = time()
while self.running:
sleep(max(0,1/FPS - time() + t0))
try:
self.tick()
t0 = time()
except Exception as e:
print e
sleep(1)
def reset(self):
self.players = {}
def kill(self):
self.running = False
game = Engine()
game.start()
def reset_game():
global game
game.kill()
game = Engine()
game.start()
@app.route('/')
def index():
return redirect(url_for('logic'))
@app.route('/logic')
def logic():
return render_template('logic.html')
@app.route('/phrases')
def phrases():
global phrases_discovered
if not phrases_discovered:
with app.app_context():
socketio.emit("logging", "phrases page discovered!", broadcast = True)
print "phrases page discovered!"
phrases_discovered = True
# just a simple Caesar cipher
soccer = "".join(map(lambda x: ENCODING.get(x,x),CRYPTO_SOCCER))
russia = "".join(map(lambda x: ENCODING.get(x,x),CRYPTO_RUSSIA))
commerce = "".join(map(lambda x: ENCODING.get(x,x),CRYPTO_GEEKBOT))
return render_template(
'phrases.html',
soccer = soccer,
russia = russia,
commerce = commerce
)
@app.route('/rosetta')
def rosetta():
global cipher_discovered
global rosetta_discovered
if not rosetta_discovered:
with app.app_context():
socketio.emit("logging", "rosetta page discovered!", broadcast = True)
print "rosetta page discovered!"
rosetta_discovered = True
cipher = request.args.get("cipher")
if cipher is None:
secret = ""
decoder = ""
else:
if not cipher_discovered:
with app.app_context():
socketio.emit("logging", "cipher tool discovered!", broadcast = True)
print "cipher tool discovered!"
cipher_discovered = True
secret = """
<form id = "cipherForm">
<label>Cipher</label>
<input id= "cipher" type="text" name="cipher" value = "">
<button type= "button" onclick = "decode()">Decode</button>
</form>
"""
decoder = """
console.log("success")
function decode(){
var key = $("#cipher").val()
var message = $("#static").text().toLowerCase()
var ABC = "abcdefghijklmnopqrstuvwxyz"
var decoded = ""
var index = 0
for (var i = 0; i < message.length; i++){
if (ABC.indexOf(message[i]) < 0){
decoded += message[i]
} else {
index = ABC.indexOf(message[i]) - ABC.indexOf(key[i%key.length]) - 1
decoded += ABC[(index + 26)%26]
}
}
$("#message").html("<h2>" + decoded + "</h2>")
return false
}
$('#cipherForm').submit(function() {
decode()
return false//important so the page won't refresh
})
"""
return render_template(
"rosetta.html",
message = vigenere_encode(CODED_MESSAGE.lower(), CIPHER),
secret = secret,
decoder = decoder
)
@app.route('/river')
def river():
return render_template("river.html")
@app.route('/dashboard')
def dashboard():
return render_template("dashboard.html")
@socketio.on('logic_solution')
def io_logic_solution(message):
global last_submission
global current_delay
global incorrect_answers
global answered_logic_correctly
actual_solution = {
"alice_city": "waterloo",
"alice_shop": "socks",
"bob_city": "toronto",
"bob_shop": "soaps",
"caroline_city": "ottawa",
"caroline_shop": "candles",
"david_city": "montreal",
"david_shop": "jewellery",
}
if not answered_logic_correctly and time() - last_submission < current_delay:
emit(
"solution",
"<h3>Incorrect answer. Answered incorrectly {} times.".format(incorrect_answers) +
"<br>Must wait {} seconds before answering again</h3>".format(
int(current_delay - time() + last_submission) + 1),
broadcast = True
)
return
submitted_solution = decode_form(message)
name = submitted_solution.pop("name", "?????")
last_submission = time()
if answered_logic_correctly or submitted_solution == actual_solution:
emit(
"solution",
"""Correct!!! Move on to <a href = "/phrases">/phrases</a>""",
broadcast = True
)
if not answer_logic_correctly:
emit(
"logging",
"""Logic question answered correctly by {}""".format(name),
broadcast = True
)
print """Logic question answered correctly by {}""".format(name)
answered_logic_correctly = True
else:
incorrect_answers += 1
current_delay += 5
emit(
"solution",
"Incorrect answer. Answered incorrectly {} times.".format(incorrect_answers) +
"<br>Must wait {} seconds before answering again.".format(current_delay),
broadcast = True
)
emit(
"logging",
"""Logic question answered incorrectly by {}""".format(name),
broadcast = True
)
print """Logic question answered incorrectly by {}""".format(name)
@socketio.on('logic_name')
def io_logic_name(message):
clues = {
"yilun": "Don't believe Khaled",
"sunil": "David works in Montreal",
"vince": "Don't Believe Mat",
"ali": "The person who sells socks works in Waterloo",
"asad": "David makes soaps",
"calvin": "Don't believe Michael or Sinan",
"come": "Bob makes soaps",
"ella": "Caroline works in Waterloo",
"hanan": "Don't believe Karen",
"joel": "Don't believe Yilun",
"karen": "Alice sells jewellery",
"katherine": "Don't believe Ella",
"khaled": "Don't believe Asad",
"mat": "Don't believe Ali",
"michael": "Don't believe Karen",
"radwan": "Bob does not work in Ottawa",
"ran": "The jewellery merchant does not work in Ottawa",
"sean": "Caroline does not sell socks",
"sinan": "Don't believe Ran"
}
name = decode_form(message).get("name").lower()
if name in clues:
emit("clue", clues[name])
emit("logging", "{} received their clue".format(name), broadcast = True)
print "{} received their clue".format(name)
else:
emit("clue", "Name unknown, make sure to use your first name as labelled in Slack")
print name
@socketio.on('crypto_solution')
def io_crypto_solution(message):
response = decode_form(message)["cryptoAnswer"].lower().replace("+"," ").replace("%3f","?")
name = decode_form(message)["name"]
print response
if response == CRYPTO_SOCCER.split("|")[0].strip():
emit("response", SLACK_CHANNEL, broadcast = True)
emit("logging", "Soccer clue decoded by {}".format(name), broadcast = True)
print "Soccer clue decoded by {}".format(name)
elif response == CRYPTO_RUSSIA.split("|")[0].strip():
emit("response", GOOGLE_HANGOUTS, broadcast = True)
emit("logging", "Can-of-worms clue decoded by {}".format(name), broadcast = True)
print "Can-of-worms clue decoded by {}".format(name)
elif response == CRYPTO_GEEKBOT.split("|")[0].strip():
emit("response", HIVE_SCRATCH, broadcast = True)
emit("logging", "Geekbot clue decoded by {}".format(name), broadcast = True)
print "Geekbot clue decoded by {}".format(name)
else:
emit("response", "That is not a correct answer")
emit("logging", "Incorrect phrase submitted by {}".format(name), broadcast = True)
print "Incorrect phrase submitted by {}".format(name)
@socketio.on('connect')
def io_connect():
global number_of_clients
new_player = Player(number_of_clients)
emit("assign_id", new_player.player_id)
number_of_clients += 1
print "client {} connected".format(number_of_clients)
@socketio.on('broadcast')
def io_broadcast(message):
emit('broadcast', message, broadcast=True)
@socketio.on('connect_river')
def io_connect_river(name):
global number_of_players
new_player = Player(number_of_players)
if name.lower() == "admin":
new_player.x = WIDTH * 0.05
client_id_map[request.sid] = number_of_players
#remove duplicate players
to_remove = []
for player_id in game.players:
if game.players[player_id].name == name and name.lower() != "admin":
to_remove.append(player_id)
for player_id in to_remove:
game.players.pop(player_id)
print "connected player id {}".format(number_of_players)
game.players[new_player.player_id] = new_player
emit("assign_id", new_player.player_id)
emit(
"logging",
"{} arrived at the river!".format(name if name.lower() != "admin" else "Todd"),
broadcast = True
)
print "{} arrived at the river!".format(name if name.lower() != "admin" else "Todd")
number_of_players += 1
@socketio.on('player_update')
def io_player_update(message):
if not message["id"] in game.players:
return
player = game.players[message["id"]]
player.left = message["a"]
player.right = message["d"]
player.up = message["w"]
player.down = message["s"]
player.name = message["name"]
@socketio.on('reset_engine')
def io_reset_engine():
print "game reset"
reset_game()
@socketio.on('disconnect')
def io_disconnect():
if request.sid in client_id_map:
game.players.pop(client_id_map[request.sid], None)
print('Client disconnected')
if __name__ == "__main__":
port = int(os.environ.get('PORT', 5000))
print "app is running on port {}".format(port)
try:
socketio.run(app, host = '0.0.0.0', port = port)
finally:
game.kill()