Mercurial > libervia-backend
diff 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 |
line wrap: on
line diff
--- a/src/plugins/plugin_misc_quiz.py Mon Jan 21 00:59:50 2013 +0100 +++ b/src/plugins/plugin_misc_quiz.py Fri Jan 18 17:55:35 2013 +0100 @@ -46,14 +46,14 @@ QG_REQUEST = MESSAGE + '/' + QG_TAG + '[@xmlns="' + NS_QG + '"]' PLUGIN_INFO = { -"name": "Quiz game plugin", -"import_name": "Quiz", -"type": "Game", -"protocols": [], -"dependencies": ["XEP-0045", "XEP-0249"], -"main": "Quiz", -"handler": "yes", -"description": _("""Implementation of Quiz game""") + "name": "Quiz game plugin", + "import_name": "Quiz", + "type": "Game", + "protocols": [], + "dependencies": ["XEP-0045", "XEP-0249"], + "main": "Quiz", + "handler": "yes", + "description": _("""Implementation of Quiz game""") } @@ -62,66 +62,66 @@ def __init__(self, host): info(_("Plugin Quiz initialization")) self.host = host - self.games={} - self.waiting_inv = {} #Invitation waiting for people to join to launch a game - host.bridge.addMethod("quizGameLaunch", ".plugin", in_sign='ass', out_sign='', method=self.quizGameLaunch) #args: players, profile - host.bridge.addMethod("quizGameCreate", ".plugin", in_sign='sass', out_sign='', method=self.quizGameCreate) #args: room_jid, players, profile - host.bridge.addMethod("quizGameReady", ".plugin", in_sign='sss', out_sign='', method=self.newPlayerReady) #args: player, referee, profile + self.games = {} + self.waiting_inv = {} # Invitation waiting for people to join to launch a game + host.bridge.addMethod("quizGameLaunch", ".plugin", in_sign='ass', out_sign='', method=self.quizGameLaunch) # args: players, profile + host.bridge.addMethod("quizGameCreate", ".plugin", in_sign='sass', out_sign='', method=self.quizGameCreate) # args: room_jid, players, profile + host.bridge.addMethod("quizGameReady", ".plugin", in_sign='sss', out_sign='', method=self.newPlayerReady) # args: player, referee, profile host.bridge.addMethod("quizGameAnswer", ".plugin", in_sign='ssss', out_sign='', method=self.playerAnswer) - host.bridge.addSignal("quizGameStarted", ".plugin", signature='ssass') #args: room_jid, referee, players, profile + host.bridge.addSignal("quizGameStarted", ".plugin", signature='ssass') # args: room_jid, referee, players, profile host.bridge.addSignal("quizGameNew", ".plugin", signature='sa{ss}s', - doc = { 'summary': 'Start a new game', - 'param_0': "room_jid: jid of game's room", - 'param_1': "game_data: data of the game", - 'param_2': '%(doc_profile)s'}) + doc={'summary': 'Start a new game', + 'param_0': "room_jid: jid of game's room", + 'param_1': "game_data: data of the game", + 'param_2': '%(doc_profile)s'}) host.bridge.addSignal("quizGameQuestion", ".plugin", - signature = 'sssis', - doc = { 'summary': "Send the current question", - 'param_0': "room_jid: jid of game's room", - 'param_1': "question_id: question id", - 'param_2': "question: question to ask", - 'param_3': "timer: timer", - 'param_4': '%(doc_profile)s'}) + signature='sssis', + doc={'summary': "Send the current question", + 'param_0': "room_jid: jid of game's room", + 'param_1': "question_id: question id", + 'param_2': "question: question to ask", + 'param_3': "timer: timer", + 'param_4': '%(doc_profile)s'}) host.bridge.addSignal("quizGamePlayerBuzzed", ".plugin", - signature = 'ssbs', - doc = { 'summary': "A player just pressed the buzzer", - 'param_0': "room_jid: jid of game's room", - 'param_1': "player: player who pushed the buzzer", - 'param_2': "pause: should the game be paused ?", - 'param_3': '%(doc_profile)s'}) + signature='ssbs', + doc={'summary': "A player just pressed the buzzer", + 'param_0': "room_jid: jid of game's room", + 'param_1': "player: player who pushed the buzzer", + 'param_2': "pause: should the game be paused ?", + 'param_3': '%(doc_profile)s'}) host.bridge.addSignal("quizGamePlayerSays", ".plugin", - signature = 'sssis', - doc = { 'summary': "A player just pressed the buzzer", - 'param_0': "room_jid: jid of game's room", - 'param_1': "player: player who pushed the buzzer", - 'param_2': "text: what the player say", - 'param_3': "delay: how long, in seconds, the text must appear", - 'param_4': '%(doc_profile)s'}) + signature='sssis', + doc={'summary': "A player just pressed the buzzer", + 'param_0': "room_jid: jid of game's room", + 'param_1': "player: player who pushed the buzzer", + 'param_2': "text: what the player say", + 'param_3': "delay: how long, in seconds, the text must appear", + 'param_4': '%(doc_profile)s'}) host.bridge.addSignal("quizGameAnswerResult", ".plugin", - signature = 'ssba{si}s', - doc = { 'summary': "Result of the just given answer", - 'param_0': "room_jid: jid of game's room", - 'param_1': "player: player who gave the answer", - 'param_2': "good_answer: True if the answer is right", - 'param_3': "score: dict of score with player as key", - 'param_4': '%(doc_profile)s'}) + signature='ssba{si}s', + doc={'summary': "Result of the just given answer", + 'param_0': "room_jid: jid of game's room", + 'param_1': "player: player who gave the answer", + 'param_2': "good_answer: True if the answer is right", + 'param_3': "score: dict of score with player as key", + 'param_4': '%(doc_profile)s'}) host.bridge.addSignal("quizGameTimerExpired", ".plugin", - signature = 'ss', - doc = { 'summary': "Nobody answered the question in time", - 'param_0': "room_jid: jid of game's room", - 'param_1': '%(doc_profile)s'}) + signature='ss', + doc={'summary': "Nobody answered the question in time", + 'param_0': "room_jid: jid of game's room", + 'param_1': '%(doc_profile)s'}) host.bridge.addSignal("quizGameTimerRestarted", ".plugin", - signature = 'sis', - doc = { 'summary': "Nobody answered the question in time", - 'param_0': "room_jid: jid of game's room", - 'param_1': "time_left: time left before timer expiration", - 'param_2': '%(doc_profile)s'}) + signature='sis', + doc={'summary': "Nobody answered the question in time", + 'param_0': "room_jid: jid of game's room", + 'param_1': "time_left: time left before timer expiration", + 'param_2': '%(doc_profile)s'}) host.trigger.add("MUC user joined", self.userJoinedTrigger) def createGameElt(self, to_jid, type="normal"): type = "normal" if to_jid.resource else "groupchat" - elt = domish.Element((None,'message')) + elt = domish.Element((None, 'message')) elt["to"] = to_jid.full() elt["type"] = type elt.addElement((NS_QG, QG_TAG)) @@ -129,9 +129,9 @@ def __game_data_to_xml(self, game_data): """Convert a game data dict to domish element""" - game_data_elt = domish.Element((None,'game_data')) + game_data_elt = domish.Element((None, 'game_data')) for data in game_data: - data_elt = domish.Element((None,data)) + data_elt = domish.Element((None, data)) data_elt.addContent(game_data[data]) game_data_elt.addChild(data_elt) return game_data_elt @@ -162,12 +162,12 @@ for player in game_data['players']: score[player] = players_data[player]['score'] - answer_result_elt = domish.Element((None,'answer_result')) + answer_result_elt = domish.Element((None, 'answer_result')) answer_result_elt['player'] = player_answering answer_result_elt['good_answer'] = str(good_answer) for player in score: - score_elt = domish.Element((None,"score")) + score_elt = domish.Element((None, "score")) score_elt['player'] = player score_elt['score'] = str(score[player]) answer_result_elt.addChild(score_elt) @@ -176,19 +176,19 @@ def __create_started_elt(self, players): """Create a game_started domish element""" - started_elt = domish.Element((None,'started')) + started_elt = domish.Element((None, 'started')) idx = 0 for player in players: - player_elt = domish.Element((None,'player')) + player_elt = domish.Element((None, 'player')) player_elt.addContent(player) player_elt['index'] = str(idx) - idx+=1 + idx += 1 started_elt.addChild(player_elt) return started_elt def __ask_question(self, question_id, question, timer): """Create a element for asking a question""" - question_elt = domish.Element((None,'question')) + question_elt = domish.Element((None, 'question')) question_elt['id'] = question_id question_elt['timer'] = str(timer) question_elt.addContent(question) @@ -197,14 +197,13 @@ def __start_play(self, room_jid, game_data, profile): """Start the game (tell to the first player after dealer to play""" game_data['stage'] = "play" - next_player_idx = game_data['current_player'] = (game_data['init_player'] + 1) % len(game_data['players']) #the player after the dealer start + next_player_idx = game_data['current_player'] = (game_data['init_player'] + 1) % len(game_data['players']) # the player after the dealer start game_data['first_player'] = next_player = game_data['players'][next_player_idx] - to_jid = jid.JID(room_jid.userhost()+"/"+next_player) + to_jid = jid.JID(room_jid.userhost() + "/" + next_player) mess = self.createGameElt(to_jid) yourturn_elt = mess.firstChildElement().addElement('your_turn') self.host.profiles[profile].xmlstream.send(mess) - def userJoinedTrigger(self, room, user, profile): """This trigger is used to check if we are waiting people in this room, and to create a game if everybody is here""" @@ -229,8 +228,8 @@ def quizRoomJoined(room): _room = room.occupantJID.userhostJID() for player in players: - self.host.plugins["XEP-0249"].invite(jid.JID(player), room.occupantJID.userhostJID(), {"game":"Quiz"}, profile) - self.waiting_inv[_room] = (time(), players) #TODO: remove invitation waiting for too long, using the time data + self.host.plugins["XEP-0249"].invite(jid.JID(player), room.occupantJID.userhostJID(), {"game": "Quiz"}, profile) + self.waiting_inv[_room] = (time(), players) # TODO: remove invitation waiting for too long, using the time data def after_init(ignore): room_name = "sat_quiz_%s" % self.host.plugins["XEP-0045"].getUniqueName(profile_key) @@ -262,28 +261,28 @@ @param room_jid_param: jid of the room @param players: list of players nick (nick must exist in the room) @param profile_key: %(doc_profile_key)s""" - debug (_("Creating Quiz game")) + debug(_("Creating Quiz game")) room_jid = jid.JID(room_jid_param) profile = self.host.memory.getProfileName(profile_key) if not profile: - error (_("profile %s is unknown") % profile_key) + error(_("profile %s is unknown") % profile_key) return - if self.games.has_key(room_jid): - warning (_("Quiz game already started in room %s") % room_jid.userhost()) + if room_jid in self.games: + warning(_("Quiz game already started in room %s") % room_jid.userhost()) else: room_nick = self.host.plugins["XEP-0045"].getRoomNick(room_jid.userhost(), profile) if not room_nick: - error ('Internal error') + error('Internal error') return referee = room_jid.userhost() + '/' + room_nick status = {} players_data = {} for player in players: - players_data[player] = {'score':0} + players_data[player] = {'score': 0} status[player] = "init" - self.games[room_jid.userhost()] = {'referee':referee, 'players':players, 'status':status, 'players_data':players_data, 'stage': None} + self.games[room_jid.userhost()] = {'referee': referee, 'players': players, 'status': status, 'players_data': players_data, 'stage': None} for player in players: - mess = self.createGameElt(jid.JID(room_jid.userhost()+'/'+player)) + mess = self.createGameElt(jid.JID(room_jid.userhost() + '/' + player)) mess.firstChildElement().addChild(self.__create_started_elt(players)) self.host.profiles[profile].xmlstream.send(mess) @@ -291,9 +290,9 @@ """Must be called when player is ready to start a new game""" profile = self.host.memory.getProfileName(profile_key) if not profile: - error (_("profile %s is unknown") % profile_key) + error(_("profile %s is unknown") % profile_key) return - debug ('new player ready: %s' % profile) + debug('new player ready: %s' % profile) mess = self.createGameElt(jid.JID(referee)) ready_elt = mess.firstChildElement().addElement('player_ready') ready_elt['player'] = player @@ -303,9 +302,9 @@ """Called when a player give an answer""" profile = self.host.memory.getProfileName(profile_key) if not profile: - error (_("profile %s is unknown") % profile_key) + error(_("profile %s is unknown") % profile_key) return - debug ('new player answer (%(profile)s): %(answer)s' % {'profile':profile, 'answer':answer}) + debug('new player answer (%(profile)s): %(answer)s' % {'profile': profile, 'answer': answer}) mess = self.createGameElt(jid.JID(referee)) answer_elt = mess.firstChildElement().addElement('player_answer') answer_elt['player'] = player @@ -333,7 +332,7 @@ def restartTimer(self, room_jid, profile): """Restart a timer with the saved time""" game_data = self.games[room_jid.userhost()] - assert(game_data['time_left'] != None) + assert game_data['time_left'] is not None mess = self.createGameElt(room_jid) restarted_elt = mess.firstChildElement().addElement('timer_restarted') restarted_elt["time_left"] = str(game_data['time_left']) @@ -359,7 +358,7 @@ """Check if the answer given is right""" game_data = self.games[room_jid.userhost()] players_data = game_data['players_data'] - good_answer = game_data['question_id'] == "1" and answer=="42" + good_answer = game_data['question_id'] == "1" and answer == "42" players_data[player]['score'] += 1 if good_answer else -1 players_data[player]['score'] = min(9, max(0, players_data[player]['score'])) @@ -374,7 +373,7 @@ def newGame(self, room_jid, profile): """Launch a new round""" - debug (_('new Quiz game')) + debug(_('new Quiz game')) game_data = self.games[room_jid.userhost()] players = game_data['players'] players_data = game_data['players_data'] @@ -401,30 +400,30 @@ for elt in game_elt.elements(): - if elt.name == 'started': #new game created + if elt.name == 'started': # new game created players = [] for player in elt.elements(): players.append(unicode(player)) self.host.bridge.quizGameStarted(room_jid.userhost(), from_jid.full(), players, profile) - elif elt.name == 'player_ready': #ready to play + elif elt.name == 'player_ready': # ready to play player = elt['player'] status = self.games[room_jid.userhost()]['status'] nb_players = len(self.games[room_jid.userhost()]['players']) status[player] = 'ready' - debug (_('Player %(player)s is ready to start [status: %(status)s]') % {'player':player, 'status':status}) - if status.values().count('ready') == nb_players: #everybody is ready, we can start the game + debug(_('Player %(player)s is ready to start [status: %(status)s]') % {'player': player, 'status': status}) + if status.values().count('ready') == nb_players: # everybody is ready, we can start the game self.newGame(room_jid, profile) elif elt.name == 'game_data': self.host.bridge.quizGameNew(room_jid.userhost(), self.__xml_to_game_data(elt), profile) - elif elt.name == 'question': #A question is asked - self.host.bridge.quizGameQuestion(room_jid.userhost(), elt["id"], unicode(elt), int(elt["timer"]), profile ) + elif elt.name == 'question': # A question is asked + self.host.bridge.quizGameQuestion(room_jid.userhost(), elt["id"], unicode(elt), int(elt["timer"]), profile) elif elt.name == 'player_answer': player = elt['player'] - pause = game_data['stage'] == 'question' #we pause the game only if we are have a question at the moment + pause = game_data['stage'] == 'question' # we pause the game only if we are have a question at the moment #we first send a buzzer message mess = self.createGameElt(room_jid) buzzer_elt = mess.firstChildElement().addElement('player_buzzed') @@ -460,10 +459,11 @@ self.host.bridge.quizGameTimerRestarted(room_jid.userhost(), int(elt['time_left']), profile) else: - error (_('Unmanaged game element: %s') % elt.name) + error(_('Unmanaged game element: %s') % elt.name) def getHandler(self, profile): - return QuizGameHandler(self) + return QuizGameHandler(self) + class QuizGameHandler (XMPPHandler): implements(iwokkel.IDisco) @@ -473,11 +473,10 @@ self.host = plugin_parent.host def connectionInitialized(self): - self.xmlstream.addObserver(QG_REQUEST, self.plugin_parent.quiz_game_cmd, profile = self.parent.profile) + self.xmlstream.addObserver(QG_REQUEST, self.plugin_parent.quiz_game_cmd, profile=self.parent.profile) def getDiscoInfo(self, requestor, target, nodeIdentifier=''): return [disco.DiscoFeature(NS_QG)] def getDiscoItems(self, requestor, target, nodeIdentifier=''): return [] -