-
Notifications
You must be signed in to change notification settings - Fork 0
/
websocket-client_20131102.py
211 lines (147 loc) · 5.62 KB
/
websocket-client_20131102.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
#!/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 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
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):
data = self.ws.recv()[4:]
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
while 1:
data = fb.recv()
if data == "":
continue
jData = json.loads(data)
message = jData['message'].encode('utf-8').lower().replace('"', """).replace("'", "'").replace('\\', '\')
username = jData['user'].encode('utf-8')
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)
# Sumar resultados
# Guardar en el log
q.remove(q[num_question])
num_question = random.randrange(len(q))
fb.send(escape(q[num_question].question))
# Comprobamos que no hayan llegado a 10
fb.disconnect()