changeset 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 8bd63daecdbf
children f47d7c09c60b
files src/plugins/plugin_misc_radiocol.py src/plugins/plugin_misc_tarot.py src/tools/plugins/games.py
diffstat 3 files changed, 67 insertions(+), 92 deletions(-) [+]
line wrap: on
line diff
--- a/src/plugins/plugin_misc_radiocol.py	Sun Nov 17 17:02:22 2013 +0100
+++ b/src/plugins/plugin_misc_radiocol.py	Mon Nov 18 14:25:40 2013 +0100
@@ -108,21 +108,16 @@
             #FIXME: add an error code
             self.host.profiles[profile].xmlstream.send(mess)"""
             return
-        title = song.get("title", ["Unknown"])[0]
-        artist = song.get("artist", ["Unknown"])[0]
-        album = song.get("album", ["Unknown"])[0]
-        length = song.info.length
-        mess = self.createGameElt(jid.JID(referee))
-        added_elt = mess.firstChildElement().addElement(('', 'song_added'))
-        added_elt['filename'] = filename = os.path.basename(song_path)
-        added_elt['title'] = title
-        added_elt['artist'] = artist
-        added_elt['album'] = album
-        added_elt['length'] = str(length)
-        self.host.profiles[profile].xmlstream.send(mess)
+        attrs = {'filename': os.path.basename(song_path),
+                 'title': song.get("title", ["Unknown"])[0],
+                 'artist': song.get("artist", ["Unknown"])[0],
+                 'album': song.get("album", ["Unknown"])[0],
+                 'length': str(song.info.length)
+                 }
+        self.send(jid.JID(referee), ('', 'song_added'), attrs, profile=profile)
 
         radio_data = self.games[jid.JID(referee).userhost()]  # FIXME: referee comes from Libervia's client side, it's unsecure
-        radio_data['to_delete'][filename] = song_path  # FIXME: works only because of the same host trick, see the note under the docstring
+        radio_data['to_delete'][attrs['filename']] = song_path  # FIXME: works only because of the same host trick, see the note under the docstring
 
     def playNext(self, room_jid, profile):
         """"Play next sont in queue if exists, and put a timer
@@ -139,16 +134,11 @@
             return
 
         filename, length = queue.pop(0)
-        mess = self.createGameElt(room_jid)
-        play_elt = mess.firstChildElement().addElement(('', 'play'))
-        play_elt['filename'] = filename
-        self.host.profiles[profile].xmlstream.send(mess)
+        self.send(room_jid, ('', 'play'), {'filename': filename}, profile=profile)
 
         if not radio_data['upload'] and len(queue) < QUEUE_LIMIT:
             #upload is blocked and we now have resources to get more, we reactivate it
-            mess = self.createGameElt(room_jid)
-            no_upload_elt = mess.firstChildElement().addElement(('', 'upload_ok'))
-            self.host.profiles[profile].xmlstream.send(mess)
+            self.send(room_jid, ('', 'upload_ok'), profile=profile)
             radio_data['upload'] = True
 
         reactor.callLater(length, self.playNext, room_jid, profile)
@@ -189,12 +179,11 @@
 
                 if len(queue) >= QUEUE_LIMIT:
                     #there are already too many songs in queue, we reject this one
-                    mess = self.createGameElt(room_jid)
-                    reject_elt = mess.firstChildElement().addElement(('', 'song_rejected'))
-                    reject_elt['sender'] = from_jid.resource
-                    reject_elt['reason'] = "Too many songs in queue"
+                    attrs = {'sender': from_jid.resource,
+                             'reason': "Too many songs in queue"
+                             }
                     #FIXME: add an error code
-                    self.host.profiles[profile].xmlstream.send(mess)
+                    self.send(room_jid, ('', 'song_rejected'), attrs, profile=profile)
                     return
 
                 #The song is accepted and added in queue
@@ -202,20 +191,16 @@
 
                 if len(queue) >= QUEUE_LIMIT:
                     #We are at the limit, we refuse new upload until next play
-                    mess = self.createGameElt(room_jid)
-                    no_upload_elt = mess.firstChildElement().addElement(('', 'no_upload'))
                     #FIXME: add an error code
-                    self.host.profiles[profile].xmlstream.send(mess)
+                    self.send(room_jid, ('', 'no_upload'), profile=profile)
                     radio_data['upload'] = False
 
-                mess = self.createGameElt(room_jid)
                 preload_elt = self.__create_preload_elt(from_jid.resource,
                                                         elt['filename'],
                                                         elt['title'],
                                                         elt['artist'],
                                                         elt['album'])
