Mercurial > libervia-backend
diff src/plugins/plugin_misc_radiocol.py @ 683:75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
author | souliane <souliane@mailoo.org> |
---|---|
date | Wed, 23 Oct 2013 12:45:13 +0200 |
parents | 84a6e83157c2 |
children | f610864eb7a5 |
line wrap: on
line diff
--- a/src/plugins/plugin_misc_radiocol.py Fri Oct 18 11:58:42 2013 +0200 +++ b/src/plugins/plugin_misc_radiocol.py Wed Oct 23 12:45:13 2013 +0200 @@ -29,7 +29,7 @@ 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: @@ -54,14 +54,15 @@ QUEUE_LIMIT = 2 -class Radiocol(object): +class Radiocol(RoomGame): 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': {}}) self.host = host - self.radios = {} - host.bridge.addMethod("radiocolLaunch", ".plugin", in_sign='ass', out_sign='', method=self.radiocolLaunch) - host.bridge.addMethod("radiocolCreate", ".plugin", in_sign='ss', out_sign='', method=self.radiocolCreate) + host.bridge.addMethod("radiocolLaunch", ".plugin", in_sign='ass', out_sign='', method=self.prepareRoom) + host.bridge.addMethod("radiocolCreate", ".plugin", in_sign='ss', out_sign='', method=self.createCollectiveGame) 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("radiocolSongRejected", ".plugin", signature='sss') # room_jid, reason, profile @@ -71,19 +72,6 @@ host.bridge.addSignal("radiocolUploadOk", ".plugin", signature='ss') # room_jid, profile host.trigger.add("MUC user joined", self.userJoinedTrigger) - def createRadiocolElt(self, to_jid, type="normal"): - type = "normal" if to_jid.resource else "groupchat" - elt = domish.Element((None, 'message')) - elt["to"] = to_jid.full() - elt["type"] = type - elt.addElement((NC_RADIOCOL, RADIOC_TAG)) - return elt - - def __create_started_elt(self): - """Create a game_started domish element""" - started_elt = domish.Element((None, 'started')) - return started_elt - def __create_preload_elt(self, sender, filename, title, artist, album): preload_elt = domish.Element((None, 'preload')) preload_elt['sender'] = sender @@ -93,82 +81,6 @@ preload_elt['album'] = album return preload_elt - def userJoinedTrigger(self, room, user, profile): - """This trigger is used to check if we are waiting people in this room, - and to create a game if everybody is here""" - room_jid = room.occupantJID.userhost() - if room_jid in self.radios and self.radios[room_jid]["referee"] == room.occupantJID.full(): - #we are in a radiocol room, let's start the party ! - mess = self.createRadiocolElt(jid.JID(room_jid + '/' + user.nick)) - mess.firstChildElement().addChild(self.__create_started_elt()) - self.host.profiles[profile].xmlstream.send(mess) - return True - - def radiocolLaunch(self, occupants, profile_key='@DEFAULT@'): - """Launch a game: helper method to create a room, invite occupants, and create the radiocol - @param occupants: list for occupants jid""" - debug(_('Launching radiocol')) - profile = self.host.memory.getProfileName(profile_key) - if not profile: - error(_("Unknown profile")) - return - - def radiocolRoomJoined(room): - print "radiocolRoomJoined" - _room_jid = room.occupantJID.userhostJID() - self.radiocolCreate(_room_jid.userhost(), profile_key=profile) - for occupant in occupants: - self.host.plugins["XEP-0249"].invite(jid.JID(occupant), room.occupantJID.userhostJID(), {"game": "Radiocol"}, profile) - - def after_init(ignore): - room_name = "sat_radiocol_%s" % self.host.plugins["XEP-0045"].getUniqueName(profile_key) - print "\n\n===> room_name:", room_name - muc_service = None - for service in self.host.memory.getServerServiceEntities("conference", "text", profile): - if not ".irc." in service.userhost(): - #FIXME: - #This awfull ugly hack is here to avoid an issue with openfire: the irc gateway - #use "conference/text" identity (instead of "conference/irc"), there is certainly a better way - #to manage this, but this hack fill do it for test purpose - muc_service = service - break - if not muc_service: - error(_("Can't find a MUC service")) - return - - _jid, xmlstream = self.host.getJidNStream(profile) - d = self.host.plugins["XEP-0045"].join(jid.JID("%s@%s" % (room_name, muc_service.userhost())), _jid.user, {}, profile) - d.addCallback(radiocolRoomJoined) - - client = self.host.getClient(profile) - if not client: - error(_('No client for this profile key: %s') % profile_key) - return - client.client_initialized.addCallback(after_init) - - def radiocolCreate(self, room_jid_param, profile_key='@DEFAULT@'): - """Create a new game - @param room_jid_param: jid of the room - @param profile_key: %(doc_profile_key)s""" - debug(_("Creating Radiocol")) - room_jid = jid.JID(room_jid_param) - profile = self.host.memory.getProfileName(profile_key) - if not profile: - error(_("profile %s is unknown") % profile_key) - return - if room_jid in self.radios: - warning(_("Radiocol already started in room %s") % room_jid.userhost()) - else: - room_nick = self.host.plugins["XEP-0045"].getRoomNick(room_jid.userhost(), profile) - if not room_nick: - error('Internal error') - return - referee = room_jid.userhost() + '/' + room_nick - self.radios[room_jid.userhost()] = {'referee': referee, 'queue': [], 'upload': True, 'playing': False, 'occupants_data': {}, 'to_delete': {}} - mess = self.createRadiocolElt(jid.JID(room_jid.userhost())) - mess.firstChildElement().addChild(self.__create_started_elt()) - self.host.profiles[profile].xmlstream.send(mess) - def radiocolSongAdded(self, referee, song_path, profile): """This method is called by libervia when a song has been uploaded @param room_jid_param: jid of the room @@ -189,7 +101,7 @@ unlink(song_path) # FIXME: same host trick (see note above) self.host.bridge.radiocolSongRejected(jid.JID(referee).userhost(), "Uploaded file is not Ogg Vorbis song, only Ogg Vorbis songs are acceptable", profile) - """mess = self.createRadiocolElt(jid.JID(referee)) + """mess = self.createGameElt(jid.JID(referee)) reject_elt = mess.firstChildElement().addElement(('','song_rejected')) reject_elt['sender'] = client.jid reject_elt['reason'] = "Uploaded file is not Ogg Vorbis song, only Ogg Vorbis songs are acceptable" @@ -200,7 +112,7 @@ artist = song.get("artist", ["Unknown"])[0] album = song.get("album", ["Unknown"])[0] length = song.info.length - mess = self.createRadiocolElt(jid.JID(referee)) + 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 @@ -209,7 +121,7 @@ added_elt['length'] = str(length) self.host.profiles[profile].xmlstream.send(mess) - radio_data = self.radios[jid.JID(referee).userhost()] # FIXME: referee comes from Libervia's client side, it's unsecure + 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 def playNext(self, room_jid, profile): @@ -219,7 +131,7 @@ # and clean the datas/stop the playlist if it's not the case #TODO: songs need to be erased once played or found invalids # ==> unlink done the Q&D way with the same host trick (see above) - radio_data = self.radios[room_jid.userhost()] + radio_data = self.games[room_jid.userhost()] queue = radio_data['queue'] if not queue: #nothing left to play, we need to wait for uploads @@ -227,14 +139,14 @@ return filename, length = queue.pop(0) - mess = self.createRadiocolElt(room_jid) + mess = self.createGameElt(room_jid) play_elt = mess.firstChildElement().addElement(('', 'play')) play_elt['filename'] = filename self.host.profiles[profile].xmlstream.send(mess) 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.createRadiocolElt(room_jid) + mess = self.createGameElt(room_jid) no_upload_elt = mess.firstChildElement().addElement(('', 'upload_ok')) self.host.profiles[profile].xmlstream.send(mess) radio_data['upload'] = True @@ -254,8 +166,7 @@ from_jid = jid.JID(mess_elt['from']) room_jid = jid.JID(from_jid.userhost()) radio_elt = mess_elt.firstChildElement() - radio_data = self.radios[room_jid.userhost()] - occupants_data = radio_data['occupants_data'] + radio_data = self.games[room_jid.userhost()] queue = radio_data['queue'] for elt in radio_elt.elements(): @@ -278,7 +189,7 @@ if len(queue) >= QUEUE_LIMIT: #there are already too many songs in queue, we reject this one - mess = self.createRadiocolElt(room_jid) + 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" @@ -291,13 +202,13 @@ if len(queue) >= QUEUE_LIMIT: #We are at the limit, we refuse new upload until next play - mess = self.createRadiocolElt(room_jid) + 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) radio_data['upload'] = False - mess = self.createRadiocolElt(room_jid) + mess = self.createGameElt(room_jid) preload_elt = self.__create_preload_elt(from_jid.resource, elt['filename'], elt['title'],