comparison src/plugins/plugin_misc_quiz.py @ 993:301b342c697a

core: use of the new core.log module: /!\ this is a massive refactoring and was largely automated, it probably did bring some bugs /!\
author Goffi <goffi@goffi.org>
date Sat, 19 Apr 2014 19:19:19 +0200
parents 1a759096ccbd
children 83127a4c89ce
comparison
equal deleted inserted replaced
992:f51a1895275c 993:301b342c697a
17 # You should have received a copy of the GNU Affero General Public License 17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. 18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 19
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 logging import debug, info, warning, error 22 from sat.core.log import getLogger
23 log = getLogger(__name__)
23 from twisted.words.xish import domish 24 from twisted.words.xish import domish
24 from twisted.internet import protocol, defer, threads, reactor 25 from twisted.internet import protocol, defer, threads, reactor
25 from twisted.words.protocols.jabber import client, jid, xmlstream 26 from twisted.words.protocols.jabber import client, jid, xmlstream
26 from twisted.words.protocols.jabber import error as jab_error 27 from twisted.words.protocols import jabber
27 from twisted.words.protocols.jabber.xmlstream import IQ 28 from twisted.words.protocols.jabber.xmlstream import IQ
28 import random 29 import random
29 30
30 from wokkel import data_form 31 from wokkel import data_form
31 from time import time 32 from time import time
52 global RoomGame 53 global RoomGame
53 RoomGame = host.plugins["ROOM-GAME"].__class__ 54 RoomGame = host.plugins["ROOM-GAME"].__class__
54 self.__class__ = type(self.__class__.__name__, (self.__class__, RoomGame, object), {}) 55 self.__class__ = type(self.__class__.__name__, (self.__class__, RoomGame, object), {})
55 56
56 def __init__(self, host): 57 def __init__(self, host):
57 info(_("Plugin Quiz initialization")) 58 log.info(_("Plugin Quiz initialization"))
58 self.inheritFromRoomGame(host) 59 self.inheritFromRoomGame(host)
59 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})
60 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
61 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
62 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
178 179
179 def playerAnswer(self, player, referee, answer, profile_key=C.PROF_KEY_NONE): 180 def playerAnswer(self, player, referee, answer, profile_key=C.PROF_KEY_NONE):
180 """Called when a player give an answer""" 181 """Called when a player give an answer"""
181 profile = self.host.memory.getProfileName(profile_key) 182 profile = self.host.memory.getProfileName(profile_key)
182 if not profile: 183 if not profile:
183 error(_("profile %s is unknown") % profile_key) 184 log.error(_("profile %s is unknown") % profile_key)
184 return 185 return
185 debug('new player answer (%(profile)s): %(answer)s' % {'profile': profile, 'answer': answer}) 186 log.debug('new player answer (%(profile)s): %(answer)s' % {'profile': profile, 'answer': answer})
186 mess = self.createGameElt(jid.JID(referee)) 187 mess = self.createGameElt(jid.JID(referee))
187 answer_elt = mess.firstChildElement().addElement('player_answer') 188 answer_elt = mess.firstChildElement().addElement('player_answer')
188 answer_elt['player'] = player 189 answer_elt['player'] = player
189 answer_elt.addContent(answer) 190 answer_elt.addContent(answer)
190 self.host.profiles[profile].xmlstream.send(mess) 191 self.host.profiles[profile].xmlstream.send(mess)
278 elif elt.name == 'player_ready': # ready to play 279 elif elt.name == 'player_ready': # ready to play
279 player = elt['player'] 280 player = elt['player']
280 status = self.games[room_jid.userhost()]['status'] 281 status = self.games[room_jid.userhost()]['status']
281 nb_players = len(self.games[room_jid.userhost()]['players']) 282 nb_players = len(self.games[room_jid.userhost()]['players'])
282 status[player] = 'ready' 283 status[player] = 'ready'
283 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})
284 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
285 self.newGame(room_jid, profile) 286 self.newGame(room_jid, profile)
286 287
287 elif elt.name == 'game_data': 288 elif elt.name == 'game_data':
288 self.host.bridge.quizGameNew(room_jid.userhost(), self.__xml_to_game_data(elt), profile) 289 self.host.bridge.quizGameNew(room_jid.userhost(), self.__xml_to_game_data(elt), profile)
326 327
327 elif elt.name == 'timer_restarted': 328 elif elt.name == 'timer_restarted':
328 self.host.bridge.quizGameTimerRestarted(room_jid.userhost(), int(elt['time_left']), profile) 329 self.host.bridge.quizGameTimerRestarted(room_jid.userhost(), int(elt['time_left']), profile)
329 330
330 else: 331 else:
331 error(_('Unmanaged game element: %s') % elt.name) 332 log.error(_('Unmanaged game element: %s') % elt.name)