diff src/plugins/plugin_misc_room_game.py @ 746:539f278bc265

plugin room_games, radiocol: send the current queue to new players
author souliane <souliane@mailoo.org>
date Thu, 28 Nov 2013 19:23:59 +0100
parents 074970227bc0
children bfabeedbf32e
line wrap: on
line diff
--- a/src/plugins/plugin_misc_room_game.py	Tue Dec 10 09:02:20 2013 +0100
+++ b/src/plugins/plugin_misc_room_game.py	Thu Nov 28 19:23:59 2013 +0100
@@ -23,6 +23,7 @@
 from time import time
 from wokkel import disco, iwokkel
 from zope.interface import implements
+import copy
 try:
     from twisted.words.protocols.xmlstream import XMPPHandler
 except ImportError:
@@ -30,7 +31,6 @@
 
 # Don't forget to set it to False before you commit
 _DEBUG = False
-_DEBUG_FILE = False
 
 PLUGIN_INFO = {
     "name": "Room game",
@@ -123,7 +123,7 @@
         """
         referee = room_jid_s + '/' + referee_nick
         self.games[room_jid_s] = {'referee': referee, 'players': [], 'started': False}
-        self.games[room_jid_s].update(self.game_init)
+        self.games[room_jid_s].update(copy.deepcopy(self.game_init))
 
     def gameExists(self, room_jid_s, started=False):
         """Return True if a game has been initialized/started.
@@ -166,7 +166,7 @@
         return auth
 
     def updatePlayers(self, room_jid_s, nicks, profile):
-        """Signal to the room or to each player that some players joined the game"""
+        """Signal to the room that some players joined the game"""
         if nicks == []:
             return
         new_nicks = set(nicks).difference(self.games[room_jid_s]['players'])
@@ -181,8 +181,19 @@
             element = self.createStartElement(self.games[room_jid_s]['players'])
         else:
             element = self.createStartElement(self.games[room_jid_s]['players'], name="players")
+        elements = [(element, None, None)]
+        for child in self.getSyncData(room_jid_s):
+            # TODO: sync data may be different and private to each player,
+            # in that case send a separate message to the new players
+            elements.append((child, None, None))
         for recipient in recipients:
-            self.send(recipient, element, profile=profile)
+            self.sendElements(recipient, elements, profile=profile)
+
+    def getSyncData(self, room_jid_s):
+        """This method may be overwritten by any child class.
+        @return: a list of child elements to be added for the game to be synchronized.
+        """
+        return []
 
     def invitePlayers(self, room, other_players, nick, profile):
         """Invite players to a room, associated game may exist or not.
@@ -408,7 +419,7 @@
         players_data = {}
         for nick in nicks:
             # The dict must be COPIED otherwise it is shared between all users
-            players_data[nick] = self.player_init.copy()
+            players_data[nick] = copy.deepcopy(self.player_init)
             status[nick] = "init"
         self.games[room_jid_s].update({'status': status, 'players_data': players_data})
 
@@ -429,7 +440,7 @@
         players_data = game_data['players_data']
         game_data['stage'] = "init"
 
-        common_data, msg_elts = data if data is not None else (None, None)
+        common_data, msg_elts = copy.deepcopy(data) if data is not None else (None, None)
 
         if isinstance(msg_elts, dict):
             for player in players:
@@ -467,6 +478,35 @@
             started_elt.addChild(player_elt)
         return started_elt
 
+    def sendElements(self, to_jid, data, profile=None):
+        """
+        @param to_jid: recipient JID
+        @param data: list of (elem, attr, content) with:
+        - elem: domish.Element, unicode or a couple:
+                - domish.Element to be directly added as a child to the message
+                - unicode name or couple (uri, name) to create a new domish.Element
+                  and add it as a child to the message (see domish.Element.addElement)
+        - attrs: dictionary of attributes for the new child
+        - content: unicode that is appended to the child content
+        @param profile: the profile from which the message is sent
+        """
+        if profile is None:
+            error(_("Message can not be sent without a sender profile"))
+            return
+        msg = self.createGameElt(to_jid)
+        for elem, attrs, content in data:
+            if elem is not None:
+                if isinstance(elem, domish.Element):
+                    msg.firstChildElement().addChild(elem)
+                else:
+                    elem = msg.firstChildElement().addElement(elem)
+                if attrs is not None:
+                    elem.attributes.update(attrs)
+                if content is not None:
+                    elem.addContent(content)
+        self.host.profiles[profile].xmlstream.send(msg)
+
+
     def send(self, to_jid, elem=None, attrs=None, content=None, profile=None):
         """
         @param to_jid: recipient JID
@@ -478,26 +518,7 @@
         @param content: unicode that is appended to the child content
         @param profile: the profile from which the message is sent
         """
-        if profile is None:
-            error(_("Message can not be sent without a sender profile"))
-            return
-        msg = self.createGameElt(to_jid)
-        if elem is not None:
-            if isinstance(elem, domish.Element):
-                msg.firstChildElement().addChild(elem)
-            else:
-                elem = msg.firstChildElement().addElement(elem)
-            if attrs is not None:
-                elem.attributes.update(attrs)
-            if content is not None:
-                elem.addContent(content)
-        self.host.profiles[profile].xmlstream.send(msg)
-
-        if _DEBUG_FILE:
-            # From here you will see all the game messages
-            file_ = open("/tmp/game_messages", "a")
-            file_.write("%s from %s to %s: %s\n" % (self.name, profile, "room" if to_jid.resource is None else to_jid.resource, elem.toXml()))
-            file_.close()
+        self.sendElements(to_jid, [(elem, attrs, content)], profile)
 
     def getHandler(self, profile):
         return RoomGameHandler(self)