From 674a3e958dc0f30efc3623101278b69b56dabb65 Mon Sep 17 00:00:00 2001 From: souliane Date: Thu, 21 Nov 2013 18:23:08 +0100 Subject: [PATCH] plugin tools: turn src/plugin/games.py into a plugin and move it to src/plugins/plugin_misc_room_game.py --- src/plugins/plugin_misc_quiz.py | 13 +++++--- src/plugins/plugin_misc_radiocol.py | 13 +++++--- .../plugin_misc_room_game.py} | 33 ++++++++++++------- src/plugins/plugin_misc_tarot.py | 13 +++++--- 4 files changed, 49 insertions(+), 23 deletions(-) rename src/{tools/plugins/games.py => plugins/plugin_misc_room_game.py} (97%) diff --git a/src/plugins/plugin_misc_quiz.py b/src/plugins/plugin_misc_quiz.py index 58d37678..b3601ce3 100755 --- a/src/plugins/plugin_misc_quiz.py +++ b/src/plugins/plugin_misc_quiz.py @@ -28,7 +28,6 @@ import random from wokkel import data_form from sat.tools.xml_tools import dataForm2XML from sat.tools.frontends.games import TarotCard -from sat.tools.plugins.games import RoomGame from time import time @@ -40,18 +39,24 @@ PLUGIN_INFO = { "import_name": "Quiz", "type": "Game", "protocols": [], - "dependencies": ["XEP-0045", "XEP-0249"], + "dependencies": ["XEP-0045", "XEP-0249", "ROOM-GAME"], "main": "Quiz", "handler": "yes", "description": _("""Implementation of Quiz game""") } -class Quiz(RoomGame): +class Quiz(object): + + def inheritFromRoomGame(self, host): + global RoomGame + RoomGame = host.plugins["ROOM-GAME"].__class__ + self.__class__ = type(self.__class__.__name__, (self.__class__, RoomGame, object), {}) def __init__(self, host): info(_("Plugin Quiz initialization")) - RoomGame.__init__(self, host, PLUGIN_INFO, (NS_QG, QG_TAG), game_init={'stage': None}, player_init={'score': 0}) + self.inheritFromRoomGame(host) + RoomGame._init_(self, host, PLUGIN_INFO, (NS_QG, QG_TAG), game_init={'stage': None}, player_init={'score': 0}) 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("quizGameReady", ".plugin", in_sign='sss', out_sign='', method=self.playerReady) # args: player, referee, profile diff --git a/src/plugins/plugin_misc_radiocol.py b/src/plugins/plugin_misc_radiocol.py index 5a495f1c..a023ba0d 100755 --- a/src/plugins/plugin_misc_radiocol.py +++ b/src/plugins/plugin_misc_radiocol.py @@ -25,7 +25,6 @@ from twisted.words.protocols.jabber import jid import os.path from os import unlink from mutagen.oggvorbis import OggVorbis, OggVorbisHeaderError -from sat.tools.plugins.games import RoomGame NC_RADIOCOL = 'http://www.goffi.org/protocol/radiocol' @@ -36,7 +35,7 @@ PLUGIN_INFO = { "import_name": "Radiocol", "type": "Exp", "protocols": [], - "dependencies": ["XEP-0045", "XEP-0249"], + "dependencies": ["XEP-0045", "XEP-0249", "ROOM-GAME"], "main": "Radiocol", "handler": "yes", "description": _("""Implementation of radio collective""") @@ -45,11 +44,17 @@ PLUGIN_INFO = { QUEUE_LIMIT = 2 -class Radiocol(RoomGame): +class Radiocol(object): + + def inheritFromRoomGame(self, host): + global RoomGame + RoomGame = host.plugins["ROOM-GAME"].__class__ + self.__class__ = type(self.__class__.__name__, (self.__class__, RoomGame, object), {}) def __init__(self, host): info(_("Radio collective initialization")) - RoomGame.__init__(self, host, PLUGIN_INFO, (NC_RADIOCOL, RADIOC_TAG), + self.inheritFromRoomGame(host) + RoomGame._init_(self, host, PLUGIN_INFO, (NC_RADIOCOL, RADIOC_TAG), 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) diff --git a/src/tools/plugins/games.py b/src/plugins/plugin_misc_room_game.py similarity index 97% rename from src/tools/plugins/games.py rename to src/plugins/plugin_misc_room_game.py index 2d818d18..69a16fb6 100644 --- a/src/tools/plugins/games.py +++ b/src/plugins/plugin_misc_room_game.py @@ -28,12 +28,20 @@ try: 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 +_DEBUG = False +_DEBUG_FILE = False + +PLUGIN_INFO = { + "name": "Room game", + "import_name": "ROOM-GAME", + "type": "MISC", + "protocols": [], + "dependencies": ["XEP-0045", "XEP-0249"], + "main": "RoomGame", + "handler": "no", # handler MUST be "no" (dynamic inheritance) + "description": _("""Base class for MUC games""") +} class RoomGame(object): @@ -51,7 +59,10 @@ class RoomGame(object): MESSAGE = '/message' REQUEST = '%s/%s[@xmlns="%s"]' - def __init__(self, host, plugin_info, ns_tag, game_init={}, player_init={}): + def __init__(self, host): + self.host = host + + def _init_(self, host, plugin_info, ns_tag, game_init={}, player_init={}): """ @param host @param plugin_info: PLUGIN_INFO map of the game plugin @@ -150,7 +161,7 @@ class RoomGame(object): auth = True break - if not auth and (verbose or debugMsg): + if not auth and (verbose or _DEBUG): debug(_("%s not allowed to join the game %s in %s") % (user_jid_s or nick, self.name, room_jid_s)) return auth @@ -208,7 +219,7 @@ class RoomGame(object): 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): + if not auth and (verbose or _DEBUG): debug(_("%s not allowed to invite for the game %s in %s") % (nick, self.name, room_jid_s)) return auth @@ -242,7 +253,7 @@ class RoomGame(object): # 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): + if not result[0] and (verbose or _DEBUG): debug(_("Still waiting for %s before starting the game %s in %s") % (result[2], self.name, room.occupantJID.userhost())) return result @@ -482,9 +493,9 @@ class RoomGame(object): elem.addContent(content) self.host.profiles[profile].xmlstream.send(msg) - if debugFile: + if _DEBUG_FILE: # From here you will see all the game messages - file_ = open("/tmp/test", "a") + 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() diff --git a/src/plugins/plugin_misc_tarot.py b/src/plugins/plugin_misc_tarot.py index 09081780..55ad043b 100755 --- a/src/plugins/plugin_misc_tarot.py +++ b/src/plugins/plugin_misc_tarot.py @@ -24,7 +24,6 @@ from wokkel import data_form from sat.tools.xml_tools import dataForm2XML from sat.tools.frontends.games import TarotCard -from sat.tools.plugins.games import RoomGame from time import time import random @@ -37,18 +36,24 @@ PLUGIN_INFO = { "import_name": "Tarot", "type": "Misc", "protocols": [], - "dependencies": ["XEP-0045", "XEP-0249"], + "dependencies": ["XEP-0045", "XEP-0249", "ROOM-GAME"], "main": "Tarot", "handler": "yes", "description": _("""Implementation of Tarot card game""") } -class Tarot(RoomGame): +class Tarot(object): + + def inheritFromRoomGame(self, host): + global RoomGame + RoomGame = host.plugins["ROOM-GAME"].__class__ + self.__class__ = type(self.__class__.__name__, (self.__class__, RoomGame, object), {}) def __init__(self, host): info(_("Plugin Tarot initialization")) - RoomGame.__init__(self, host, PLUGIN_INFO, (NS_CG, CG_TAG), + self.inheritFromRoomGame(host) + RoomGame._init_(self, host, PLUGIN_INFO, (NS_CG, CG_TAG), 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')]