Mercurial > libervia-backend
comparison src/tools/plugins/games.py @ 714:ecc5a5b34ee1
plugins (games): add a method to send messages more easily
author | souliane <souliane@mailoo.org> |
---|---|
date | Mon, 18 Nov 2013 14:25:40 +0100 |
parents | f610864eb7a5 |
children | 358018c5c398 |
comparison
equal
deleted
inserted
replaced
713:8bd63daecdbf | 714:ecc5a5b34ee1 |
---|---|
103 _room_jid = room.occupantJID.userhostJID() | 103 _room_jid = room.occupantJID.userhostJID() |
104 if self.collectiveGame is True: | 104 if self.collectiveGame is True: |
105 room_s = _room_jid.userhost() | 105 room_s = _room_jid.userhost() |
106 if room_s in self.games and self.games[room_s]["referee"] == room.occupantJID.full(): | 106 if room_s in self.games and self.games[room_s]["referee"] == room.occupantJID.full(): |
107 #we are in a radiocol room, let's start the party ! | 107 #we are in a radiocol room, let's start the party ! |
108 mess = self.createGameElt(JID(room_s + '/' + user.nick)) | 108 self.send(JID(room_s + '/' + user.nick), self.createStartElement(), profile=profile) |
109 mess.firstChildElement().addChild(self.__create_started_elt()) | |
110 self.host.profiles[profile].xmlstream.send(mess) | |
111 return True | 109 return True |
112 if _room_jid in self.waiting_inv and len(room.roster) >= len(self.waiting_inv[_room_jid][1]): | 110 if _room_jid in self.waiting_inv and len(room.roster) >= len(self.waiting_inv[_room_jid][1]): |
113 expected_players = self.waiting_inv[_room_jid][1] | 111 expected_players = self.waiting_inv[_room_jid][1] |
114 players = [] | 112 players = [] |
115 for player in expected_players: | 113 for player in expected_players: |
151 warning(_("%s game already started in room %s") % (self.name, room)) | 149 warning(_("%s game already started in room %s") % (self.name, room)) |
152 return | 150 return |
153 self.games[room] = {'referee': referee} | 151 self.games[room] = {'referee': referee} |
154 self.games[room].update(self.options) | 152 self.games[room].update(self.options) |
155 if self.collectiveGame is True: | 153 if self.collectiveGame is True: |
156 mess = self.createGameElt(JID(room)) | 154 self.send(JID(room), self.createStartElement(), profile=profile) |
157 mess.firstChildElement().addChild(self.__create_started_elt()) | |
158 self.host.profiles[profile].xmlstream.send(mess) | |
159 return | 155 return |
160 # non collaborative game = individual data and messages | 156 # non collaborative game = individual data and messages |
161 status = {} | 157 status = {} |
162 players_data = {} | 158 players_data = {} |
163 for player in players: | 159 for player in players: |
164 # The dict must be COPIED otherwise it is shared between all users | 160 # The dict must be COPIED otherwise it is shared between all users |
165 players_data[player] = self.player_init_data.copy() | 161 players_data[player] = self.player_init_data.copy() |
166 status[player] = "init" | 162 status[player] = "init" |
167 # each player send a message to all the others | 163 # each player send a message to all the others |
168 mess = self.createGameElt(JID(room + '/' + player)) | 164 self.send(JID(room + '/' + player), self.createStartElement(players), profile=profile) |
169 mess.firstChildElement().addChild(self.__create_started_elt(players)) | |
170 self.host.profiles[profile].xmlstream.send(mess) | |
171 # specific data to each player | 165 # specific data to each player |
172 self.games[room].update({'players': players, 'status': status, 'players_data': players_data}) | 166 self.games[room].update({'players': players, 'status': status, 'players_data': players_data}) |
173 | 167 |
174 def createCollectiveGame(self, room_jid, profile_key='@NONE@'): | 168 def createCollectiveGame(self, room_jid, profile_key='@NONE@'): |
175 return self.createGame(self, room_jid, players=[], profile_key=profile_key) | 169 return self.createGame(self, room_jid, players=[], profile_key=profile_key) |
179 profile = self.host.memory.getProfileName(profile_key) | 173 profile = self.host.memory.getProfileName(profile_key) |
180 if not profile: | 174 if not profile: |
181 error(_("profile %s is unknown") % profile_key) | 175 error(_("profile %s is unknown") % profile_key) |
182 return | 176 return |
183 debug('new player ready: %s' % profile) | 177 debug('new player ready: %s' % profile) |
184 mess = self.createGameElt(JID(referee)) | 178 self.send(JID(referee), 'player_ready', {'player': player}, profile=profile) |
185 ready_elt = mess.firstChildElement().addElement('player_ready') | |
186 ready_elt['player'] = player | |
187 self.host.profiles[profile].xmlstream.send(mess) | |
188 | 179 |
189 def newRound(self, room_jid, data, profile): | 180 def newRound(self, room_jid, data, profile): |
190 """Launch a new round (reinit the user data)""" | 181 """Launch a new round (reinit the user data)""" |
191 debug(_('new round for %s game') % self.name) | 182 debug(_('new round for %s game') % self.name) |
192 game_data = self.games[room_jid.userhost()] | 183 game_data = self.games[room_jid.userhost()] |
197 common_data, msg_elts = data if data is not None else (None, None) | 188 common_data, msg_elts = data if data is not None else (None, None) |
198 | 189 |
199 if isinstance(msg_elts, dict): | 190 if isinstance(msg_elts, dict): |
200 for player in players: | 191 for player in players: |
201 to_jid = JID(room_jid.userhost() + "/" + player) # FIXME: gof: | 192 to_jid = JID(room_jid.userhost() + "/" + player) # FIXME: gof: |
202 mess = self.createGameElt(to_jid) | 193 elem = msg_elts[player] if isinstance(msg_elts[player], domish.Element) else None |
203 if isinstance(msg_elts[player], domish.Element): | 194 self.send(to_jid, elem, profile=profile) |
204 mess.firstChildElement().addChild(msg_elts[player]) | |
205 self.host.profiles[profile].xmlstream.send(mess) | |
206 elif isinstance(msg_elts, domish.Element): | 195 elif isinstance(msg_elts, domish.Element): |
207 mess = self.createGameElt(room_jid) | 196 self.send(room_jid, msg_elts, profile=profile) |
208 mess.firstChildElement().addChild(msg_elts) | |
209 self.host.profiles[profile].xmlstream.send(mess) | |
210 if common_data is not None: | 197 if common_data is not None: |
211 for player in players: | 198 for player in players: |
212 players_data[player].update(common_data) | 199 players_data[player].update(common_data) |
213 | 200 |
214 def createGameElt(self, to_jid, type_="normal"): | 201 def createGameElt(self, to_jid, type_="normal"): |
218 elt["to"] = to_jid.full() | 205 elt["to"] = to_jid.full() |
219 elt["type"] = type_ | 206 elt["type"] = type_ |
220 elt.addElement(self.ns_tag) | 207 elt.addElement(self.ns_tag) |
221 return elt | 208 return elt |
222 | 209 |
223 def __create_started_elt(self, players=None): | 210 def createStartElement(self, players=None): |
224 """Create a game "started" domish Element""" | 211 """Create a game "started" domish Element""" |
225 started_elt = domish.Element((None, 'started')) | 212 started_elt = domish.Element((None, 'started')) |
226 if players is None: | 213 if players is None: |
227 return started_elt | 214 return started_elt |
228 idx = 0 | 215 idx = 0 |
231 player_elt.addContent(player) | 218 player_elt.addContent(player) |
232 player_elt['index'] = str(idx) | 219 player_elt['index'] = str(idx) |
233 idx += 1 | 220 idx += 1 |
234 started_elt.addChild(player_elt) | 221 started_elt.addChild(player_elt) |
235 return started_elt | 222 return started_elt |
223 | |
224 def send(self, to_jid, elem=None, attrs=None, content=None, profile=None): | |
225 """ | |
226 @param to_jid: recipient JID | |
227 @param elem: domish.Element, unicode or a couple: | |
228 - domish.Element to be directly added as a child to the message | |
229 - unicode name or couple (uri, name) to create a new domish.Element | |
230 and add it as a child to the message (see domish.Element.addElement) | |
231 @param attrs: dictionary of attributes for the new child | |
232 @param content: unicode that is appended to the child content | |
233 @param profile: the profile from which the message is sent | |
234 """ | |
235 if profile is None: | |
236 error(_("Message can not be sent without a sender profile")) | |
237 return | |
238 msg = self.createGameElt(to_jid) | |
239 if elem is not None: | |
240 if isinstance(elem, domish.Element): | |
241 msg.firstChildElement().addChild(elem) | |
242 else: | |
243 elem = msg.firstChildElement().addElement(elem) | |
244 if attrs is not None: | |
245 elem.attributes.update(attrs) | |
246 if content is not None: | |
247 elem.addContent(content) | |
248 self.host.profiles[profile].xmlstream.send(msg) |