-
Notifications
You must be signed in to change notification settings - Fork 0
/
websocket-client.py
426 lines (317 loc) · 14.8 KB
/
websocket-client.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from django.core.management import setup_environ
import settings
setup_environ(settings)
import os
import sys
import time
import random
import datetime
from quizbowl.models import *
from quizbowl.views import notify_user
from django.contrib.auth.models import User
from django.utils.html import escape
import httplib
import websocket
import threading
import time
import json
NUM_CORRECT = 10
QUESTIONS = 20
# NUM_CORRECT = 10
# QUESTIONS = 20
game_id = sys.argv[1]
# Extract quiz questions and fix the start time
game = Game.objects.get(pk=game_id)
game.start_time_committed = True
game.save()
c = Category.objects.filter(tournament = game.round.tournament)
#q = list(Question.objects.filter(categories = c))
q = []
for category in c:
questions = list(Question.objects.filter(categories = category))
for question in questions:
q.append(question)
# Set up codec
reload(sys)
sys.setdefaultencoding("utf-8")
break_thread = 0
class send_ping(threading.Thread):
def __init__(self, ws):
threading.Thread.__init__(self) # no need for extra args
self.ws = ws
def run(self):
global break_thread
while 1:
time.sleep(5)
self.ws.send("2::")
if break_thread == 1:
self.ws.send('0::')
break
class Fleqbot():
def __init__(self):
self.ws = ""
self.ping = ""
return
def connect(self, room):
conn = httplib.HTTPConnection("localhost:8004")
conn.request('GET','/socket.io/1/')
resp = conn.getresponse()
hk = resp.read()
hskey = ''.join(hk).split(':')[0]
self.ws = websocket.create_connection('ws://localhost:8004/socket.io/1/websocket/' + hskey)
data = self.ws.recv()
self.ws.send('3:::{"code": "1", "room": "' + room + '", "user": "FLEQBOT"}')
if self.ws.recv() == '3:::{"code":"1"}':
self.ping = send_ping(self.ws)
self.ping.start()
return 1
return 0
def send(self, message):
self.ws.send('3:::{"code": "2", "room": "' + game_id + '", "user": "FLEQBOT", "message": "' + message + '"}')
return
def recv(self):
try:
data = self.ws.recv()[4:]
except:
return ""
return data
def disconnect(self):
global break_thread
break_thread = 1
return
if __name__ == "__main__":
fb = Fleqbot()
fb.connect(game_id)
# Start countdown
fb.send('La partida empieza en 3 min')
time.sleep(60)
fb.send('Quedan 2 minutos...')
time.sleep(30)
fb.send('Solo 1 min 30 seg...')
time.sleep(30)
fb.send('Un minuto para el comienzo!')
time.sleep(30)
fb.send('30 segundos')
time.sleep(20)
fb.send('La partida entre ' + game.player1.username + " y " + game.player2.username + ' correspondiente al torneo ' + str(game.round) + ' va a empezar')
time.sleep(5)
fb.send('Recordamos que es indiferente contestar en mayúsculas o en minúsculas')
time.sleep(5)
fb.send('Una vez lanzada la pregunta, se dispone de 90 segundos para contestar')
time.sleep(5)
fb.send('El primer jugador en conseguir ' + str(NUM_CORRECT) + ' respuestas correctas gana la partida')
time.sleep(5)
fb.send('Se dispone de un total de ' + str(QUESTIONS) + ' preguntas')
time.sleep(5)
fb.send('Si se consumen sin que ningún jugador alcance ' + str(NUM_CORRECT) + ' aciertos, ganará el que más haya acertado')
time.sleep(5)
fb.send('En caso de haber empate, se considerará que ambos jugadores pierden')
time.sleep(5)
fb.send('SUERTE!!')
time.sleep(3)
# Create log file
logfile = open('/pfc-jgonzalez-data/home/jgonzalez/virtualenvs/fleq/fleq/logs/' + str(game_id), 'w')
# Set up auxiliar variables
num_msg = 0
score1 = 0
score2 = 0
game_finished = False
winner = ''
lap = 0
# Start game
# Select and send first question
num_question = random.randrange(len(q))
fb.send(escape(q[num_question].question))
QUESTIONS = QUESTIONS - 1
what = '4'
while 1:
fb.ws.settimeout(1)
data = fb.recv()
if data == "":
lap = lap + 1
#print lap
#cCMBIAR LOS "= SEGUNDOS A 90 SEGUNDOS
if lap == 90:
fb.send("Los 90 segundos se han cumplido sin que se haya acertado la respuesta")
time.sleep(3)
fb.send("La respuesta correcta era: " + q[num_question].answer)
time.sleep(3)
if QUESTIONS == 1:
fb.send("Última pregunta disponible...")
time.sleep(3)
if QUESTIONS != 0:
q.remove(q[num_question])
num_question = random.randrange(len(q))
fb.send(escape(q[num_question].question))
else:
game_finished = True
fb.send("Se han acabado las preguntas dispobibles y la partida ha terminado")
time.sleep(1)
fb.send ('El resultado final ha sido: ' + str(score1) + ' - ' + str(score2))
time.sleep(1)
fb.send("Podéis consultar la tabla del torneo para conocer la clasificación. Hasta la próxima!")
break
QUESTIONS = QUESTIONS - 1
lap = 0
continue
jData = json.loads(data)
message = jData['message'].encode('utf-8').lower().replace('"', """).replace("'", "'").replace('\\', '\')
username = jData['user'].encode('utf-8')
#OJO GUARDAR TB EN LOS LOGS ESTO:
#tournament = game.round.tournament
#game.round.round_number
if (username == 'FLEQBOT') and (message == (q[num_question].question).lower()):
what = '0' #pregunta lanzada por fleqbot
success = '2' #ni acierta ni falla
datequestion = time.time()
tiempo = "0"
numquestion = str(q[num_question].id)
elif ((username == game.player1.username) and (what !='4') and (what !='3')):
what ='1' # mensaje del usuario1
if (message == str(escape(q[num_question].answer)).lower() or message == str(escape(q[num_question].alt_answer1)).lower() \
or message == str(escape(q[num_question].alt_answer2)).lower() or message == str(escape(q[num_question].alt_answer3)).lower()) \
and message != "":
success = '1' #acierta
restatiempo = time.time()- datequestion
tiempo = str(restatiempo)
numquestion = str(q[num_question].id)
else:
success = '0' #ha fallado
tiempo = "0"
numquestion = str(q[num_question].id)
elif ((username == game.player1.username) and (what =='4')):
what = '4'
success = '2'
tiempo = "0"
elif ((username == game.player2.username)and (what !='4') and (what !='3')):
what = '2' #mensaje del usuario2
if (message == str(escape(q[num_question].answer)).lower() or message == str(escape(q[num_question].alt_answer1)).lower() \
or message == str(escape(q[num_question].alt_answer2)).lower() or message == str(escape(q[num_question].alt_answer3)).lower()) \
and message != "":
success = '1' #acierta
restatiempo = time.time()- datequestion
tiempo = str(restatiempo)
numquestion = str(q[num_question].id)
else:
success = '0' #ha fallado
tiempo = "0"
numquestion = str(q[num_question].id)
elif ((username == game.player2.username) and (what =='4')):
what = '4'
success = '2'
tiempo = "0"
numquestion = "0"
elif (username == 'FLEQBOT') and not (message == (q[num_question].question).lower()):
what = '3' #mensajes de instrucciones de fleqbot
success = '2' #ni acierta ni falla
tiempo = "0"
numquestion = "0"
logfile.write(str(datetime.datetime.utcnow().time()) + ";" + username + ";" + message + ";" + what + ";" + success + ";" + tiempo + ";" + numquestion + ";\n")
print "-------------------------------"
print username
print message
print "-------------------------------"
print "CORRECT ANSWER"
print q[num_question].answer
if (message == str(escape(q[num_question].answer)).lower() or message == str(escape(q[num_question].alt_answer1)).lower() \
or message == str(escape(q[num_question].alt_answer2)).lower() or message == str(escape(q[num_question].alt_answer3)).lower()) \
and message != "":
time.sleep(1)
fb.send("Respuesta correcta de " + username + "!")
time.sleep(3)
if username == game.player1.username:
fb.send(game.player1.username + ' ha acertado!')
score1 = score1 + 1
elif username == game.player2.username:
fb.send(game.player2.username + ' ha acertado!')
score2 = score2 + 1
time.sleep(3)
fb.send(game.player1.username + " " + str(score1) + ':' + str(score2) + " " + game.player2.username)
time.sleep(2)
if score1 == NUM_CORRECT or score2 == NUM_CORRECT:
game_finished = True
fb.send('La partida ha terminado')
time.sleep(3)
fb.send ('El resultado final ha sido: ' + str(score1) + ' - ' + str(score2))
time.sleep(3)
if score1 > score2:
winner = game.player1
loser = game.player2
print "PLAYER1"
elif score2 > score1:
winner = game.player2
loser = game.player1
print "PLAYER2"
if score1 != score2:
game.winner = winner
fb.send(winner.username + " ha sido el vencedor")
time.sleep(2)
fb.send("ENHORABUENA!!")
else:
fleqbot = User.objects.get(username="FLEQBOT")
game.winner = fleqbot
elem.send_keys("Habéis empatado tras agotar el lote de preguntas disponibles para la partida")
time.sleep(3)
elem.send_keys("El sistema de puntuación aún no contempla empates, por lo que ambos habéis perdido esta partida...")
time.sleep(3)
elem.send_keys("Al empatar por lo menos habéis evitado que vuestro oponente sume una victoria, no está mal!")
time.sleep(3)
fb.send("Podéis consultar la tabla del torneo para conocer la clasificación. Hasta la próxima!")
scorepl1 = Score.objects.get(player=game.player1, tournament=game.round.tournament)
scorepl2 = Score.objects.get(player=game.player2, tournament=game.round.tournament)
game.score_player1 = score1
game.score_player2 = score2
scorepl1.questions_won += score1
scorepl1.questions_lost += score2
scorepl2.questions_lost += score1
scorepl2.questions_won += score2
game.save()
scorepl1.save()
scorepl2.save()
print "SAVE GAME"
if score2 != score1:
winnerProfile = UserProfile.objects.get(user=winner)
loserProfile = UserProfile.objects.get(user=loser)
winnerProfile.winner_games = winnerProfile.winner_games + 1
loserProfile.loser_games = loserProfile.loser_games + 1
winnerProfile.save()
loserProfile.save()
if score1 > score2:
scorepl1.points = scorepl1.points + 1
scorepl1.save()
elif score2 > score1:
scorepl2.points = scorepl2.points + 1
scorepl2.save()
else:
player1pf = UserProfile.objects.get(user=game.player1)
player2pf = UserProfile.objects.get(user=game.player2)
player1pf.loser_games = player1pf.loser_games + 1
player2pf.loser_games = player2pf.loser_games + 1
player1pf.save()
player2pf.save()
if game.round.round_number == game.round.tournament.rounds:
if not Game.objects.filter(round = game.round):
for score in Score.objects.filter(tournament = game.round.tournament):
notify_user(score.player, 'tournament_over', game.round.tournament)
notify_user(game.round.tournament.admin, 'tournament_over', game.round.tournament)
break
#Comprobamos si es la última pregunta disponible
if QUESTIONS == 1:
fb.send("Última pregunta disponible...")
time.sleep(3)
q.remove(q[num_question])
num_question = random.randrange(len(q))
fb.send(escape(q[num_question].question))
QUESTIONS = QUESTIONS - 1
print "Preguntas que quedan por hacer...."
print QUESTIONS
lap = 0
# Sumar resultados
# Guardar en el log
logfile.close()
game.log = True
game.save()
time.sleep(2)
fb.disconnect()