comparison src/plugins/plugin_misc_quiz.py @ 1359:83127a4c89ce frontends_multi_profiles

plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
author souliane <souliane@mailoo.org>
date Wed, 11 Mar 2015 12:36:22 +0100
parents 301b342c697a
children 069ad98b360d
comparison
equal deleted inserted replaced
1358:bf3f669a6052 1359:83127a4c89ce
56 56
57 def __init__(self, host): 57 def __init__(self, host):
58 log.info(_("Plugin Quiz initialization")) 58 log.info(_("Plugin Quiz initialization"))
59 self.inheritFromRoomGame(host) 59 self.inheritFromRoomGame(host)
60 RoomGame._init_(self, host, PLUGIN_INFO, (NS_QG, QG_TAG), game_init={'stage': None}, player_init={'score': 0}) 60 RoomGame._init_(self, host, PLUGIN_INFO, (NS_QG, QG_TAG), game_init={'stage': None}, player_init={'score': 0})
61 host.bridge.addMethod("quizGameLaunch", ".plugin", in_sign='asss', out_sign='', method=self.prepareRoom) # args: players, room_jid, profile 61 host.bridge.addMethod("quizGameLaunch", ".plugin", in_sign='asss', out_sign='', method=self._prepareRoom) # args: players, room_jid, profile
62 host.bridge.addMethod("quizGameCreate", ".plugin", in_sign='sass', out_sign='', method=self.createGame) # args: room_jid, players, profile 62 host.bridge.addMethod("quizGameCreate", ".plugin", in_sign='sass', out_sign='', method=self._createGame) # args: room_jid, players, profile
63 host.bridge.addMethod("quizGameReady", ".plugin", in_sign='sss', out_sign='', method=self.playerReady) # args: player, referee, profile 63 host.bridge.addMethod("quizGameReady", ".plugin", in_sign='sss', out_sign='', method=self._playerReady) # args: player, referee, profile
64 host.bridge.addMethod("quizGameAnswer", ".plugin", in_sign='ssss', out_sign='', method=self.playerAnswer) 64 host.bridge.addMethod("quizGameAnswer", ".plugin", in_sign='ssss', out_sign='', method=self.playerAnswer)
65 host.bridge.addSignal("quizGameStarted", ".plugin", signature='ssass') # args: room_jid, referee, players, profile 65 host.bridge.addSignal("quizGameStarted", ".plugin", signature='ssass') # args: room_jid, referee, players, profile
66 host.bridge.addSignal("quizGameNew", ".plugin", 66 host.bridge.addSignal("quizGameNew", ".plugin",
67 signature='sa{ss}s', 67 signature='sa{ss}s',
68 doc={'summary': 'Start a new game', 68 doc={'summary': 'Start a new game',
190 answer_elt.addContent(answer) 190 answer_elt.addContent(answer)
191 self.host.profiles[profile].xmlstream.send(mess) 191 self.host.profiles[profile].xmlstream.send(mess)
192 192
193 def timerExpired(self, room_jid, profile): 193 def timerExpired(self, room_jid, profile):
194 """Called when nobody answered the question in time""" 194 """Called when nobody answered the question in time"""
195 game_data = self.games[room_jid.userhost()] 195 game_data = self.games[room_jid]
196 game_data['stage'] = 'expired' 196 game_data['stage'] = 'expired'
197 mess = self.createGameElt(room_jid) 197 mess = self.createGameElt(room_jid)
198 expired_elt = mess.firstChildElement().addElement('timer_expired') 198 expired_elt = mess.firstChildElement().addElement('timer_expired')
199 self.host.profiles[profile].xmlstream.send(mess) 199 self.host.profiles[profile].xmlstream.send(mess)
200 reactor.callLater(4, self.askQuestion, room_jid, profile) 200 reactor.callLater(4, self.askQuestion, room_jid, profile)
201 201
202 def pauseTimer(self, room_jid): 202 def pauseTimer(self, room_jid):
203 """Stop the timer and save the time left""" 203 """Stop the timer and save the time left"""
204 game_data = self.games[room_jid.userhost()] 204 game_data = self.games[room_jid]
205 left = max(0, game_data["timer"].getTime() - time()) 205 left = max(0, game_data["timer"].getTime() - time())
206 game_data['timer'].cancel() 206 game_data['timer'].cancel()
207 game_data['time_left'] = int(left) 207 game_data['time_left'] = int(left)
208 game_data['previous_stage'] = game_data['stage'] 208 game_data['previous_stage'] = game_data['stage']
209 game_data['stage'] = "paused" 209 game_data['stage'] = "paused"
210 210
211 def restartTimer(self, room_jid, profile): 211 def restartTimer(self, room_jid, profile):
212 """Restart a timer with the saved time""" 212 """Restart a timer with the saved time"""
213 game_data = self.games[room_jid.userhost()] 213 game_data = self.games[room_jid]
214 assert game_data['time_left'] is not None 214 assert game_data['time_left'] is not None
215 mess = self.createGameElt(room_jid) 215 mess = self.createGameElt(room_jid)
216 restarted_elt = mess.firstChildElement().addElement('timer_restarted') 216 restarted_elt = mess.firstChildElement().addElement('timer_restarted')
217 restarted_elt["time_left"] = str(game_data['time_left']) 217 restarted_elt["time_left"] = str(game_data['time_left'])
218 self.host.profiles[profile].xmlstream.send(mess) 218 self.host.profiles[profile].xmlstream.send(mess)
221 game_data['stage'] = game_data['previous_stage'] 221 game_data['stage'] = game_data['previous_stage']
222 del game_data['previous_stage'] 222 del game_data['previous_stage']
223 223
224 def askQuestion(self, room_jid, profile): 224 def askQuestion(self, room_jid, profile):
225 """Ask a new question""" 225 """Ask a new question"""
226 game_data = self.games[room_jid.userhost()] 226 game_data = self.games[room_jid]
227 game_data['stage'] = "question" 227 game_data['stage'] = "question"
228 game_data['question_id'] = "1" 228 game_data['question_id'] = "1"
229 timer = 30 229 timer = 30
230 mess = self.createGameElt(room_jid) 230 mess = self.createGameElt(room_jid)
231 mess.firstChildElement().addChild(self.__ask_question(game_data['question_id'], u"Quel est l'âge du capitaine ?", timer)) 231 mess.firstChildElement().addChild(self.__ask_question(game_data['question_id'], u"Quel est l'âge du capitaine ?", timer))
233 game_data["timer"] = reactor.callLater(timer, self.timerExpired, room_jid, profile) 233 game_data["timer"] = reactor.callLater(timer, self.timerExpired, room_jid, profile)
234 game_data["time_left"] = None 234 game_data["time_left"] = None
235 235
236 def checkAnswer(self, room_jid, player, answer, profile): 236 def checkAnswer(self, room_jid, player, answer, profile):
237 """Check if the answer given is right""" 237 """Check if the answer given is right"""
238 game_data = self.games[room_jid.userhost()] 238 game_data = self.games[room_jid]
239 players_data = game_data['players_data'] 239 players_data = game_data['players_data']
240 good_answer = game_data['question_id'] == "1" and answer == "42" 240 good_answer = game_data['question_id'] == "1" and answer == "42"
241 players_data[player]['score'] += 1 if good_answer else -1 241 players_data[player]['score'] += 1 if good_answer else -1
242 players_data[player]['score'] = min(9, max(0, players_data[player]['score'])) 242 players_data[player]['score'] = min(9, max(0, players_data[player]['score']))
243 243
262 262
263 def room_game_cmd(self, mess_elt, profile): 263 def room_game_cmd(self, mess_elt, profile):
264 from_jid = jid.JID(mess_elt['from']) 264 from_jid = jid.JID(mess_elt['from'])
265 room_jid = jid.JID(from_jid.userhost()) 265 room_jid = jid.JID(from_jid.userhost())
266 game_elt = mess_elt.firstChildElement() 266 game_elt = mess_elt.firstChildElement()
267 game_data = self.games[room_jid.userhost()] 267 game_data = self.games[room_jid]
268 if 'players_data' in game_data: 268 if 'players_data' in game_data:
269 players_data = game_data['players_data'] 269 players_data = game_data['players_data']
270 270
271 for elt in game_elt.elements(): 271 for elt in game_elt.elements():
272 272
276 players.append(unicode(player)) 276 players.append(unicode(player))
277 self.host.bridge.quizGameStarted(room_jid.userhost(), from_jid.full(), players, profile) 277 self.host.bridge.quizGameStarted(room_jid.userhost(), from_jid.full(), players, profile)
278 278
279 elif elt.name == 'player_ready': # ready to play 279 elif elt.name == 'player_ready': # ready to play
280 player = elt['player'] 280 player = elt['player']
281 status = self.games[room_jid.userhost()]['status'] 281 status = self.games[room_jid]['status']
282 nb_players = len(self.games[room_jid.userhost()]['players']) 282 nb_players = len(self.games[room_jid]['players'])
283 status[player] = 'ready' 283 status[player] = 'ready'
284 log.debug(_('Player %(player)s is ready to start [status: %(status)s]') % {'player': player, 'status': status}) 284 log.debug(_('Player %(player)s is ready to start [status: %(status)s]') % {'player': player, 'status': status})
285 if status.values().count('ready') == nb_players: # everybody is ready, we can start the game 285 if status.values().count('ready') == nb_players: # everybody is ready, we can start the game
286 self.newGame(room_jid, profile) 286 self.newGame(room_jid, profile)
287 287
292 self.host.bridge.quizGameQuestion(room_jid.userhost(), elt["id"], unicode(elt), int(elt["timer"]), profile) 292 self.host.bridge.quizGameQuestion(room_jid.userhost(), elt["id"], unicode(elt), int(elt["timer"]), profile)
293 293
294 elif elt.name == 'player_answer': 294 elif elt.name == 'player_answer':
295 player = elt['player'] 295 player = elt['player']
296 pause = game_data['stage'] == 'question' # we pause the game only if we are have a question at the moment 296 pause = game_data['stage'] == 'question' # we pause the game only if we are have a question at the moment
297 #we first send a buzzer message 297 # we first send a buzzer message
298 mess = self.createGameElt(room_jid) 298 mess = self.createGameElt(room_jid)
299 buzzer_elt = mess.firstChildElement().addElement('player_buzzed') 299 buzzer_elt = mess.firstChildElement().addElement('player_buzzed')
300 buzzer_elt['player'] = player 300 buzzer_elt['player'] = player
301 buzzer_elt['pause'] = str(pause) 301 buzzer_elt['pause'] = str(pause)
302 self.host.profiles[profile].xmlstream.send(mess) 302 self.host.profiles[profile].xmlstream.send(mess)
303 if pause: 303 if pause:
304 self.pauseTimer(room_jid) 304 self.pauseTimer(room_jid)
305 #and we send the player answer 305 # and we send the player answer
306 mess = self.createGameElt(room_jid) 306 mess = self.createGameElt(room_jid)
307 _answer = unicode(elt) 307 _answer = unicode(elt)
308 say_elt = mess.firstChildElement().addElement('player_says') 308 say_elt = mess.firstChildElement().addElement('player_says')
309 say_elt['player'] = player 309 say_elt['player'] = player
310 say_elt.addContent(_answer) 310 say_elt.addContent(_answer)