comparison sat_frontends/quick_frontend/quick_games.py @ 2624:56f94936df1e

code style reformatting using black
author Goffi <goffi@goffi.org>
date Wed, 27 Jun 2018 20:14:46 +0200
parents 26edcf3a30eb
children 003b8b4b56a7
comparison
equal deleted inserted replaced
2623:49533de4540b 2624:56f94936df1e
16 16
17 # You should have received a copy of the GNU Affero General Public License 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/>. 18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 19
20 from sat.core.log import getLogger 20 from sat.core.log import getLogger
21
21 log = getLogger(__name__) 22 log = getLogger(__name__)
22 23
23 from sat.core.i18n import _ 24 from sat.core.i18n import _
24 25
25 from sat_frontends.tools import jid 26 from sat_frontends.tools import jid
34 _signal_prefix = None 35 _signal_prefix = None
35 _signal_suffixes = None 36 _signal_suffixes = None
36 37
37 @classmethod 38 @classmethod
38 def registerSignals(cls, host): 39 def registerSignals(cls, host):
39
40 def make_handler(suffix, signal): 40 def make_handler(suffix, signal):
41 def handler(*args): 41 def handler(*args):
42 if suffix in ("Started", "Players"): 42 if suffix in ("Started", "Players"):
43 return cls.startedHandler(host, suffix, *args) 43 return cls.startedHandler(host, suffix, *args)
44 return cls.genericHandler(host, signal, *args) 44 return cls.genericHandler(host, signal, *args)
45
45 return handler 46 return handler
46 47
47 for suffix in cls._signal_suffixes: 48 for suffix in cls._signal_suffixes:
48 signal = cls._signal_prefix + suffix 49 signal = cls._signal_prefix + suffix
49 host.registerSignal(signal, handler=make_handler(suffix, signal), iface="plugin") 50 host.registerSignal(
51 signal, handler=make_handler(suffix, signal), iface="plugin"
52 )
50 53
51 @classmethod 54 @classmethod
52 def startedHandler(cls, host, suffix, *args): 55 def startedHandler(cls, host, suffix, *args):
53 room_jid, args, profile = jid.JID(args[0]), args[1:-1], args[-1] 56 room_jid, args, profile = jid.JID(args[0]), args[1:-1], args[-1]
54 referee, players, args = args[0], args[1], args[2:] 57 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) 58 chat_widget = host.widgets.getOrCreateWidget(
59 quick_chat.QuickChat, room_jid, type_=C.CHAT_GROUP, profile=profile
60 )
56 61
57 # update symbols 62 # update symbols
58 if cls._game_name not in chat_widget.visible_states: 63 if cls._game_name not in chat_widget.visible_states:
59 chat_widget.visible_states.append(cls._game_name) 64 chat_widget.visible_states.append(cls._game_name)
60 symbols = games.SYMBOLS[cls._game_name] 65 symbols = games.SYMBOLS[cls._game_name]
61 index = 0 66 index = 0
62 contact_list = host.contact_lists[profile] 67 contact_list = host.contact_lists[profile]
63 for occupant in chat_widget.occupants: 68 for occupant in chat_widget.occupants:
64 occupant_jid = jid.newResource(room_jid, occupant) 69 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) 70 contact_list.setCache(
71 occupant_jid,
72 cls._game_name,
73 symbols[index % len(symbols)] if occupant in players else None,
74 )
66 chat_widget.update(occupant_jid) 75 chat_widget.update(occupant_jid)
67 76
68 if suffix == "Players" or chat_widget.nick not in players: 77 if suffix == "Players" or chat_widget.nick not in players:
69 return # waiting for other players to join, or not playing 78 return # waiting for other players to join, or not playing
70 if cls._game_name in chat_widget.games: 79 if cls._game_name in chat_widget.games:
71 return # game panel is already there 80 return # game panel is already there
72 real_class = host.widgets.getRealClass(cls) 81 real_class = host.widgets.getRealClass(cls)
73 if real_class == cls: 82 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)), 83 host.showDialog(
75 _(u"{game} Game").format(game=cls._game_name)) 84 _(
85 u"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 _(u"{game} Game").format(game=cls._game_name),
88 )
76 return 89 return
77 panel = real_class(chat_widget, referee, players, *args) 90 panel = real_class(chat_widget, referee, players, *args)
78 chat_widget.games[cls._game_name] = panel 91 chat_widget.games[cls._game_name] = panel
79 chat_widget.addGamePanel(panel) 92 chat_widget.addGamePanel(panel)
80 93
84 chat_widget = host.widgets.getWidget(quick_chat.QuickChat, room_jid, profile) 97 chat_widget = host.widgets.getWidget(quick_chat.QuickChat, room_jid, profile)
85 if chat_widget: 98 if chat_widget:
86 try: 99 try:
87 game_panel = chat_widget.games[cls._game_name] 100 game_panel = chat_widget.games[cls._game_name]
88 except KeyError: 101 except KeyError:
89 log.error("TODO: better game synchronisation - received signal %s but no panel is found" % signal) 102 log.error(
103 "TODO: better game synchronisation - received signal %s but no panel is found"
104 % signal
105 )
90 return 106 return
91 else: 107 else:
92 getattr(game_panel, "%sHandler" % signal)(*args) 108 getattr(game_panel, "%sHandler" % signal)(*args)
93 109
94 110
95 class Tarot(RoomGame): 111 class Tarot(RoomGame):
96 _game_name = "Tarot" 112 _game_name = "Tarot"
97 _signal_prefix = "tarotGame" 113 _signal_prefix = "tarotGame"
98 _signal_suffixes = ("Started", "Players", "New", "ChooseContrat", 114 _signal_suffixes = (
99 "ShowCards", "YourTurn", "Score", "CardsPlayed", 115 "Started",
100 "InvalidCards", 116 "Players",
101 ) 117 "New",
118 "ChooseContrat",
119 "ShowCards",
120 "YourTurn",
121 "Score",
122 "CardsPlayed",
123 "InvalidCards",
124 )
102 125
103 126
104 class Quiz(RoomGame): 127 class Quiz(RoomGame):
105 _game_name = "Quiz" 128 _game_name = "Quiz"
106 _signal_prefix = "quizGame" 129 _signal_prefix = "quizGame"
107 _signal_suffixes = ("Started", "New", "Question", "PlayerBuzzed", 130 _signal_suffixes = (
108 "PlayerSays", "AnswerResult", "TimerExpired", 131 "Started",
109 "TimerRestarted", 132 "New",
110 ) 133 "Question",
134 "PlayerBuzzed",
135 "PlayerSays",
136 "AnswerResult",
137 "TimerExpired",
138 "TimerRestarted",
139 )
111 140
112 141
113 class Radiocol(RoomGame): 142 class Radiocol(RoomGame):
114 _game_name = "Radiocol" 143 _game_name = "Radiocol"
115 _signal_prefix = "radiocol" 144 _signal_prefix = "radiocol"
116 _signal_suffixes = ("Started", "Players", "SongRejected", "Preload", 145 _signal_suffixes = (
117 "Play", "NoUpload", "UploadOk") 146 "Started",
147 "Players",
148 "SongRejected",
149 "Preload",
150 "Play",
151 "NoUpload",
152 "UploadOk",
153 )