plugin room_games, radiocol: send the current queue to new players
This commit is contained in:
parent
481bee01c2
commit
e8ae4435a3
1 changed files with 100 additions and 60 deletions
|
|
@ -23,6 +23,7 @@ from twisted.words.xish import domish
|
|||
from time import time
|
||||
from wokkel import disco, iwokkel
|
||||
from zope.interface import implements
|
||||
import copy
|
||||
try:
|
||||
from twisted.words.protocols.xmlstream import XMPPHandler
|
||||
except ImportError:
|
||||
|
|
@ -30,7 +31,6 @@ except ImportError:
|
|||
|
||||
# Don't forget to set it to False before you commit
|
||||
_DEBUG = False
|
||||
_DEBUG_FILE = False
|
||||
|
||||
PLUGIN_INFO = {
|
||||
"name": "Room game",
|
||||
|
|
@ -123,7 +123,7 @@ class RoomGame(object):
|
|||
"""
|
||||
referee = room_jid_s + '/' + referee_nick
|
||||
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):
|
||||
"""Return True if a game has been initialized/started.
|
||||
|
|
@ -166,7 +166,7 @@ class RoomGame(object):
|
|||
return auth
|
||||
|
||||
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 == []:
|
||||
return
|
||||
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'])
|
||||
else:
|
||||
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:
|
||||
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):
|
||||
"""Invite players to a room, associated game may exist or not.
|
||||
|
|
@ -408,7 +419,7 @@ class RoomGame(object):
|
|||
players_data = {}
|
||||
for nick in nicks:
|
||||
# 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"
|
||||
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']
|
||||
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):
|
||||
for player in players:
|
||||
|
|
@ -467,6 +478,35 @@ class RoomGame(object):
|
|||
started_elt.addChild(player_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):
|
||||
"""
|
||||
@param to_jid: recipient JID
|
||||
|
|
@ -478,26 +518,7 @@ class RoomGame(object):
|
|||
@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)
|
||||
|
||||
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()
|
||||
self.sendElements(to_jid, [(elem, attrs, content)], profile)
|
||||
|
||||
def getHandler(self, profile):
|
||||
return RoomGameHandler(self)
|
||||
|
|
|
|||
Loading…
Reference in a new issue