-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.py
1033 lines (835 loc) · 39.9 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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import datetime
import json
import os
import random
import socketio
import urlparse
import user_agents
from collections import deque
from flask import Flask, render_template, request
from flask_sqlalchemy import SQLAlchemy
from flask_httpauth import HTTPBasicAuth
from database.db_utils import (get_dialogues, get_dialogue_stats,
get_dialogue_guess, get_dialogue_object,
get_dialogue_conversation, get_dialogue_info,
get_workers, get_worker,
insert_question, insert_answer, insert_guess,
insert_session, end_session, update_session,
update_dialogue_status, start_dialogue,
remove_from_queue, insert_into_queue,
get_worker_status, get_assignment_stats,
get_one_worker_status, update_one_worker_status,
assignment_completed, is_worker_playing, get_ongoing_workers,
report_answer)
from worker import (check_qualified, check_blocked, check_assignment_completed,
pay_questioner_bonus, pay_oracle_bonus)
from players import QualifyOracle, Oracle, QualifyQuestioner, Questioner
# set this to 'threading', 'eventlet', or 'gevent'
async_mode = 'eventlet'
if async_mode == 'gevent':
from gevent import monkey
monkey.patch_all()
if async_mode == 'eventlet':
import eventlet
eventlet.monkey_patch()
sio = socketio.Server(logger=True, engineio_logger=True, async_mode=async_mode, ping_interval=20)
app = Flask(__name__)
app.wsgi_app = socketio.Middleware(sio, app.wsgi_app)
app.config['SECRET_KEY'] = 'spywithmylittleeye!'
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ['HEROKU_POSTGRESQL_SILVER_URL']
db = SQLAlchemy(app)
engine = db.engine
thread = None
""" Dictionaries for dialogue info that remains in RAM """
q_oracle_queue = deque()
q_questioner_queue = deque()
oracle_queue = deque()
questioner_queue = deque()
players = {} # indexed by socket id
dialogues = {} # indexed by dialogue_id
auth = HTTPBasicAuth()
def time_out(dialogue):
conn = engine.connect()
oracle = players[dialogue.oracle_sid] if dialogue.oracle_sid in players else None
questioner = players[dialogue.questioner_sid] if dialogue.questioner_sid in players else None
print("time out")
print("oracle : " + oracle.worker_id + " \t dialogue.sid: " + str(dialogue.oracle_sid))
print("questioner: " + questioner.worker_id + " \t dialogue.sid: " + str(dialogue.questioner_sid))
if dialogue.turn == 'oracle':
# Oracle time out
update_dialogue_status(conn, dialogue.id, 'oracle_timeout')
delete_game([oracle, questioner])
sio.emit('timeout', '',
room=oracle.sid, namespace=oracle.namespace)
sio.emit('partner timeout', '',
room=questioner.sid, namespace=questioner.namespace)
if questioner.role == 'Questioner':
find_normal_oracle(questioner.sid)
else:
find_qualification_oracle(questioner.sid)
check_blocked(conn, oracle)
else:
# Questioner timeout
update_dialogue_status(conn, dialogue.id, 'questioner_timeout')
delete_game([oracle, questioner])
sio.emit('timeout', '',
room=questioner.sid, namespace=questioner.namespace)
sio.emit('partner timeout', '',
room=oracle.sid, namespace=oracle.namespace)
if oracle.role == 'Oracle':
find_normal_questioner(oracle.sid)
else:
find_qualification_questioner(oracle.sid)
check_blocked(conn, questioner)
conn.close()
"""Background process checking for dialogues that time out."""
def check_for_timeouts():
while True:
sio.sleep(0.01) # return control so other threads can execute
keys = dialogues.keys()
for key in keys:
if key in dialogues:
dialogue = dialogues[key]
if datetime.datetime.now() > dialogue.deadline:
time_out(dialogue)
sio.sleep(0.01)
@auth.get_password
def get_pw(username):
return "multimodal"
@app.route('/')
def index():
return render_template('index.html')
def check_browser(user_agent_string):
browser_exclude_rule = ['MSIE', 'mobile', 'tablet']
user_agent_obj = user_agents.parse(user_agent_string)
browser_ok = True
for rule in browser_exclude_rule:
myrule = rule.strip()
if myrule in ["mobile", "tablet", "touchcapable", "pc", "bot"]:
if (myrule == "mobile" and user_agent_obj.is_mobile) or\
(myrule == "tablet" and user_agent_obj.is_tablet) or\
(myrule == "touchcapable" and user_agent_obj.is_touch_capable) or\
(myrule == "pc" and user_agent_obj.is_pc) or\
(myrule == "bot" and user_agent_obj.is_bot):
browser_ok = False
elif (myrule == "Safari" or myrule == "safari"):
if "Chrome" in user_agent_string and "Safari" in user_agent_string:
pass
elif "Safari" in user_agent_string:
browser_ok = False
elif myrule in user_agent_string:
browser_ok = False
return browser_ok
@app.route('/qualify_oracle')
def q_oracle():
if not check_browser(request.user_agent.string):
# Handler for IE users if IE is not supported.
msg = 'Your browser is not supported.'
return render_template('error.html', msg=msg)
if not ('hitId' in request.args and
'assignmentId' in request.args):
return render_template('error.html', title='Oracle - ',
msg='Missing mturk parameters.')
assignment_id = request.args['assignmentId']
if len(players) > 1000:
msg = ('Sorry, there are currently'
'more than 1000 players.')
return render_template('error.html', title='Oracle - ',
msg=msg)
turk_submit_to = 'https://workersandbox.mturk.com'
if 'turkSubmitTo' in request.args:
turk_submit_to = request.args['turkSubmitTo']
accepted_hit = False
nr_success, nr_failure, nr_disconnects = 0, 0, 0
if 'workerId' in request.args:
worker_id = request.args['workerId']
accepted_hit = True
conn = engine.connect()
worker_status = get_worker_status(conn, worker_id)
if worker_status == 'blocked':
return render_template('error.html', title='Oracle - ',
msg='Your account is currently blocked, probably because you\'ve made too many mistakes or disconnected too many times while completing the HIT. Contact Harm de Vries at [email protected] for more information about your account (include your worker id).')
if worker_status == 'qualified':
return render_template('error.html', title='Oracle - ',
msg='You are already qualified as an oracle. Please search for Guesswhat?! HIT with [QUALIFIED ONLY] in the title.')
stats = get_assignment_stats(conn, assignment_id, questioner=False)
nr_success, nr_failure = stats['success'], stats['failure'] + stats['oracle_reported']
nr_disconnects = stats['oracle_disconnect'] + stats['oracle_timeout']
for player in players.itervalues():
if player.worker_id == worker_id and player.role == 'QualifyQuestioner':
msg = ('You are not allowed to play '
'multiple games at the same time.')
return render_template('error.html', title='Oracle - ',
msg=msg)
global thread
if thread is None:
thread = sio.start_background_task(check_for_timeouts)
return render_template('oracle.html',
title='Oracle - ',
accepted_hit=accepted_hit,
assignmentId=assignment_id,
success=nr_success,
failure=nr_failure,
disconnect=nr_disconnects,
turkSubmitTo=turk_submit_to,
namespace='/q_oracle')
@app.route('/oracle')
def oracle():
if not check_browser(request.user_agent.string):
# Handler for IE users if IE is not supported.
msg = 'Your browswer is not supported.'
return render_template('error.html', msg=msg)
if not ('hitId' in request.args and
'assignmentId' in request.args):
return render_template('error.html', title='Oracle - ',
msg='Missing mturk parameters.')
assignment_id = request.args['assignmentId']
if len(players) > 1000:
msg = ('Sorry, there are currently'
'more than 1000 players.')
return render_template('error.html', title='Oracle - ',
msg=msg)
turk_submit_to = 'https://workersandbox.mturk.com'
if 'turkSubmitTo' in request.args:
turk_submit_to = request.args['turkSubmitTo']
accepted_hit = False
nr_success, nr_failure, nr_disconnects = 0, 0, 0
if 'workerId' in request.args:
worker_id = request.args['workerId']
accepted_hit = True
conn = engine.connect()
worker_status = get_worker_status(conn, worker_id, questioner=False)
if worker_status == 'blocked':
return render_template('error.html', title='Oracle - ',
msg='Your account is currently blocked, probably because you\'ve made too many mistakes or disconnected too many times while completing the HIT. Contact Harm de Vries - [email protected] - for more information.')
if worker_status is None or worker_status == 'default':
return render_template('error.html', title='Oracle - ',
msg='You are not qualified yet to play Guesswhat?!. Please search for GuessWhat?! HIT without [QUALIFIED ONLY] in the title.')
if assignment_completed(conn, assignment_id):
return render_template('error.html', title='Questioner - ',
msg='You have already completed this assignment.')
stats = get_assignment_stats(conn, assignment_id, questioner=False)
nr_success, nr_failure = stats['success'], stats['failure'] + stats['oracle_reported']
nr_disconnects = stats['oracle_disconnect'] + stats['oracle_timeout']
if (nr_failure + nr_disconnects) > 3:
return render_template('error.html', title='Oracle - ',
msg='You have made too many mistakes to successfully complete this assignment. Please return accepted HIT. ')
for player in players.itervalues():
if player.worker_id == request.args['workerId'] and player.role == 'Questioner':
msg = ('You are allowed to play at most '
'one game at the same time.')
return render_template('error.html', title='Oracle - ',
msg=msg)
global thread
if thread is None or not thread.is_alive():
thread = sio.start_background_task(check_for_timeouts)
return render_template('oracle.html',
title='Oracle - ',
success=nr_success,
failure=nr_failure,
disconnect=nr_disconnects,
accepted_hit=accepted_hit,
assignmentId=assignment_id,
turkSubmitTo=turk_submit_to,
namespace='/oracle')
@app.route('/qualify_questioner')
def q_questioner():
if not check_browser(request.user_agent.string):
# Handler for IE users if IE is not supported.
msg = 'Your browswer is not supported.'
return render_template('error.html', title='Questioner - ', msg=msg)
if not ('hitId' in request.args and
'assignmentId' in request.args):
return render_template('error.html', title='Questioner - ',
msg='Missing mturk parameters.')
if len(players) > 1000:
msg = ('Sorry, there are currently'
'more than 1000 players.')
return render_template('error.html', title='Questioner - ',
msg=msg)
assignment_id = request.args['assignmentId']
turk_submit_to = 'https://workersandbox.mturk.com'
if 'turkSubmitTo' in request.args:
turk_submit_to = request.args['turkSubmitTo']
accepted_hit = False
nr_success, nr_failure, nr_disconnects = 0, 0, 0
if 'workerId' in request.args:
worker_id = request.args['workerId']
accepted_hit = True
conn = engine.connect()
worker_status = get_worker_status(conn, worker_id, questioner=True)
if worker_status == 'blocked':
return render_template('error.html', title='Questioner - ',
msg='Your account is currently blocked, probably because you\'ve made too many mistakes or disconnected too many times while completing the HIT. Contact Harm de Vries - [email protected] - for more information.')
if worker_status == 'qualified':
return render_template('error.html', title='Questioner - ',
msg='You are already qualified as a questioner. Please search for GuessWhat?! HIT with [QUALIFIED ONLY] in the title.')
stats = get_assignment_stats(conn, assignment_id, questioner=True)
nr_success, nr_failure = stats['success'], stats['failure'] + stats['oracle_reported']
nr_disconnects = stats['questioner_disconnect'] + stats['questioner_timeout']
for player in players.itervalues():
if player.worker_id == request.args['workerId'] and player.role == 'QualifyOracle':
msg = ('You are allowed to play at most '
'one game at the same time.')
return render_template('error.html', title='Questioner - ',
msg=msg)
global thread
if thread is None or not thread.is_alive():
thread = sio.start_background_task(check_for_timeouts)
return render_template('questioner.html',
title='Questioner - ',
assignmentId=assignment_id,
accepted_hit=accepted_hit,
success=nr_success,
failure=nr_failure,
disconnect=nr_disconnects,
turkSubmitTo=turk_submit_to,
namespace='/q_questioner')
@app.route('/questioner')
def questioner():
if not check_browser(request.user_agent.string):
# Handler for IE users if IE is not supported.
msg = 'Your browswer is not supported.'
return render_template('error.html', title='Questioner - ', msg=msg)
if not ('hitId' in request.args and
'assignmentId' in request.args):
return render_template('error.html', title='Questioner - ',
msg='Missing mturk parameters.')
assignment_id = request.args['assignmentId']
if len(players) > 1000:
msg = ('Sorry, there are currently'
'more than 1000 players.')
return render_template('error.html', title='Questioner - ',
msg=msg)
turk_submit_to = 'https://workersandbox.mturk.com'
if 'turkSubmitTo' in request.args:
turk_submit_to = request.args['turkSubmitTo']
accepted_hit = False
nr_success, nr_failure, nr_disconnects = 0, 0, 0
if 'workerId' in request.args:
accepted_hit = True
worker_id = request.args['workerId']
conn = engine.connect()
worker_status = get_worker_status(conn, worker_id, questioner=True)
if worker_status == 'blocked':
return render_template('error.html', title='Questioner - ',
msg='Your account is currently blocked, probably because you\'ve made too many mistakes or disconnected too many times while completing the HIT. Contact Harm de Vries - [email protected] - for more information.')
if worker_status is None or worker_status == 'default':
return render_template('error.html', title='Questioner - ',
msg='You are not qualified yet to play Guesswhat?!. Please search for GuessWhat?! HIT without [QUALIFIED ONLY] in the title.')
if assignment_completed(conn, assignment_id):
return render_template('error.html', title='Questioner - ',
msg='You have already completed this assignment.')
stats = get_assignment_stats(conn, assignment_id, questioner=True)
nr_success, nr_failure = stats['success'], stats['failure'] + stats['oracle_reported']
nr_disconnects = stats['questioner_disconnect'] + stats['questioner_timeout']
if (nr_failure + nr_disconnects) > 3:
return render_template('error.html', title='Questioner - ',
msg='You have made too many mistakes to successfully complete this assignment. Please return accepted HIT. ')
for player in players.itervalues():
if player.worker_id == request.args['workerId'] and player.role == 'Oracle':
msg = ('You are allowed to play at most '
'one game at the same time.')
return render_template('error.html', title='Questioner - ',
msg=msg)
global thread
if thread is None or not thread.is_alive():
thread = sio.start_background_task(check_for_timeouts)
return render_template('questioner.html',
title='Questioner - ',
success=nr_success,
failure=nr_failure,
disconnect=nr_disconnects,
accepted_hit=accepted_hit,
assignmentId=assignment_id,
turkSubmitTo=turk_submit_to,
namespace='/questioner')
@app.route('/dialogues')
@auth.login_required
def getdialogues():
return render_template('dialogues.html', dialogues=get_dialogues(engine))
@app.route('/dialogue_stats/<mode>')
@auth.login_required
def dialogue_stats(mode):
print (mode)
counts, avg_seconds, avg_questions = get_dialogue_stats(engine, mode=mode)
return render_template('dialogue_stats.html', counts=counts,
avg_questions=avg_questions,
avg_seconds=avg_seconds)
@app.route('/dialogue/<id>')
@auth.login_required
def show_dialogue(id):
conn = engine.connect()
(picture_id, width, height, status,
oracle_id, questioner_id, time) = get_dialogue_info(conn, id)
if picture_id is None:
return render_template('error.html', msg='Dialogue not found.')
image = ('https://msvocds.blob.core.windows.net/imgs/'
'{}.jpg').format(picture_id)
guess = get_dialogue_guess(conn, id)
obj = get_dialogue_object(conn, id)
objs = [obj.to_json()]
if guess is not None and guess.object_id != obj.object_id:
objs.append(guess.to_json())
qas = get_dialogue_conversation(conn, id)
conn.close()
return render_template('dialogue.html',
status=status,
oracle_id=oracle_id,
questioner_id=questioner_id,
time=time,
qas=qas,
img={'src': image, 'width': width,
'height': height},
obj=json.dumps(objs))
@app.route('/workers')
@auth.login_required
def workers():
conn = engine.connect()
workers = get_workers(conn)
conn.close()
return render_template('workers.html', workers=workers)
def render_worker(id):
with engine.begin() as conn:
worker_dialogues = get_worker(conn, id)
status = get_one_worker_status(conn, id)
ongoing_worker = is_worker_playing(conn, id)
status["playing"] = ongoing_worker.is_playing
status["role"] = ongoing_worker.role
status["socket_db"] = ongoing_worker.socket_id
status["socket_io"] = 0
for sid, player in players.iteritems():
if player.worker_id == id:
status["socket_io"] = sid
break
return render_template('worker.html', dialogues=worker_dialogues, worker=status)
@app.route('/worker/<id>')
@auth.login_required
def worker(id):
return render_worker(id)
@auth.login_required
@app.route('/worker/<id>/questioner_status', methods=['POST'])
def one_worker_questioner_status(id):
with engine.begin() as conn:
update_one_worker_status(conn, id, "questioner_status", request.form['questioner_status'])
return render_worker(id)
@auth.login_required
@app.route('/worker/<id>/oracle_status', methods=['POST'])
def one_worker_oracle_status(id):
with engine.begin() as conn:
update_one_worker_status(conn, id, "oracle_status", request.form['oracle_status'])
return render_worker(id)
@auth.login_required
@app.route('/worker/<id>/remove_socket', methods=['POST'])
def one_worker_remove_socket(id):
sid_to_remove = request.form['text_socket_io']
if sid_to_remove in players:
del players[sid_to_remove]
return render_worker(id)
def render_stats_io_error():
# retrieve workers that are currently playing a dialogue
with engine.begin() as conn:
ongoing_workers = get_ongoing_workers(conn)
# helper to get sid from worker id
def get_sid(worker_id):
for sid, w in players.iteritems():
if w.worker_id == worker_id:
return sid
return 0
# list all the current open sockets
workers = []
for socket_id, player in players.iteritems():
one_worker = dict()
one_worker["id"] = player.worker_id
one_worker["playing"] = player.worker_id in ongoing_workers
one_worker["role"] = ongoing_workers[player.worker_id].role
one_worker["socket_db"] = ongoing_workers[player.worker_id].socket_id
one_worker["socket_io"] = get_sid(player.worker_id)
workers.append(one_worker)
return render_template('socket_io.html', workers=workers)
@auth.login_required
@app.route('/stats/io_error')
def stats_io_error():
return render_stats_io_error()
@auth.login_required
@app.route('/stats/io_error', methods=['POST'])
def stats_io_error_update():
# get the checkbox with the sid to remove
sid_to_remove = request.form.getlist('check')
for sid in sid_to_remove:
del players[sid]
return render_stats_io_error()
@app.route('/stats')
def stats():
msg = '<br />'.join([', '.join([sid, str(sio.eio.sockets[sid].closed), str(sio.eio.sockets[sid].upgraded), str(sio.eio.sockets[sid].connected), str(sio.eio.sockets[sid].last_ping)]) for sid in sio.eio.sockets.keys()])
return render_template('error.html', msg=msg)
def get_dialogue_and_players(sid):
player = players[sid]
partner = player.partner
dialogue = dialogues[player.dialogue_id]
return player, partner, dialogue
@sio.on('report oracle', namespace='/q_questioner')
@sio.on('report oracle', namespace='/questioner')
def report_oracle(sid, reason):
player, partner, dialogue = get_dialogue_and_players(sid)
with engine.begin() as conn:
update_dialogue_status(conn, dialogue.id, 'oracle_reported',
reason=reason)
delete_game([player, partner])
sio.emit('reported', '', room=partner.sid, namespace=partner.namespace)
if partner.role == "QualifyOracle":
find_qualification_questioner(partner.sid)
find_qualification_oracle(player.sid)
else:
find_normal_questioner(partner.sid)
find_normal_oracle(player.sid)
@sio.on('report oracle endgame', namespace='/q_questioner')
@sio.on('report oracle endgame', namespace='/questioner')
def report_oracle_end(sid, data):
# Retrieve json data
ranks_to_report = data["id_to_report"]
dialogue_id = data["dialogue_id"]
comments = data["comments"]
# store reported data in db
with engine.begin() as conn:
report_answer(conn, dialogue_id=dialogue_id, ranks=ranks_to_report)
update_dialogue_status(conn, dialogue_id, 'oracle_reported', reason=comments)
@sio.on('report questioner', namespace='/q_oracle')
@sio.on('report questioner', namespace='/oracle')
def report_questioner(sid, reason):
player, partner, dialogue = get_dialogue_and_players(sid)
with engine.begin() as conn:
update_dialogue_status(conn, dialogue.id, 'questioner_reported',
reason=reason)
delete_game([player, partner])
sio.emit('reported', '', room=partner.sid, namespace=partner.namespace)
if partner.role == "QualifyQuestioner":
find_qualification_oracle(partner.sid)
find_qualification_questioner(player.sid)
else:
find_normal_oracle(partner.sid)
find_normal_questioner(player.sid)
@sio.on('newquestion', namespace='/q_questioner')
@sio.on('newquestion', namespace='/questioner')
def new_question(sid, message):
player, partner, dialogue = get_dialogue_and_players(sid)
with engine.begin() as conn:
dialogue.question_ids.append(
insert_question(conn, dialogue.id, message))
dialogue.turn = 'oracle'
dialogue.deadline = datetime.datetime.now() + datetime.timedelta(seconds=30)
sio.emit('new question', message,
room=player.partner.sid, namespace=partner.namespace)
@sio.on('new answer', namespace='/q_oracle')
@sio.on('new answer', namespace='/oracle')
def new_answer(sid, message):
player, partner, dialogue = get_dialogue_and_players(sid)
with engine.begin() as conn:
insert_answer(conn, dialogue.question_ids[-1], message)
dialogue.turn = 'questioner'
dialogue.deadline = datetime.datetime.now() + datetime.timedelta(seconds=90)
sio.emit('new answer', message,
room=partner.sid, namespace=partner.namespace)
@sio.on('update answer', namespace='/q_oracle')
@sio.on('update answer', namespace='/oracle')
def update_answer(sid, msg):
player, partner, dialogue = get_dialogue_and_players(sid)
with engine.begin() as conn:
ans = {'Yes': 'Yes', 'No': 'No', 'Not applicable': 'N/A'}[msg['new_msg']]
insert_answer(conn, dialogue.question_ids[msg['round']], ans)
sio.emit('update answer', msg, room=partner.sid, namespace=partner.namespace)
@sio.on('guess', namespace='/q_questioner')
@sio.on('guess', namespace='/questioner')
def guess(sid):
player = players[sid]
dialogue = dialogues[player.dialogue_id]
dialogue.turn = 'questioner'
dialogue.deadline = datetime.datetime.now() + datetime.timedelta(seconds=30)
objs = [obj.to_json() for obj in dialogue.picture.objects]
sio.emit('all annotations', {'objs': objs},
room=sid, namespace=player.namespace)
sio.emit('start guessing', '',
room=player.partner.sid, namespace=player.partner.namespace)
@sio.on('guess annotation', namespace='/q_questioner')
def guess_annotation(sid, object_id):
player = players[sid]
dialogue = dialogues[player.dialogue_id]
conn = engine.connect()
insert_guess(conn, dialogue.id, object_id)
selected_obj = dialogue.object
if selected_obj.object_id == object_id:
update_dialogue_status(conn, dialogue.id, 'success')
stats, finished_flag = check_qualified(conn, player)
sio.emit('correct annotation', {'object': selected_obj.to_json(),
'stats': stats,
'finished': finished_flag,
'qualified': False},
room=sid, namespace=player.namespace)
stats, finished_flag = check_qualified(conn, player.partner)
sio.emit('correct annotation', {'object': selected_obj.to_json(),
'stats': stats,
'finished': finished_flag,
'qualified': False},
room=player.partner.sid, namespace=player.partner.namespace)
else:
update_dialogue_status(conn, dialogue.id, 'failure')
for obj in dialogue.picture.objects:
if obj.object_id == object_id:
guessed_obj = obj
stats, blocked = check_blocked(conn, player)
sio.emit('wrong annotation', {'object': selected_obj.to_json(),
'stats': stats,
'blocked': blocked,
'qualified': False},
room=sid, namespace=player.namespace)
stats, blocked = check_blocked(conn, player.partner)
sio.emit('wrong annotation', {'object': guessed_obj.to_json(),
'stats': stats,
'blocked': blocked,
'qualified': False},
room=player.partner.sid, namespace=player.partner.namespace)
delete_game([player, player.partner])
conn.close()
@sio.on('guess annotation', namespace='/questioner')
def guess_annotation2(sid, object_id):
player = players[sid]
dialogue = dialogues[player.dialogue_id]
conn = engine.connect()
insert_guess(conn, dialogue.id, object_id)
selected_obj = dialogue.object
if selected_obj.object_id == object_id:
update_dialogue_status(conn, dialogue.id, 'success')
stats, finished_flag1 = check_assignment_completed(conn, player)
sio.emit('correct annotation', {'object': selected_obj.to_json(),
'stats': stats,
'finished': finished_flag1,
'qualified': True},
room=sid, namespace=player.namespace)
stats = get_assignment_stats(conn, player.partner.worker_id,
questioner=False)
stats, finished_flag2 = check_assignment_completed(conn, player.partner)
sio.emit('correct annotation', {'object': selected_obj.to_json(),
'stats': stats,
'finished': finished_flag2,
'qualified': True},
room=player.partner.sid, namespace='/oracle')
if finished_flag1:
pay_questioner_bonus(conn, player)
if finished_flag2:
pay_oracle_bonus(conn, player.partner)
else:
update_dialogue_status(conn, dialogue.id, 'failure')
for obj in dialogue.picture.objects:
if obj.object_id == object_id:
guessed_obj = obj
stats = get_assignment_stats(conn, player.assignment_id,
questioner=True)
blocked = False
if (stats['failure'] + stats['questioner_disconnect'] + stats['questioner_timeout'] + stats['oracle_reported']) > 3:
blocked = True
sio.emit('wrong annotation', {'object': selected_obj.to_json(),
'stats': stats,
'blocked': blocked,
'qualified': True},
room=sid, namespace=player.namespace)
stats = get_assignment_stats(conn, player.partner.assignment_id,
questioner=False)
blocked = False
if (stats['failure'] + stats['oracle_disconnect'] + stats['oracle_timeout'] + stats['oracle_reported']) > 3:
blocked = True
sio.emit('wrong annotation', {'object': guessed_obj.to_json(),
'stats': stats,
'blocked': blocked,
'qualified': True},
room=player.partner.sid, namespace='/oracle')
delete_game([player, player.partner])
conn.close()
def get_difficulty(player):
sample = random.random()
if player.is_qualified():
if sample > 0.3:
difficulty = 2
else:
difficulty = 1
else:
if sample > 0.3:
difficulty = 1
else:
difficulty = 2
return difficulty
@sio.on('next questioner', namespace='/q_oracle')
def find_qualification_questioner(sid):
find_player(sid, _player_queue=q_oracle_queue, _partner_queue=q_questioner_queue, mode='qualification')
@sio.on('next questioner', namespace='/oracle')
def find_normal_questioner(sid):
find_player(sid, _player_queue=oracle_queue, _partner_queue=questioner_queue, mode='normal')
@sio.on('next oracle', namespace='/q_questioner')
def find_qualification_oracle(sid):
find_player(sid, _player_queue=q_questioner_queue, _partner_queue=q_oracle_queue, mode='qualification')
@sio.on('next oracle', namespace='/questioner')
def find_normal_oracle(sid):
find_player(sid, _player_queue=questioner_queue, _partner_queue=oracle_queue, mode='normal')
def find_player(sid, _player_queue, _partner_queue, mode):
# Retrieve players by socket id
player = players[sid]
# If a partner is available, start a new dialogue with the current player
if len(_partner_queue) > 0:
# remove partner from queue
with engine.begin() as conn:
partner = _partner_queue.pop()
remove_from_queue(conn, partner, 'dialogue')
# Provide each other partners
partner.partner = player
player.partner = partner
instantiate_dialogue(player=player, partner=partner, mode=mode)
# Otherwise, queue the player
else:
player.partner_sid = None
with engine.begin() as conn:
player.queue_id = insert_into_queue(conn, player)
_player_queue.appendleft(player)
def instantiate_dialogue(player, partner, mode):
# define roles
oracle_ping, questioner_ping = False, False
if player.is_questioner():
_questioner = player
_oracle = partner
oracle_ping = True
else:
_questioner = partner
_oracle = player
questioner_ping = True
# Instantiate a new dialogue in the db
with engine.begin() as conn:
dialogue = start_dialogue(conn, difficulty=get_difficulty(player), mode=mode,
questioner_session_id=_questioner.session_id,
oracle_session_id=_oracle.session_id)
# Store dialogue id
dialogues[dialogue.id] = dialogue
partner.dialogue_id = dialogue.id
player.dialogue_id = dialogue.id
# provide sid
dialogue.oracle_sid = _oracle.sid
dialogue.questioner_sid = _questioner.sid
# Create json msg for both questioner/oracle
image_src = ('https://msvocds.blob.core.windows.net/imgs/{}.jpg').format(dialogue.picture.id)
sio.emit('questioner',
{'img': {'src': image_src,
'width': dialogue.picture.width,
'height': dialogue.picture.height},
'dialogue_id': dialogue.id,
'ping': questioner_ping},
room=_questioner.sid,
namespace=_questioner.namespace)
sio.emit('answerer',
{'img': {'src': image_src,
'width': dialogue.picture.width,
'height': dialogue.picture.height},
'object': dialogue.object.to_json(),
'dialogue_id': dialogue.id,
'ping': oracle_ping},
room=_oracle.sid,
namespace=_oracle.namespace,
)
def get_hit_info(url):
par = urlparse.parse_qs(urlparse.urlparse(url).query)
return par['hitId'][0], par['assignmentId'][0], par['workerId'][0]
def connect_player(_Player, sid, response):
# Retrieve info from HTTP header
ip = response['REMOTE_ADDR']
hit_id, assignment_id, worker_id = get_hit_info(response['HTTP_REFERER'])
# Create player
player = _Player(sid, hit_id, assignment_id, worker_id, ip)
# Insert player in the database
with engine.begin() as conn:
player.session_id = insert_session(conn, player)
players[sid] = player
print('add: ' + str(sid))
@sio.on('connect', namespace='/q_oracle')
def q_oracle_connect(sid, re):
connect_player(QualifyOracle, sid, re)
@sio.on('connect', namespace='/oracle')
def oracle_connect(sid, re):
connect_player(Oracle, sid, re)
@sio.on('connect', namespace='/questioner')
def questioner_connect(sid, re):
connect_player(Questioner, sid, re)
@sio.on('connect', namespace='/q_questioner')
def q_questioner_connect(sid, re):
connect_player(QualifyQuestioner, sid, re)
@sio.on('update session', namespace='/q_oracle')
@sio.on('update session', namespace='/oracle')
@sio.on('update session', namespace='/q_questioner')
@sio.on('update session', namespace='/questioner')
def up_session(sid, msg):
player = players[sid]
player.assignment_id = msg['assignmentId']
player.hit_id = msg['hitId']
player.worker_id = msg['workerId']
with engine.begin() as conn:
update_session(conn, player)
@sio.on('disconnect', namespace='/oracle')
@sio.on('disconnect', namespace='/q_oracle')
@sio.on('disconnect', namespace='/questioner')
@sio.on('disconnect', namespace='/q_questioner')
def disconnect(sid):
if sid in players:
player = players[sid]
with engine.begin() as conn:
"""Four cases:
1. Player is in involved in dialogue."""
if player.partner is not None:
dialogue = dialogues[player.dialogue_id]
partner = player.partner
sio.emit('partner_disconnect',
'',
room=partner.sid,
namespace=partner.namespace)
if partner.role in ['Oracle', 'QualifyOracle']:
update_dialogue_status(conn, dialogue.id,
'questioner_disconnect')
else:
update_dialogue_status(conn, dialogue.id,
'oracle_disconnect')
if player.role == 'QualifyOracle' or player.role == 'QualifyQuestioner':
check_blocked(conn, player)
delete_game([player, partner])
if partner.role == 'Oracle':
find_normal_questioner(partner.sid)
elif partner.role == 'QualifyOracle':
find_qualification_questioner(partner.sid)
elif partner.role == 'QualifyQuestioner':
find_qualification_oracle(partner.sid)
else:
find_normal_oracle(partner.sid)
"""2. Player is in oracle queue."""
if player in oracle_queue:
oracle_queue.remove(player)
remove_from_queue(conn, player, 'disconnect')