diff src/plugins/plugin_misc_tarot.py @ 717:358018c5c398

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
author souliane <souliane@mailoo.org>
date Thu, 21 Nov 2013 15:49:53 +0100
parents ecc5a5b34ee1
children 074970227bc0
line wrap: on
line diff
--- a/src/plugins/plugin_misc_tarot.py	Thu Nov 21 15:38:53 2013 +0100
+++ b/src/plugins/plugin_misc_tarot.py	Thu Nov 21 15:49:53 2013 +0100
@@ -20,26 +20,17 @@
 from logging import debug, info, warning, error
 from twisted.words.xish import domish
 from twisted.words.protocols.jabber import jid
-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.frontends.games import TarotCard
 from sat.tools.plugins.games import RoomGame
 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'
 CG_TAG = 'card_game'
-CG_REQUEST = MESSAGE + '/' + CG_TAG + '[@xmlns="' + NS_CG + '"]'
 
 PLUGIN_INFO = {
     "name": "Tarot cards plugin",
@@ -57,14 +48,16 @@
 
     def __init__(self, host):
         info(_("Plugin Tarot initialization"))
-        RoomGame.__init__(self, host, PLUGIN_INFO, (NS_CG, CG_TAG), player_init_data={'score': 0},
-                          options={'hand_size': 18, 'init_player': 0, 'current_player': None, 'contrat': None, 'stage': None})
+        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')]
         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("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("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("tarotGameNew", ".plugin", signature='sa(ss)s')  # args: room_jid, hand, profile
         host.bridge.addSignal("tarotGameChooseContrat", ".plugin", signature='sss')  # args: room_jid, xml_data, profile
@@ -73,7 +66,6 @@
         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("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 = []
         for value in ['excuse'] + map(str, range(1, 22)):
             self.deck_ordered.append(TarotCard(("atout", value)))
@@ -443,7 +435,7 @@
         to_jid = jid.JID(room_jid.userhost() + "/" + player)  # FIXME: gof:
         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
         """
@@ -451,15 +443,17 @@
         room_jid = jid.JID(from_jid.userhost())
         game_elt = mess_elt.firstChildElement()
         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():
 
-            if elt.name == 'started':  # new game created
+            if elt.name == 'started' or elt.name == 'players':  # new game created
                 players = []
                 for player in elt.elements():
                     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
                 player = elt['player']
@@ -625,23 +619,3 @@
                     error(_('Unmanaged error type: %s') % elt['type'])
             else:
                 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 []