comparison src/plugins/plugin_misc_quiz.py @ 594:e629371a28d3

Fix pep8 support in src/plugins.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Fri, 18 Jan 2013 17:55:35 +0100
parents beaf6bec2fcd
children 84a6e83157c2
comparison
equal deleted inserted replaced
593:70bae685d05c 594:e629371a28d3
44 NS_QG = 'http://www.goffi.org/protocol/quiz' 44 NS_QG = 'http://www.goffi.org/protocol/quiz'
45 QG_TAG = 'quiz' 45 QG_TAG = 'quiz'
46 QG_REQUEST = MESSAGE + '/' + QG_TAG + '[@xmlns="' + NS_QG + '"]' 46 QG_REQUEST = MESSAGE + '/' + QG_TAG + '[@xmlns="' + NS_QG + '"]'
47 47
48 PLUGIN_INFO = { 48 PLUGIN_INFO = {
49 "name": "Quiz game plugin", 49 "name": "Quiz game plugin",
50 "import_name": "Quiz", 50 "import_name": "Quiz",
51 "type": "Game", 51 "type": "Game",
52 "protocols": [], 52 "protocols": [],
53 "dependencies": ["XEP-0045", "XEP-0249"], 53 "dependencies": ["XEP-0045", "XEP-0249"],
54 "main": "Quiz", 54 "main": "Quiz",
55 "handler": "yes", 55 "handler": "yes",
56 "description": _("""Implementation of Quiz game""") 56 "description": _("""Implementation of Quiz game""")
57 } 57 }
58 58
59 59
60 class Quiz(object): 60 class Quiz(object):
61 61
62 def __init__(self, host): 62 def __init__(self, host):
63 info(_("Plugin Quiz initialization")) 63 info(_("Plugin Quiz initialization"))
64 self.host = host 64 self.host = host
65 self.games={} 65 self.games = {}
66 self.waiting_inv = {} #Invitation waiting for people to join to launch a game 66 self.waiting_inv = {} # Invitation waiting for people to join to launch a game
67 host.bridge.addMethod("quizGameLaunch", ".plugin", in_sign='ass', out_sign='', method=self.quizGameLaunch) #args: players, profile 67 host.bridge.addMethod("quizGameLaunch", ".plugin", in_sign='ass', out_sign='', method=self.quizGameLaunch) # args: players, profile
68 host.bridge.addMethod("quizGameCreate", ".plugin", in_sign='sass', out_sign='', method=self.quizGameCreate) #args: room_jid, players, profile 68 host.bridge.addMethod("quizGameCreate", ".plugin", in_sign='sass', out_sign='', method=self.quizGameCreate) # args: room_jid, players, profile
69 host.bridge.addMethod("quizGameReady", ".plugin", in_sign='sss', out_sign='', method=self.newPlayerReady) #args: player, referee, profile 69 host.bridge.addMethod("quizGameReady", ".plugin", in_sign='sss', out_sign='', method=self.newPlayerReady) # args: player, referee, profile
70 host.bridge.addMethod("quizGameAnswer", ".plugin", in_sign='ssss', out_sign='', method=self.playerAnswer) 70 host.bridge.addMethod("quizGameAnswer", ".plugin", in_sign='ssss', out_sign='', method=self.playerAnswer)
71 host.bridge.addSignal("quizGameStarted", ".plugin", signature='ssass') #args: room_jid, referee, players, profile 71 host.bridge.addSignal("quizGameStarted", ".plugin", signature='ssass') # args: room_jid, referee, players, profile
72 host.bridge.addSignal("quizGameNew", ".plugin", 72 host.bridge.addSignal("quizGameNew", ".plugin",
73 signature='sa{ss}s', 73 signature='sa{ss}s',
74 doc = { 'summary': 'Start a new game', 74 doc={'summary': 'Start a new game',
75 'param_0': "room_jid: jid of game's room", 75 'param_0': "room_jid: jid of game's room",
76 'param_1': "game_data: data of the game", 76 'param_1': "game_data: data of the game",
77 'param_2': '%(doc_profile)s'}) 77 'param_2': '%(doc_profile)s'})
78 host.bridge.addSignal("quizGameQuestion", ".plugin", 78 host.bridge.addSignal("quizGameQuestion", ".plugin",
79 signature = 'sssis', 79 signature='sssis',
80 doc = { 'summary': "Send the current question", 80 doc={'summary': "Send the current question",
81 'param_0': "room_jid: jid of game's room", 81 'param_0': "room_jid: jid of game's room",
82 'param_1': "question_id: question id", 82 'param_1': "question_id: question id",
83 'param_2': "question: question to ask", 83 'param_2': "question: question to ask",
84 'param_3': "timer: timer", 84 'param_3': "timer: timer",
85 'param_4': '%(doc_profile)s'}) 85 'param_4': '%(doc_profile)s'})
86 host.bridge.addSignal("quizGamePlayerBuzzed", ".plugin", 86 host.bridge.addSignal("quizGamePlayerBuzzed", ".plugin",
87 signature = 'ssbs', 87 signature='ssbs',
88 doc = { 'summary': "A player just pressed the buzzer", 88 doc={'summary': "A player just pressed the buzzer",
89 'param_0': "room_jid: jid of game's room", 89 'param_0': "room_jid: jid of game's room",
90 'param_1': "player: player who pushed the buzzer", 90 'param_1': "player: player who pushed the buzzer",
91 'param_2': "pause: should the game be paused ?", 91 'param_2': "pause: should the game be paused ?",
92 'param_3': '%(doc_profile)s'}) 92 'param_3': '%(doc_profile)s'})
93 host.bridge.addSignal("quizGamePlayerSays", ".plugin", 93 host.bridge.addSignal("quizGamePlayerSays", ".plugin",
94 signature = 'sssis', 94 signature='sssis',
95 doc = { 'summary': "A player just pressed the buzzer", 95 doc={'summary': "A player just pressed the buzzer",
96 'param_0': "room_jid: jid of game's room", 96 'param_0': "room_jid: jid of game's room",
97 'param_1': "player: player who pushed the buzzer", 97 'param_1': "player: player who pushed the buzzer",
98 'param_2': "text: what the player say", 98 'param_2': "text: what the player say",
99 'param_3': "delay: how long, in seconds, the text must appear", 99 'param_3': "delay: how long, in seconds, the text must appear",
100 'param_4': '%(doc_profile)s'}) 100 'param_4': '%(doc_profile)s'})
101 host.bridge.addSignal("quizGameAnswerResult", ".plugin", 101 host.bridge.addSignal("quizGameAnswerResult", ".plugin",
102 signature = 'ssba{si}s', 102 signature='ssba{si}s',
103 doc = { 'summary': "Result of the just given answer", 103 doc={'summary': "Result of the just given answer",
104 'param_0': "room_jid: jid of game's room", 104 'param_0': "room_jid: jid of game's room",
105 'param_1': "player: player who gave the answer", 105 'param_1': "player: player who gave the answer",
106 'param_2': "good_answer: True if the answer is right", 106 'param_2': "good_answer: True if the answer is right",
107 'param_3': "score: dict of score with player as key", 107 'param_3': "score: dict of score with player as key",
108 'param_4': '%(doc_profile)s'}) 108 'param_4': '%(doc_profile)s'})
109 host.bridge.addSignal("quizGameTimerExpired", ".plugin", 109 host.bridge.addSignal("quizGameTimerExpired", ".plugin",
110 signature = 'ss', 110 signature='ss',
111 doc = { 'summary': "Nobody answered the question in time", 111 doc={'summary': "Nobody answered the question in time",
112 'param_0': "room_jid: jid of game's room", 112 'param_0': "room_jid: jid of game's room",
113 'param_1': '%(doc_profile)s'}) 113 'param_1': '%(doc_profile)s'})
114 host.bridge.addSignal("quizGameTimerRestarted", ".plugin", 114 host.bridge.addSignal("quizGameTimerRestarted", ".plugin",
115 signature = 'sis', 115 signature='sis',
116 doc = { 'summary': "Nobody answered the question in time", 116 doc={'summary': "Nobody answered the question in time",
117 'param_0': "room_jid: jid of game's room", 117 'param_0': "room_jid: jid of game's room",
118 'param_1': "time_left: time left before timer expiration", 118 'param_1': "time_left: time left before timer expiration",
119 'param_2': '%(doc_profile)s'}) 119 'param_2': '%(doc_profile)s'})
120 host.trigger.add("MUC user joined", self.userJoinedTrigger) 120 host.trigger.add("MUC user joined", self.userJoinedTrigger)
121 121
122 def createGameElt(self, to_jid, type="normal"): 122 def createGameElt(self, to_jid, type="normal"):
123 type = "normal" if to_jid.resource else "groupchat" 123 type = "normal" if to_jid.resource else "groupchat"
124 elt = domish.Element((None,'message')) 124 elt = domish.Element((None, 'message'))
125 elt["to"] = to_jid.full() 125 elt["to"] = to_jid.full()
126 elt["type"] = type 126 elt["type"] = type
127 elt.addElement((NS_QG, QG_TAG)) 127 elt.addElement((NS_QG, QG_TAG))
128 return elt 128 return elt
129 129
130 def __game_data_to_xml(self, game_data): 130 def __game_data_to_xml(self, game_data):
131 """Convert a game data dict to domish element""" 131 """Convert a game data dict to domish element"""
132 game_data_elt = domish.Element((None,'game_data')) 132 game_data_elt = domish.Element((None, 'game_data'))
133 for data in game_data: 133 for data in game_data:
134 data_elt = domish.Element((None,data)) 134 data_elt = domish.Element((None, data))
135 data_elt.addContent(game_data[data]) 135 data_elt.addContent(game_data[data])
136 game_data_elt.addChild(data_elt) 136 game_data_elt.addChild(data_elt)
137 return game_data_elt 137 return game_data_elt
138 138
139 def __xml_to_game_data(self, game_data_elt): 139 def __xml_to_game_data(self, game_data_elt):
160 players_data = game_data['players_data'] 160 players_data = game_data['players_data']
161 score = {} 161 score = {}
162 for player in game_data['players']: 162 for player in game_data['players']:
163 score[player] = players_data[player]['score'] 163 score[player] = players_data[player]['score']
164 164
165 answer_result_elt = domish.Element((None,'answer_result')) 165 answer_result_elt = domish.Element((None, 'answer_result'))
166 answer_result_elt['player'] = player_answering 166 answer_result_elt['player'] = player_answering
167 answer_result_elt['good_answer'] = str(good_answer) 167 answer_result_elt['good_answer'] = str(good_answer)
168 168
169 for player in score: 169 for player in score:
170 score_elt = domish.Element((None,"score")) 170 score_elt = domish.Element((None, "score"))
171 score_elt['player'] = player 171 score_elt['player'] = player
172 score_elt['score'] = str(score[player]) 172 score_elt['score'] = str(score[player])
173 answer_result_elt.addChild(score_elt) 173 answer_result_elt.addChild(score_elt)
174 174
175 return answer_result_elt 175 return answer_result_elt
176 176
177 def __create_started_elt(self, players): 177 def __create_started_elt(self, players):
178 """Create a game_started domish element""" 178 """Create a game_started domish element"""
179 started_elt = domish.Element((None,'started')) 179 started_elt = domish.Element((None, 'started'))
180 idx = 0 180 idx = 0
181 for player in players: 181 for player in players:
182 player_elt = domish.Element((None,'player')) 182 player_elt = domish.Element((None, 'player'))
183 player_elt.addContent(player) 183 player_elt.addContent(player)
184 player_elt['index'] = str(idx) 184 player_elt['index'] = str(idx)
185 idx+=1 185 idx += 1
186 started_elt.addChild(player_elt) 186 started_elt.addChild(player_elt)
187 return started_elt 187 return started_elt
188 188
189 def __ask_question(self, question_id, question, timer): 189 def __ask_question(self, question_id, question, timer):
190 """Create a element for asking a question""" 190 """Create a element for asking a question"""
191 question_elt = domish.Element((None,'question')) 191 question_elt = domish.Element((None, 'question'))
192 question_elt['id'] = question_id 192 question_elt['id'] = question_id
193 question_elt['timer'] = str(timer) 193 question_elt['timer'] = str(timer)
194 question_elt.addContent(question) 194 question_elt.addContent(question)
195 return question_elt 195 return question_elt
196 196
197 def __start_play(self, room_jid, game_data, profile): 197 def __start_play(self, room_jid, game_data, profile):
198 """Start the game (tell to the first player after dealer to play""" 198 """Start the game (tell to the first player after dealer to play"""
199 game_data['stage'] = "play" 199 game_data['stage'] = "play"
200 next_player_idx = game_data['current_player'] = (game_data['init_player'] + 1) % len(game_data['players']) #the player after the dealer start 200 next_player_idx = game_data['current_player'] = (game_data['init_player'] + 1) % len(game_data['players']) # the player after the dealer start
201 game_data['first_player'] = next_player = game_data['players'][next_player_idx] 201 game_data['first_player'] = next_player = game_data['players'][next_player_idx]
202 to_jid = jid.JID(room_jid.userhost()+"/"+next_player) 202 to_jid = jid.JID(room_jid.userhost() + "/" + next_player)
203 mess = self.createGameElt(to_jid) 203 mess = self.createGameElt(to_jid)
204 yourturn_elt = mess.firstChildElement().addElement('your_turn') 204 yourturn_elt = mess.firstChildElement().addElement('your_turn')
205 self.host.profiles[profile].xmlstream.send(mess) 205 self.host.profiles[profile].xmlstream.send(mess)
206
207 206
208 def userJoinedTrigger(self, room, user, profile): 207 def userJoinedTrigger(self, room, user, profile):
209 """This trigger is used to check if we are waiting people in this room, 208 """This trigger is used to check if we are waiting people in this room,
210 and to create a game if everybody is here""" 209 and to create a game if everybody is here"""
211 _room_jid = room.occupantJID.userhostJID() 210 _room_jid = room.occupantJID.userhostJID()
227 return 226 return
228 227
229 def quizRoomJoined(room): 228 def quizRoomJoined(room):
230 _room = room.occupantJID.userhostJID() 229 _room = room.occupantJID.userhostJID()
231 for player in players: 230 for player in players:
232 self.host.plugins["XEP-0249"].invite(jid.JID(player), room.occupantJID.userhostJID(), {"game":"Quiz"}, profile) 231 self.host.plugins["XEP-0249"].invite(jid.JID(player), room.occupantJID.userhostJID(), {"game": "Quiz"}, profile)
233 self.waiting_inv[_room] = (time(), players) #TODO: remove invitation waiting for too long, using the time data 232 self.waiting_inv[_room] = (time(), players) # TODO: remove invitation waiting for too long, using the time data
234 233
235 def after_init(ignore): 234 def after_init(ignore):
236 room_name = "sat_quiz_%s" % self.host.plugins["XEP-0045"].getUniqueName(profile_key) 235 room_name = "sat_quiz_%s" % self.host.plugins["XEP-0045"].getUniqueName(profile_key)
237 print "\n\n===> room_name:", room_name 236 print "\n\n===> room_name:", room_name
238 muc_service = None 237 muc_service = None
260 def quizGameCreate(self, room_jid_param, players, profile_key='@DEFAULT@'): 259 def quizGameCreate(self, room_jid_param, players, profile_key='@DEFAULT@'):
261 """Create a new game 260 """Create a new game
262 @param room_jid_param: jid of the room 261 @param room_jid_param: jid of the room
263 @param players: list of players nick (nick must exist in the room) 262 @param players: list of players nick (nick must exist in the room)
264 @param profile_key: %(doc_profile_key)s""" 263 @param profile_key: %(doc_profile_key)s"""
265 debug (_("Creating Quiz game")) 264 debug(_("Creating Quiz game"))
266 room_jid = jid.JID(room_jid_param) 265 room_jid = jid.JID(room_jid_param)
267 profile = self.host.memory.getProfileName(profile_key) 266 profile = self.host.memory.getProfileName(profile_key)
268 if not profile: 267 if not profile:
269 error (_("profile %s is unknown") % profile_key) 268 error(_("profile %s is unknown") % profile_key)
270 return 269 return
271 if self.games.has_key(room_jid): 270 if room_jid in self.games:
272 warning (_("Quiz game already started in room %s") % room_jid.userhost()) 271 warning(_("Quiz game already started in room %s") % room_jid.userhost())
273 else: 272 else:
274 room_nick = self.host.plugins["XEP-0045"].getRoomNick(room_jid.userhost(), profile) 273 room_nick = self.host.plugins["XEP-0045"].getRoomNick(room_jid.userhost(), profile)
275 if not room_nick: 274 if not room_nick:
276 error ('Internal error') 275 error('Internal error')
277 return 276 return
278 referee = room_jid.userhost() + '/' + room_nick 277 referee = room_jid.userhost() + '/' + room_nick
279 status = {} 278 status = {}
280 players_data = {} 279 players_data = {}
281 for player in players: 280 for player in players:
282 players_data[player] = {'score':0} 281 players_data[player] = {'score': 0}
283 status[player] = "init" 282 status[player] = "init"
284 self.games[room_jid.userhost()] = {'referee':referee, 'players':players, 'status':status, 'players_data':players_data, 'stage': None} 283 self.games[room_jid.userhost()] = {'referee': referee, 'players': players, 'status': status, 'players_data': players_data, 'stage': None}
285 for player in players: 284 for player in players:
286 mess = self.createGameElt(jid.JID(room_jid.userhost()+'/'+player)) 285 mess = self.createGameElt(jid.JID(room_jid.userhost() + '/' + player))
287 mess.firstChildElement().addChild(self.__create_started_elt(players)) 286 mess.firstChildElement().addChild(self.__create_started_elt(players))
288 self.host.profiles[profile].xmlstream.send(mess) 287 self.host.profiles[profile].xmlstream.send(mess)
289 288
290 def newPlayerReady(self, player, referee, profile_key='@DEFAULT@'): 289 def newPlayerReady(self, player, referee, profile_key='@DEFAULT@'):
291 """Must be called when player is ready to start a new game""" 290 """Must be called when player is ready to start a new game"""
292 profile = self.host.memory.getProfileName(profile_key) 291 profile = self.host.memory.getProfileName(profile_key)
293 if not profile: 292 if not profile:
294 error (_("profile %s is unknown") % profile_key) 293 error(_("profile %s is unknown") % profile_key)
295 return 294 return
296 debug ('new player ready: %s' % profile) 295 debug('new player ready: %s' % profile)
297 mess = self.createGameElt(jid.JID(referee)) 296 mess = self.createGameElt(jid.JID(referee))
298 ready_elt = mess.firstChildElement().addElement('player_ready') 297 ready_elt = mess.firstChildElement().addElement('player_ready')
299 ready_elt['player'] = player 298 ready_elt['player'] = player
300 self.host.profiles[profile].xmlstream.send(mess) 299 self.host.profiles[profile].xmlstream.send(mess)
301 300
302 def playerAnswer(self, player, referee, answer, profile_key='@DEFAULT@'): 301 def playerAnswer(self, player, referee, answer, profile_key='@DEFAULT@'):
303 """Called when a player give an answer""" 302 """Called when a player give an answer"""
304 profile = self.host.memory.getProfileName(profile_key) 303 profile = self.host.memory.getProfileName(profile_key)
305 if not profile: 304 if not profile:
306 error (_("profile %s is unknown") % profile_key) 305 error(_("profile %s is unknown") % profile_key)
307 return 306 return
308 debug ('new player answer (%(profile)s): %(answer)s' % {'profile':profile, 'answer':answer}) 307 debug('new player answer (%(profile)s): %(answer)s' % {'profile': profile, 'answer': answer})
309 mess = self.createGameElt(jid.JID(referee)) 308 mess = self.createGameElt(jid.JID(referee))
310 answer_elt = mess.firstChildElement().addElement('player_answer') 309 answer_elt = mess.firstChildElement().addElement('player_answer')
311 answer_elt['player'] = player 310 answer_elt['player'] = player
312 answer_elt.addContent(answer) 311 answer_elt.addContent(answer)
313 self.host.profiles[profile].xmlstream.send(mess) 312 self.host.profiles[profile].xmlstream.send(mess)
331 game_data['stage'] = "paused" 330 game_data['stage'] = "paused"
332 331
333 def restartTimer(self, room_jid, profile): 332 def restartTimer(self, room_jid, profile):
334 """Restart a timer with the saved time""" 333 """Restart a timer with the saved time"""
335 game_data = self.games[room_jid.userhost()] 334 game_data = self.games[room_jid.userhost()]
336 assert(game_data['time_left'] != None) 335 assert game_data['time_left'] is not None
337 mess = self.createGameElt(room_jid) 336 mess = self.createGameElt(room_jid)
338 restarted_elt = mess.firstChildElement().addElement('timer_restarted') 337 restarted_elt = mess.firstChildElement().addElement('timer_restarted')
339 restarted_elt["time_left"] = str(game_data['time_left']) 338 restarted_elt["time_left"] = str(game_data['time_left'])
340 self.host.profiles[profile].xmlstream.send(mess) 339 self.host.profiles[profile].xmlstream.send(mess)
341 game_data["timer"] = reactor.callLater(game_data['time_left'], self.timerExpired, room_jid, profile) 340 game_data["timer"] = reactor.callLater(game_data['time_left'], self.timerExpired, room_jid, profile)
357 356
358 def checkAnswer(self, room_jid, player, answer, profile): 357 def checkAnswer(self, room_jid, player, answer, profile):
359 """Check if the answer given is right""" 358 """Check if the answer given is right"""
360 game_data = self.games[room_jid.userhost()] 359 game_data = self.games[room_jid.userhost()]
361 players_data = game_data['players_data'] 360 players_data = game_data['players_data']
362 good_answer = game_data['question_id'] == "1" and answer=="42" 361 good_answer = game_data['question_id'] == "1" and answer == "42"
363 players_data[player]['score'] += 1 if good_answer else -1 362 players_data[player]['score'] += 1 if good_answer else -1
364 players_data[player]['score'] = min(9, max(0, players_data[player]['score'])) 363 players_data[player]['score'] = min(9, max(0, players_data[player]['score']))
365 364
366 mess = self.createGameElt(room_jid) 365 mess = self.createGameElt(room_jid)
367 mess.firstChildElement().addChild(self.__answer_result(player, good_answer, game_data)) 366 mess.firstChildElement().addChild(self.__answer_result(player, good_answer, game_data))
372 else: 371 else:
373 reactor.callLater(4, self.restartTimer, room_jid, profile) 372 reactor.callLater(4, self.restartTimer, room_jid, profile)
374 373
375 def newGame(self, room_jid, profile): 374 def newGame(self, room_jid, profile):
376 """Launch a new round""" 375 """Launch a new round"""
377 debug (_('new Quiz game')) 376 debug(_('new Quiz game'))
378 game_data = self.games[room_jid.userhost()] 377 game_data = self.games[room_jid.userhost()]
379 players = game_data['players'] 378 players = game_data['players']
380 players_data = game_data['players_data'] 379 players_data = game_data['players_data']
381 game_data['stage'] = "init" 380 game_data['stage'] = "init"
382 381
399 game_data = self.games[room_jid.userhost()] 398 game_data = self.games[room_jid.userhost()]
400 players_data = game_data['players_data'] 399 players_data = game_data['players_data']
401 400
402 for elt in game_elt.elements(): 401 for elt in game_elt.elements():
403 402
404 if elt.name == 'started': #new game created 403 if elt.name == 'started': # new game created
405 players = [] 404 players = []
406 for player in elt.elements(): 405 for player in elt.elements():
407 players.append(unicode(player)) 406 players.append(unicode(player))
408 self.host.bridge.quizGameStarted(room_jid.userhost(), from_jid.full(), players, profile) 407 self.host.bridge.quizGameStarted(room_jid.userhost(), from_jid.full(), players, profile)
409 408
410 elif elt.name == 'player_ready': #ready to play 409 elif elt.name == 'player_ready': # ready to play
411 player = elt['player'] 410 player = elt['player']
412 status = self.games[room_jid.userhost()]['status'] 411 status = self.games[room_jid.userhost()]['status']
413 nb_players = len(self.games[room_jid.userhost()]['players']) 412 nb_players = len(self.games[room_jid.userhost()]['players'])
414 status[player] = 'ready' 413 status[player] = 'ready'
415 debug (_('Player %(player)s is ready to start [status: %(status)s]') % {'player':player, 'status':status}) 414 debug(_('Player %(player)s is ready to start [status: %(status)s]') % {'player': player, 'status': status})
416 if status.values().count('ready') == nb_players: #everybody is ready, we can start the game 415 if status.values().count('ready') == nb_players: # everybody is ready, we can start the game
417 self.newGame(room_jid, profile) 416 self.newGame(room_jid, profile)
418 417
419 elif elt.name == 'game_data': 418 elif elt.name == 'game_data':
420 self.host.bridge.quizGameNew(room_jid.userhost(), self.__xml_to_game_data(elt), profile) 419 self.host.bridge.quizGameNew(room_jid.userhost(), self.__xml_to_game_data(elt), profile)
421 420
422 elif elt.name == 'question': #A question is asked 421 elif elt.name == 'question': # A question is asked
423 self.host.bridge.quizGameQuestion(room_jid.userhost(), elt["id"], unicode(elt), int(elt["timer"]), profile ) 422 self.host.bridge.quizGameQuestion(room_jid.userhost(), elt["id"], unicode(elt), int(elt["timer"]), profile)
424 423
425 elif elt.name == 'player_answer': 424 elif elt.name == 'player_answer':
426 player = elt['player'] 425 player = elt['player']
427 pause = game_data['stage'] == 'question' #we pause the game only if we are have a question at the moment 426 pause = game_data['stage'] == 'question' # we pause the game only if we are have a question at the moment
428 #we first send a buzzer message 427 #we first send a buzzer message
429 mess = self.createGameElt(room_jid) 428 mess = self.createGameElt(room_jid)
430 buzzer_elt = mess.firstChildElement().addElement('player_buzzed') 429 buzzer_elt = mess.firstChildElement().addElement('player_buzzed')
431 buzzer_elt['player'] = player 430 buzzer_elt['player'] = player
432 buzzer_elt['pause'] = str(pause) 431 buzzer_elt['pause'] = str(pause)
458 457
459 elif elt.name == 'timer_restarted': 458 elif elt.name == 'timer_restarted':
460 self.host.bridge.quizGameTimerRestarted(room_jid.userhost(), int(elt['time_left']), profile) 459 self.host.bridge.quizGameTimerRestarted(room_jid.userhost(), int(elt['time_left']), profile)
461 460
462 else: 461 else:
463 error (_('Unmanaged game element: %s') % elt.name) 462 error(_('Unmanaged game element: %s') % elt.name)
464 463
465 def getHandler(self, profile): 464 def getHandler(self, profile):
466 return QuizGameHandler(self) 465 return QuizGameHandler(self)
466
467 467
468 class QuizGameHandler (XMPPHandler): 468 class QuizGameHandler (XMPPHandler):
469 implements(iwokkel.IDisco) 469 implements(iwokkel.IDisco)
470 470
471 def __init__(self, plugin_parent): 471 def __init__(self, plugin_parent):
472 self.plugin_parent = plugin_parent 472 self.plugin_parent = plugin_parent
473 self.host = plugin_parent.host 473 self.host = plugin_parent.host
474 474
475 def connectionInitialized(self): 475 def connectionInitialized(self):
476 self.xmlstream.addObserver(QG_REQUEST, self.plugin_parent.quiz_game_cmd, profile = self.parent.profile) 476 self.xmlstream.addObserver(QG_REQUEST, self.plugin_parent.quiz_game_cmd, profile=self.parent.profile)
477 477
478 def getDiscoInfo(self, requestor, target, nodeIdentifier=''): 478 def getDiscoInfo(self, requestor, target, nodeIdentifier=''):
479 return [disco.DiscoFeature(NS_QG)] 479 return [disco.DiscoFeature(NS_QG)]
480 480
481 def getDiscoItems(self, requestor, target, nodeIdentifier=''): 481 def getDiscoItems(self, requestor, target, nodeIdentifier=''):
482 return [] 482 return []
483