annotate frontends/src/quick_frontend/quick_games.py @ 1443:32d1089df687

add PyOpenSSL to the dependency list
author souliane <souliane@mailoo.org>
date Tue, 21 Jul 2015 11:56:53 +0200
parents 069ad98b360d
children d17772b0fe22
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1360
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
1 #!/usr/bin/python
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
3
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
4 # helper class for making a SAT frontend
1396
069ad98b360d 2015 copyright dates update
Goffi <goffi@goffi.org>
parents: 1388
diff changeset
5 # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014, 2015 Jérôme Poisson (goffi@goffi.org)
1360
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
6
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
10 # (at your option) any later version.
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
11
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
16
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
19
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
20 from sat.core.log import getLogger
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
21 log = getLogger(__name__)
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
22
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
23 from sat.core.i18n import _
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
24
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
25 from sat_frontends.tools import jid
1379
da2ea16fabc6 quick_frontend: display MUC games symbols
souliane <souliane@mailoo.org>
parents: 1360
diff changeset
26 from sat_frontends.tools import games
1360
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
27 from sat_frontends.quick_frontend.constants import Const as C
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
28
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
29 import quick_chat
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
30
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
31
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
32 class RoomGame(object):
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
33 _game_name = None
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
34 _signal_prefix = None
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
35 _signal_suffixes = None
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
36
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
37 @classmethod
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
38 def registerSignals(cls, host):
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
39
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
40 def make_handler(suffix, signal):
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
41 def handler(*args):
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
42 if suffix in ("Started", "Players"):
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
43 return cls.startedHandler(host, suffix, *args)
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
44 return cls.genericHandler(host, signal, *args)
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
45 return handler
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
46
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
47 for suffix in cls._signal_suffixes:
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
48 signal = cls._signal_prefix + suffix
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
49 host.registerSignal(signal, handler=make_handler(suffix, signal), iface="plugin")
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
50
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
51 @classmethod
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
52 def startedHandler(cls, host, suffix, *args):
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
53 room_jid, args, profile = jid.JID(args[0]), args[1:-1], args[-1]
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
54 referee, players, args = args[0], args[1], args[2:]
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
55 chat_widget = host.widgets.getOrCreateWidget(quick_chat.QuickChat, room_jid, type_=C.CHAT_GROUP, profile=profile)
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
56
1379
da2ea16fabc6 quick_frontend: display MUC games symbols
souliane <souliane@mailoo.org>
parents: 1360
diff changeset
57 # update symbols
da2ea16fabc6 quick_frontend: display MUC games symbols
souliane <souliane@mailoo.org>
parents: 1360
diff changeset
58 if cls._game_name not in chat_widget.visible_states:
da2ea16fabc6 quick_frontend: display MUC games symbols
souliane <souliane@mailoo.org>
parents: 1360
diff changeset
59 chat_widget.visible_states.append(cls._game_name)
da2ea16fabc6 quick_frontend: display MUC games symbols
souliane <souliane@mailoo.org>
parents: 1360
diff changeset
60 symbols = games.SYMBOLS[cls._game_name]
da2ea16fabc6 quick_frontend: display MUC games symbols
souliane <souliane@mailoo.org>
parents: 1360
diff changeset
61 index = 0
1388
a025242bebe7 quick_frontend, primitivus: remove QuickChat.updateEntityState and QuickChat.setContactStates, use more general QuickChat.update (also called when joining the room to initialise the occupants list)
souliane <souliane@mailoo.org>
parents: 1379
diff changeset
62 contact_list = host.contact_lists[profile]
1379
da2ea16fabc6 quick_frontend: display MUC games symbols
souliane <souliane@mailoo.org>
parents: 1360
diff changeset
63 for occupant in chat_widget.occupants:
da2ea16fabc6 quick_frontend: display MUC games symbols
souliane <souliane@mailoo.org>
parents: 1360
diff changeset
64 occupant_jid = jid.newResource(room_jid, occupant)
1388
a025242bebe7 quick_frontend, primitivus: remove QuickChat.updateEntityState and QuickChat.setContactStates, use more general QuickChat.update (also called when joining the room to initialise the occupants list)
souliane <souliane@mailoo.org>
parents: 1379
diff changeset
65 contact_list.setCache(occupant_jid, cls._game_name, symbols[index % len(symbols)] if occupant in players else None)
a025242bebe7 quick_frontend, primitivus: remove QuickChat.updateEntityState and QuickChat.setContactStates, use more general QuickChat.update (also called when joining the room to initialise the occupants list)
souliane <souliane@mailoo.org>
parents: 1379
diff changeset
66 chat_widget.update(occupant_jid)
1379
da2ea16fabc6 quick_frontend: display MUC games symbols
souliane <souliane@mailoo.org>
parents: 1360
diff changeset
67
1360
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
68 if suffix == "Players" or chat_widget.nick not in players:
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
69 return # waiting for other players to join, or not playing
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
70 if cls._game_name in chat_widget.games:
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
71 return # game panel is already there
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
72 real_class = host.widgets.getRealClass(cls)
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
73 if real_class == cls:
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
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)),
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
75 _(u"{game} Game").format(game=cls._game_name))
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
76 return
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
77 panel = real_class(chat_widget, referee, players, *args)
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
78 chat_widget.games[cls._game_name] = panel
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
79 chat_widget.addGamePanel(panel)
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
80
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
81 @classmethod
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
82 def genericHandler(cls, host, signal, *args):
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
83 room_jid, args, profile = jid.JID(args[0]), args[1:-1], args[-1]
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
84 chat_widget = host.widgets.getWidget(quick_chat.QuickChat, room_jid, profile)
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
85 if chat_widget:
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
86 try:
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
87 game_panel = chat_widget.games[cls._game_name]
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
88 except KeyError:
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
89 log.error("TODO: better game synchronisation - received signal %s but no panel is found" % signal)
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
90 return
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
91 else:
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
92 getattr(game_panel, "%sHandler" % signal)(*args)
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
93
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
94
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
95 class Tarot(RoomGame):
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
96 _game_name = "Tarot"
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
97 _signal_prefix = "tarotGame"
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
98 _signal_suffixes = ("Started", "Players", "New", "ChooseContrat",
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
99 "ShowCards", "YourTurn", "Score", "CardsPlayed",
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
100 "InvalidCards",
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
101 )
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
102
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
103
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
104 class Quiz(RoomGame):
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
105 _game_name = "Quiz"
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
106 _signal_prefix = "quizGame"
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
107 _signal_suffixes = ("Started", "New", "Question", "PlayerBuzzed",
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
108 "PlayerSays", "AnswerResult", "TimerExpired",
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
109 "TimerRestarted",
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
110 )
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
111
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
112
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
113 class Radiocol(RoomGame):
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
114 _game_name = "Radiocol"
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
115 _signal_prefix = "radiocol"
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
116 _signal_suffixes = ("Started", "Players", "SongRejected", "Preload",
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents:
diff changeset
117 "Play", "NoUpload", "UploadOk")