Mercurial > libervia-web
annotate libervia.py @ 37:b306aa090438
Tarot game: game launching (first hand showed), and contract selection
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 18 May 2011 01:45:28 +0200 |
parents | 1d406077b49b |
children | 7bea2ae0c4fb |
rev | line source |
---|---|
0 | 1 #!/usr/bin/python |
2 # -*- coding: utf-8 -*- | |
3 | |
4 """ | |
5 Libervia: a Salut à Toi frontend | |
6 Copyright (C) 2011 Jérôme Poisson (goffi@goffi.org) | |
7 | |
8 This program is free software: you can redistribute it and/or modify | |
9 it under the terms of the GNU Affero General Public License as published by | |
10 the Free Software Foundation, either version 3 of the License, or | |
11 (at your option) any later version. | |
12 | |
13 This program is distributed in the hope that it will be useful, | |
14 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 GNU Affero General Public License for more details. | |
17 | |
18 You should have received a copy of the GNU Affero General Public License | |
19 along with this program. If not, see <http://www.gnu.org/licenses/>. | |
20 """ | |
21 | |
22 import pyjd # this is dummy in pyjs | |
23 from pyjamas.ui.RootPanel import RootPanel | |
21
77c2e48efa29
browser side: a warning message now show who will receive the message entered in UniBox, with a color depending on how many people will be able to see it
Goffi <goffi@goffi.org>
parents:
20
diff
changeset
|
24 from pyjamas.ui.HTML import HTML |
0 | 25 from pyjamas import Window |
26 from pyjamas.JSONService import JSONProxy | |
17
c725b702e927
register.py and contact.py moved to new directory browser_side
Goffi <goffi@goffi.org>
parents:
16
diff
changeset
|
27 from browser_side.register import RegisterPanel, RegisterBox |
c725b702e927
register.py and contact.py moved to new directory browser_side
Goffi <goffi@goffi.org>
parents:
16
diff
changeset
|
28 from browser_side.contact import ContactPanel |
19 | 29 from browser_side.panels import MainPanel, EmptyPanel, MicroblogPanel, ChatPanel, StatusPanel |
30 from browser_side.jid import JID | |
0 | 31 |
32 | |
33 class LiberviaJsonProxy(JSONProxy): | |
34 def __init__(self, *args, **kwargs): | |
35 JSONProxy.__init__(self, *args, **kwargs) | |
36 self.handler=self | |
37 self.cb={} | |
38 | |
39 def call(self, method, cb, *args): | |
33 | 40 id = self.callMethod(method,args) |
11
331c093e4eb3
magicBox is now able to send global microblog
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
41 if cb: |
33 | 42 self.cb[id] = cb |
0 | 43 |
44 def onRemoteResponse(self, response, request_info): | |
33 | 45 if self.cb.has_key(request_info.id): |
46 self.cb[request_info.id](response) | |
47 del self.cb[request_info.id] | |
0 | 48 |
49 def onRemoteError(self, code, errobj, request_info): | |
50 if code != 0: | |
51 Window.alert("Internal server error") | |
52 else: | |
53 if isinstance(errobj['message'],dict): | |
54 Window.alert("Error %s: %s" % (errobj['message']['faultCode'], errobj['message']['faultString'])) | |
55 else: | |
56 Window.alert("Error: %s" % errobj['message']) | |
57 | |
58 class RegisterCall(LiberviaJsonProxy): | |
59 def __init__(self): | |
60 LiberviaJsonProxy.__init__(self, "/register_api", | |
61 ["isRegistered","isConnected","connect"]) | |
62 self.handler=self | |
63 self.cb={} | |
64 | |
2
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
65 class BridgeCall(LiberviaJsonProxy): |
0 | 66 def __init__(self): |
67 LiberviaJsonProxy.__init__(self, "/json_api", | |
33 | 68 ["getContacts", "sendMessage", "sendMblog", "getMblogNodes", "getProfileJid", "getHistory", |
37
b306aa090438
Tarot game: game launching (first hand showed), and contract selection
Goffi <goffi@goffi.org>
parents:
36
diff
changeset
|
69 "getPresenceStatus", "getRoomJoined", "launchTarotGame", "getTarotCardsPaths", "tarotGameReady", "tarotGameContratChoosed"]) |
0 | 70 |
2
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
71 class BridgeSignals(LiberviaJsonProxy): |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
72 def __init__(self): |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
73 LiberviaJsonProxy.__init__(self, "/json_signal_api", |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
74 ["getSignals"]) |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
75 |
0 | 76 class SatWebFrontend: |
77 def onModuleLoad(self): | |
19 | 78 self.whoami = None |
11
331c093e4eb3
magicBox is now able to send global microblog
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
79 self.bridge = BridgeCall() |
331c093e4eb3
magicBox is now able to send global microblog
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
80 self.bridge_signals = BridgeSignals() |
19 | 81 self.selected = None |
33 | 82 self.uni_box = None |
28 | 83 self.status_panel = StatusPanel(self) |
84 self.contact_panel = ContactPanel(self) | |
1 | 85 self.panel = MainPanel(self) |
23 | 86 self.discuss_panel = self.panel.discuss_panel |
87 self.tab_panel = self.panel.tab_panel | |
16
099c05a0dcab
browser side: microblog panel improvments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
88 self.mpanels = [EmptyPanel(self), MicroblogPanel(self, accept_all=True), EmptyPanel(self)] |
33 | 89 self.other_panels = [] #panels not on the main tab #FIXME: temporary, need to be changed |
90 self.room_list = set() #set of rooms | |
23 | 91 self.discuss_panel.changePanel(0,self.mpanels[0]) |
92 self.discuss_panel.changePanel(1,self.mpanels[1]) | |
93 self.discuss_panel.changePanel(2,self.mpanels[2]) | |
0 | 94 self._dialog = None |
95 RootPanel().add(self.panel) | |
33 | 96 self.resize() |
0 | 97 self._register = RegisterCall() |
1 | 98 self._register.call('isRegistered',self._isRegisteredCB) |
0 | 99 |
33 | 100 def resize(self): |
101 """Resize elements""" | |
102 Window.onResize() | |
103 | |
104 def setUniBox(self, unibox): | |
105 """register the unibox widget""" | |
106 self.uni_box = unibox | |
107 self.uni_box.addKey("@@: ") | |
108 | |
19 | 109 def select(self, widget): |
110 """Define the selected widget""" | |
111 if self.selected: | |
112 self.selected.removeStyleName('selected_widget') | |
113 self.selected = widget | |
114 if widget: | |
115 self.selected.addStyleName('selected_widget') | |
116 | |
33 | 117 def addTab(self, panel, label): |
118 """Add a panel in a tab | |
119 @param panel: panel to add | |
120 @param label: label of the tab""" | |
121 self.tab_panel.add(panel, label) | |
122 self.other_panels.append(panel) | |
123 | |
1 | 124 def _isRegisteredCB(self, registered): |
0 | 125 if not registered: |
126 self._dialog = RegisterBox(self.logged,centered=True) | |
127 self._dialog.show() | |
128 else: | |
1 | 129 self._register.call('isConnected', self._isConnectedCB) |
0 | 130 |
1 | 131 def _isConnectedCB(self, connected): |
0 | 132 if not connected: |
133 self._register.call('connect',self.logged) | |
134 else: | |
135 self.logged() | |
136 | |
137 def logged(self): | |
138 if self._dialog: | |
139 self._dialog.hide() | |
140 del self._dialog # don't work if self._dialog is None | |
1 | 141 |
142 #it's time to fill the page | |
11
331c093e4eb3
magicBox is now able to send global microblog
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
143 self.bridge.call('getContacts', self._getContactsCB) |
331c093e4eb3
magicBox is now able to send global microblog
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
144 self.bridge_signals.call('getSignals', self._getSignalsCB) |
19 | 145 #We want to know our own jid |
146 self.bridge.call('getProfileJid', self._getProfileJidCB) | |
2
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
147 |
1 | 148 def _getContactsCB(self, contacts_data): |
149 for contact in contacts_data: | |
150 jid, attributes, groups = contact | |
28 | 151 self.contact_panel.addContact(jid, attributes, groups) |
0 | 152 |
2
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
153 def _getSignalsCB(self, signal_data): |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
154 bridge_signals = BridgeSignals() |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
155 bridge_signals.call('getSignals', self._getSignalsCB) |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
156 print('Got signal ==> name: %s, params: %s' % (signal_data[0],signal_data[1])) |
4
7325e787c22b
personalEvent management signal first draft, microblogs are now put in a central grid
Goffi <goffi@goffi.org>
parents:
2
diff
changeset
|
157 name,args = signal_data |
7325e787c22b
personalEvent management signal first draft, microblogs are now put in a central grid
Goffi <goffi@goffi.org>
parents:
2
diff
changeset
|
158 if name == 'personalEvent': |
7325e787c22b
personalEvent management signal first draft, microblogs are now put in a central grid
Goffi <goffi@goffi.org>
parents:
2
diff
changeset
|
159 self._personalEventCb(*args) |
19 | 160 elif name == 'newMessage': |
161 self._newMessageCb(*args) | |
20 | 162 elif name == 'presenceUpdate': |
163 self._presenceUpdateCb(*args) | |
28 | 164 elif name == 'roomJoined': |
165 self._roomJoinedCb(*args) | |
33 | 166 elif name == 'roomUserJoined': |
167 self._roomUserJoinedCb(*args) | |
168 elif name == 'roomUserLeft': | |
169 self._roomUserLeftCb(*args) | |
36 | 170 elif name == 'tarotGameStarted': |
171 self._tarotGameStartedCb(*args) | |
37
b306aa090438
Tarot game: game launching (first hand showed), and contract selection
Goffi <goffi@goffi.org>
parents:
36
diff
changeset
|
172 elif name == 'tarotGameNew' or \ |
b306aa090438
Tarot game: game launching (first hand showed), and contract selection
Goffi <goffi@goffi.org>
parents:
36
diff
changeset
|
173 name == 'tarotGameChooseContrat': |
b306aa090438
Tarot game: game launching (first hand showed), and contract selection
Goffi <goffi@goffi.org>
parents:
36
diff
changeset
|
174 self._tarotGameGenericCb(name, args[0], args[1:]) |
19 | 175 |
176 def _getProfileJidCB(self, jid): | |
177 self.whoami = JID(jid) | |
20 | 178 #we can now ask our status |
179 self.bridge.call('getPresenceStatus', self._getPresenceStatusCB) | |
33 | 180 #and the rooms where we are |
181 self.bridge.call('getRoomJoined', self._getRoomJoinedCB) | |
20 | 182 |
4
7325e787c22b
personalEvent management signal first draft, microblogs are now put in a central grid
Goffi <goffi@goffi.org>
parents:
2
diff
changeset
|
183 ## Signals callbacks ## |
7325e787c22b
personalEvent management signal first draft, microblogs are now put in a central grid
Goffi <goffi@goffi.org>
parents:
2
diff
changeset
|
184 |
7325e787c22b
personalEvent management signal first draft, microblogs are now put in a central grid
Goffi <goffi@goffi.org>
parents:
2
diff
changeset
|
185 def _personalEventCb(self, sender, event_type, data, profile): |
7325e787c22b
personalEvent management signal first draft, microblogs are now put in a central grid
Goffi <goffi@goffi.org>
parents:
2
diff
changeset
|
186 if event_type == "MICROBLOG": |
7325e787c22b
personalEvent management signal first draft, microblogs are now put in a central grid
Goffi <goffi@goffi.org>
parents:
2
diff
changeset
|
187 if not data.has_key('content'): |
7325e787c22b
personalEvent management signal first draft, microblogs are now put in a central grid
Goffi <goffi@goffi.org>
parents:
2
diff
changeset
|
188 print ("WARNING: No content found in microblog data") |
7325e787c22b
personalEvent management signal first draft, microblogs are now put in a central grid
Goffi <goffi@goffi.org>
parents:
2
diff
changeset
|
189 return |
16
099c05a0dcab
browser side: microblog panel improvments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
190 if data.has_key('groups'): |
099c05a0dcab
browser side: microblog panel improvments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
191 _groups = set(data['groups'].split() if data['groups'] else []) |
099c05a0dcab
browser side: microblog panel improvments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
192 else: |
099c05a0dcab
browser side: microblog panel improvments
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
193 _groups=None |
4
7325e787c22b
personalEvent management signal first draft, microblogs are now put in a central grid
Goffi <goffi@goffi.org>
parents:
2
diff
changeset
|
194 for panel in self.mpanels: |
36 | 195 if isinstance(panel,MicroblogPanel) and (panel.isJidAccepted(sender) or _groups == None or _groups.intersection(panel.accepted_groups)): #TODO: check this |
4
7325e787c22b
personalEvent management signal first draft, microblogs are now put in a central grid
Goffi <goffi@goffi.org>
parents:
2
diff
changeset
|
196 content = data['content'] |
7325e787c22b
personalEvent management signal first draft, microblogs are now put in a central grid
Goffi <goffi@goffi.org>
parents:
2
diff
changeset
|
197 author = data.get('author') |
9 | 198 timestamp = float(data.get('timestamp',0)) #XXX: int doesn't work here |
4
7325e787c22b
personalEvent management signal first draft, microblogs are now put in a central grid
Goffi <goffi@goffi.org>
parents:
2
diff
changeset
|
199 panel.addEntry(content, author, timestamp) |
0 | 200 |
19 | 201 def _newMessageCb(self, from_jid, msg, msg_type, to_jid): |
202 _from = JID(from_jid) | |
203 _to = JID(to_jid) | |
33 | 204 for panel in self.mpanels + self.other_panels: |
19 | 205 if isinstance(panel,ChatPanel) and (panel.target.bare == _from.bare or panel.target.bare == _to.bare): |
206 panel.printMessage(_from, msg) | |
207 | |
20 | 208 def _presenceUpdateCb(self, entity, show, priority, statuses): |
209 _entity = JID(entity) | |
28 | 210 #XXX: QnD way to get our status |
20 | 211 if self.whoami and self.whoami.bare == _entity.bare and statuses: |
28 | 212 self.status_panel.changeStatus(statuses.values()[0]) |
33 | 213 if (not self.whoami or self.whoami.bare != _entity.bare): |
28 | 214 self.contact_panel.setConnected(_entity.bare, _entity.resource, show, priority, statuses) |
215 | |
33 | 216 def _roomJoinedCb(self, room_id, room_service, room_nicks, user_nick): |
28 | 217 _target = JID("%s@%s" % (room_id,room_service)) |
33 | 218 self.room_list.add(_target) |
219 chat_panel = ChatPanel(self, _target, type='group') | |
220 chat_panel.setUserNick(user_nick) | |
28 | 221 if room_id.startswith('sat_tarot_'): #XXX: it's not really beautiful, but it works :) |
33 | 222 self.addTab(chat_panel, "Tarot") |
28 | 223 else: |
33 | 224 self.addTab(chat_panel, str(_target)) |
225 chat_panel.setPresents(room_nicks) | |
226 chat_panel.historyPrint() | |
227 | |
35 | 228 def _roomUserJoinedCb(self, room_id, room_service, user_nick, user_data): |
229 for panel in self.mpanels + self.other_panels: | |
230 if isinstance(panel,ChatPanel) and panel.type == 'group' and panel.target.bare == "%s@%s" % (room_id, room_service): | |
231 panel.userJoined(user_nick, user_data) | |
33 | 232 |
35 | 233 def _roomUserLeftCb(self, room_id, room_service, user_nick, user_data): |
234 for panel in self.mpanels + self.other_panels: | |
235 if isinstance(panel,ChatPanel) and panel.type == 'group' and panel.target.bare == "%s@%s" % (room_id, room_service): | |
236 panel.userLeft(user_nick, user_data) | |
36 | 237 |
238 def _tarotGameStartedCb(self, room_jid, referee, players): | |
239 print ("Tarot Game Started \o/") | |
240 for panel in self.mpanels + self.other_panels: | |
241 if isinstance(panel,ChatPanel) and panel.type == 'group' and panel.target.bare == room_jid: | |
242 panel.startGame("Tarot", referee, players) | |
243 | |
37
b306aa090438
Tarot game: game launching (first hand showed), and contract selection
Goffi <goffi@goffi.org>
parents:
36
diff
changeset
|
244 def _tarotGameGenericCb(self, event_name, room_jid, args): |
b306aa090438
Tarot game: game launching (first hand showed), and contract selection
Goffi <goffi@goffi.org>
parents:
36
diff
changeset
|
245 for panel in self.mpanels + self.other_panels: |
b306aa090438
Tarot game: game launching (first hand showed), and contract selection
Goffi <goffi@goffi.org>
parents:
36
diff
changeset
|
246 if isinstance(panel,ChatPanel) and panel.type == 'group' and panel.target.bare == room_jid: |
b306aa090438
Tarot game: game launching (first hand showed), and contract selection
Goffi <goffi@goffi.org>
parents:
36
diff
changeset
|
247 getattr(panel.getGame("Tarot"), event_name)(*args) |
b306aa090438
Tarot game: game launching (first hand showed), and contract selection
Goffi <goffi@goffi.org>
parents:
36
diff
changeset
|
248 |
20 | 249 def _getPresenceStatusCB(self, presence_data): |
28 | 250 for entity in presence_data: |
251 for resource in presence_data[entity]: | |
252 args = presence_data[entity][resource] | |
253 self._presenceUpdateCb("%s/%s" % (entity, resource), *args) | |
20 | 254 |
33 | 255 def _getRoomJoinedCB(self, room_data): |
256 for room in room_data: | |
257 self._roomJoinedCb(*room) | |
258 | |
0 | 259 if __name__ == '__main__': |
1 | 260 pyjd.setup("http://localhost:8080/libervia.html") |
0 | 261 app = SatWebFrontend() |
262 app.onModuleLoad() | |
263 pyjd.run() | |
264 |