comparison sat_frontends/quick_frontend/quick_games.py @ 2562:26edcf3a30eb

core, setup: huge cleaning: - moved directories from src and frontends/src to sat and sat_frontends, which is the recommanded naming convention - move twisted directory to root - removed all hacks from setup.py, and added missing dependencies, it is now clean - use https URL for website in setup.py - removed "Environment :: X11 Applications :: GTK", as wix is deprecated and removed - renamed sat.sh to sat and fixed its installation - added python_requires to specify Python version needed - replaced glib2reactor which use deprecated code by gtk3reactor sat can now be installed directly from virtualenv without using --system-site-packages anymore \o/
author Goffi <goffi@goffi.org>
date Mon, 02 Apr 2018 19:44:50 +0200
parents frontends/src/quick_frontend/quick_games.py@0046283a285d
children 56f94936df1e
comparison
equal deleted inserted replaced
2561:bd30dc3ffe5a 2562:26edcf3a30eb
1 #!/usr/bin/env python2
2 # -*- coding: utf-8 -*-
3
4 # helper class for making a SAT frontend
5 # Copyright (C) 2009-2018 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.tools import games
27 from sat_frontends.quick_frontend.constants import Const as C
28
29 import quick_chat
30
31
32 class RoomGame(object):
33 _game_name = None
34 _signal_prefix = None
35 _signal_suffixes = None
36
37 @classmethod
38 def registerSignals(cls, host):
39
40 def make_handler(suffix, signal):
41 def handler(*args):
42 if suffix in ("Started", "Players"):
43 return cls.startedHandler(host, suffix, *args)
44 return cls.genericHandler(host, signal, *args)
45 return handler
46
47 for suffix in cls._signal_suffixes:
48 signal = cls._signal_prefix + suffix
49 host.registerSignal(signal, handler=make_handler(suffix, signal), iface="plugin")
50
51 @classmethod
52 def startedHandler(cls, host, suffix, *args):
53 room_jid, args, profile = jid.JID(args[0]), args[1:-1], args[-1]
54 referee, players, args = args[0], args[1], args[2:]
55 chat_widget = host.widgets.getOrCreateWidget(quick_chat.QuickChat, room_jid, type_=C.CHAT_GROUP, profile=profile)
56
57 # update symbols
58 if cls._game_name not in chat_widget.visible_states:
59 chat_widget.visible_states.append(cls._game_name)
60 symbols = games.SYMBOLS[cls._game_name]
61 index = 0
62 contact_list = host.contact_lists[profile]
63 for occupant in chat_widget.occupants:
64 occupant_jid = jid.newResource(room_jid, occupant)
65 contact_list.setCache(occupant_jid, cls._game_name, symbols[index % len(symbols)] if occupant in players else None)
66 chat_widget.update(occupant_jid)
67
68 if suffix == "Players" or chat_widget.nick not in players:
69 return # waiting for other players to join, or not playing
70 if cls._game_name in chat_widget.games:
71 return # game panel is already there
72 real_class = host.widgets.getRealClass(cls)
73 if real_class == cls:
74 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)),
75 _(u"{game} Game").format(game=cls._game_name))
76 return
77 panel = real_class(chat_widget, referee, players, *args)
78 chat_widget.games[cls._game_name] = panel
79 chat_widget.addGamePanel(panel)
80
81 @classmethod
82 def genericHandler(cls, host, signal, *args):
83 room_jid, args, profile = jid.JID(args[0]), args[1:-1], args[-1]
84 chat_widget = host.widgets.getWidget(quick_chat.QuickChat, room_jid, profile)
85 if chat_widget:
86 try:
87 game_panel = chat_widget.games[cls._game_name]
88 except KeyError:
89 log.error("TODO: better game synchronisation - received signal %s but no panel is found" % signal)
90 return
91 else:
92 getattr(game_panel, "%sHandler" % signal)(*args)
93
94
95 class Tarot(RoomGame):
96 _game_name = "Tarot"
97 _signal_prefix = "tarotGame"
98 _signal_suffixes = ("Started", "Players", "New", "ChooseContrat",
99 "ShowCards", "YourTurn", "Score", "CardsPlayed",
100 "InvalidCards",
101 )
102
103
104 class Quiz(RoomGame):
105 _game_name = "Quiz"
106 _signal_prefix = "quizGame"
107 _signal_suffixes = ("Started", "New", "Question", "PlayerBuzzed",
108 "PlayerSays", "AnswerResult", "TimerExpired",
109 "TimerRestarted",
110 )
111
112
113 class Radiocol(RoomGame):
114 _game_name = "Radiocol"
115 _signal_prefix = "radiocol"
116 _signal_suffixes = ("Started", "Players", "SongRejected", "Preload",
117 "Play", "NoUpload", "UploadOk")