diff src/plugins/plugin_misc_radiocol.py @ 717:358018c5c398

plugins (games): more factorization and flexibility for launching and joining games: - "MUC user joined", "MUC user left" and class XMPPHandler are managed directly in RoomGame - renamed __init__ parameters 'player_init_data' to 'player_init' and 'options' to 'game_init' - pass the players list in radiocol method 'createGame' and signal 'radiocolStarted' (needed for invitation system and for UI players identification) - added some parameters to manage who can invite, who can join, who to wait for... managed with check***Auth methods - joining a game that is already launched may be possible, regarding these parameters and the invitation list - leave and join a game again is partly managed: new tarot round is launched, we should keep playing the same round instead
author souliane <souliane@mailoo.org>
date Thu, 21 Nov 2013 15:49:53 +0100
parents ecc5a5b34ee1
children 074970227bc0
line wrap: on
line diff
--- a/src/plugins/plugin_misc_radiocol.py	Thu Nov 21 15:38:53 2013 +0100
+++ b/src/plugins/plugin_misc_radiocol.py	Thu Nov 21 15:49:53 2013 +0100
@@ -22,23 +22,14 @@
 from twisted.internet import reactor
 from twisted.words.protocols.jabber import jid
 
-from wokkel import disco, iwokkel
-
-from zope.interface import implements
-
 import os.path
 from os import unlink
 from mutagen.oggvorbis import OggVorbis, OggVorbisHeaderError
 from sat.tools.plugins.games import RoomGame
-try:
-    from twisted.words.protocols.xmlstream import XMPPHandler
-except ImportError:
-    from wokkel.subprotocols import XMPPHandler
 
-MESSAGE = '/message'
+
 NC_RADIOCOL = 'http://www.goffi.org/protocol/radiocol'
 RADIOC_TAG = 'radiocol'
-RADIOC_REQUEST = MESSAGE + '/' + RADIOC_TAG + '[@xmlns="' + NC_RADIOCOL + '"]'
 
 PLUGIN_INFO = {
     "name": "Radio collective plugin",
@@ -59,18 +50,18 @@
     def __init__(self, host):
         info(_("Radio collective initialization"))
         RoomGame.__init__(self, host, PLUGIN_INFO, (NC_RADIOCOL, RADIOC_TAG),
-                          options={'queue': [], 'upload': True, 'playing': False, 'to_delete': {}})
+                          game_init={'queue': [], 'upload': True, 'playing': False, 'to_delete': {}})
         self.host = host
         host.bridge.addMethod("radiocolLaunch", ".plugin", in_sign='asss', out_sign='', method=self.prepareRoom)
-        host.bridge.addMethod("radiocolCreate", ".plugin", in_sign='ss', out_sign='', method=self.createCollectiveGame)
+        host.bridge.addMethod("radiocolCreate", ".plugin", in_sign='sass', out_sign='', method=self.createGame)
         host.bridge.addMethod("radiocolSongAdded", ".plugin", in_sign='sss', out_sign='', method=self.radiocolSongAdded)
-        host.bridge.addSignal("radiocolStarted", ".plugin", signature='sss')  # room_jid, referee, profile
+        host.bridge.addSignal("radiocolPlayers", ".plugin", signature='ssass')  # room_jid, referee, players, profile
+        host.bridge.addSignal("radiocolStarted", ".plugin", signature='ssass')  # room_jid, referee, players, profile
         host.bridge.addSignal("radiocolSongRejected", ".plugin", signature='sss')  # room_jid, reason, profile
         host.bridge.addSignal("radiocolPreload", ".plugin", signature='ssssss')  # room_jid, filename, title, artist, album, profile
         host.bridge.addSignal("radiocolPlay", ".plugin", signature='sss')  # room_jid, filename, profile
         host.bridge.addSignal("radiocolNoUpload", ".plugin", signature='ss')  # room_jid, profile
         host.bridge.addSignal("radiocolUploadOk", ".plugin", signature='ss')  # room_jid, profile
-        host.trigger.add("MUC user joined", self.userJoinedTrigger)
 
     def __create_preload_elt(self, sender, filename, title, artist, album):
         preload_elt = domish.Element((None, 'preload'))
@@ -151,18 +142,23 @@
         #we wait more than the song length to delete the file, to manage poorly reactive networks/clients
         reactor.callLater(length + 90, unlink, file_to_delete)  # FIXME: same host trick (see above)
 
-    def radiocol_game_cmd(self, mess_elt, profile):
+    def room_game_cmd(self, mess_elt, profile):
         #FIXME: we should check sender (is it referee ?) here before accepting commands
         from_jid = jid.JID(mess_elt['from'])
         room_jid = jid.JID(from_jid.userhost())
         radio_elt = mess_elt.firstChildElement()
         radio_data = self.games[room_jid.userhost()]
-        queue = radio_data['queue']
+        if 'queue' in radio_data:
+            queue = radio_data['queue']
 
         for elt in radio_elt.elements():
 
-            if elt.name == 'started':  # new game created
-                self.host.bridge.radiocolStarted(room_jid.userhost(), from_jid.full(), profile)
+            if elt.name == 'started' or elt.name == 'players':  # new game created
+                players = []
+                for player in elt.elements():
+                    players.append(unicode(player))
+                signal = self.host.bridge.radiocolStarted if elt.name == 'started' else self.host.bridge.radiocolPlayers
+                signal(room_jid.userhost(), from_jid.full(), players, profile)
             elif elt.name == 'preload':  # a song is in queue and must be preloaded
                 self.host.bridge.radiocolPreload(room_jid.userhost(), elt['filename'], elt['title'], elt['artist'], elt['album'], profile)
             elif elt.name == 'play':
@@ -208,23 +204,3 @@
                     self.playNext(room_jid, profile)
             else:
                 error(_('Unmanaged game element: %s') % elt.name)
-
-    def getHandler(self, profile):
-        return RadiocolHandler(self)
-
-
-class RadiocolHandler (XMPPHandler):
-    implements(iwokkel.IDisco)
-
-    def __init__(self, plugin_parent):
-        self.plugin_parent = plugin_parent
-        self.host = plugin_parent.host
-
-    def connectionInitialized(self):
-        self.xmlstream.addObserver(RADIOC_REQUEST, self.plugin_parent.radiocol_game_cmd, profile=self.parent.profile)
-
-    def getDiscoInfo(self, requestor, target, nodeIdentifier=''):
-        return [disco.DiscoFeature(NC_RADIOCOL)]
-
-    def getDiscoItems(self, requestor, target, nodeIdentifier=''):
-        return []