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
This commit is contained in:
parent
6a99705523
commit
ea67ed7a61
4 changed files with 378 additions and 196 deletions
|
|
@ -25,23 +25,15 @@ from twisted.words.protocols.jabber import error as jab_error
|
||||||
from twisted.words.protocols.jabber.xmlstream import IQ
|
from twisted.words.protocols.jabber.xmlstream import IQ
|
||||||
import random
|
import random
|
||||||
|
|
||||||
from zope.interface import implements
|
from wokkel import data_form
|
||||||
|
|
||||||
from wokkel import disco, iwokkel, data_form
|
|
||||||
from sat.tools.xml_tools import dataForm2XML
|
from sat.tools.xml_tools import dataForm2XML
|
||||||
from sat.tools.frontends.games import TarotCard
|
from sat.tools.frontends.games import TarotCard
|
||||||
from sat.tools.plugins.games import RoomGame
|
from sat.tools.plugins.games import RoomGame
|
||||||
from time import time
|
from time import time
|
||||||
|
|
||||||
try:
|
|
||||||
from twisted.words.protocols.xmlstream import XMPPHandler
|
|
||||||
except ImportError:
|
|
||||||
from wokkel.subprotocols import XMPPHandler
|
|
||||||
|
|
||||||
MESSAGE = '/message'
|
|
||||||
NS_QG = 'http://www.goffi.org/protocol/quiz'
|
NS_QG = 'http://www.goffi.org/protocol/quiz'
|
||||||
QG_TAG = 'quiz'
|
QG_TAG = 'quiz'
|
||||||
QG_REQUEST = MESSAGE + '/' + QG_TAG + '[@xmlns="' + NS_QG + '"]'
|
|
||||||
|
|
||||||
PLUGIN_INFO = {
|
PLUGIN_INFO = {
|
||||||
"name": "Quiz game plugin",
|
"name": "Quiz game plugin",
|
||||||
|
|
@ -59,8 +51,7 @@ class Quiz(RoomGame):
|
||||||
|
|
||||||
def __init__(self, host):
|
def __init__(self, host):
|
||||||
info(_("Plugin Quiz initialization"))
|
info(_("Plugin Quiz initialization"))
|
||||||
RoomGame.__init__(self, host, PLUGIN_INFO, (NS_QG, QG_TAG), player_init_data={'score': 0},
|
RoomGame.__init__(self, host, PLUGIN_INFO, (NS_QG, QG_TAG), game_init={'stage': None}, player_init={'score': 0})
|
||||||
options={'stage': None})
|
|
||||||
host.bridge.addMethod("quizGameLaunch", ".plugin", in_sign='asss', out_sign='', method=self.prepareRoom) # args: players, room_jid, profile
|
host.bridge.addMethod("quizGameLaunch", ".plugin", in_sign='asss', out_sign='', method=self.prepareRoom) # args: players, room_jid, profile
|
||||||
host.bridge.addMethod("quizGameCreate", ".plugin", in_sign='sass', out_sign='', method=self.createGame) # args: room_jid, players, profile
|
host.bridge.addMethod("quizGameCreate", ".plugin", in_sign='sass', out_sign='', method=self.createGame) # args: room_jid, players, profile
|
||||||
host.bridge.addMethod("quizGameReady", ".plugin", in_sign='sss', out_sign='', method=self.playerReady) # args: player, referee, profile
|
host.bridge.addMethod("quizGameReady", ".plugin", in_sign='sss', out_sign='', method=self.playerReady) # args: player, referee, profile
|
||||||
|
|
@ -114,7 +105,6 @@ class Quiz(RoomGame):
|
||||||
'param_0': "room_jid: jid of game's room",
|
'param_0': "room_jid: jid of game's room",
|
||||||
'param_1': "time_left: time left before timer expiration",
|
'param_1': "time_left: time left before timer expiration",
|
||||||
'param_2': '%(doc_profile)s'})
|
'param_2': '%(doc_profile)s'})
|
||||||
host.trigger.add("MUC user joined", self.userJoinedTrigger)
|
|
||||||
|
|
||||||
def __game_data_to_xml(self, game_data):
|
def __game_data_to_xml(self, game_data):
|
||||||
"""Convert a game data dict to domish element"""
|
"""Convert a game data dict to domish element"""
|
||||||
|
|
@ -264,12 +254,13 @@ Attention, tu es prêt ?""")}
|
||||||
RoomGame.newRound(self, room_jid, (common_data, msg_elts), profile)
|
RoomGame.newRound(self, room_jid, (common_data, msg_elts), profile)
|
||||||
reactor.callLater(10, self.askQuestion, room_jid, profile)
|
reactor.callLater(10, self.askQuestion, room_jid, profile)
|
||||||
|
|
||||||
def quiz_game_cmd(self, mess_elt, profile):
|
def room_game_cmd(self, mess_elt, profile):
|
||||||
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())
|
||||||
game_elt = mess_elt.firstChildElement()
|
game_elt = mess_elt.firstChildElement()
|
||||||
game_data = self.games[room_jid.userhost()]
|
game_data = self.games[room_jid.userhost()]
|
||||||
players_data = game_data['players_data']
|
if 'players_data' in game_data:
|
||||||
|
players_data = game_data['players_data']
|
||||||
|
|
||||||
for elt in game_elt.elements():
|
for elt in game_elt.elements():
|
||||||
|
|
||||||
|
|
@ -333,23 +324,3 @@ Attention, tu es prêt ?""")}
|
||||||
|
|
||||||
else:
|
else:
|
||||||
error(_('Unmanaged game element: %s') % elt.name)
|
error(_('Unmanaged game element: %s') % elt.name)
|
||||||
|
|
||||||
def getHandler(self, profile):
|
|
||||||
return QuizGameHandler(self)
|
|
||||||
|
|
||||||
|
|
||||||
class QuizGameHandler (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(QG_REQUEST, self.plugin_parent.quiz_game_cmd, profile=self.parent.profile)
|
|
||||||
|
|
||||||
def getDiscoInfo(self, requestor, target, nodeIdentifier=''):
|
|
||||||
return [disco.DiscoFeature(NS_QG)]
|
|
||||||
|
|
||||||
def getDiscoItems(self, requestor, target, nodeIdentifier=''):
|
|
||||||
return []
|
|
||||||
|
|
|
||||||
|
|
@ -22,23 +22,14 @@ from twisted.words.xish import domish
|
||||||
from twisted.internet import reactor
|
from twisted.internet import reactor
|
||||||
from twisted.words.protocols.jabber import jid
|
from twisted.words.protocols.jabber import jid
|
||||||
|
|
||||||
from wokkel import disco, iwokkel
|
|
||||||
|
|
||||||
from zope.interface import implements
|
|
||||||
|
|
||||||
import os.path
|
import os.path
|
||||||
from os import unlink
|
from os import unlink
|
||||||
from mutagen.oggvorbis import OggVorbis, OggVorbisHeaderError
|
from mutagen.oggvorbis import OggVorbis, OggVorbisHeaderError
|
||||||
from sat.tools.plugins.games import RoomGame
|
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'
|
NC_RADIOCOL = 'http://www.goffi.org/protocol/radiocol'
|
||||||
RADIOC_TAG = 'radiocol'
|
RADIOC_TAG = 'radiocol'
|
||||||
RADIOC_REQUEST = MESSAGE + '/' + RADIOC_TAG + '[@xmlns="' + NC_RADIOCOL + '"]'
|
|
||||||
|
|
||||||
PLUGIN_INFO = {
|
PLUGIN_INFO = {
|
||||||
"name": "Radio collective plugin",
|
"name": "Radio collective plugin",
|
||||||
|
|
@ -59,18 +50,18 @@ class Radiocol(RoomGame):
|
||||||
def __init__(self, host):
|
def __init__(self, host):
|
||||||
info(_("Radio collective initialization"))
|
info(_("Radio collective initialization"))
|
||||||
RoomGame.__init__(self, host, PLUGIN_INFO, (NC_RADIOCOL, RADIOC_TAG),
|
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
|
self.host = host
|
||||||
host.bridge.addMethod("radiocolLaunch", ".plugin", in_sign='asss', out_sign='', method=self.prepareRoom)
|
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.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("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='ssssss') # room_jid, 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
|
||||||
host.trigger.add("MUC user joined", self.userJoinedTrigger)
|
|
||||||
|
|
||||||
def __create_preload_elt(self, sender, filename, title, artist, album):
|
def __create_preload_elt(self, sender, filename, title, artist, album):
|
||||||
preload_elt = domish.Element((None, 'preload'))
|
preload_elt = domish.Element((None, 'preload'))
|
||||||
|
|
@ -151,18 +142,23 @@ class Radiocol(RoomGame):
|
||||||
#we wait more than the song length to delete the file, to manage poorly reactive networks/clients
|
#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)
|
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
|
#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()
|
||||||
radio_data = self.games[room_jid.userhost()]
|
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():
|
for elt in radio_elt.elements():
|
||||||
|
|
||||||
if elt.name == 'started': # new game created
|
if elt.name == 'started' or elt.name == 'players': # new game created
|
||||||
self.host.bridge.radiocolStarted(room_jid.userhost(), from_jid.full(), profile)
|
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
|
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['filename'], elt['title'], elt['artist'], elt['album'], profile)
|
||||||
elif elt.name == 'play':
|
elif elt.name == 'play':
|
||||||
|
|
@ -208,23 +204,3 @@ class Radiocol(RoomGame):
|
||||||
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 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 []
|
|
||||||
|
|
|
||||||
|
|
@ -20,26 +20,17 @@
|
||||||
from logging import debug, info, warning, error
|
from logging import debug, info, warning, error
|
||||||
from twisted.words.xish import domish
|
from twisted.words.xish import domish
|
||||||
from twisted.words.protocols.jabber import jid
|
from twisted.words.protocols.jabber import jid
|
||||||
import random
|
from wokkel import data_form
|
||||||
|
|
||||||
from zope.interface import implements
|
|
||||||
|
|
||||||
from wokkel import disco, iwokkel, data_form
|
|
||||||
from sat.tools.xml_tools import dataForm2XML
|
from sat.tools.xml_tools import dataForm2XML
|
||||||
|
|
||||||
from sat.tools.frontends.games import TarotCard
|
from sat.tools.frontends.games import TarotCard
|
||||||
from sat.tools.plugins.games import RoomGame
|
from sat.tools.plugins.games import RoomGame
|
||||||
from time import time
|
from time import time
|
||||||
|
import random
|
||||||
|
|
||||||
try:
|
|
||||||
from twisted.words.protocols.xmlstream import XMPPHandler
|
|
||||||
except ImportError:
|
|
||||||
from wokkel.subprotocols import XMPPHandler
|
|
||||||
|
|
||||||
MESSAGE = '/message'
|
|
||||||
NS_CG = 'http://www.goffi.org/protocol/card_game'
|
NS_CG = 'http://www.goffi.org/protocol/card_game'
|
||||||
CG_TAG = 'card_game'
|
CG_TAG = 'card_game'
|
||||||
CG_REQUEST = MESSAGE + '/' + CG_TAG + '[@xmlns="' + NS_CG + '"]'
|
|
||||||
|
|
||||||
PLUGIN_INFO = {
|
PLUGIN_INFO = {
|
||||||
"name": "Tarot cards plugin",
|
"name": "Tarot cards plugin",
|
||||||
|
|
@ -57,14 +48,16 @@ class Tarot(RoomGame):
|
||||||
|
|
||||||
def __init__(self, host):
|
def __init__(self, host):
|
||||||
info(_("Plugin Tarot initialization"))
|
info(_("Plugin Tarot initialization"))
|
||||||
RoomGame.__init__(self, host, PLUGIN_INFO, (NS_CG, CG_TAG), player_init_data={'score': 0},
|
RoomGame.__init__(self, host, PLUGIN_INFO, (NS_CG, CG_TAG),
|
||||||
options={'hand_size': 18, 'init_player': 0, 'current_player': None, 'contrat': None, 'stage': None})
|
game_init={'hand_size': 18, 'init_player': 0, 'current_player': None, 'contrat': None, 'stage': None},
|
||||||
|
player_init={'score': 0})
|
||||||
self.contrats = [_('Passe'), _('Petite'), _('Garde'), _('Garde Sans'), _('Garde Contre')]
|
self.contrats = [_('Passe'), _('Petite'), _('Garde'), _('Garde Sans'), _('Garde Contre')]
|
||||||
host.bridge.addMethod("tarotGameLaunch", ".plugin", in_sign='asss', out_sign='', method=self.prepareRoom) # args: players, room_jid, profile
|
host.bridge.addMethod("tarotGameLaunch", ".plugin", in_sign='asss', out_sign='', method=self.prepareRoom) # args: players, room_jid, profile
|
||||||
host.bridge.addMethod("tarotGameCreate", ".plugin", in_sign='sass', out_sign='', method=self.createGame) # args: room_jid, players, profile
|
host.bridge.addMethod("tarotGameCreate", ".plugin", in_sign='sass', out_sign='', method=self.createGame) # args: room_jid, players, profile
|
||||||
host.bridge.addMethod("tarotGameReady", ".plugin", in_sign='sss', out_sign='', method=self.playerReady) # args: player, referee, profile
|
host.bridge.addMethod("tarotGameReady", ".plugin", in_sign='sss', out_sign='', method=self.playerReady) # args: player, referee, profile
|
||||||
host.bridge.addMethod("tarotGameContratChoosed", ".plugin", in_sign='ssss', out_sign='', method=self.contratChoosed) # args: player, referee, contrat, profile
|
host.bridge.addMethod("tarotGameContratChoosed", ".plugin", in_sign='ssss', out_sign='', method=self.contratChoosed) # args: player, referee, contrat, profile
|
||||||
host.bridge.addMethod("tarotGamePlayCards", ".plugin", in_sign='ssa(ss)s', out_sign='', method=self.play_cards) # args: player, referee, cards, profile
|
host.bridge.addMethod("tarotGamePlayCards", ".plugin", in_sign='ssa(ss)s', out_sign='', method=self.play_cards) # args: player, referee, cards, profile
|
||||||
|
host.bridge.addSignal("tarotGamePlayers", ".plugin", signature='ssass') # args: room_jid, referee, players, profile
|
||||||
host.bridge.addSignal("tarotGameStarted", ".plugin", signature='ssass') # args: room_jid, referee, players, profile
|
host.bridge.addSignal("tarotGameStarted", ".plugin", signature='ssass') # args: room_jid, referee, players, profile
|
||||||
host.bridge.addSignal("tarotGameNew", ".plugin", signature='sa(ss)s') # args: room_jid, hand, profile
|
host.bridge.addSignal("tarotGameNew", ".plugin", signature='sa(ss)s') # args: room_jid, hand, profile
|
||||||
host.bridge.addSignal("tarotGameChooseContrat", ".plugin", signature='sss') # args: room_jid, xml_data, profile
|
host.bridge.addSignal("tarotGameChooseContrat", ".plugin", signature='sss') # args: room_jid, xml_data, profile
|
||||||
|
|
@ -73,7 +66,6 @@ class Tarot(RoomGame):
|
||||||
host.bridge.addSignal("tarotGameYourTurn", ".plugin", signature='ss') # args: room_jid, profile
|
host.bridge.addSignal("tarotGameYourTurn", ".plugin", signature='ss') # args: room_jid, profile
|
||||||
host.bridge.addSignal("tarotGameScore", ".plugin", signature='ssasass') # args: room_jid, xml_data, winners (list of nicks), loosers (list of nicks), profile
|
host.bridge.addSignal("tarotGameScore", ".plugin", signature='ssasass') # args: room_jid, xml_data, winners (list of nicks), loosers (list of nicks), profile
|
||||||
host.bridge.addSignal("tarotGameInvalidCards", ".plugin", signature='ssa(ss)a(ss)s') # args: room_jid, game phase, played_cards, invalid_cards, profile
|
host.bridge.addSignal("tarotGameInvalidCards", ".plugin", signature='ssa(ss)a(ss)s') # args: room_jid, game phase, played_cards, invalid_cards, profile
|
||||||
host.trigger.add("MUC user joined", self.userJoinedTrigger)
|
|
||||||
self.deck_ordered = []
|
self.deck_ordered = []
|
||||||
for value in ['excuse'] + map(str, range(1, 22)):
|
for value in ['excuse'] + map(str, range(1, 22)):
|
||||||
self.deck_ordered.append(TarotCard(("atout", value)))
|
self.deck_ordered.append(TarotCard(("atout", value)))
|
||||||
|
|
@ -443,7 +435,7 @@ class Tarot(RoomGame):
|
||||||
to_jid = jid.JID(room_jid.userhost() + "/" + player) # FIXME: gof:
|
to_jid = jid.JID(room_jid.userhost() + "/" + player) # FIXME: gof:
|
||||||
self.send(to_jid, self.__ask_contrat(), profile=profile)
|
self.send(to_jid, self.__ask_contrat(), profile=profile)
|
||||||
|
|
||||||
def card_game_cmd(self, mess_elt, profile):
|
def room_game_cmd(self, mess_elt, profile):
|
||||||
"""
|
"""
|
||||||
@param mess_elt: instance of twisted.words.xish.domish.Element
|
@param mess_elt: instance of twisted.words.xish.domish.Element
|
||||||
"""
|
"""
|
||||||
|
|
@ -451,15 +443,17 @@ class Tarot(RoomGame):
|
||||||
room_jid = jid.JID(from_jid.userhost())
|
room_jid = jid.JID(from_jid.userhost())
|
||||||
game_elt = mess_elt.firstChildElement()
|
game_elt = mess_elt.firstChildElement()
|
||||||
game_data = self.games[room_jid.userhost()]
|
game_data = self.games[room_jid.userhost()]
|
||||||
players_data = game_data['players_data']
|
if 'players_data' in game_data:
|
||||||
|
players_data = game_data['players_data']
|
||||||
|
|
||||||
for elt in game_elt.elements():
|
for elt in game_elt.elements():
|
||||||
|
|
||||||
if elt.name == 'started': # 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))
|
||||||
self.host.bridge.tarotGameStarted(room_jid.userhost(), from_jid.full(), players, profile)
|
signal = self.host.bridge.tarotGameStarted if elt.name == 'started' else self.host.bridge.tarotGamePlayers
|
||||||
|
signal(room_jid.userhost(), from_jid.full(), players, profile)
|
||||||
|
|
||||||
elif elt.name == 'player_ready': # ready to play
|
elif elt.name == 'player_ready': # ready to play
|
||||||
player = elt['player']
|
player = elt['player']
|
||||||
|
|
@ -625,23 +619,3 @@ class Tarot(RoomGame):
|
||||||
error(_('Unmanaged error type: %s') % elt['type'])
|
error(_('Unmanaged error type: %s') % elt['type'])
|
||||||
else:
|
else:
|
||||||
error(_('Unmanaged card game element: %s') % elt.name)
|
error(_('Unmanaged card game element: %s') % elt.name)
|
||||||
|
|
||||||
def getHandler(self, profile):
|
|
||||||
return CardGameHandler(self)
|
|
||||||
|
|
||||||
|
|
||||||
class CardGameHandler (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(CG_REQUEST, self.plugin_parent.card_game_cmd, profile=self.parent.profile)
|
|
||||||
|
|
||||||
def getDiscoInfo(self, requestor, target, nodeIdentifier=''):
|
|
||||||
return [disco.DiscoFeature(NS_CG)]
|
|
||||||
|
|
||||||
def getDiscoItems(self, requestor, target, nodeIdentifier=''):
|
|
||||||
return []
|
|
||||||
|
|
|
||||||
|
|
@ -21,152 +21,385 @@ from logging import debug, warning, error
|
||||||
from twisted.words.protocols.jabber.jid import JID
|
from twisted.words.protocols.jabber.jid import JID
|
||||||
from twisted.words.xish import domish
|
from twisted.words.xish import domish
|
||||||
from time import time
|
from time import time
|
||||||
"""This library help manage general games (e.g. card games) and it can be used by plugins"""
|
from wokkel import disco, iwokkel
|
||||||
|
from zope.interface import implements
|
||||||
|
try:
|
||||||
|
from twisted.words.protocols.xmlstream import XMPPHandler
|
||||||
|
except ImportError:
|
||||||
|
from wokkel.subprotocols import XMPPHandler
|
||||||
|
|
||||||
|
|
||||||
|
"""This library help manage general games (e.g. card games) and it can be used by plugins."""
|
||||||
|
|
||||||
|
# Don't forget to set it to False before you commit
|
||||||
|
debugMsg = True
|
||||||
|
debugFile = True
|
||||||
|
|
||||||
|
|
||||||
class RoomGame(object):
|
class RoomGame(object):
|
||||||
"""This class is used to help launching a MUC game."""
|
"""This class is used to help launching a MUC game."""
|
||||||
|
|
||||||
def __init__(self, host, plugin_info, ns_tag, player_init_data={}, options={}):
|
# Values for self.invite_mode (who can invite after the game creation)
|
||||||
|
FROM_ALL, FROM_NONE, FROM_REFEREE, FROM_PLAYERS = xrange(0, 4)
|
||||||
|
# Values for self.wait_mode (for who we should wait before creating the game)
|
||||||
|
FOR_ALL, FOR_NONE = xrange(0, 2)
|
||||||
|
# Values for self.join_mode (who can join the game - NONE means solo game)
|
||||||
|
ALL, INVITED, NONE = xrange(0, 3)
|
||||||
|
# Values for ready_mode (how to turn a MUC user into a player)
|
||||||
|
ASK, FORCE = xrange(0, 2)
|
||||||
|
|
||||||
|
MESSAGE = '/message'
|
||||||
|
REQUEST = '%s/%s[@xmlns="%s"]'
|
||||||
|
|
||||||
|
def __init__(self, host, plugin_info, ns_tag, game_init={}, player_init={}):
|
||||||
"""
|
"""
|
||||||
@param host
|
@param host
|
||||||
@param plugin_info: PLUGIN_INFO map of the game plugin
|
@param plugin_info: PLUGIN_INFO map of the game plugin
|
||||||
@ns_tag: couple (nameservice, tag) to construct the messages
|
@ns_tag: couple (nameservice, tag) to construct the messages
|
||||||
@param player_init_data: dictionary for initialization data, applicable to each player
|
@param game_init: dictionary for general game initialization
|
||||||
@param options: dictionary for game options
|
@param player_init: dictionary for player initialization, applicable to each player
|
||||||
"""
|
"""
|
||||||
self.host = host
|
self.host = host
|
||||||
self.name = plugin_info["import_name"]
|
self.name = plugin_info["import_name"]
|
||||||
self.ns_tag = ns_tag
|
self.ns_tag = ns_tag
|
||||||
self.player_init_data = player_init_data
|
self.request = self.REQUEST % (self.MESSAGE, ns_tag[1], ns_tag[0])
|
||||||
self.collectiveGame = self.player_init_data == {}
|
self.game_init = game_init
|
||||||
self.options = options
|
self.player_init = player_init
|
||||||
self.games = {}
|
self.games = {}
|
||||||
self.waiting_inv = {} # Invitation waiting for people to join to launch a game
|
self.invitations = {} # list of couple (x, y) with x the time and y a list of users
|
||||||
|
|
||||||
|
# These are the default settings, which can be overwritten by child class after initialization
|
||||||
|
self.invite_mode = self.FROM_PLAYERS if self.player_init == {} else self.FROM_NONE
|
||||||
|
self.wait_mode = self.FOR_NONE if self.player_init == {} else self.FOR_ALL
|
||||||
|
self.join_mode = self.INVITED
|
||||||
|
self.ready_mode = self.FORCE # TODO: asking for confirmation is not implemented
|
||||||
|
|
||||||
|
host.trigger.add("MUC user joined", self.userJoinedTrigger)
|
||||||
|
host.trigger.add("MUC user left", self.userLeftTrigger)
|
||||||
|
|
||||||
|
def createOrInvite(self, room, other_players, profile):
|
||||||
|
"""
|
||||||
|
This is called only when someone explicitly wants to play.
|
||||||
|
The game must not be created if one already exists in the room,
|
||||||
|
or its creation could be postponed until all the expected players
|
||||||
|
join the room (in that case it will be created from userJoinedTrigger).
|
||||||
|
@param room: instance of wokkel.muc.Room
|
||||||
|
@param other_players: list for other players JID userhosts
|
||||||
|
"""
|
||||||
|
user_jid = self.host.getJidNStream(profile)[0]
|
||||||
|
room_jid_s = room.occupantJID.userhost()
|
||||||
|
nick = self.host.plugins["XEP-0045"].getRoomNick(room_jid_s, profile)
|
||||||
|
nicks = [nick]
|
||||||
|
if self.gameExists(room_jid_s):
|
||||||
|
if not self.checkJoinAuth(room_jid_s, user_jid.userhost(), nick):
|
||||||
|
return
|
||||||
|
nicks.extend(self.invitePlayers(room, other_players, nick, profile))
|
||||||
|
self.updatePlayers(room_jid_s, nicks, profile)
|
||||||
|
else:
|
||||||
|
self.initGame(room_jid_s, nick)
|
||||||
|
(auth, waiting, missing) = self.checkWaitAuth(room, other_players)
|
||||||
|
nicks.extend(waiting)
|
||||||
|
nicks.extend(self.invitePlayers(room, missing, nick, profile))
|
||||||
|
if auth:
|
||||||
|
self.createGame(room_jid_s, nicks, profile)
|
||||||
|
else:
|
||||||
|
self.updatePlayers(room_jid_s, nicks, profile)
|
||||||
|
|
||||||
|
def initGame(self, room_jid_s, referee_nick):
|
||||||
|
"""Important: do not add the referee to 'players' yet. For a
|
||||||
|
<players /> message to be emitted whenever a new player is joining,
|
||||||
|
it is necessary to not modify 'players' outside of updatePlayers.
|
||||||
|
"""
|
||||||
|
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)
|
||||||
|
|
||||||
|
def gameExists(self, room_jid_s, started=False):
|
||||||
|
"""Return True if a game has been initialized/started.
|
||||||
|
@param started: if False, the game must be initialized only,
|
||||||
|
otherwise it must be initialized and started with createGame.
|
||||||
|
@return: True if a game is initialized/started in that room"""
|
||||||
|
return room_jid_s in self.games and (not started or self.games[room_jid_s]['started'])
|
||||||
|
|
||||||
|
def checkJoinAuth(self, room_jid_s, user_jid_s=None, nick="", verbose=False):
|
||||||
|
"""Checks if this profile is allowed to join the game.
|
||||||
|
The parameter nick is used to check if the user is already
|
||||||
|
a player in that game. When this method is called from
|
||||||
|
userJoinedTrigger, nick is also used to check the user
|
||||||
|
identity instead of user_jid_s (see TODO remark below).
|
||||||
|
@param room_jid_s: the room hosting the game
|
||||||
|
@param user_jid_s: JID userhost of the user
|
||||||
|
@param nick: nick of the user
|
||||||
|
"""
|
||||||
|
auth = False
|
||||||
|
if not self.gameExists(room_jid_s):
|
||||||
|
auth = False
|
||||||
|
elif self.join_mode == self.ALL or self.isPlayer(room_jid_s, nick):
|
||||||
|
auth = True
|
||||||
|
elif self.join_mode == self.INVITED:
|
||||||
|
# considering all the batches of invitations
|
||||||
|
for invitations in self.invitations[room_jid_s]:
|
||||||
|
if user_jid_s is not None:
|
||||||
|
if user_jid_s in invitations[1]:
|
||||||
|
auth = True
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
# TODO: that's not secure enough but what to do if
|
||||||
|
# wokkel.muc.User's 'entity' attribute is not set?!
|
||||||
|
if nick in [JID(invited).user for invited in invitations[1]]:
|
||||||
|
auth = True
|
||||||
|
break
|
||||||
|
|
||||||
|
if not auth and (verbose or debugMsg):
|
||||||
|
debug(_("%s not allowed to join the game %s in %s") % (user_jid_s or nick, self.name, room_jid_s))
|
||||||
|
return auth
|
||||||
|
|
||||||
|
def updatePlayers(self, room_jid_s, nicks, profile):
|
||||||
|
"""Signal to the room or to each player that some players joined the game"""
|
||||||
|
if nicks == []:
|
||||||
|
return
|
||||||
|
new_nicks = set(nicks).difference(self.games[room_jid_s]['players'])
|
||||||
|
if len(new_nicks) == 0:
|
||||||
|
return
|
||||||
|
self.games[room_jid_s]['players'].extend(new_nicks)
|
||||||
|
self.signalPlayers(room_jid_s, [JID(room_jid_s)], profile)
|
||||||
|
|
||||||
|
def signalPlayers(self, room_jid_s, recipients, profile):
|
||||||
|
"""Let these guys know that we are playing (they may not play themselves)."""
|
||||||
|
if self.gameExists(room_jid_s, started=True):
|
||||||
|
element = self.createStartElement(self.games[room_jid_s]['players'])
|
||||||
|
else:
|
||||||
|
element = self.createStartElement(self.games[room_jid_s]['players'], name="players")
|
||||||
|
for recipient in recipients:
|
||||||
|
self.send(recipient, element, profile=profile)
|
||||||
|
|
||||||
|
def invitePlayers(self, room, other_players, nick, profile):
|
||||||
|
"""Invite players to a room, associated game may exist or not.
|
||||||
|
@param room: wokkel.muc.Room instance
|
||||||
|
@param other_players: list of JID userhosts to invite
|
||||||
|
@param nick: nick of the user who send the invitation
|
||||||
|
@return: list of the invited players who were already in the room
|
||||||
|
"""
|
||||||
|
room_jid = room.occupantJID.userhostJID()
|
||||||
|
room_jid_s = room.occupantJID.userhost()
|
||||||
|
if not self.checkInviteAuth(room_jid_s, nick):
|
||||||
|
return []
|
||||||
|
self.invitations.setdefault(room_jid_s, [])
|
||||||
|
# TODO: remove invitation waiting for too long, using the time data
|
||||||
|
self.invitations[room_jid_s].append((time(), other_players))
|
||||||
|
nicks = [nick]
|
||||||
|
for player_jid in [JID(player) for player in other_players]:
|
||||||
|
# TODO: find a way to make it secure
|
||||||
|
other_nick = self.host.plugins["XEP-0045"].getRoomNickOfUser(room, player_jid, secure=False)
|
||||||
|
if other_nick is None:
|
||||||
|
self.host.plugins["XEP-0249"].invite(player_jid, room_jid, {"game": self.name}, profile)
|
||||||
|
else:
|
||||||
|
nicks.append(other_nick)
|
||||||
|
return nicks
|
||||||
|
|
||||||
|
def checkInviteAuth(self, room_jid_s, nick, verbose=False):
|
||||||
|
"""Checks if this profile is allowed to invite players"""
|
||||||
|
auth = False
|
||||||
|
if self.invite_mode == self.FROM_ALL or not self.gameExists(room_jid_s):
|
||||||
|
auth = True
|
||||||
|
elif self.invite_mode == self.FROM_NONE:
|
||||||
|
auth = not self.gameExists(room_jid_s, started=True)
|
||||||
|
elif self.invite_mode == self.FROM_REFEREE:
|
||||||
|
auth = self.isReferee(room_jid_s, nick)
|
||||||
|
elif self.invite_mode == self.FROM_PLAYERS:
|
||||||
|
auth = self.isPlayer(room_jid_s, nick)
|
||||||
|
if not auth and (verbose or debugMsg):
|
||||||
|
debug(_("%s not allowed to invite for the game %s in %s") % (nick, self.name, room_jid_s))
|
||||||
|
return auth
|
||||||
|
|
||||||
|
def isReferee(self, room_jid_s, nick):
|
||||||
|
"""Checks if the player with this nick is the referee for the game in this room"""
|
||||||
|
if not self.gameExists(room_jid_s):
|
||||||
|
return False
|
||||||
|
return room_jid_s + '/' + nick == self.games[room_jid_s]['referee']
|
||||||
|
|
||||||
|
def isPlayer(self, room_jid_s, nick):
|
||||||
|
"""Checks if the player with this nick is a player for the game in this room.
|
||||||
|
Important: the referee is not in the 'players' list right after the game
|
||||||
|
initialization - check with isReferee to be sure nick is not a player.
|
||||||
|
"""
|
||||||
|
if not self.gameExists(room_jid_s):
|
||||||
|
return False
|
||||||
|
return nick in self.games[room_jid_s]['players'] or self.isReferee(room_jid_s, nick)
|
||||||
|
|
||||||
|
def checkWaitAuth(self, room, other_players, verbose=False):
|
||||||
|
"""Check if we must wait before starting the game or not.
|
||||||
|
@return: (x, y, z) with:
|
||||||
|
x: False if we must wait, True otherwise
|
||||||
|
y: the nicks of the players that have been checked and confirmed
|
||||||
|
z: the players that have not been checked or that are missing
|
||||||
|
"""
|
||||||
|
if self.wait_mode == self.FOR_NONE or other_players == []:
|
||||||
|
result = (True, [], other_players)
|
||||||
|
elif len(room.roster) < len(other_players) + 1:
|
||||||
|
result = (False, [], other_players)
|
||||||
|
else:
|
||||||
|
# TODO: find a way to make it secure
|
||||||
|
(nicks, missing) = self.host.plugins["XEP-0045"].getRoomNicksOfUsers(room, other_players, secure=False)
|
||||||
|
result = (len(nicks) == len(other_players), nicks, missing)
|
||||||
|
if not result[0] and (verbose or debugMsg):
|
||||||
|
debug(_("Still waiting for %s before starting the game %s in %s") % (result[2], self.name, room.occupantJID.userhost()))
|
||||||
|
return result
|
||||||
|
|
||||||
def getUniqueName(self, muc_service="", profile_key='@DEFAULT@'):
|
def getUniqueName(self, muc_service="", profile_key='@DEFAULT@'):
|
||||||
room = self.host.plugins["XEP-0045"].getUniqueName(muc_service, profile_key=profile_key)
|
room = self.host.plugins["XEP-0045"].getUniqueName(muc_service, profile_key=profile_key)
|
||||||
return "sat_%s_%s" % (self.name.lower(), room) if room != "" else ""
|
return "sat_%s_%s" % (self.name.lower(), room) if room != "" else ""
|
||||||
|
|
||||||
def prepareRoom(self, other_players, room_jid=None, profile_key='@NONE@'):
|
def prepareRoom(self, other_players=[], room_jid=None, profile_key='@NONE@'):
|
||||||
"""Prepare the room for a game: create it and invite players.
|
"""Prepare the room for a game: create it and invite players.
|
||||||
@param other_players: list for other players JID userhosts
|
@param other_players: list for other players JID userhosts
|
||||||
@param room_jid: JID of the room to reuse or None to create a new room
|
@param room_jid: JID userhost of the room to reuse or None to create a new room
|
||||||
"""
|
"""
|
||||||
debug(_('Preparing room for %s game') % self.name)
|
debug(_('Preparing room for %s game') % self.name)
|
||||||
profile = self.host.memory.getProfileName(profile_key)
|
profile = self.host.memory.getProfileName(profile_key)
|
||||||
if not profile:
|
if not profile:
|
||||||
error(_("Unknown profile"))
|
error(_("Unknown profile"))
|
||||||
return
|
return
|
||||||
_jid, xmlstream = self.host.getJidNStream(profile)
|
|
||||||
if other_players is None:
|
|
||||||
other_players = []
|
|
||||||
players = other_players[:]
|
|
||||||
players.append(_jid.userhost())
|
|
||||||
|
|
||||||
def roomJoined(room):
|
def roomJoined(room):
|
||||||
"""@param room: instance of wokkel.muc.Room"""
|
"""@param room: instance of wokkel.muc.Room"""
|
||||||
_room = room.occupantJID.userhostJID()
|
self.createOrInvite(room, other_players, profile)
|
||||||
if self.collectiveGame is True or other_players == [] and _jid in [user.entity for user in room.roster.values()]:
|
|
||||||
self.createGame(_room.userhost(), [] if self.collectiveGame is True else players, profile_key=profile)
|
|
||||||
else:
|
|
||||||
self.waiting_inv[_room] = (time(), players) # TODO: remove invitation waiting for too long, using the time data
|
|
||||||
for player in other_players:
|
|
||||||
self.host.plugins["XEP-0249"].invite(JID(player), room.occupantJID.userhostJID(), {"game": self.name}, profile)
|
|
||||||
|
|
||||||
def after_init(room_jid):
|
def afterClientInit(room_jid):
|
||||||
if room_jid is not None and room_jid != "":
|
"""Create/join the given room, or a unique generated one if no room is specified.
|
||||||
# a room name has been specified...
|
@param room_jid: room to join
|
||||||
|
"""
|
||||||
|
if room_jid is not None and room_jid != "": # a room name has been specified
|
||||||
if room_jid in self.host.plugins["XEP-0045"].clients[profile].joined_rooms:
|
if room_jid in self.host.plugins["XEP-0045"].clients[profile].joined_rooms:
|
||||||
# and we're already in
|
|
||||||
roomJoined(self.host.plugins["XEP-0045"].clients[profile].joined_rooms[room_jid])
|
roomJoined(self.host.plugins["XEP-0045"].clients[profile].joined_rooms[room_jid])
|
||||||
return
|
return
|
||||||
else:
|
else:
|
||||||
room_jid = self.getUniqueName(profile_key=profile_key)
|
room_jid = self.getUniqueName(profile_key=profile_key)
|
||||||
if room_jid == "":
|
if room_jid == "":
|
||||||
return
|
return
|
||||||
d = self.host.plugins["XEP-0045"].join(JID(room_jid), _jid.user, {}, profile)
|
user_jid = self.host.getJidNStream(profile)[0]
|
||||||
|
d = self.host.plugins["XEP-0045"].join(JID(room_jid), user_jid.user, {}, profile)
|
||||||
d.addCallback(roomJoined)
|
d.addCallback(roomJoined)
|
||||||
|
|
||||||
client = self.host.getClient(profile)
|
client = self.host.getClient(profile)
|
||||||
if not client:
|
if not client:
|
||||||
error(_('No client for this profile key: %s') % profile_key)
|
error(_('No client for this profile key: %s') % profile_key)
|
||||||
return
|
return
|
||||||
client.client_initialized.addCallback(lambda ignore: after_init(room_jid))
|
client.client_initialized.addCallback(lambda ignore: afterClientInit(room_jid))
|
||||||
|
|
||||||
def userJoinedTrigger(self, room, user, profile):
|
def userJoinedTrigger(self, room, user, profile):
|
||||||
"""This trigger is used to check if we are waiting for people in this room,
|
"""This trigger is used to check if the new user can take part of a game,
|
||||||
and to create a game if everybody is here.
|
create the game if we were waiting for him or just update the players list.
|
||||||
@room: wokkel.muc.Room object. room.roster is a dict{wokkel.muc.User.nick: wokkel.muc.User}
|
@room: wokkel.muc.Room object. room.roster is a dict{wokkel.muc.User.nick: wokkel.muc.User}
|
||||||
@user: wokkel.muc.User object. user.nick is a unicode and user.entity a JID
|
@user: wokkel.muc.User object. user.nick is a unicode and user.entity a JID
|
||||||
|
@return: True to not interrupt the main process.
|
||||||
"""
|
"""
|
||||||
_room_jid = room.occupantJID.userhostJID()
|
room_jid_s = room.occupantJID.userhost()
|
||||||
if self.collectiveGame is True:
|
profile_nick = room.occupantJID.resource
|
||||||
room_s = _room_jid.userhost()
|
if not self.isReferee(room_jid_s, profile_nick):
|
||||||
if room_s in self.games and self.games[room_s]["referee"] == room.occupantJID.full():
|
return True # profile is not the referee
|
||||||
#we are in a radiocol room, let's start the party !
|
if not self.checkJoinAuth(room_jid_s, nick=user.nick):
|
||||||
self.send(JID(room_s + '/' + user.nick), self.createStartElement(), profile=profile)
|
# user not allowed but let him know that we are playing :p
|
||||||
|
self.signalPlayers(room_jid_s, [JID(room_jid_s + '/' + user.nick)], profile)
|
||||||
return True
|
return True
|
||||||
if _room_jid in self.waiting_inv and len(room.roster) >= len(self.waiting_inv[_room_jid][1]):
|
if self.wait_mode == self.FOR_ALL:
|
||||||
expected_players = self.waiting_inv[_room_jid][1]
|
# considering the last batch of invitations
|
||||||
players = []
|
batch = len(self.invitations[room_jid_s]) - 1
|
||||||
for player in expected_players:
|
if batch < 0:
|
||||||
for user in room.roster.values():
|
error("Invitations from %s to play %s in %s have been lost!" % (profile_nick, self.name, room_jid_s))
|
||||||
if user.entity is not None:
|
|
||||||
# check people identity
|
|
||||||
if user.entity.userhost() == player:
|
|
||||||
players.append(user.nick)
|
|
||||||
continue
|
|
||||||
else:
|
|
||||||
# TODO: how to check the identity with only a nickname??
|
|
||||||
if user.nick == JID(player).user:
|
|
||||||
players.append(user.nick)
|
|
||||||
if len(players) < len(expected_players):
|
|
||||||
# Someone here was not invited! He can stay but he doesn't play :p
|
|
||||||
return True
|
return True
|
||||||
# When we have all people in the room, we create the game
|
other_players = self.invitations[room_jid_s][batch][1]
|
||||||
del self.waiting_inv[_room_jid]
|
(auth, nicks, dummy) = self.checkWaitAuth(room, other_players)
|
||||||
self.createGame(_room_jid.userhost(), players, profile_key=profile)
|
if auth:
|
||||||
|
del self.invitations[room_jid_s][batch]
|
||||||
|
nicks.insert(0, profile_nick) # add the referee
|
||||||
|
self.createGame(room_jid_s, nicks, profile_key=profile)
|
||||||
|
return True
|
||||||
|
# let the room know that a new player joined
|
||||||
|
self.updatePlayers(room_jid_s, [user.nick], profile)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def createGame(self, room_jid, players=[], profile_key='@NONE@'):
|
def userLeftTrigger(self, room, user, profile):
|
||||||
"""Create a new game
|
"""This trigger is used to update or stop the game when a user leaves.
|
||||||
@param room_jid: jid of the room
|
@room: wokkel.muc.Room object. room.roster is a dict{wokkel.muc.User.nick: wokkel.muc.User}
|
||||||
@param players: list of players nick (nick must exist in the room)
|
@user: wokkel.muc.User object. user.nick is a unicode and user.entity a JID
|
||||||
|
@return: True to not interrupt the main process.
|
||||||
|
"""
|
||||||
|
room_jid_s = room.occupantJID.userhost()
|
||||||
|
profile_nick = room.occupantJID.resource
|
||||||
|
if not self.isReferee(room_jid_s, profile_nick):
|
||||||
|
return True # profile is not the referee
|
||||||
|
if self.isPlayer(room_jid_s, user.nick):
|
||||||
|
try:
|
||||||
|
self.games[room_jid_s]['players'].remove(user.nick)
|
||||||
|
except ValueError:
|
||||||
|
pass
|
||||||
|
if self.wait_mode == self.FOR_ALL:
|
||||||
|
# allow this user to join the game again
|
||||||
|
user_jid = user.entity.userhost()
|
||||||
|
if len(self.invitations[room_jid_s]) == 0:
|
||||||
|
self.invitations[room_jid_s].append((time(), [user_jid]))
|
||||||
|
else:
|
||||||
|
batch = 0 # add to the first batch of invitations
|
||||||
|
if user_jid not in self.invitations[room_jid_s][batch][1]:
|
||||||
|
self.invitations[room_jid_s][batch][1].append(user_jid)
|
||||||
|
return True
|
||||||
|
|
||||||
|
def checkCreateGameAndInit(self, room_jid_s, profile):
|
||||||
|
"""Check if that profile can create the game. If the game can be created
|
||||||
|
but is not initialized yet, this method will also do the initialization
|
||||||
|
@return: a couple (create, sync) with:
|
||||||
|
- create: set to True to allow the game creation
|
||||||
|
- sync: set to True to advice a game synchronization
|
||||||
|
"""
|
||||||
|
user_nick = self.host.plugins["XEP-0045"].getRoomNick(room_jid_s, profile)
|
||||||
|
if not user_nick:
|
||||||
|
error('Internal error')
|
||||||
|
return False, False
|
||||||
|
if self.gameExists(room_jid_s):
|
||||||
|
referee = self.isReferee(room_jid_s, user_nick)
|
||||||
|
if self.gameExists(room_jid_s, started=True):
|
||||||
|
warning(_("%s game already created in room %s") % (self.name, room_jid_s))
|
||||||
|
return False, referee
|
||||||
|
elif not referee:
|
||||||
|
warning(_("%s game in room %s can only be created by %s") % (self.name, room_jid_s, user_nick))
|
||||||
|
return False, False
|
||||||
|
else:
|
||||||
|
self.initGame(room_jid_s, user_nick)
|
||||||
|
return True, False
|
||||||
|
|
||||||
|
def createGame(self, room_jid_s, nicks=[], profile_key='@NONE@'):
|
||||||
|
"""Create a new game - this can be called directly from a frontend
|
||||||
|
and skips all the checks and invitation system, but the game must
|
||||||
|
not exist and all the players must be in the room already.
|
||||||
|
@param room_jid: JID userhost of the room
|
||||||
|
@param nicks: list of players nicks in the room
|
||||||
@param profile_key: %(doc_profile_key)s"""
|
@param profile_key: %(doc_profile_key)s"""
|
||||||
debug(_("Creating %s game") % self.name)
|
debug(_("Creating %s game in room %s") % (self.name, room_jid_s))
|
||||||
room = JID(room_jid).userhost()
|
|
||||||
profile = self.host.memory.getProfileName(profile_key)
|
profile = self.host.memory.getProfileName(profile_key)
|
||||||
if not profile:
|
if not profile:
|
||||||
error(_("profile %s is unknown") % profile_key)
|
error(_("profile %s is unknown") % profile_key)
|
||||||
return
|
return
|
||||||
room_nick = self.host.plugins["XEP-0045"].getRoomNick(room, profile)
|
(create, sync) = self.checkCreateGameAndInit(room_jid_s, profile)
|
||||||
if not room_nick:
|
if not create:
|
||||||
error('Internal error')
|
if sync:
|
||||||
|
debug(_('Synchronize game %s in %s for %s') % (self.name, room_jid_s, ', '.join(nicks)))
|
||||||
|
# TODO: we should call a method to re-send the information to a player who left
|
||||||
|
# and joined the room again, currently: we may restart a whole new round...
|
||||||
|
self.updatePlayers(room_jid_s, nicks, profile)
|
||||||
return
|
return
|
||||||
referee = room + '/' + room_nick
|
self.games[room_jid_s]['started'] = True
|
||||||
if room in self.games:
|
self.updatePlayers(room_jid_s, nicks, profile)
|
||||||
warning(_("%s game already started in room %s") % (self.name, room))
|
if self.player_init == {}:
|
||||||
return
|
return
|
||||||
self.games[room] = {'referee': referee}
|
# specific data to each player
|
||||||
self.games[room].update(self.options)
|
|
||||||
if self.collectiveGame is True:
|
|
||||||
self.send(JID(room), self.createStartElement(), profile=profile)
|
|
||||||
return
|
|
||||||
# non collaborative game = individual data and messages
|
|
||||||
status = {}
|
status = {}
|
||||||
players_data = {}
|
players_data = {}
|
||||||
for player in players:
|
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[player] = self.player_init_data.copy()
|
players_data[nick] = self.player_init.copy()
|
||||||
status[player] = "init"
|
status[nick] = "init"
|
||||||
# each player send a message to all the others
|
self.games[room_jid_s].update({'status': status, 'players_data': players_data})
|
||||||
self.send(JID(room + '/' + player), self.createStartElement(players), profile=profile)
|
|
||||||
# specific data to each player
|
|
||||||
self.games[room].update({'players': players, 'status': status, 'players_data': players_data})
|
|
||||||
|
|
||||||
def createCollectiveGame(self, room_jid, profile_key='@NONE@'):
|
|
||||||
return self.createGame(self, room_jid, players=[], profile_key=profile_key)
|
|
||||||
|
|
||||||
def playerReady(self, player, referee, profile_key='@NONE@'):
|
def playerReady(self, player, referee, profile_key='@NONE@'):
|
||||||
"""Must be called when player is ready to start a new game"""
|
"""Must be called when player is ready to start a new game"""
|
||||||
|
|
@ -207,9 +440,11 @@ class RoomGame(object):
|
||||||
elt.addElement(self.ns_tag)
|
elt.addElement(self.ns_tag)
|
||||||
return elt
|
return elt
|
||||||
|
|
||||||
def createStartElement(self, players=None):
|
def createStartElement(self, players=None, name="started"):
|
||||||
"""Create a game "started" domish Element"""
|
"""Create a game "started" domish Element
|
||||||
started_elt = domish.Element((None, 'started'))
|
@param name: element name (default: "started").
|
||||||
|
"""
|
||||||
|
started_elt = domish.Element((None, name))
|
||||||
if players is None:
|
if players is None:
|
||||||
return started_elt
|
return started_elt
|
||||||
idx = 0
|
idx = 0
|
||||||
|
|
@ -246,3 +481,29 @@ class RoomGame(object):
|
||||||
if content is not None:
|
if content is not None:
|
||||||
elem.addContent(content)
|
elem.addContent(content)
|
||||||
self.host.profiles[profile].xmlstream.send(msg)
|
self.host.profiles[profile].xmlstream.send(msg)
|
||||||
|
|
||||||
|
if debugFile:
|
||||||
|
# From here you will see all the game messages
|
||||||
|
file_ = open("/tmp/test", "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):
|
||||||
|
return RoomGameHandler(self)
|
||||||
|
|
||||||
|
|
||||||
|
class RoomGameHandler (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(self.plugin_parent.request, self.plugin_parent.room_game_cmd, profile=self.parent.profile)
|
||||||
|
|
||||||
|
def getDiscoInfo(self, requestor, target, nodeIdentifier=''):
|
||||||
|
return [disco.DiscoFeature(self.plugin_parent.ns_tag[0])]
|
||||||
|
|
||||||
|
def getDiscoItems(self, requestor, target, nodeIdentifier=''):
|
||||||
|
return []
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue