diff 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
line wrap: on
line diff
--- a/src/tools/plugins/games.py	Sun Nov 17 17:02:22 2013 +0100
+++ b/src/tools/plugins/games.py	Mon Nov 18 14:25:40 2013 +0100
@@ -105,9 +105,7 @@
             room_s = _room_jid.userhost()
             if room_s in self.games and self.games[room_s]["referee"] == room.occupantJID.full():
                 #we are in a radiocol room, let's start the party !
-                mess = self.createGameElt(JID(room_s + '/' + user.nick))
-                mess.firstChildElement().addChild(self.__create_started_elt())
-                self.host.profiles[profile].xmlstream.send(mess)
+                self.send(JID(room_s + '/' + user.nick), self.createStartElement(), profile=profile)
             return True
         if _room_jid in self.waiting_inv and len(room.roster) >= len(self.waiting_inv[_room_jid][1]):
             expected_players = self.waiting_inv[_room_jid][1]
@@ -153,9 +151,7 @@
         self.games[room] = {'referee': referee}
         self.games[room].update(self.options)
         if self.collectiveGame is True:
-            mess = self.createGameElt(JID(room))
-            mess.firstChildElement().addChild(self.__create_started_elt())
-            self.host.profiles[profile].xmlstream.send(mess)
+            self.send(JID(room), self.createStartElement(), profile=profile)
             return
         # non collaborative game = individual data and messages
         status = {}
@@ -165,9 +161,7 @@
             players_data[player] = self.player_init_data.copy()
             status[player] = "init"
             # each player send a message to all the others
-            mess = self.createGameElt(JID(room + '/' + player))
-            mess.firstChildElement().addChild(self.__create_started_elt(players))
-            self.host.profiles[profile].xmlstream.send(mess)
+            self.send(JID(room + '/' + player), self.createStartElement(players), profile=profile)
         # specific data to each player
         self.games[room].update({'players': players, 'status': status, 'players_data': players_data})
 
@@ -181,10 +175,7 @@
             error(_("profile %s is unknown") % profile_key)
             return
         debug('new player ready: %s' % profile)
-        mess = self.createGameElt(JID(referee))
-        ready_elt = mess.firstChildElement().addElement('player_ready')
-        ready_elt['player'] = player
-        self.host.profiles[profile].xmlstream.send(mess)
+        self.send(JID(referee), 'player_ready', {'player': player}, profile=profile)
 
     def newRound(self, room_jid, data, profile):
         """Launch a new round (reinit the user data)"""
@@ -199,14 +190,10 @@
         if isinstance(msg_elts, dict):
             for player in players:
                 to_jid = JID(room_jid.userhost() + "/" + player)  # FIXME: gof:
-                mess = self.createGameElt(to_jid)
-                if isinstance(msg_elts[player], domish.Element):
-                    mess.firstChildElement().addChild(msg_elts[player])
-                self.host.profiles[profile].xmlstream.send(mess)
+                elem = msg_elts[player] if isinstance(msg_elts[player], domish.Element) else None
+                self.send(to_jid, elem, profile=profile)
         elif isinstance(msg_elts, domish.Element):
-            mess = self.createGameElt(room_jid)
-            mess.firstChildElement().addChild(msg_elts)
-            self.host.profiles[profile].xmlstream.send(mess)
+            self.send(room_jid, msg_elts, profile=profile)
         if common_data is not None:
             for player in players:
                 players_data[player].update(common_data)
@@ -220,7 +207,7 @@
         elt.addElement(self.ns_tag)
         return elt
 
-    def __create_started_elt(self, players=None):
+    def createStartElement(self, players=None):
         """Create a game "started" domish Element"""
         started_elt = domish.Element((None, 'started'))
         if players is None:
@@ -233,3 +220,29 @@
             idx += 1
             started_elt.addChild(player_elt)
         return started_elt
+
+    def send(self, to_jid, elem=None, attrs=None, content=None, profile=None):
+        """
+        @param to_jid: recipient JID
+        @param 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)
+        @param attrs: dictionary of attributes for the new child
+        @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)