diff src/plugins/plugin_misc_radiocol.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/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 :)