comparison libervia/frontends/quick_frontend/quick_games.py @ 4074:26b7ed2817da

refactoring: rename `sat_frontends` to `libervia.frontends`
author Goffi <goffi@goffi.org>
date Fri, 02 Jun 2023 14:12:38 +0200
parents sat_frontends/quick_frontend/quick_games.py@4b842c1fb686
children
comparison
equal deleted inserted replaced
4073:7c5654c54fed 4074:26b7ed2817da
1 #!/usr/bin/env python3
2
3
4 # helper class for making a SAT frontend
5 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
6
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
16
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20 from libervia.backend.core.log import getLogger
21
22 log = getLogger(__name__)
23
24 from libervia.backend.core.i18n import _
25
26 from libervia.frontends.tools import jid
27 from libervia.frontends.tools import games
28 from libervia.frontends.quick_frontend.constants import Const as C
29
30 from . import quick_chat
31
32
33 class RoomGame(object):
34 _game_name = None
35 _signal_prefix = None
36 _signal_suffixes = None
37
38 @classmethod
39 def register_signals(cls, host):
40 def make_handler(suffix, signal):
41 def handler(*args):
42 if suffix in ("Started", "Players"):
43 return cls.started_handler(host, suffix, *args)
44 return cls.generic_handler(host, signal, *args)
45
46 return handler
47
48 for suffix in cls._signal_suffixes:
49 signal = cls._signal_prefix + suffix
50 host.register_signal(
51 signal, handler=make_handler(suffix, signal), iface="plugin"
52 )
53
54 @classmethod
55 def started_handler(cls, host, suffix, *args):
56 room_jid, args, profile = jid.JID(args[0]), args[1:-1], args[-1]
57 referee, players, args = args[0], args[1], args[2:]
58 chat_widget = host.widgets.get_or_create_widget(
59 quick_chat.QuickChat, room_jid, type_=C.CHAT_GROUP, profile=profile
60 )
61
62 # update symbols
63 if cls._game_name not in chat_widget.visible_states:
64 chat_widget.visible_states.append(cls._game_name)
65 symbols = games.SYMBOLS[cls._game_name]
66 index = 0
67 contact_list = host.contact_lists[profile]
68 for occupant in chat_widget.occupants:
69 occupant_jid = jid.new_resource(room_jid, occupant)
70 contact_list.set_cache(
71 occupant_jid,
72 cls._game_name,
73 symbols[index % len(symbols)] if occupant in players else None,
74 )
75 chat_widget.update(occupant_jid)
76
77 if suffix == "Players" or chat_widget.nick not in players:
78 return # waiting for other players to join, or not playing
79 if cls._game_name in chat_widget.games:
80 return # game panel is already there
81 real_class = host.widgets.get_real_class(cls)
82 if real_class == cls:
83 host.show_dialog(
84 _(
85 "A {game} activity between {players} has been started, but you couldn't take part because your client doesn't support it."
86 ).format(game=cls._game_name, players=", ".join(players)),
87 _("{game} Game").format(game=cls._game_name),
88 )
89 return
90 panel = real_class(chat_widget, referee, players, *args)
91 chat_widget.games[cls._game_name] = panel
92 chat_widget.add_game_panel(panel)
93
94 @classmethod
95 def generic_handler(cls, host, signal, *args):
96 room_jid, args, profile = jid.JID(args[0]), args[1:-1], args[-1]
97 chat_widget = host.widgets.get_widget(quick_chat.QuickChat, room_jid, profile)
98 if chat_widget:
99 try:
100 game_panel = chat_widget.games[cls._game_name]
101 except KeyError:
102 log.error(
103 "TODO: better game synchronisation - received signal %s but no panel is found"
104 % signal
105 )
106 return
107 else:
108 getattr(game_panel, "%sHandler" % signal)(*args)
109
110
111 class Tarot(RoomGame):
112 _game_name = "Tarot"
113 _signal_prefix = "tarotGame"
114 _signal_suffixes = (
115 "Started",
116 "Players",
117 "New",
118 "ChooseContrat",
119 "ShowCards",
120 "YourTurn",
121 "Score",
122 "CardsPlayed",
123 "InvalidCards",
124 )
125
126
127 class Quiz(RoomGame):
128 _game_name = "Quiz"
129 _signal_prefix = "quizGame"
130 _signal_suffixes = (
131 "Started",
132 "New",
133 "Question",
134 "PlayerBuzzed",
135 "PlayerSays",
136 "AnswerResult",
137 "TimerExpired",
138 "TimerRestarted",
139 )
140
141
142 class Radiocol(RoomGame):
143 _game_name = "Radiocol"
144 _signal_prefix = "radiocol"
145 _signal_suffixes = (
146 "Started",
147 "Players",
148 "SongRejected",
149 "Preload",
150 "Play",
151 "NoUpload",
152 "UploadOk",
153 )