Mercurial > libervia-backend
comparison frontends/src/quick_frontend/quick_games.py @ 1360:8ea8fa13c351 frontends_multi_profiles
frontends (quick_frontend, primitivus): fixes room games:
- add quick_frontend.quick_games for registering the signals and registering the UI classes
- rename the signals handlers to fit the convention (e.g.: tarotGameScoreHandler)
- rename card_game to game_tarot, quick_card_game to quick_game_tarot, CardGame to TarotGame
author | souliane <souliane@mailoo.org> |
---|---|
date | Wed, 11 Mar 2015 12:43:48 +0100 |
parents | |
children | da2ea16fabc6 |
comparison
equal
deleted
inserted
replaced
1359:83127a4c89ce | 1360:8ea8fa13c351 |
---|---|
1 #!/usr/bin/python | |
2 # -*- coding: utf-8 -*- | |
3 | |
4 # helper class for making a SAT frontend | |
5 # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 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 sat.core.log import getLogger | |
21 log = getLogger(__name__) | |
22 | |
23 from sat.core.i18n import _ | |
24 | |
25 from sat_frontends.tools import jid | |
26 from sat_frontends.quick_frontend.constants import Const as C | |
27 | |
28 import quick_chat | |
29 | |
30 | |
31 class RoomGame(object): | |
32 _game_name = None | |
33 _signal_prefix = None | |
34 _signal_suffixes = None | |
35 | |
36 @classmethod | |
37 def registerSignals(cls, host): | |
38 | |
39 def make_handler(suffix, signal): | |
40 def handler(*args): | |
41 if suffix in ("Started", "Players"): | |
42 return cls.startedHandler(host, suffix, *args) | |
43 return cls.genericHandler(host, signal, *args) | |
44 return handler | |
45 | |
46 for suffix in cls._signal_suffixes: | |
47 signal = cls._signal_prefix + suffix | |
48 host.registerSignal(signal, handler=make_handler(suffix, signal), iface="plugin") | |
49 | |
50 @classmethod | |
51 def startedHandler(cls, host, suffix, *args): | |
52 room_jid, args, profile = jid.JID(args[0]), args[1:-1], args[-1] | |
53 referee, players, args = args[0], args[1], args[2:] | |
54 chat_widget = host.widgets.getOrCreateWidget(quick_chat.QuickChat, room_jid, type_=C.CHAT_GROUP, profile=profile) | |
55 | |
56 # self.occupants_panel.updateSpecials(players, SYMBOLS[self._game_name.lower()]) # FIXME | |
57 if suffix == "Players" or chat_widget.nick not in players: | |
58 return # waiting for other players to join, or not playing | |
59 if cls._game_name in chat_widget.games: | |
60 return # game panel is already there | |
61 real_class = host.widgets.getRealClass(cls) | |
62 if real_class == cls: | |
63 host.showDialog(_(u"A {game} activity between {players} has been started, but you couldn't take part because your client doesn't support it.").format(game=cls._game_name, players=', '.join(players)), | |
64 _(u"{game} Game").format(game=cls._game_name)) | |
65 return | |
66 panel = real_class(chat_widget, referee, players, *args) | |
67 chat_widget.games[cls._game_name] = panel | |
68 chat_widget.addGamePanel(panel) | |
69 | |
70 @classmethod | |
71 def genericHandler(cls, host, signal, *args): | |
72 room_jid, args, profile = jid.JID(args[0]), args[1:-1], args[-1] | |
73 chat_widget = host.widgets.getWidget(quick_chat.QuickChat, room_jid, profile) | |
74 if chat_widget: | |
75 try: | |
76 game_panel = chat_widget.games[cls._game_name] | |
77 except KeyError: | |
78 log.error("TODO: better game synchronisation - received signal %s but no panel is found" % signal) | |
79 return | |
80 else: | |
81 getattr(game_panel, "%sHandler" % signal)(*args) | |
82 | |
83 | |
84 class Tarot(RoomGame): | |
85 _game_name = "Tarot" | |
86 _signal_prefix = "tarotGame" | |
87 _signal_suffixes = ("Started", "Players", "New", "ChooseContrat", | |
88 "ShowCards", "YourTurn", "Score", "CardsPlayed", | |
89 "InvalidCards", | |
90 ) | |
91 | |
92 | |
93 class Quiz(RoomGame): | |
94 _game_name = "Quiz" | |
95 _signal_prefix = "quizGame" | |
96 _signal_suffixes = ("Started", "New", "Question", "PlayerBuzzed", | |
97 "PlayerSays", "AnswerResult", "TimerExpired", | |
98 "TimerRestarted", | |
99 ) | |
100 | |
101 | |
102 class Radiocol(RoomGame): | |
103 _game_name = "Radiocol" | |
104 _signal_prefix = "radiocol" | |
105 _signal_suffixes = ("Started", "Players", "SongRejected", "Preload", | |
106 "Play", "NoUpload", "UploadOk") |