comparison src/plugins/plugin_misc_room_game.py @ 877:386ab96af575

plugin room_game: fix issues with data structures initialization
author souliane <souliane@mailoo.org>
date Wed, 26 Feb 2014 02:13:29 +0100
parents 3c270d691e56
children 1a759096ccbd
comparison
equal deleted inserted replaced
876:65bf1bc70f6b 877:386ab96af575
84 RoomGame._init_(self, host, ...) 84 RoomGame._init_(self, host, ...)
85 85
86 """ 86 """
87 self.host = host 87 self.host = host
88 88
89 def _init_(self, host, plugin_info, ns_tag, game_init={}, player_init={}): 89 def _init_(self, host, plugin_info, ns_tag, game_init=None, player_init=None):
90 """ 90 """
91 @param host 91 @param host
92 @param plugin_info: PLUGIN_INFO map of the game plugin 92 @param plugin_info: PLUGIN_INFO map of the game plugin
93 @param ns_tag: couple (nameservice, tag) to construct the messages 93 @param ns_tag: couple (nameservice, tag) to construct the messages
94 @param game_init: dictionary for general game initialization 94 @param game_init: dictionary for general game initialization
96 """ 96 """
97 self.host = host 97 self.host = host
98 self.name = plugin_info["import_name"] 98 self.name = plugin_info["import_name"]
99 self.ns_tag = ns_tag 99 self.ns_tag = ns_tag
100 self.request = self.REQUEST % (self.MESSAGE, ns_tag[1], ns_tag[0]) 100 self.request = self.REQUEST % (self.MESSAGE, ns_tag[1], ns_tag[0])
101 if game_init is None:
102 game_init = {}
103 if player_init is None:
104 player_init = {}
101 self.game_init = game_init 105 self.game_init = game_init
102 self.player_init = player_init 106 self.player_init = player_init
103 self.games = {} 107 self.games = {}
104 self.invitations = {} # list of couple (x, y) with x the time and y a list of users 108 self.invitations = {} # list of couple (x, y) with x the time and y a list of users
105 109
255 for recipient in recipients: 259 for recipient in recipients:
256 self._sendElements(recipient, elements, profile=profile) 260 self._sendElements(recipient, elements, profile=profile)
257 for args, kwargs in sync_args: 261 for args, kwargs in sync_args:
258 self._sendElements(*args, **kwargs) 262 self._sendElements(*args, **kwargs)
259 263
260 def _getSyncData(self, room_jid_s, force_nicks=[]): 264 def _getSyncData(self, room_jid_s, force_nicks=None):
261 """The synchronization data are returned for each player who 265 """The synchronization data are returned for each player who
262 has the state 'desync' or if he's been contained by force_nicks. 266 has the state 'desync' or if he's been contained by force_nicks.
263 @param room_jid_s: room userhost 267 @param room_jid_s: room userhost
264 @param force_nicks: force the synchronization for this list of the nicks 268 @param force_nicks: force the synchronization for this list of the nicks
265 @return: a mapping between player nicks and a list of elements to 269 @return: a mapping between player nicks and a list of elements to
268 if not self._gameExists(room_jid_s): 272 if not self._gameExists(room_jid_s):
269 return {} 273 return {}
270 data = {} 274 data = {}
271 status = self.games[room_jid_s]['status'] 275 status = self.games[room_jid_s]['status']
272 nicks = [nick for nick in status if status[nick] == 'desync'] 276 nicks = [nick for nick in status if status[nick] == 'desync']
277 if force_nicks is None:
278 force_nicks = []
273 for nick in force_nicks: 279 for nick in force_nicks:
274 if nick not in nicks: 280 if nick not in nicks:
275 nicks.append(nick) 281 nicks.append(nick)
276 for nick in nicks: 282 for nick in nicks:
277 elements = self.getSyncDataForPlayer(room_jid_s, nick) 283 elements = self.getSyncDataForPlayer(room_jid_s, nick)
383 @return: a unique name for a new room to be created 389 @return: a unique name for a new room to be created
384 """ 390 """
385 room = self.host.plugins["XEP-0045"].getUniqueName(muc_service, profile_key=profile_key) 391 room = self.host.plugins["XEP-0045"].getUniqueName(muc_service, profile_key=profile_key)
386 return "sat_%s_%s" % (self.name.lower(), room) if room != "" else "" 392 return "sat_%s_%s" % (self.name.lower(), room) if room != "" else ""
387 393
388 def prepareRoom(self, other_players=[], room_jid_s=None, profile_key='@NONE@'): 394 def prepareRoom(self, other_players=None, room_jid_s=None, profile_key='@NONE@'):
389 """Prepare the room for a game: create it if it doesn't exist and invite players. 395 """Prepare the room for a game: create it if it doesn't exist and invite players.
390 @param other_players: list for other players JID userhosts 396 @param other_players: list for other players JID userhosts
391 @param room_jid_s: JID userhost of the room, or None to generate a unique name 397 @param room_jid_s: JID userhost of the room, or None to generate a unique name
392 @param profile_key 398 @param profile_key
393 """ 399 """
394 debug(_('Preparing room for %s game') % self.name) 400 debug(_('Preparing room for %s game') % self.name)
395 profile = self.host.memory.getProfileName(profile_key) 401 profile = self.host.memory.getProfileName(profile_key)
396 if not profile: 402 if not profile:
397 error(_("Unknown profile")) 403 error(_("Unknown profile"))
398 return 404 return
405 if other_players is None:
406 other_players = []
399 407
400 def roomJoined(room): 408 def roomJoined(room):
401 """@param room: instance of wokkel.muc.Room""" 409 """@param room: instance of wokkel.muc.Room"""
402 self._createOrInvite(room, [JID(player).userhost() for player in other_players], profile) 410 self._createOrInvite(room, [JID(player).userhost() for player in other_players], profile)
403 411
507 return False, False 515 return False, False
508 else: 516 else:
509 self._initGame(room_jid_s, user_nick) 517 self._initGame(room_jid_s, user_nick)
510 return True, False 518 return True, False
511 519
512 def createGame(self, room_jid_s, nicks=[], profile_key='@NONE@'): 520 def createGame(self, room_jid_s, nicks=None, profile_key='@NONE@'):
513 """Create a new game - this can be called directly from a frontend 521 """Create a new game - this can be called directly from a frontend
514 and skips all the checks and invitation system, but the game must 522 and skips all the checks and invitation system, but the game must
515 not exist and all the players must be in the room already. 523 not exist and all the players must be in the room already.
516 @param room_jid: JID userhost of the room 524 @param room_jid: JID userhost of the room
517 @param nicks: list of players nicks in the room (referee included, in first position) 525 @param nicks: list of players nicks in the room (referee included, in first position)
520 profile = self.host.memory.getProfileName(profile_key) 528 profile = self.host.memory.getProfileName(profile_key)
521 if not profile: 529 if not profile:
522 error(_("profile %s is unknown") % profile_key) 530 error(_("profile %s is unknown") % profile_key)
523 return 531 return
524 (create, sync) = self._checkCreateGameAndInit(room_jid_s, profile) 532 (create, sync) = self._checkCreateGameAndInit(room_jid_s, profile)
533 if nicks is None:
534 nicks = []
525 if not create: 535 if not create:
526 if sync: 536 if sync:
527 self._updatePlayers(room_jid_s, nicks, True, profile) 537 self._updatePlayers(room_jid_s, nicks, True, profile)
528 return 538 return
529 self.games[room_jid_s]['started'] = True 539 self.games[room_jid_s]['started'] = True
571 self.send(to_jid, elem, profile=profile) 581 self.send(to_jid, elem, profile=profile)
572 elif isinstance(msg_elts, domish.Element): 582 elif isinstance(msg_elts, domish.Element):
573 self.send(room_jid, msg_elts, profile=profile) 583 self.send(room_jid, msg_elts, profile=profile)
574 if common_data is not None: 584 if common_data is not None:
575 for player in players: 585 for player in players:
576 players_data[player].update(common_data) 586 players_data[player].update(copy.deepcopy(common_data))
577 587
578 def _createGameElt(self, to_jid): 588 def _createGameElt(self, to_jid):
579 """Create a generic domish Element for the game messages 589 """Create a generic domish Element for the game messages
580 @param to_jid: JID of the recipient 590 @param to_jid: JID of the recipient
581 @return: the created element 591 @return: the created element