-                mess.firstChildElement().addChild(preload_elt)
-                self.host.profiles[profile].xmlstream.send(mess)
+                self.send(room_jid, preload_elt, profile=profile)
                 if not radio_data['playing'] and len(queue) == 2:
                     #we have not started playing yet, and we have 2 songs in queue
                     #we can now start the party :)
--- a/src/plugins/plugin_misc_tarot.py	Sun Nov 17 17:02:22 2013 +0100
+++ b/src/plugins/plugin_misc_tarot.py	Mon Nov 18 14:25:40 2013 +0100
@@ -380,9 +380,7 @@
         next_player_idx = game_data['current_player'] = (game_data['init_player'] + 1) % len(game_data['players'])  # the player after the dealer start
         game_data['first_player'] = next_player = game_data['players'][next_player_idx]
         to_jid = jid.JID(room_jid.userhost() + "/" + next_player)  # FIXME: gof:
-        mess = self.createGameElt(to_jid)
-        yourturn_elt = mess.firstChildElement().addElement('your_turn')
-        self.host.profiles[profile].xmlstream.send(mess)
+        self.send(to_jid, 'your_turn', profile=profile)
 
     def contratChoosed(self, player, referee, contrat, profile_key='@NONE@'):
         """Must be call by player when the contrat is selected
@@ -396,10 +394,7 @@
             error(_("profile %s is unknown") % profile_key)
             return
         debug(_('contrat [%(contrat)s] choosed by %(profile)s') % {'contrat': contrat, 'profile': profile})
-        mess = self.createGameElt(jid.JID(referee))
-        contrat_elt = mess.firstChildElement().addElement(('', 'contrat_choosed'), content=contrat)
-        contrat_elt['player'] = player
-        self.host.profiles[profile].xmlstream.send(mess)
+        self.send(jid.JID(referee), ('', 'contrat_choosed'), {'player': player}, content=contrat, profile=profile)
 
     def play_cards(self, player, referee, cards, profile_key='@NONE@'):
         """Must be call by player when the contrat is selected
@@ -413,10 +408,8 @@
             error(_("profile %s is unknown") % profile_key)
             return
         debug(_('Cards played by %(profile)s: [%(cards)s]') % {'profile': profile, 'cards': cards})
-        mess = self.createGameElt(jid.JID(referee))
-        playcard_elt = mess.firstChildElement().addChild(self.__card_list_to_xml(TarotCard.from_tuples(cards), 'cards_played'))
-        playcard_elt['player'] = player
-        self.host.profiles[profile].xmlstream.send(mess)
+        elem = self.__card_list_to_xml(TarotCard.from_tuples(cards), 'cards_played')
+        self.send(jid.JID(referee), elem, {'player': player}, profile=profile)
 
     def newRound(self, room_jid, profile):
         game_data = self.games[room_jid.userhost()]
@@ -448,9 +441,7 @@
         pl_idx = game_data['current_player'] = (game_data['init_player'] + 1) % len(players)  # the player after the dealer start
         player = players[pl_idx]
         to_jid = jid.JID(room_jid.userhost() + "/" + player)  # FIXME: gof:
