plugin room_games, radiocol: send the current queue to new players

This commit is contained in:
souliane 2013-11-28 19:23:59 +01:00
commit e8ae4435a3
2 changed files with 100 additions and 60 deletions

View file

@ -23,6 +23,8 @@ from twisted.internet import reactor
from twisted.words.protocols.jabber import jid from twisted.words.protocols.jabber import jid
import os.path import os.path
import copy
import time
from os import unlink from os import unlink
from mutagen.oggvorbis import OggVorbis, OggVorbisHeaderError from mutagen.oggvorbis import OggVorbis, OggVorbisHeaderError
@ -41,6 +43,10 @@ PLUGIN_INFO = {
"description": _("""Implementation of radio collective""") "description": _("""Implementation of radio collective""")
} }
# Number of songs needed in the queue before we start playing
QUEUE_TO_START = 2
# Maximum number of songs in the queue (the song being currently played doesn't count)
QUEUE_LIMIT = 2 QUEUE_LIMIT = 2
@ -61,20 +67,20 @@ class Radiocol(object):
host.bridge.addMethod("radiocolCreate", ".plugin", in_sign='sass', out_sign='', method=self.createGame) 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.addMethod("radiocolSongAdded", ".plugin", in_sign='sss', out_sign='', method=self.radiocolSongAdded)
host.bridge.addSignal("radiocolPlayers", ".plugin", signature='ssass') # room_jid, referee, players, 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("radiocolStarted", ".plugin", signature='ssasais') # room_jid, referee, players, [QUEUE_TO_START, QUEUE_LIMIT], profile
host.bridge.addSignal("radiocolSongRejected", ".plugin", signature='sss') # room_jid, reason, 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("radiocolPreload", ".plugin", signature='sssssss') # room_jid, timestamp, filename, title, artist, album, profile
host.bridge.addSignal("radiocolPlay", ".plugin", signature='sss') # room_jid, filename, 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("radiocolNoUpload", ".plugin", signature='ss') # room_jid, profile
host.bridge.addSignal("radiocolUploadOk", ".plugin", signature='ss') # room_jid, profile host.bridge.addSignal("radiocolUploadOk", ".plugin", signature='ss') # room_jid, profile
def __create_preload_elt(self, sender, filename, title, artist, album): def __create_preload_elt(self, sender, song_added_elt):
preload_elt = domish.Element((None, 'preload')) preload_elt = copy.deepcopy(song_added_elt)
preload_elt.name = 'preload'
preload_elt['sender'] = sender preload_elt['sender'] = sender
preload_elt['filename'] = filename # XXX: the frontend should know the temporary directory where file is put preload_elt['timestamp'] = str(time.time())
preload_elt['title'] = title # attributes filename, title, artist, album, length have been copied
preload_elt['artist'] = artist # XXX: the frontend should know the temporary directory where file is put
preload_elt['album'] = album
return preload_elt return preload_elt
def radiocolSongAdded(self, referee, song_path, profile): def radiocolSongAdded(self, referee, song_path, profile):
@ -94,7 +100,7 @@ class Radiocol(object):
song = OggVorbis(song_path) song = OggVorbis(song_path)
except OggVorbisHeaderError: except OggVorbisHeaderError:
#this file is not ogg vorbis, we reject it #this file is not ogg vorbis, we reject it
unlink(song_path) # FIXME: same host trick (see note above) self.deleteFile(song_path) # FIXME: same host trick (see note above)
self.host.bridge.radiocolSongRejected(jid.JID(referee).userhost(), self.host.bridge.radiocolSongRejected(jid.JID(referee).userhost(),
"Uploaded file is not Ogg Vorbis song, only Ogg Vorbis songs are acceptable", profile) "Uploaded file is not Ogg Vorbis song, only Ogg Vorbis songs are acceptable", profile)
"""mess = self.createGameElt(jid.JID(referee)) """mess = self.createGameElt(jid.JID(referee))
@ -116,20 +122,25 @@ class Radiocol(object):
radio_data['to_delete'][attrs['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): def playNext(self, room_jid, profile):
""""Play next sont in queue if exists, and put a timer """"Play next song in queue if exists, and put a timer
which trigger after the song has been played to play next one""" which trigger after the song has been played to play next one"""
#TODO: need to check that there are still peoples in the room
# and clean the datas/stop the playlist if it's not the case
#TODO: songs need to be erased once played or found invalids #TODO: songs need to be erased once played or found invalids
# ==> unlink done the Q&D way with the same host trick (see above) # ==> unlink done the Q&D way with the same host trick (see above)
radio_data = self.games[room_jid.userhost()] radio_data = self.games[room_jid.userhost()]
if len(radio_data['players']) == 0:
debug(_('No more participants in the radiocol: cleaning data'))
radio_data['queue'] = []
for filename in radio_data['to_delete']:
self.deleteFile(radio_data, filename)
radio_data['to_delete'] = {}
queue = radio_data['queue'] queue = radio_data['queue']
if not queue: if not queue:
#nothing left to play, we need to wait for uploads #nothing left to play, we need to wait for uploads
radio_data['playing'] = False radio_data['playing'] = False
return return
filename, length = queue.pop(0) song = queue.pop(0)
filename, length = song['filename'], float(song['length'])
self.send(room_jid, ('', 'play'), {'filename': filename}, profile=profile) self.send(room_jid, ('', 'play'), {'filename': filename}, profile=profile)
if not radio_data['upload'] and len(queue) < QUEUE_LIMIT: if not radio_data['upload'] and len(queue) < QUEUE_LIMIT:
@ -138,17 +149,23 @@ class Radiocol(object):
radio_data['upload'] = True radio_data['upload'] = True
reactor.callLater(length, self.playNext, room_jid, profile) reactor.callLater(length, self.playNext, room_jid, profile)
#we wait more than the song length to delete the file, to manage poorly reactive networks/clients
reactor.callLater(length + 90, self.deleteFile, radio_data, filename) # FIXME: same host trick (see above)
def deleteFile(self, radio_data, filename):
try: try:
file_to_delete = radio_data['to_delete'][filename] file_to_delete = radio_data['to_delete'][filename]
except KeyError: except KeyError:
error(_("INTERNAL ERROR: can't find full path of the song to delete")) error(_("INTERNAL ERROR: can't find full path of the song to delete"))
return return False
try:
#we wait more than the song length to delete the file, to manage poorly reactive networks/clients unlink(file_to_delete)
reactor.callLater(length + 90, unlink, file_to_delete) # FIXME: same host trick (see above) except OSError:
error(_("INTERNAL ERROR: can't find %s on the file system" % file_to_delete))
return False
return True
def room_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']) from_jid = jid.JID(mess_elt['from'])
room_jid = jid.JID(from_jid.userhost()) room_jid = jid.JID(from_jid.userhost())
radio_elt = mess_elt.firstChildElement() radio_elt = mess_elt.firstChildElement()
@ -156,16 +173,20 @@ class Radiocol(object):
if 'queue' in radio_data: if 'queue' in radio_data:
queue = radio_data['queue'] queue = radio_data['queue']
from_referee = self.isReferee(room_jid.userhost(), from_jid.resource)
to_referee = self.isReferee(room_jid.userhost(), jid.JID(mess_elt['to']).user)
for elt in radio_elt.elements(): for elt in radio_elt.elements():
if not from_referee and not (to_referee and elt.name == 'song_added'):
continue # sender must be referee, expect when a song is submitted
if elt.name == 'started' or elt.name == 'players': # new game created if elt.name == 'started' or elt.name == 'players': # new game created
players = [] players = []
for player in elt.elements(): for player in elt.elements():
players.append(unicode(player)) players.append(unicode(player))
signal = self.host.bridge.radiocolStarted if elt.name == 'started' else self.host.bridge.radiocolPlayers signal = self.host.bridge.radiocolStarted if elt.name == 'started' else self.host.bridge.radiocolPlayers
signal(room_jid.userhost(), from_jid.full(), players, profile) signal(room_jid.userhost(), from_jid.full(), players, [QUEUE_TO_START, QUEUE_LIMIT], profile)
elif elt.name == 'preload': # a song is in queue and must be preloaded 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) self.host.bridge.radiocolPreload(room_jid.userhost(), elt['timestamp'], elt['filename'], elt['title'], elt['artist'], elt['album'], profile)
elif elt.name == 'play': elif elt.name == 'play':
self.host.bridge.radiocolPlay(room_jid.userhost(), elt['filename'], profile) self.host.bridge.radiocolPlay(room_jid.userhost(), elt['filename'], profile)
elif elt.name == 'song_rejected': # a song has been refused elif elt.name == 'song_rejected': # a song has been refused
@ -180,32 +201,30 @@ class Radiocol(object):
if len(queue) >= QUEUE_LIMIT: if len(queue) >= QUEUE_LIMIT:
#there are already too many songs in queue, we reject this one #there are already too many songs in queue, we reject this one
attrs = {'sender': from_jid.resource,
'reason': "Too many songs in queue"
}
#FIXME: add an error code #FIXME: add an error code
self.send(room_jid, ('', 'song_rejected'), attrs, profile=profile) self.send(from_jid, ('', 'song_rejected'), {'reason': "Too many songs in queue"}, profile=profile)
return return
#The song is accepted and added in queue #The song is accepted and added in queue
queue.append((elt['filename'], float(elt['length']))) preload_elt = self.__create_preload_elt(from_jid.resource, elt)
queue.append(preload_elt)
if len(queue) >= QUEUE_LIMIT: if len(queue) >= QUEUE_LIMIT:
#We are at the limit, we refuse new upload until next play #We are at the limit, we refuse new upload until next play
#FIXME: add an error code
self.send(room_jid, ('', 'no_upload'), profile=profile) self.send(room_jid, ('', 'no_upload'), profile=profile)
radio_data['upload'] = False radio_data['upload'] = False
preload_elt = self.__create_preload_elt(from_jid.resource,
elt['filename'],
elt['title'],
elt['artist'],
elt['album'])
self.send(room_jid, preload_elt, profile=profile) self.send(room_jid, preload_elt, profile=profile)
if not radio_data['playing'] and len(queue) == 2: if not radio_data['playing'] and len(queue) == QUEUE_TO_START:
#we have not started playing yet, and we have 2 songs in queue # We have not started playing yet, and we have QUEUE_TO_START
#we can now start the party :) # songs in queue. We can now start the party :)
radio_data['playing'] = True radio_data['playing'] = True
self.playNext(room_jid, profile) self.playNext(room_jid, profile)
else: else:
error(_('Unmanaged game element: %s') % elt.name) error(_('Unmanaged game element: %s') % elt.name)
def getSyncData(self, room_jid_s):
data = self.games[room_jid_s]['queue']
if len(data) == QUEUE_LIMIT:
data.append(domish.Element((None, 'no_upload')))
return data

View file

@ -23,6 +23,7 @@ from twisted.words.xish import domish
from time import time from time import time
from wokkel import disco, iwokkel from wokkel import disco, iwokkel
from zope.interface import implements from zope.interface import implements
import copy
try: try:
from twisted.words.protocols.xmlstream import XMPPHandler from twisted.words.protocols.xmlstream import XMPPHandler
except ImportError: except ImportError:
@ -30,7 +31,6 @@ except ImportError:
# Don't forget to set it to False before you commit # Don't forget to set it to False before you commit
_DEBUG = False _DEBUG = False
_DEBUG_FILE = False
PLUGIN_INFO = { PLUGIN_INFO = {
"name": "Room game", "name": "Room game",
@ -123,7 +123,7 @@ class RoomGame(object):
""" """
referee = room_jid_s + '/' + referee_nick referee = room_jid_s + '/' + referee_nick
self.games[room_jid_s] = {'referee': referee, 'players': [], 'started': False} self.games[room_jid_s] = {'referee': referee, 'players': [], 'started': False}
self.games[room_jid_s].update(self.game_init) self.games[room_jid_s].update(copy.deepcopy(self.game_init))
def gameExists(self, room_jid_s, started=False): def gameExists(self, room_jid_s, started=False):
"""Return True if a game has been initialized/started. """Return True if a game has been initialized/started.
@ -166,7 +166,7 @@ class RoomGame(object):
return auth return auth
def updatePlayers(self, room_jid_s, nicks, profile): def updatePlayers(self, room_jid_s, nicks, profile):
"""Signal to the room or to each player that some players joined the game""" """Signal to the room that some players joined the game"""
if nicks == []: if nicks == []:
return return
new_nicks = set(nicks).difference(self.games[room_jid_s]['players']) new_nicks = set(nicks).difference(self.games[room_jid_s]['players'])
@ -181,8 +181,19 @@ class RoomGame(object):
element = self.createStartElement(self.games[room_jid_s]['players']) element = self.createStartElement(self.games[room_jid_s]['players'])
else: else:
element = self.createStartElement(self.games[room_jid_s]['players'], name="players") element = self.createStartElement(self.games[room_jid_s]['players'], name="players")
elements = [(element, None, None)]
for child in self.getSyncData(room_jid_s):
# TODO: sync data may be different and private to each player,
# in that case send a separate message to the new players
elements.append((child, None, None))
for recipient in recipients: for recipient in recipients:
self.send(recipient, element, profile=profile) self.sendElements(recipient, elements, profile=profile)
def getSyncData(self, room_jid_s):
"""This method may be overwritten by any child class.
@return: a list of child elements to be added for the game to be synchronized.
"""
return []
def invitePlayers(self, room, other_players, nick, profile): def invitePlayers(self, room, other_players, nick, profile):
"""Invite players to a room, associated game may exist or not. """Invite players to a room, associated game may exist or not.
@ -408,7 +419,7 @@ class RoomGame(object):
players_data = {} players_data = {}
for nick in nicks: for nick in nicks:
# The dict must be COPIED otherwise it is shared between all users # The dict must be COPIED otherwise it is shared between all users
players_data[nick] = self.player_init.copy() players_data[nick] = copy.deepcopy(self.player_init)
status[nick] = "init" status[nick] = "init"
self.games[room_jid_s].update({'status': status, 'players_data': players_data}) self.games[room_jid_s].update({'status': status, 'players_data': players_data})
@ -429,7 +440,7 @@ class RoomGame(object):
players_data = game_data['players_data'] players_data = game_data['players_data']
game_data['stage'] = "init" game_data['stage'] = "init"
common_data, msg_elts = data if data is not None else (None, None) common_data, msg_elts = copy.deepcopy(data) if data is not None else (None, None)
if isinstance(msg_elts, dict): if isinstance(msg_elts, dict):
for player in players: for player in players:
@ -467,6 +478,35 @@ class RoomGame(object):
started_elt.addChild(player_elt) started_elt.addChild(player_elt)
return started_elt return started_elt
def sendElements(self, to_jid, data, profile=None):
"""
@param to_jid: recipient JID
@param data: list of (elem, attr, content) with:
- 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)
- attrs: dictionary of attributes for the new child
- 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)
for elem, attrs, content in data:
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)
def send(self, to_jid, elem=None, attrs=None, content=None, profile=None): def send(self, to_jid, elem=None, attrs=None, content=None, profile=None):
""" """
@param to_jid: recipient JID @param to_jid: recipient JID
@ -478,26 +518,7 @@ class RoomGame(object):
@param content: unicode that is appended to the child content @param content: unicode that is appended to the child content
@param profile: the profile from which the message is sent @param profile: the profile from which the message is sent
""" """
if profile is None: self.sendElements(to_jid, [(elem, attrs, content)], profile)
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)
if _DEBUG_FILE:
# From here you will see all the game messages
file_ = open("/tmp/game_messages", "a")
file_.write("%s from %s to %s: %s\n" % (self.name, profile, "room" if to_jid.resource is None else to_jid.resource, elem.toXml()))
file_.close()
def getHandler(self, profile): def getHandler(self, profile):
return RoomGameHandler(self) return RoomGameHandler(self)