Mercurial > libervia-backend
comparison sat/plugins/plugin_misc_quiz.py @ 3028:ab2696e34d29
Python 3 port:
/!\ this is a huge commit
/!\ starting from this commit, SàT is needs Python 3.6+
/!\ SàT maybe be instable or some feature may not work anymore, this will improve with time
This patch port backend, bridge and frontends to Python 3.
Roughly this has been done this way:
- 2to3 tools has been applied (with python 3.7)
- all references to python2 have been replaced with python3 (notably shebangs)
- fixed files not handled by 2to3 (notably the shell script)
- several manual fixes
- fixed issues reported by Python 3 that where not handled in Python 2
- replaced "async" with "async_" when needed (it's a reserved word from Python 3.7)
- replaced zope's "implements" with @implementer decorator
- temporary hack to handle data pickled in database, as str or bytes may be returned,
to be checked later
- fixed hash comparison for password
- removed some code which is not needed anymore with Python 3
- deactivated some code which needs to be checked (notably certificate validation)
- tested with jp, fixed reported issues until some basic commands worked
- ported Primitivus (after porting dependencies like urwid satext)
- more manual fixes
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 13 Aug 2019 19:08:41 +0200 |
parents | 003b8b4b56a7 |
children | 9d0df638c8b4 |
comparison
equal
deleted
inserted
replaced
3027:ff5bcb12ae60 | 3028:ab2696e34d29 |
---|---|
1 #!/usr/bin/env python2 | 1 #!/usr/bin/env python3 |
2 # -*- coding: utf-8 -*- | 2 # -*- coding: utf-8 -*- |
3 | 3 |
4 # SAT plugin for managing Quiz game | 4 # SAT plugin for managing Quiz game |
5 # Copyright (C) 2009-2019 Jérôme Poisson (goffi@goffi.org) | 5 # Copyright (C) 2009-2019 Jérôme Poisson (goffi@goffi.org) |
6 | 6 |
188 | 188 |
189 def __xml_to_game_data(self, game_data_elt): | 189 def __xml_to_game_data(self, game_data_elt): |
190 """Convert a domish element with game_data to a dict""" | 190 """Convert a domish element with game_data to a dict""" |
191 game_data = {} | 191 game_data = {} |
192 for data_elt in game_data_elt.elements(): | 192 for data_elt in game_data_elt.elements(): |
193 game_data[data_elt.name] = unicode(data_elt) | 193 game_data[data_elt.name] = str(data_elt) |
194 return game_data | 194 return game_data |
195 | 195 |
196 def __answer_result_to_signal_args(self, answer_result_elt): | 196 def __answer_result_to_signal_args(self, answer_result_elt): |
197 """Parse answer result element and return a tuple of signal arguments | 197 """Parse answer result element and return a tuple of signal arguments |
198 @param answer_result_elt: answer result element | 198 @param answer_result_elt: answer result element |
253 | 253 |
254 def playerAnswer(self, player, referee, answer, profile_key=C.PROF_KEY_NONE): | 254 def playerAnswer(self, player, referee, answer, profile_key=C.PROF_KEY_NONE): |
255 """Called when a player give an answer""" | 255 """Called when a player give an answer""" |
256 client = self.host.getClient(profile_key) | 256 client = self.host.getClient(profile_key) |
257 log.debug( | 257 log.debug( |
258 u"new player answer (%(profile)s): %(answer)s" | 258 "new player answer (%(profile)s): %(answer)s" |
259 % {"profile": client.profile, "answer": answer} | 259 % {"profile": client.profile, "answer": answer} |
260 ) | 260 ) |
261 mess = self.createGameElt(jid.JID(referee)) | 261 mess = self.createGameElt(jid.JID(referee)) |
262 answer_elt = mess.firstChildElement().addElement("player_answer") | 262 answer_elt = mess.firstChildElement().addElement("player_answer") |
263 answer_elt["player"] = player | 263 answer_elt["player"] = player |
307 game_data["question_id"] = "1" | 307 game_data["question_id"] = "1" |
308 timer = 30 | 308 timer = 30 |
309 mess = self.createGameElt(room_jid) | 309 mess = self.createGameElt(room_jid) |
310 mess.firstChildElement().addChild( | 310 mess.firstChildElement().addChild( |
311 self.__ask_question( | 311 self.__ask_question( |
312 game_data["question_id"], u"Quel est l'âge du capitaine ?", timer | 312 game_data["question_id"], "Quel est l'âge du capitaine ?", timer |
313 ) | 313 ) |
314 ) | 314 ) |
315 client.send(mess) | 315 client.send(mess) |
316 game_data["timer"] = reactor.callLater( | 316 game_data["timer"] = reactor.callLater( |
317 timer, self.timerExpired, room_jid, profile | 317 timer, self.timerExpired, room_jid, profile |
341 def newGame(self, room_jid, profile): | 341 def newGame(self, room_jid, profile): |
342 """Launch a new round""" | 342 """Launch a new round""" |
343 common_data = {"game_score": 0} | 343 common_data = {"game_score": 0} |
344 new_game_data = { | 344 new_game_data = { |
345 "instructions": _( | 345 "instructions": _( |
346 u"""Bienvenue dans cette partie rapide de quizz, le premier à atteindre le score de 9 remporte le jeu | 346 """Bienvenue dans cette partie rapide de quizz, le premier à atteindre le score de 9 remporte le jeu |
347 | 347 |
348 Attention, tu es prêt ?""" | 348 Attention, tu es prêt ?""" |
349 ) | 349 ) |
350 } | 350 } |
351 msg_elts = self.__game_data_to_xml(new_game_data) | 351 msg_elts = self.__game_data_to_xml(new_game_data) |
364 for elt in game_elt.elements(): | 364 for elt in game_elt.elements(): |
365 | 365 |
366 if elt.name == "started": # new game created | 366 if elt.name == "started": # new game created |
367 players = [] | 367 players = [] |
368 for player in elt.elements(): | 368 for player in elt.elements(): |
369 players.append(unicode(player)) | 369 players.append(str(player)) |
370 self.host.bridge.quizGameStarted( | 370 self.host.bridge.quizGameStarted( |
371 room_jid.userhost(), from_jid.full(), players, profile | 371 room_jid.userhost(), from_jid.full(), players, profile |
372 ) | 372 ) |
373 | 373 |
374 elif elt.name == "player_ready": # ready to play | 374 elif elt.name == "player_ready": # ready to play |
375 player = elt["player"] | 375 player = elt["player"] |
376 status = self.games[room_jid]["status"] | 376 status = self.games[room_jid]["status"] |
377 nb_players = len(self.games[room_jid]["players"]) | 377 nb_players = len(self.games[room_jid]["players"]) |
378 status[player] = "ready" | 378 status[player] = "ready" |
379 log.debug( | 379 log.debug( |
380 _(u"Player %(player)s is ready to start [status: %(status)s]") | 380 _("Player %(player)s is ready to start [status: %(status)s]") |
381 % {"player": player, "status": status} | 381 % {"player": player, "status": status} |
382 ) | 382 ) |
383 if ( | 383 if ( |
384 status.values().count("ready") == nb_players | 384 list(status.values()).count("ready") == nb_players |
385 ): # everybody is ready, we can start the game | 385 ): # everybody is ready, we can start the game |
386 self.newGame(room_jid, profile) | 386 self.newGame(room_jid, profile) |
387 | 387 |
388 elif elt.name == "game_data": | 388 elif elt.name == "game_data": |
389 self.host.bridge.quizGameNew( | 389 self.host.bridge.quizGameNew( |
392 | 392 |
393 elif elt.name == "question": # A question is asked | 393 elif elt.name == "question": # A question is asked |
394 self.host.bridge.quizGameQuestion( | 394 self.host.bridge.quizGameQuestion( |
395 room_jid.userhost(), | 395 room_jid.userhost(), |
396 elt["id"], | 396 elt["id"], |
397 unicode(elt), | 397 str(elt), |
398 int(elt["timer"]), | 398 int(elt["timer"]), |
399 profile, | 399 profile, |
400 ) | 400 ) |
401 | 401 |
402 elif elt.name == "player_answer": | 402 elif elt.name == "player_answer": |
412 client.send(mess) | 412 client.send(mess) |
413 if pause: | 413 if pause: |
414 self.pauseTimer(room_jid) | 414 self.pauseTimer(room_jid) |
415 # and we send the player answer | 415 # and we send the player answer |
416 mess = self.createGameElt(room_jid) | 416 mess = self.createGameElt(room_jid) |
417 _answer = unicode(elt) | 417 _answer = str(elt) |
418 say_elt = mess.firstChildElement().addElement("player_says") | 418 say_elt = mess.firstChildElement().addElement("player_says") |
419 say_elt["player"] = player | 419 say_elt["player"] = player |
420 say_elt.addContent(_answer) | 420 say_elt.addContent(_answer) |
421 say_elt["delay"] = "3" | 421 say_elt["delay"] = "3" |
422 reactor.callLater(2, client.send, mess) | 422 reactor.callLater(2, client.send, mess) |
431 | 431 |
432 elif elt.name == "player_says": | 432 elif elt.name == "player_says": |
433 self.host.bridge.quizGamePlayerSays( | 433 self.host.bridge.quizGamePlayerSays( |
434 room_jid.userhost(), | 434 room_jid.userhost(), |
435 elt["player"], | 435 elt["player"], |
436 unicode(elt), | 436 str(elt), |
437 int(elt["delay"]), | 437 int(elt["delay"]), |
438 profile, | 438 profile, |
439 ) | 439 ) |
440 | 440 |
441 elif elt.name == "answer_result": | 441 elif elt.name == "answer_result": |
451 self.host.bridge.quizGameTimerRestarted( | 451 self.host.bridge.quizGameTimerRestarted( |
452 room_jid.userhost(), int(elt["time_left"]), profile | 452 room_jid.userhost(), int(elt["time_left"]), profile |
453 ) | 453 ) |
454 | 454 |
455 else: | 455 else: |
456 log.error(_(u"Unmanaged game element: %s") % elt.name) | 456 log.error(_("Unmanaged game element: %s") % elt.name) |