comparison src/plugins/plugin_misc_quiz.py @ 2129:6a66c8c5a567

core: replaced calls to client.xmlstream.send by client.send which is the right method to use. client.xmlstream should not be used directly
author Goffi <goffi@goffi.org>
date Sat, 04 Feb 2017 17:59:13 +0100
parents 2daf7b4c6756
children 33c8c4973743
comparison
equal deleted inserted replaced
2128:aa94f33fd2ad 2129:6a66c8c5a567
20 from sat.core.i18n import _ 20 from sat.core.i18n import _
21 from sat.core.constants import Const as C 21 from sat.core.constants import Const as C
22 from sat.core.log import getLogger 22 from sat.core.log import getLogger
23 log = getLogger(__name__) 23 log = getLogger(__name__)
24 from twisted.words.xish import domish 24 from twisted.words.xish import domish
25 from twisted.internet import protocol, defer, threads, reactor 25 from twisted.internet import reactor
26 from twisted.words.protocols.jabber import client, jid, xmlstream 26 from twisted.words.protocols.jabber import client as jabber_client, jid
27 from twisted.words.protocols import jabber
28 from twisted.words.protocols.jabber.xmlstream import IQ
29 import random
30
31 from wokkel import data_form
32 from time import time 27 from time import time
33 28
34 29
35 NS_QG = 'http://www.goffi.org/protocol/quiz' 30 NS_QG = 'http://www.goffi.org/protocol/quiz'
36 QG_TAG = 'quiz' 31 QG_TAG = 'quiz'
167 question_elt.addContent(question) 162 question_elt.addContent(question)
168 return question_elt 163 return question_elt
169 164
170 def __start_play(self, room_jid, game_data, profile): 165 def __start_play(self, room_jid, game_data, profile):
171 """Start the game (tell to the first player after dealer to play""" 166 """Start the game (tell to the first player after dealer to play"""
167 client = self.host.getClient(profile)
172 game_data['stage'] = "play" 168 game_data['stage'] = "play"
173 next_player_idx = game_data['current_player'] = (game_data['init_player'] + 1) % len(game_data['players']) # the player after the dealer start 169 next_player_idx = game_data['current_player'] = (game_data['init_player'] + 1) % len(game_data['players']) # the player after the dealer start
174 game_data['first_player'] = next_player = game_data['players'][next_player_idx] 170 game_data['first_player'] = next_player = game_data['players'][next_player_idx]
175 to_jid = jid.JID(room_jid.userhost() + "/" + next_player) 171 to_jid = jid.JID(room_jid.userhost() + "/" + next_player)
176 mess = self.createGameElt(to_jid) 172 mess = self.createGameElt(to_jid)
177 yourturn_elt = mess.firstChildElement().addElement('your_turn') 173 mess.firstChildElement().addElement('your_turn')
178 self.host.profiles[profile].xmlstream.send(mess) 174 client.send(mess)
179 175
180 def playerAnswer(self, player, referee, answer, profile_key=C.PROF_KEY_NONE): 176 def playerAnswer(self, player, referee, answer, profile_key=C.PROF_KEY_NONE):
181 """Called when a player give an answer""" 177 """Called when a player give an answer"""
182 profile = self.host.memory.getProfileName(profile_key) 178 client = self.host.getClient(profile_key)
183 if not profile: 179 log.debug(u'new player answer (%(profile)s): %(answer)s' % {'profile': client.profile, 'answer': answer})
184 log.error(_(u"profile %s is unknown") % profile_key)
185 return
186 log.debug(u'new player answer (%(profile)s): %(answer)s' % {'profile': profile, 'answer': answer})
187 mess = self.createGameElt(jid.JID(referee)) 180 mess = self.createGameElt(jid.JID(referee))
188 answer_elt = mess.firstChildElement().addElement('player_answer') 181 answer_elt = mess.firstChildElement().addElement('player_answer')
189 answer_elt['player'] = player 182 answer_elt['player'] = player
190 answer_elt.addContent(answer) 183 answer_elt.addContent(answer)
191 self.host.profiles[profile].xmlstream.send(mess) 184 client.send(mess)
192 185
193 def timerExpired(self, room_jid, profile): 186 def timerExpired(self, room_jid, profile):
194 """Called when nobody answered the question in time""" 187 """Called when nobody answered the question in time"""
188 client = self.host.getClient(profile)
195 game_data = self.games[room_jid] 189 game_data = self.games[room_jid]
196 game_data['stage'] = 'expired' 190 game_data['stage'] = 'expired'
197 mess = self.createGameElt(room_jid) 191 mess = self.createGameElt(room_jid)
198 expired_elt = mess.firstChildElement().addElement('timer_expired') 192 mess.firstChildElement().addElement('timer_expired')
199 self.host.profiles[profile].xmlstream.send(mess) 193 client.send(mess)
200 reactor.callLater(4, self.askQuestion, room_jid, profile) 194 reactor.callLater(4, self.askQuestion, room_jid, client.profile)
201 195
202 def pauseTimer(self, room_jid): 196 def pauseTimer(self, room_jid):
203 """Stop the timer and save the time left""" 197 """Stop the timer and save the time left"""
204 game_data = self.games[room_jid] 198 game_data = self.games[room_jid]
205 left = max(0, game_data["timer"].getTime() - time()) 199 left = max(0, game_data["timer"].getTime() - time())
208 game_data['previous_stage'] = game_data['stage'] 202 game_data['previous_stage'] = game_data['stage']
209 game_data['stage'] = "paused" 203 game_data['stage'] = "paused"
210 204
211 def restartTimer(self, room_jid, profile): 205 def restartTimer(self, room_jid, profile):
212 """Restart a timer with the saved time""" 206 """Restart a timer with the saved time"""
207 client = self.host.getClient(profile)
213 game_data = self.games[room_jid] 208 game_data = self.games[room_jid]
214 assert game_data['time_left'] is not None 209 assert game_data['time_left'] is not None
215 mess = self.createGameElt(room_jid) 210 mess = self.createGameElt(room_jid)
216 restarted_elt = mess.firstChildElement().addElement('timer_restarted') 211 mess.firstChildElement().addElement('timer_restarted')
217 restarted_elt["time_left"] = str(game_data['time_left']) 212 jabber_client.restarted_elt["time_left"] = str(game_data['time_left'])
218 self.host.profiles[profile].xmlstream.send(mess) 213 client.send(mess)
219 game_data["timer"] = reactor.callLater(game_data['time_left'], self.timerExpired, room_jid, profile) 214 game_data["timer"] = reactor.callLater(game_data['time_left'], self.timerExpired, room_jid, profile)
220 game_data["time_left"] = None 215 game_data["time_left"] = None
221 game_data['stage'] = game_data['previous_stage'] 216 game_data['stage'] = game_data['previous_stage']
222 del game_data['previous_stage'] 217 del game_data['previous_stage']
223 218
224 def askQuestion(self, room_jid, profile): 219 def askQuestion(self, room_jid, profile):
225 """Ask a new question""" 220 """Ask a new question"""
221 client = self.host.getClient(profile)
226 game_data = self.games[room_jid] 222 game_data = self.games[room_jid]
227 game_data['stage'] = "question" 223 game_data['stage'] = "question"
228 game_data['question_id'] = "1" 224 game_data['question_id'] = "1"
229 timer = 30 225 timer = 30
230 mess = self.createGameElt(room_jid) 226 mess = self.createGameElt(room_jid)
231 mess.firstChildElement().addChild(self.__ask_question(game_data['question_id'], u"Quel est l'âge du capitaine ?", timer)) 227 mess.firstChildElement().addChild(self.__ask_question(game_data['question_id'], u"Quel est l'âge du capitaine ?", timer))
232 self.host.profiles[profile].xmlstream.send(mess) 228 client.send(mess)
233 game_data["timer"] = reactor.callLater(timer, self.timerExpired, room_jid, profile) 229 game_data["timer"] = reactor.callLater(timer, self.timerExpired, room_jid, profile)
234 game_data["time_left"] = None 230 game_data["time_left"] = None
235 231
236 def checkAnswer(self, room_jid, player, answer, profile): 232 def checkAnswer(self, room_jid, player, answer, profile):
237 """Check if the answer given is right""" 233 """Check if the answer given is right"""
234 client = self.host.getClient(profile)
238 game_data = self.games[room_jid] 235 game_data = self.games[room_jid]
239 players_data = game_data['players_data'] 236 players_data = game_data['players_data']
240 good_answer = game_data['question_id'] == "1" and answer == "42" 237 good_answer = game_data['question_id'] == "1" and answer == "42"
241 players_data[player]['score'] += 1 if good_answer else -1 238 players_data[player]['score'] += 1 if good_answer else -1
242 players_data[player]['score'] = min(9, max(0, players_data[player]['score'])) 239 players_data[player]['score'] = min(9, max(0, players_data[player]['score']))
243 240
244 mess = self.createGameElt(room_jid) 241 mess = self.createGameElt(room_jid)
245 mess.firstChildElement().addChild(self.__answer_result(player, good_answer, game_data)) 242 mess.firstChildElement().addChild(self.__answer_result(player, good_answer, game_data))
246 self.host.profiles[profile].xmlstream.send(mess) 243 client.send(mess)
247 244
248 if good_answer: 245 if good_answer:
249 reactor.callLater(4, self.askQuestion, room_jid, profile) 246 reactor.callLater(4, self.askQuestion, room_jid, profile)
250 else: 247 else:
251 reactor.callLater(4, self.restartTimer, room_jid, profile) 248 reactor.callLater(4, self.restartTimer, room_jid, profile)
259 msg_elts = self.__game_data_to_xml(new_game_data) 256 msg_elts = self.__game_data_to_xml(new_game_data)
260 RoomGame.newRound(self, room_jid, (common_data, msg_elts), profile) 257 RoomGame.newRound(self, room_jid, (common_data, msg_elts), profile)
261 reactor.callLater(10, self.askQuestion, room_jid, profile) 258 reactor.callLater(10, self.askQuestion, room_jid, profile)
262 259
263 def room_game_cmd(self, mess_elt, profile): 260 def room_game_cmd(self, mess_elt, profile):
261 client = self.host.getClient(profile)
264 from_jid = jid.JID(mess_elt['from']) 262 from_jid = jid.JID(mess_elt['from'])
265 room_jid = jid.JID(from_jid.userhost()) 263 room_jid = jid.JID(from_jid.userhost())
266 game_elt = mess_elt.firstChildElement() 264 game_elt = mess_elt.firstChildElement()
267 game_data = self.games[room_jid] 265 game_data = self.games[room_jid]
268 if 'players_data' in game_data: 266 # if 'players_data' in game_data:
269 players_data = game_data['players_data'] 267 #  players_data = game_data['players_data']
270 268
271 for elt in game_elt.elements(): 269 for elt in game_elt.elements():
272 270
273 if elt.name == 'started': # new game created 271 if elt.name == 'started': # new game created
274 players = [] 272 players = []
297 # we first send a buzzer message 295 # we first send a buzzer message
298 mess = self.createGameElt(room_jid) 296 mess = self.createGameElt(room_jid)
299 buzzer_elt = mess.firstChildElement().addElement('player_buzzed') 297 buzzer_elt = mess.firstChildElement().addElement('player_buzzed')
300 buzzer_elt['player'] = player 298 buzzer_elt['player'] = player
301 buzzer_elt['pause'] = str(pause) 299 buzzer_elt['pause'] = str(pause)
302 self.host.profiles[profile].xmlstream.send(mess) 300 client.send(mess)
303 if pause: 301 if pause:
304 self.pauseTimer(room_jid) 302 self.pauseTimer(room_jid)
305 # and we send the player answer 303 # and we send the player answer
306 mess = self.createGameElt(room_jid) 304 mess = self.createGameElt(room_jid)
307 _answer = unicode(elt) 305 _answer = unicode(elt)
308 say_elt = mess.firstChildElement().addElement('player_says') 306 say_elt = mess.firstChildElement().addElement('player_says')
309 say_elt['player'] = player 307 say_elt['player'] = player
310 say_elt.addContent(_answer) 308 say_elt.addContent(_answer)
311 say_elt['delay'] = "3" 309 say_elt['delay'] = "3"
312 reactor.callLater(2, self.host.profiles[profile].xmlstream.send, mess) 310 reactor.callLater(2, client.send, mess)
313 reactor.callLater(6, self.checkAnswer, room_jid, player, _answer, profile=profile) 311 reactor.callLater(6, self.checkAnswer, room_jid, player, _answer, profile=profile)
314 312
315 elif elt.name == 'player_buzzed': 313 elif elt.name == 'player_buzzed':
316 self.host.bridge.quizGamePlayerBuzzed(room_jid.userhost(), elt["player"], elt['pause'] == str(True), profile) 314 self.host.bridge.quizGamePlayerBuzzed(room_jid.userhost(), elt["player"], elt['pause'] == str(True), profile)
317 315