-        mess = self.createGameElt(to_jid)
-        mess.firstChildElement().addChild(self.__ask_contrat())
-        self.host.profiles[profile].xmlstream.send(mess)
+        self.send(to_jid, self.__ask_contrat(), profile=profile)
 
     def card_game_cmd(self, mess_elt, profile):
         """
@@ -497,9 +488,7 @@
                     #not everybody has choosed his contrat, it's next one turn
                     player = self.__next_player(game_data)
                     to_jid = jid.JID(room_jid.userhost() + "/" + player)  # FIXME: gof:
-                    mess = self.createGameElt(to_jid)
-                    mess.firstChildElement().addChild(self.__ask_contrat())
-                    self.host.profiles[profile].xmlstream.send(mess)
+                    self.send(to_jid, self.__ask_contrat(), profile=profile)
                 else:
                     best_contrat = [None, "Passe"]
                     for player in game_data['players']:
@@ -512,9 +501,7 @@
                     if best_contrat[1] == "Passe":
                         debug(_("Everybody is passing, round ended"))
                         to_jid = jid.JID(room_jid.userhost())
-                        mess = self.createGameElt(to_jid)
-                        mess.firstChildElement().addChild(self.__give_scores(*self.__draw_game(game_data)))
-                        self.host.profiles[profile].xmlstream.send(mess)
+                        self.send(to_jid, self.__give_scores(*self.__draw_game(game_data)), profile=profile)
                         game_data['init_player'] = (game_data['init_player'] + 1) % len(game_data['players'])  # we change the dealer
                         for player in game_data['players']:
                             game_data['status'][player] = "init"
@@ -528,10 +515,8 @@
                     else:
                         #Time to show the chien to everybody
                         to_jid = jid.JID(room_jid.userhost())  # FIXME: gof:
-                        mess = self.createGameElt(to_jid)
-                        chien_elt = mess.firstChildElement().addChild(self.__card_list_to_xml(game_data['chien'], 'chien'))
-                        chien_elt['attaquant'] = best_contrat[0]
-                        self.host.profiles[profile].xmlstream.send(mess)
+                        elem = self.__card_list_to_xml(game_data['chien'], 'chien')
+                        self.send(to_jid, elem, {'attaquant': best_contrat[0]}, profile=profile)
                         #the attacker (attaquant) get the chien
                         game_data['hand'][best_contrat[0]].extend(game_data['chien'])
                         del game_data['chien'][:]
@@ -556,9 +541,8 @@
                     #we now check validity of card
                     invalid_cards = self.__invalid_cards(game_data, list_cards)
                     if invalid_cards:
-                        mess = self.createGameElt(jid.JID(room_jid.userhost() + '/' + elt['player']))
-                        mess.firstChildElement().addChild(self.__invalid_cards_elt(list_cards, invalid_cards, game_data['stage']))
-                        self.host.profiles[profile].xmlstream.send(mess)
+                        elem = self.__invalid_cards_elt(list_cards, invalid_cards, game_data['stage'])
+                        self.send(jid.JID(room_jid.userhost() + '/' + elt['player']), elem, profile=profile)
                         return
 
                     #FIXME: gof: manage Garde Sans & Garde Contre cases
@@ -578,9 +562,8 @@
                         #we first check validity of card
                         invalid_cards = self.__invalid_cards(game_data, cards)
                         if invalid_cards:
-                            mess = self.createGameElt(jid.JID(room_jid.userhost() + '/' + current_player))
-                            mess.firstChildElement().addChild(self.__invalid_cards_elt(cards, invalid_cards, game_data['stage']))
-                            self.host.profiles[profile].xmlstream.send(mess)
+                            elem = self.__invalid_cards_elt(cards, invalid_cards, game_data['stage'])
+                            self.send(jid.JID(room_jid.userhost() + '/' + current_player), elem, profile=profile)
                             return
                         #the card played is ok, we forward it to everybody
                         #first we remove it from the hand and put in on the table
@@ -588,9 +571,7 @@
                         players_data[current_player]['played'] = cards[0]
 
                         #then we forward the message
-                        mess = self.createGameElt(room_jid)
-                        playcard_elt = mess.firstChildElement().addChild(elt)
-                        self.host.profiles[profile].xmlstream.send(mess)
+                        self.send(room_jid, elt, profile=profile)
 
                         #Did everybody played ?
                         played = [players_data[player]['played'] for player in game_data['players']]
@@ -606,10 +587,8 @@
                                 players_data[player]['played'] = None
                             if len(game_data['hand'][current_player]) == 0:
                                 #no card lef: the game is finished
-                                to_jid = room_jid
-                                mess = self.createGameElt(to_jid)
-                                chien_elt = mess.firstChildElement().addChild(self.__give_scores(*self.__calculate_scores(game_data)))
-                                self.host.profiles[profile].xmlstream.send(mess)
+                                elem = self.__give_scores(*self.__calculate_scores(game_data))
+                                self.send(room_jid, elem, profile=profile)
                                 game_data['init_player'] = (game_data['init_player'] + 1) % len(game_data['players'])  # we change the dealer
                                 for player in game_data['players']:
                                     game_data['status'][player] = "init"
@@ -621,9 +600,7 @@
 
                         #finally, we tell to the next player to play
                         to_jid = jid.JID(room_jid.userhost() + "/" + next_player)
-                        mess = self.createGameElt(to_jid)
-                        yourturn_elt = mess.firstChildElement().addElement('your_turn')
-                        self.host.profiles[profile].xmlstream.send(mess)
+                        self.send(to_jid, 'your_turn', profile=profile)
 
             elif elt.name == 'your_turn':
                 self.host.bridge.tarotGameYourTurn(room_jid.userhost(), profile)
--- 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)