annotate src/plugins/plugin_misc_quiz.py @ 2451:d1153ce68ca0

tools (config): complexe data can now be set using json and the "_json" suffix
author Goffi <goffi@goffi.org>
date Thu, 30 Nov 2017 20:53:47 +0100
parents 8b37a62336c3
children 0046283a285d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1934
2daf7b4c6756 use of /usr/bin/env instead of /usr/bin/python in shebang
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
1 #!/usr/bin/env python2
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
3
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
4 # SAT plugin for managing Quiz game
2414
8b37a62336c3 misc: date update (yes it's a bit late :p )
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
5 # Copyright (C) 2009-2017 Jérôme Poisson (goffi@goffi.org)
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
6
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
7 # This program is free software: you can redistribute it and/or modify
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
10 # (at your option) any later version.
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
11
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
12 # This program is distributed in the hope that it will be useful,
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
15 # GNU Affero General Public License for more details.
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
16
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
19
771
bfabeedbf32e core: i18n refactoring:
Goffi <goffi@goffi.org>
parents: 759
diff changeset
20 from sat.core.i18n import _
916
1a759096ccbd core: use of Const for profile_key + replaced '@DEFAULT@' default profile_key by '@NONE@'
Goffi <goffi@goffi.org>
parents: 811
diff changeset
21 from sat.core.constants import Const as C
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 916
diff changeset
22 from sat.core.log import getLogger
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 916
diff changeset
23 log = getLogger(__name__)
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from twisted.words.xish import domish
2129
6a66c8c5a567 core: replaced calls to client.xmlstream.send by client.send which is the right method to use. client.xmlstream should not be used directly
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
25 from twisted.internet import reactor
6a66c8c5a567 core: replaced calls to client.xmlstream.send by client.send which is the right method to use. client.xmlstream should not be used directly
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
26 from twisted.words.protocols.jabber import client as jabber_client, jid
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
27 from time import time
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
28
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
29
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
30 NS_QG = 'http://www.goffi.org/protocol/quiz'
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
31 QG_TAG = 'quiz'
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
32
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
33 PLUGIN_INFO = {
2145
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2129
diff changeset
34 C.PI_NAME: "Quiz game plugin",
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2129
diff changeset
35 C.PI_IMPORT_NAME: "Quiz",
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2129
diff changeset
36 C.PI_TYPE: "Game",
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2129
diff changeset
37 C.PI_PROTOCOLS: [],
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2129
diff changeset
38 C.PI_DEPENDENCIES: ["XEP-0045", "XEP-0249", "ROOM-GAME"],
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2129
diff changeset
39 C.PI_MAIN: "Quiz",
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2129
diff changeset
40 C.PI_HANDLER: "yes",
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2129
diff changeset
41 C.PI_DESCRIPTION: _("""Implementation of Quiz game""")
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
42 }
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
43
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
44
718
074970227bc0 plugin tools: turn src/plugin/games.py into a plugin and move it to src/plugins/plugin_misc_room_game.py
souliane <souliane@mailoo.org>
parents: 717
diff changeset
45 class Quiz(object):
074970227bc0 plugin tools: turn src/plugin/games.py into a plugin and move it to src/plugins/plugin_misc_room_game.py
souliane <souliane@mailoo.org>
parents: 717
diff changeset
46
074970227bc0 plugin tools: turn src/plugin/games.py into a plugin and move it to src/plugins/plugin_misc_room_game.py
souliane <souliane@mailoo.org>
parents: 717
diff changeset
47 def inheritFromRoomGame(self, host):
074970227bc0 plugin tools: turn src/plugin/games.py into a plugin and move it to src/plugins/plugin_misc_room_game.py
souliane <souliane@mailoo.org>
parents: 717
diff changeset
48 global RoomGame
074970227bc0 plugin tools: turn src/plugin/games.py into a plugin and move it to src/plugins/plugin_misc_room_game.py
souliane <souliane@mailoo.org>
parents: 717
diff changeset
49 RoomGame = host.plugins["ROOM-GAME"].__class__
074970227bc0 plugin tools: turn src/plugin/games.py into a plugin and move it to src/plugins/plugin_misc_room_game.py
souliane <souliane@mailoo.org>
parents: 717
diff changeset
50 self.__class__ = type(self.__class__.__name__, (self.__class__, RoomGame, object), {})
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
51
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
52 def __init__(self, host):
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 916
diff changeset
53 log.info(_("Plugin Quiz initialization"))
718
074970227bc0 plugin tools: turn src/plugin/games.py into a plugin and move it to src/plugins/plugin_misc_room_game.py
souliane <souliane@mailoo.org>
parents: 717
diff changeset
54 self.inheritFromRoomGame(host)
074970227bc0 plugin tools: turn src/plugin/games.py into a plugin and move it to src/plugins/plugin_misc_room_game.py
souliane <souliane@mailoo.org>
parents: 717
diff changeset
55 RoomGame._init_(self, host, PLUGIN_INFO, (NS_QG, QG_TAG), game_init={'stage': None}, player_init={'score': 0})
1359
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 993
diff changeset
56 host.bridge.addMethod("quizGameLaunch", ".plugin", in_sign='asss', out_sign='', method=self._prepareRoom) # args: players, room_jid, profile
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 993
diff changeset
57 host.bridge.addMethod("quizGameCreate", ".plugin", in_sign='sass', out_sign='', method=self._createGame) # args: room_jid, players, profile
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 993
diff changeset
58 host.bridge.addMethod("quizGameReady", ".plugin", in_sign='sss', out_sign='', method=self._playerReady) # args: player, referee, profile
372
f964dcec1611 core: plugins refactored according to bridge + updatedValue now use profile
Goffi <goffi@goffi.org>
parents: 362
diff changeset
59 host.bridge.addMethod("quizGameAnswer", ".plugin", in_sign='ssss', out_sign='', method=self.playerAnswer)
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
60 host.bridge.addSignal("quizGameStarted", ".plugin", signature='ssass') # args: room_jid, referee, players, profile
372
f964dcec1611 core: plugins refactored according to bridge + updatedValue now use profile
Goffi <goffi@goffi.org>
parents: 362
diff changeset
61 host.bridge.addSignal("quizGameNew", ".plugin",
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
62 signature='sa{ss}s',
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
63 doc={'summary': 'Start a new game',
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
64 'param_0': "room_jid: jid of game's room",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
65 'param_1': "game_data: data of the game",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
66 'param_2': '%(doc_profile)s'})
372
f964dcec1611 core: plugins refactored according to bridge + updatedValue now use profile
Goffi <goffi@goffi.org>
parents: 362
diff changeset
67 host.bridge.addSignal("quizGameQuestion", ".plugin",
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
68 signature='sssis',
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
69 doc={'summary': "Send the current question",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
70 'param_0': "room_jid: jid of game's room",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
71 'param_1': "question_id: question id",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
72 'param_2': "question: question to ask",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
73 'param_3': "timer: timer",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
74 'param_4': '%(doc_profile)s'})
372
f964dcec1611 core: plugins refactored according to bridge + updatedValue now use profile
Goffi <goffi@goffi.org>
parents: 362
diff changeset
75 host.bridge.addSignal("quizGamePlayerBuzzed", ".plugin",
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
76 signature='ssbs',
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
77 doc={'summary': "A player just pressed the buzzer",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
78 'param_0': "room_jid: jid of game's room",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
79 'param_1': "player: player who pushed the buzzer",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
80 'param_2': "pause: should the game be paused ?",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
81 'param_3': '%(doc_profile)s'})
372
f964dcec1611 core: plugins refactored according to bridge + updatedValue now use profile
Goffi <goffi@goffi.org>
parents: 362
diff changeset
82 host.bridge.addSignal("quizGamePlayerSays", ".plugin",
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
83 signature='sssis',
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
84 doc={'summary': "A player just pressed the buzzer",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
85 'param_0': "room_jid: jid of game's room",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
86 'param_1': "player: player who pushed the buzzer",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
87 'param_2': "text: what the player say",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
88 'param_3': "delay: how long, in seconds, the text must appear",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
89 'param_4': '%(doc_profile)s'})
372
f964dcec1611 core: plugins refactored according to bridge + updatedValue now use profile
Goffi <goffi@goffi.org>
parents: 362
diff changeset
90 host.bridge.addSignal("quizGameAnswerResult", ".plugin",
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
91 signature='ssba{si}s',
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
92 doc={'summary': "Result of the just given answer",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
93 'param_0': "room_jid: jid of game's room",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
94 'param_1': "player: player who gave the answer",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
95 'param_2': "good_answer: True if the answer is right",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
96 'param_3': "score: dict of score with player as key",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
97 'param_4': '%(doc_profile)s'})
372
f964dcec1611 core: plugins refactored according to bridge + updatedValue now use profile
Goffi <goffi@goffi.org>
parents: 362
diff changeset
98 host.bridge.addSignal("quizGameTimerExpired", ".plugin",
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
99 signature='ss',
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
100 doc={'summary': "Nobody answered the question in time",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
101 'param_0': "room_jid: jid of game's room",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
102 'param_1': '%(doc_profile)s'})
372
f964dcec1611 core: plugins refactored according to bridge + updatedValue now use profile
Goffi <goffi@goffi.org>
parents: 362
diff changeset
103 host.bridge.addSignal("quizGameTimerRestarted", ".plugin",
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
104 signature='sis',
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
105 doc={'summary': "Nobody answered the question in time",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
106 'param_0': "room_jid: jid of game's room",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
107 'param_1': "time_left: time left before timer expiration",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
108 'param_2': '%(doc_profile)s'})
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
109
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
110 def __game_data_to_xml(self, game_data):
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
111 """Convert a game data dict to domish element"""
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
112 game_data_elt = domish.Element((None, 'game_data'))
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
113 for data in game_data:
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
114 data_elt = domish.Element((None, data))
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
115 data_elt.addContent(game_data[data])
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
116 game_data_elt.addChild(data_elt)
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
117 return game_data_elt
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
118
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
119 def __xml_to_game_data(self, game_data_elt):
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
120 """Convert a domish element with game_data to a dict"""
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
121 game_data = {}
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
122 for data_elt in game_data_elt.elements():
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
123 game_data[data_elt.name] = unicode(data_elt)
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
124 return game_data
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
125
362
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
126 def __answer_result_to_signal_args(self, answer_result_elt):
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
127 """Parse answer result element and return a tuple of signal arguments
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
128 @param answer_result_elt: answer result element
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
129 @return: (player, good_answer, score)"""
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
130 score = {}
660
69a8bfd266a5 core, plugins: fixed bad use of children instead of elements() for domish.Element instances.
Goffi <goffi@goffi.org>
parents: 630
diff changeset
131 for score_elt in answer_result_elt.elements():
362
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
132 score[score_elt['player']] = int(score_elt['score'])
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
133 return (answer_result_elt['player'], answer_result_elt['good_answer'] == str(True), score)
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
134
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
135 def __answer_result(self, player_answering, good_answer, game_data):
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
136 """Convert a domish an answer_result element
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
137 @param player_answering: player who gave the answer
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
138 @param good_answer: True is the answer is right
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
139 @param game_data: data of the game"""
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
140 players_data = game_data['players_data']
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
141 score = {}
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
142 for player in game_data['players']:
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
143 score[player] = players_data[player]['score']
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
144
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
145 answer_result_elt = domish.Element((None, 'answer_result'))
362
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
146 answer_result_elt['player'] = player_answering
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
147 answer_result_elt['good_answer'] = str(good_answer)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
148
362
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
149 for player in score:
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
150 score_elt = domish.Element((None, "score"))
362
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
151 score_elt['player'] = player
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
152 score_elt['score'] = str(score[player])
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
153 answer_result_elt.addChild(score_elt)
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
154
362
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
155 return answer_result_elt
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
156
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
157 def __ask_question(self, question_id, question, timer):
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
158 """Create a element for asking a question"""
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
159 question_elt = domish.Element((None, 'question'))
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
160 question_elt['id'] = question_id
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
161 question_elt['timer'] = str(timer)
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
162 question_elt.addContent(question)
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
163 return question_elt
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
164
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
165 def __start_play(self, room_jid, game_data, profile):
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
166 """Start the game (tell to the first player after dealer to play"""
2129
6a66c8c5a567 core: replaced calls to client.xmlstream.send by client.send which is the right method to use. client.xmlstream should not be used directly
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
167 client = self.host.getClient(profile)
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
168 game_data['stage'] = "play"
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
169 next_player_idx = game_data['current_player'] = (game_data['init_player'] + 1) % len(game_data['players']) # the player after the dealer start
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
170 game_data['first_player'] = next_player = game_data['players'][next_player_idx]
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
171 to_jid = jid.JID(room_jid.userhost() + "/" + next_player)
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
172 mess = self.createGameElt(to_jid)
2129
6a66c8c5a567 core: replaced calls to client.xmlstream.send by client.send which is the right method to use. client.xmlstream should not be used directly
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
173 mess.firstChildElement().addElement('your_turn')
6a66c8c5a567 core: replaced calls to client.xmlstream.send by client.send which is the right method to use. client.xmlstream should not be used directly
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
174 client.send(mess)
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
175
916
1a759096ccbd core: use of Const for profile_key + replaced '@DEFAULT@' default profile_key by '@NONE@'
Goffi <goffi@goffi.org>
parents: 811
diff changeset
176 def playerAnswer(self, player, referee, answer, profile_key=C.PROF_KEY_NONE):
362
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
177 """Called when a player give an answer"""
2129
6a66c8c5a567 core: replaced calls to client.xmlstream.send by client.send which is the right method to use. client.xmlstream should not be used directly
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
178 client = self.host.getClient(profile_key)
6a66c8c5a567 core: replaced calls to client.xmlstream.send by client.send which is the right method to use. client.xmlstream should not be used directly
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
179 log.debug(u'new player answer (%(profile)s): %(answer)s' % {'profile': client.profile, 'answer': answer})
362
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
180 mess = self.createGameElt(jid.JID(referee))
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
181 answer_elt = mess.firstChildElement().addElement('player_answer')
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
182 answer_elt['player'] = player
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
183 answer_elt.addContent(answer)
2129
6a66c8c5a567 core: replaced calls to client.xmlstream.send by client.send which is the right method to use. client.xmlstream should not be used directly
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
184 client.send(mess)
362
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
185
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
186 def timerExpired(self, room_jid, profile):
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
187 """Called when nobody answered the question in time"""
2129
6a66c8c5a567 core: replaced calls to client.xmlstream.send by client.send which is the right method to use. client.xmlstream should not be used directly
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
188 client = self.host.getClient(profile)
1359
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 993
diff changeset
189 game_data = self.games[room_jid]
362
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
190 game_data['stage'] = 'expired'
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
191 mess = self.createGameElt(room_jid)
2129
6a66c8c5a567 core: replaced calls to client.xmlstream.send by client.send which is the right method to use. client.xmlstream should not be used directly
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
192 mess.firstChildElement().addElement('timer_expired')
6a66c8c5a567 core: replaced calls to client.xmlstream.send by client.send which is the right method to use. client.xmlstream should not be used directly
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
193 client.send(mess)
6a66c8c5a567 core: replaced calls to client.xmlstream.send by client.send which is the right method to use. client.xmlstream should not be used directly
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
194 reactor.callLater(4, self.askQuestion, room_jid, client.profile)
362
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
195
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
196 def pauseTimer(self, room_jid):
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
197 """Stop the timer and save the time left"""
1359
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 993
diff changeset
198 game_data = self.games[room_jid]
362
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
199 left = max(0, game_data["timer"].getTime() - time())
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
200 game_data['timer'].cancel()
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
201 game_data['time_left'] = int(left)
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
202 game_data['previous_stage'] = game_data['stage']
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
203 game_data['stage'] = "paused"
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
204
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
205 def restartTimer(self, room_jid, profile):
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
206 """Restart a timer with the saved time"""
2129
6a66c8c5a567 core: replaced calls to client.xmlstream.send by client.send which is the right method to use. client.xmlstream should not be used directly
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
207 client = self.host.getClient(profile)
1359
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 993
diff changeset
208 game_data = self.games[room_jid]
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
209 assert game_data['time_left'] is not None
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
210 mess = self.createGameElt(room_jid)
2129
6a66c8c5a567 core: replaced calls to client.xmlstream.send by client.send which is the right method to use. client.xmlstream should not be used directly
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
211 mess.firstChildElement().addElement('timer_restarted')
6a66c8c5a567 core: replaced calls to client.xmlstream.send by client.send which is the right method to use. client.xmlstream should not be used directly
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
212 jabber_client.restarted_elt["time_left"] = str(game_data['time_left'])
6a66c8c5a567 core: replaced calls to client.xmlstream.send by client.send which is the right method to use. client.xmlstream should not be used directly
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
213 client.send(mess)
362
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
214 game_data["timer"] = reactor.callLater(game_data['time_left'], self.timerExpired, room_jid, profile)
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
215 game_data["time_left"] = None
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
216 game_data['stage'] = game_data['previous_stage']
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
217 del game_data['previous_stage']
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
218
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
219 def askQuestion(self, room_jid, profile):
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
220 """Ask a new question"""
2129
6a66c8c5a567 core: replaced calls to client.xmlstream.send by client.send which is the right method to use. client.xmlstream should not be used directly
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
221 client = self.host.getClient(profile)
1359
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 993
diff changeset
222 game_data = self.games[room_jid]
362
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
223 game_data['stage'] = "question"
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
224 game_data['question_id'] = "1"
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
225 timer = 30
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
226 mess = self.createGameElt(room_jid)
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
227 mess.firstChildElement().addChild(self.__ask_question(game_data['question_id'], u"Quel est l'âge du capitaine ?", timer))
2129
6a66c8c5a567 core: replaced calls to client.xmlstream.send by client.send which is the right method to use. client.xmlstream should not be used directly
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
228 client.send(mess)
362
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
229 game_data["timer"] = reactor.callLater(timer, self.timerExpired, room_jid, profile)
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
230 game_data["time_left"] = None
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
231
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
232 def checkAnswer(self, room_jid, player, answer, profile):
362
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
233 """Check if the answer given is right"""
2129
6a66c8c5a567 core: replaced calls to client.xmlstream.send by client.send which is the right method to use. client.xmlstream should not be used directly
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
234 client = self.host.getClient(profile)
1359
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 993
diff changeset
235 game_data = self.games[room_jid]
362
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
236 players_data = game_data['players_data']
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
237 good_answer = game_data['question_id'] == "1" and answer == "42"
362
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
238 players_data[player]['score'] += 1 if good_answer else -1
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
239 players_data[player]['score'] = min(9, max(0, players_data[player]['score']))
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
240
362
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
241 mess = self.createGameElt(room_jid)
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
242 mess.firstChildElement().addChild(self.__answer_result(player, good_answer, game_data))
2129
6a66c8c5a567 core: replaced calls to client.xmlstream.send by client.send which is the right method to use. client.xmlstream should not be used directly
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
243 client.send(mess)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
244
362
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
245 if good_answer:
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
246 reactor.callLater(4, self.askQuestion, room_jid, profile)
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
247 else:
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
248 reactor.callLater(4, self.restartTimer, room_jid, profile)
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
249
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
250 def newGame(self, room_jid, profile):
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
251 """Launch a new round"""
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents: 682
diff changeset
252 common_data = {'game_score': 0}
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
253 new_game_data = {"instructions": _(u"""Bienvenue dans cette partie rapide de quizz, le premier à atteindre le score de 9 remporte le jeu
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
254
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
255 Attention, tu es prêt ?""")}
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents: 682
diff changeset
256 msg_elts = self.__game_data_to_xml(new_game_data)
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents: 682
diff changeset
257 RoomGame.newRound(self, room_jid, (common_data, msg_elts), profile)
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
258 reactor.callLater(10, self.askQuestion, room_jid, profile)
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
259
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 712
diff changeset
260 def room_game_cmd(self, mess_elt, profile):
2129
6a66c8c5a567 core: replaced calls to client.xmlstream.send by client.send which is the right method to use. client.xmlstream should not be used directly
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
261 client = self.host.getClient(profile)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
262 from_jid = jid.JID(mess_elt['from'])
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
263 room_jid = jid.JID(from_jid.userhost())
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
264 game_elt = mess_elt.firstChildElement()
1359
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 993
diff changeset
265 game_data = self.games[room_jid]
2129
6a66c8c5a567 core: replaced calls to client.xmlstream.send by client.send which is the right method to use. client.xmlstream should not be used directly
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
266 # if 'players_data' in game_data:
6a66c8c5a567 core: replaced calls to client.xmlstream.send by client.send which is the right method to use. client.xmlstream should not be used directly
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
267 #  players_data = game_data['players_data']
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
268
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
269 for elt in game_elt.elements():
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
270
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
271 if elt.name == 'started': # new game created
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
272 players = []
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
273 for player in elt.elements():
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
274 players.append(unicode(player))
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
275 self.host.bridge.quizGameStarted(room_jid.userhost(), from_jid.full(), players, profile)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
276
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
277 elif elt.name == 'player_ready': # ready to play
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
278 player = elt['player']
1359
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 993
diff changeset
279 status = self.games[room_jid]['status']
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 993
diff changeset
280 nb_players = len(self.games[room_jid]['players'])
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
281 status[player] = 'ready'
1409
3265a2639182 massive (preventive) addition of 'u' (unicode) before the strings passed to logging functions
souliane <souliane@mailoo.org>
parents: 1396
diff changeset
282 log.debug(_(u'Player %(player)s is ready to start [status: %(status)s]') % {'player': player, 'status': status})
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
283 if status.values().count('ready') == nb_players: # everybody is ready, we can start the game
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
284 self.newGame(room_jid, profile)
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
285
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
286 elif elt.name == 'game_data':
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
287 self.host.bridge.quizGameNew(room_jid.userhost(), self.__xml_to_game_data(elt), profile)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
288
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
289 elif elt.name == 'question': # A question is asked
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
290 self.host.bridge.quizGameQuestion(room_jid.userhost(), elt["id"], unicode(elt), int(elt["timer"]), profile)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
291
362
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
292 elif elt.name == 'player_answer':
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
293 player = elt['player']
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
294 pause = game_data['stage'] == 'question' # we pause the game only if we are have a question at the moment
1359
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 993
diff changeset
295 # we first send a buzzer message
362
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
296 mess = self.createGameElt(room_jid)
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
297 buzzer_elt = mess.firstChildElement().addElement('player_buzzed')
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
298 buzzer_elt['player'] = player
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
299 buzzer_elt['pause'] = str(pause)
2129
6a66c8c5a567 core: replaced calls to client.xmlstream.send by client.send which is the right method to use. client.xmlstream should not be used directly
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
300 client.send(mess)
362
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
301 if pause:
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
302 self.pauseTimer(room_jid)
1359
83127a4c89ce plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents: 993
diff changeset
303 # and we send the player answer
362
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
304 mess = self.createGameElt(room_jid)
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
305 _answer = unicode(elt)
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
306 say_elt = mess.firstChildElement().addElement('player_says')
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
307 say_elt['player'] = player
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
308 say_elt.addContent(_answer)
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
309 say_elt['delay'] = "3"
2129
6a66c8c5a567 core: replaced calls to client.xmlstream.send by client.send which is the right method to use. client.xmlstream should not be used directly
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
310 reactor.callLater(2, client.send, mess)
362
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
311 reactor.callLater(6, self.checkAnswer, room_jid, player, _answer, profile=profile)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
312
362
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
313 elif elt.name == 'player_buzzed':
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
314 self.host.bridge.quizGamePlayerBuzzed(room_jid.userhost(), elt["player"], elt['pause'] == str(True), profile)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
315
362
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
316 elif elt.name == 'player_says':
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
317 self.host.bridge.quizGamePlayerSays(room_jid.userhost(), elt["player"], unicode(elt), int(elt["delay"]), profile)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
318
362
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
319 elif elt.name == 'answer_result':
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
320 player, good_answer, score = self.__answer_result_to_signal_args(elt)
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
321 self.host.bridge.quizGameAnswerResult(room_jid.userhost(), player, good_answer, score, profile)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
322
362
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
323 elif elt.name == 'timer_expired':
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
324 self.host.bridge.quizGameTimerExpired(room_jid.userhost(), profile)
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
325
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
326 elif elt.name == 'timer_restarted':
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
327 self.host.bridge.quizGameTimerRestarted(room_jid.userhost(), int(elt['time_left']), profile)
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
328
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
329 else:
1409
3265a2639182 massive (preventive) addition of 'u' (unicode) before the strings passed to logging functions
souliane <souliane@mailoo.org>
parents: 1396
diff changeset
330 log.error(_(u'Unmanaged game element: %s') % elt.name)