annotate sat/plugins/plugin_misc_quiz.py @ 3080:16925f494820

plugin XEP-0045: don't fail on `item-not-found` with MAM: Some servers don't store permanently the whole history of MUC with MAM. As a result, an "item-not-found" stanza error can be received when message id used as reference doesn't exist anymore on the server. When this case happens, the history is retrieved in the same way as for a newly joined room (i.e. last 50 messages are requested).
author Goffi <goffi@goffi.org>
date Thu, 05 Dec 2019 23:05:16 +0100
parents ab2696e34d29
children 9d0df638c8b4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
1 #!/usr/bin/env python3
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
2771
003b8b4b56a7 date update
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
5 # Copyright (C) 2009-2019 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
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
23
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 916
diff changeset
24 log = getLogger(__name__)
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
25 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
26 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
27 from twisted.words.protocols.jabber import client as jabber_client, jid
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
28 from time import time
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
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
31 NS_QG = "http://www.goffi.org/protocol/quiz"
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
32 QG_TAG = "quiz"
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
33
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
34 PLUGIN_INFO = {
2145
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2129
diff changeset
35 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
36 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
37 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
38 C.PI_PROTOCOLS: [],
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2129
diff changeset
39 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
40 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
41 C.PI_HANDLER: "yes",
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
42 C.PI_DESCRIPTION: _("""Implementation of Quiz game"""),
361
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
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
45
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
46 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
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__
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
50 self.__class__ = type(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
51 self.__class__.__name__, (self.__class__, RoomGame, object), {}
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
52 )
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
53
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
54 def __init__(self, host):
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 916
diff changeset
55 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
56 self.inheritFromRoomGame(host)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
57 RoomGame._init_(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
58 self,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
59 host,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
60 PLUGIN_INFO,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
61 (NS_QG, QG_TAG),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
62 game_init={"stage": None},
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
63 player_init={"score": 0},
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
64 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
65 host.bridge.addMethod(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
66 "quizGameLaunch",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
67 ".plugin",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
68 in_sign="asss",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
69 out_sign="",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
70 method=self._prepareRoom,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
71 ) # args: players, room_jid, profile
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
72 host.bridge.addMethod(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
73 "quizGameCreate",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
74 ".plugin",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
75 in_sign="sass",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
76 out_sign="",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
77 method=self._createGame,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
78 ) # args: room_jid, players, profile
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
79 host.bridge.addMethod(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
80 "quizGameReady",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
81 ".plugin",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
82 in_sign="sss",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
83 out_sign="",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
84 method=self._playerReady,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
85 ) # args: player, referee, profile
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
86 host.bridge.addMethod(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
87 "quizGameAnswer",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
88 ".plugin",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
89 in_sign="ssss",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
90 out_sign="",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
91 method=self.playerAnswer,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
92 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
93 host.bridge.addSignal(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
94 "quizGameStarted", ".plugin", signature="ssass"
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
95 ) # args: room_jid, referee, players, profile
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
96 host.bridge.addSignal(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
97 "quizGameNew",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
98 ".plugin",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
99 signature="sa{ss}s",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
100 doc={
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
101 "summary": "Start a new game",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
102 "param_0": "room_jid: jid of game's room",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
103 "param_1": "game_data: data of the game",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
104 "param_2": "%(doc_profile)s",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
105 },
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
106 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
107 host.bridge.addSignal(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
108 "quizGameQuestion",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
109 ".plugin",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
110 signature="sssis",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
111 doc={
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
112 "summary": "Send the current question",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
113 "param_0": "room_jid: jid of game's room",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
114 "param_1": "question_id: question id",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
115 "param_2": "question: question to ask",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
116 "param_3": "timer: timer",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
117 "param_4": "%(doc_profile)s",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
118 },
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
119 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
120 host.bridge.addSignal(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
121 "quizGamePlayerBuzzed",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
122 ".plugin",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
123 signature="ssbs",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
124 doc={
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
125 "summary": "A player just pressed the buzzer",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
126 "param_0": "room_jid: jid of game's room",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
127 "param_1": "player: player who pushed the buzzer",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
128 "param_2": "pause: should the game be paused ?",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
129 "param_3": "%(doc_profile)s",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
130 },
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
131 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
132 host.bridge.addSignal(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
133 "quizGamePlayerSays",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
134 ".plugin",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
135 signature="sssis",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
136 doc={
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
137 "summary": "A player just pressed the buzzer",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
138 "param_0": "room_jid: jid of game's room",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
139 "param_1": "player: player who pushed the buzzer",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
140 "param_2": "text: what the player say",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
141 "param_3": "delay: how long, in seconds, the text must appear",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
142 "param_4": "%(doc_profile)s",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
143 },
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
144 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
145 host.bridge.addSignal(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
146 "quizGameAnswerResult",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
147 ".plugin",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
148 signature="ssba{si}s",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
149 doc={
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
150 "summary": "Result of the just given answer",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
151 "param_0": "room_jid: jid of game's room",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
152 "param_1": "player: player who gave the answer",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
153 "param_2": "good_answer: True if the answer is right",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
154 "param_3": "score: dict of score with player as key",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
155 "param_4": "%(doc_profile)s",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
156 },
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
157 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
158 host.bridge.addSignal(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
159 "quizGameTimerExpired",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
160 ".plugin",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
161 signature="ss",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
162 doc={
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
163 "summary": "Nobody answered the question in time",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
164 "param_0": "room_jid: jid of game's room",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
165 "param_1": "%(doc_profile)s",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
166 },
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
167 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
168 host.bridge.addSignal(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
169 "quizGameTimerRestarted",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
170 ".plugin",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
171 signature="sis",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
172 doc={
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
173 "summary": "Nobody answered the question in time",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
174 "param_0": "room_jid: jid of game's room",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
175 "param_1": "time_left: time left before timer expiration",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
176 "param_2": "%(doc_profile)s",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
177 },
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
178 )
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
179
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
180 def __game_data_to_xml(self, game_data):
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
181 """Convert a game data dict to domish element"""
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
182 game_data_elt = domish.Element((None, "game_data"))
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
183 for data in game_data:
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
184 data_elt = domish.Element((None, data))
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
185 data_elt.addContent(game_data[data])
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
186 game_data_elt.addChild(data_elt)
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
187 return game_data_elt
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
188
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
189 def __xml_to_game_data(self, game_data_elt):
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
190 """Convert a domish element with game_data to a dict"""
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
191 game_data = {}
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
192 for data_elt in game_data_elt.elements():
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
193 game_data[data_elt.name] = str(data_elt)
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
194 return game_data
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
195
362
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
196 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
197 """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
198 @param answer_result_elt: answer result element
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
199 @return: (player, good_answer, score)"""
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
200 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
201 for score_elt in answer_result_elt.elements():
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
202 score[score_elt["player"]] = int(score_elt["score"])
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
203 return (
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
204 answer_result_elt["player"],
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
205 answer_result_elt["good_answer"] == str(True),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
206 score,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
207 )
362
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
208
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
209 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
210 """Convert a domish an answer_result element
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
211 @param player_answering: player who gave the answer
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
212 @param good_answer: True is the answer is right
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
213 @param game_data: data of the game"""
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
214 players_data = game_data["players_data"]
362
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
215 score = {}
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
216 for player in game_data["players"]:
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
217 score[player] = players_data[player]["score"]
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
218
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
219 answer_result_elt = domish.Element((None, "answer_result"))
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
220 answer_result_elt["player"] = player_answering
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
221 answer_result_elt["good_answer"] = str(good_answer)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
222
362
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
223 for player in score:
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
224 score_elt = domish.Element((None, "score"))
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
225 score_elt["player"] = player
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
226 score_elt["score"] = str(score[player])
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
227 answer_result_elt.addChild(score_elt)
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
228
362
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
229 return answer_result_elt
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
230
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
231 def __ask_question(self, question_id, question, timer):
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
232 """Create a element for asking a question"""
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
233 question_elt = domish.Element((None, "question"))
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
234 question_elt["id"] = question_id
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
235 question_elt["timer"] = str(timer)
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
236 question_elt.addContent(question)
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
237 return question_elt
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
238
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
239 def __start_play(self, room_jid, game_data, profile):
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
240 """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
241 client = self.host.getClient(profile)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
242 game_data["stage"] = "play"
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
243 next_player_idx = game_data["current_player"] = (
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
244 game_data["init_player"] + 1
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
245 ) % len(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
246 game_data["players"]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
247 ) # the player after the dealer start
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
248 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
249 to_jid = jid.JID(room_jid.userhost() + "/" + next_player)
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
250 mess = self.createGameElt(to_jid)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
251 mess.firstChildElement().addElement("your_turn")
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
252 client.send(mess)
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
253
916
1a759096ccbd core: use of Const for profile_key + replaced '@DEFAULT@' default profile_key by '@NONE@'
Goffi <goffi@goffi.org>
parents: 811
diff changeset
254 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
255 """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
256 client = self.host.getClient(profile_key)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
257 log.debug(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
258 "new player answer (%(profile)s): %(answer)s"
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
259 % {"profile": client.profile, "answer": answer}
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
260 )
362
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
261 mess = self.createGameElt(jid.JID(referee))
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
262 answer_elt = mess.firstChildElement().addElement("player_answer")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
263 answer_elt["player"] = player
362
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
264 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
265 client.send(mess)
362
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
266
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
267 def timerExpired(self, room_jid, profile):
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
268 """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
269 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
270 game_data = self.games[room_jid]
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
271 game_data["stage"] = "expired"
362
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
272 mess = self.createGameElt(room_jid)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
273 mess.firstChildElement().addElement("timer_expired")
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
274 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
275 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
276
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
277 def pauseTimer(self, room_jid):
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
278 """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
279 game_data = self.games[room_jid]
362
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
280 left = max(0, game_data["timer"].getTime() - time())
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
281 game_data["timer"].cancel()
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
282 game_data["time_left"] = int(left)
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
283 game_data["previous_stage"] = game_data["stage"]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
284 game_data["stage"] = "paused"
362
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
285
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
286 def restartTimer(self, room_jid, profile):
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
287 """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
288 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
289 game_data = self.games[room_jid]
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
290 assert game_data["time_left"] is not None
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
291 mess = self.createGameElt(room_jid)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
292 mess.firstChildElement().addElement("timer_restarted")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
293 jabber_client.restarted_elt["time_left"] = str(game_data["time_left"])
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
294 client.send(mess)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
295 game_data["timer"] = reactor.callLater(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
296 game_data["time_left"], self.timerExpired, room_jid, profile
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
297 )
362
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
298 game_data["time_left"] = None
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
299 game_data["stage"] = game_data["previous_stage"]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
300 del game_data["previous_stage"]
362
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
301
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
302 def askQuestion(self, room_jid, profile):
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
303 """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
304 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
305 game_data = self.games[room_jid]
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
306 game_data["stage"] = "question"
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
307 game_data["question_id"] = "1"
362
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
308 timer = 30
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
309 mess = self.createGameElt(room_jid)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
310 mess.firstChildElement().addChild(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
311 self.__ask_question(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
312 game_data["question_id"], "Quel est l'âge du capitaine ?", timer
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
313 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
314 )
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
315 client.send(mess)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
316 game_data["timer"] = reactor.callLater(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
317 timer, self.timerExpired, room_jid, profile
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
318 )
362
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
319 game_data["time_left"] = None
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
320
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
321 def checkAnswer(self, room_jid, player, answer, profile):
362
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
322 """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
323 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
324 game_data = self.games[room_jid]
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
325 players_data = game_data["players_data"]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
326 good_answer = game_data["question_id"] == "1" and answer == "42"
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
327 players_data[player]["score"] += 1 if good_answer else -1
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
328 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
329
362
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
330 mess = self.createGameElt(room_jid)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
331 mess.firstChildElement().addChild(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
332 self.__answer_result(player, good_answer, game_data)
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
333 )
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
334 client.send(mess)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
335
362
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
336 if good_answer:
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
337 reactor.callLater(4, self.askQuestion, room_jid, profile)
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
338 else:
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
339 reactor.callLater(4, self.restartTimer, room_jid, profile)
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
340
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
341 def newGame(self, room_jid, profile):
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
342 """Launch a new round"""
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
343 common_data = {"game_score": 0}
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
344 new_game_data = {
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
345 "instructions": _(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
346 """Bienvenue dans cette partie rapide de quizz, le premier à atteindre le score de 9 remporte le jeu
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
347
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
348 Attention, tu es prêt ?"""
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
349 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
350 }
683
75e4f5e2cc65 plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents: 682
diff changeset
351 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
352 RoomGame.newRound(self, room_jid, (common_data, msg_elts), profile)
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
353 reactor.callLater(10, self.askQuestion, room_jid, profile)
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
354
717
358018c5c398 plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents: 712
diff changeset
355 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
356 client = self.host.getClient(profile)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
357 from_jid = jid.JID(mess_elt["from"])
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
358 room_jid = jid.JID(from_jid.userhost())
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
359 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
360 game_data = self.games[room_jid]
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
361 #  if 'players_data' in game_data:
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
362 #   players_data = game_data['players_data']
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
363
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
364 for elt in game_elt.elements():
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
365
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
366 if elt.name == "started": # new game created
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
367 players = []
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
368 for player in elt.elements():
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
369 players.append(str(player))
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
370 self.host.bridge.quizGameStarted(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
371 room_jid.userhost(), from_jid.full(), players, profile
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
372 )
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
373
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
374 elif elt.name == "player_ready": # ready to play
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
375 player = elt["player"]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
376 status = self.games[room_jid]["status"]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
377 nb_players = len(self.games[room_jid]["players"])
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
378 status[player] = "ready"
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
379 log.debug(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
380 _("Player %(player)s is ready to start [status: %(status)s]")
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
381 % {"player": player, "status": status}
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
382 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
383 if (
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
384 list(status.values()).count("ready") == nb_players
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
385 ): # everybody is ready, we can start the game
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
386 self.newGame(room_jid, profile)
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
387
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
388 elif elt.name == "game_data":
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
389 self.host.bridge.quizGameNew(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
390 room_jid.userhost(), self.__xml_to_game_data(elt), profile
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
391 )
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
392
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
393 elif elt.name == "question": # A question is asked
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
394 self.host.bridge.quizGameQuestion(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
395 room_jid.userhost(),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
396 elt["id"],
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
397 str(elt),
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
398 int(elt["timer"]),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
399 profile,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
400 )
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
401
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
402 elif elt.name == "player_answer":
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
403 player = elt["player"]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
404 pause = (
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
405 game_data["stage"] == "question"
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
406 ) # 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
407 # we first send a buzzer message
362
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
408 mess = self.createGameElt(room_jid)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
409 buzzer_elt = mess.firstChildElement().addElement("player_buzzed")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
410 buzzer_elt["player"] = player
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
411 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
412 client.send(mess)
362
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
413 if pause:
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
414 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
415 # and we send the player answer
362
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
416 mess = self.createGameElt(room_jid)
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
417 _answer = str(elt)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
418 say_elt = mess.firstChildElement().addElement("player_says")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
419 say_elt["player"] = player
362
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
420 say_elt.addContent(_answer)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
421 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
422 reactor.callLater(2, client.send, mess)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
423 reactor.callLater(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
424 6, self.checkAnswer, room_jid, player, _answer, profile=profile
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
425 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
426
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
427 elif elt.name == "player_buzzed":
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
428 self.host.bridge.quizGamePlayerBuzzed(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
429 room_jid.userhost(), elt["player"], elt["pause"] == str(True), profile
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
430 )
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
431
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
432 elif elt.name == "player_says":
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
433 self.host.bridge.quizGamePlayerSays(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
434 room_jid.userhost(),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
435 elt["player"],
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
436 str(elt),
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
437 int(elt["delay"]),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
438 profile,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
439 )
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
440
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
441 elif elt.name == "answer_result":
362
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
442 player, good_answer, score = self.__answer_result_to_signal_args(elt)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
443 self.host.bridge.quizGameAnswerResult(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
444 room_jid.userhost(), player, good_answer, score, profile
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
445 )
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
446
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
447 elif elt.name == "timer_expired":
362
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
448 self.host.bridge.quizGameTimerExpired(room_jid.userhost(), profile)
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
449
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
450 elif elt.name == "timer_restarted":
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
451 self.host.bridge.quizGameTimerRestarted(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
452 room_jid.userhost(), int(elt["time_left"]), profile
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
453 )
362
208107419b17 Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents: 361
diff changeset
454
361
141eeb7cd9e6 Quizz game: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
455 else:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
456 log.error(_("Unmanaged game element: %s") % elt.name)