changeset 90:4020931569b8

Tarot Game: session initialization
author Goffi <goffi@goffi.org>
date Sun, 23 May 2010 16:39:05 +0930
parents 23caf1051099
children 39c672544593
files frontends/quick_frontend/quick_app.py frontends/sat_bridge_frontend/DBus.py frontends/wix/card_game.py frontends/wix/chat.py frontends/wix/main_window.py plugins/plugin_misc_tarot.py sat.tac
diffstat 7 files changed, 139 insertions(+), 47 deletions(-) [+]
line wrap: on
line diff
--- a/frontends/quick_frontend/quick_app.py	Thu May 13 16:27:48 2010 +0930
+++ b/frontends/quick_frontend/quick_app.py	Sun May 23 16:39:05 2010 +0930
@@ -248,20 +248,21 @@
             self.chat_wins[room_jid].setSubject(subject)
             debug (_("new subject for room [%(room_jid)s]: %(subject)s") % {'room_jid':room_jid, "subject":subject})
     
-    def tarotGameStarted(self, room_jid, players, profile):
+    def tarotGameStarted(self, room_jid, referee, players, profile):
         if not self.__check_profile(profile):
             return
         debug  (_("Tarot Game Started \o/"))
         if self.chat_wins.has_key(room_jid):
-            self.chat_wins[room_jid].startGame("Tarot", players)
-            debug (_("new Tarot game started in room [%(room_jid)s] with %(players)s") % {'room_jid':room_jid, 'players':[str(player) for player in players]})
+            self.chat_wins[room_jid].startGame("Tarot", referee, players)
+            debug (_("new Tarot game started by [%(referee)s] in room [%(room_jid)s] with %(players)s") % {'referee':referee, 'room_jid':room_jid, 'players':[str(player) for player in players]})
        
     def tarotGameNew(self, room_jid, hand, profile):
         if not self.__check_profile(profile):
             return
         debug (_("New Tarot Game"))
-        print "hand:", hand
+        print "room: %s, hand: %s" % (room_jid,hand)
         if self.chat_wins.has_key(room_jid):
+            print "gof:",self.chat_wins[room_jid].getGame("Tarot")
             self.chat_wins[room_jid].getGame("Tarot").newGame(hand)
 
 
--- a/frontends/sat_bridge_frontend/DBus.py	Thu May 13 16:27:48 2010 +0930
+++ b/frontends/sat_bridge_frontend/DBus.py	Sun May 23 16:39:05 2010 +0930
@@ -119,8 +119,11 @@
     def joinMUC(self, service, roomId, nick, profile_key='@DEFAULT@'):
         return self.db_comm_iface.joinMUC(service, roomId, nick, profile_key)
 
-    def createTarotGame(self, room_jid, players, profile_key='@DEFAULT@'):
-        return self.db_comm_iface.createTarotGame(room_jid, players, profile_key)
+    def tarotGameCreate(self, room_jid, players, profile_key='@DEFAULT@'):
+        return self.db_comm_iface.tarotGameCreate(room_jid, players, profile_key)
+
+    def tarotGameReady(self, user, referee, profile_key='@DEFAULT@'):
+        return self.db_comm_iface.tarotGameReady(user, referee, profile_key)
 
     def sendFile(self, to, path, profile_key='@DEFAULT@'):
         return self.db_comm_iface.sendFile(to, path, profile_key)
--- a/frontends/wix/card_game.py	Thu May 13 16:27:48 2010 +0930
+++ b/frontends/wix/card_game.py	Sun May 23 16:39:05 2010 +0930
@@ -83,8 +83,10 @@
 class CardPanel(wx.Panel):
     """This class is used to display the cards"""
 
-    def __init__(self, parent, players, user):
+    def __init__(self, parent, referee, players, user):
         wx.Panel.__init__(self, parent)
+        self.parent = parent
+        self.referee = referee
         self.players = players
         self.user = user
         self.bottom_nick = self.user
@@ -107,6 +109,7 @@
         self.Bind(wx.EVT_PAINT, self.onPaint)
         self.Bind(wx.EVT_MOTION, self.onMouseMove)
         self.Bind(wx.EVT_LEFT_UP, self.onMouseClick)
+        self.parent.host.bridge.tarotGameReady(user, referee, profile_key = self.parent.host.profile)
 
     def load_cards(self, dir):
         """Load all the cards in memory
@@ -130,6 +133,7 @@
 
     def newGame(self, hand):
         """Start a new game, with given hand"""
+        print "gof: new game ici avec",hand
         assert (len(self.hand) == 0)
         for family, value in hand:
             self.hand.append(self.cards[family, value])
@@ -198,21 +202,3 @@
                del self.hand[idx]
                self._recalc_ori()
                self.Refresh()
-
-class CardGame(wx.Frame, QuickChat):
-    """The window used to play all kind of card games"""
-
-    def __init__(self, host):
-        wx.Frame.__init__(self, None, pos=(0,0), size=(950,500))
-
-        self.host = host
-        
-        self.sizer = wx.BoxSizer(wx.VERTICAL)
-        self.panel = CardPanel(self)
-        self.sizer.Add(self.panel, 1, flag=wx.EXPAND)
-        self.SetSizer(self.sizer)
-        self.SetAutoLayout(True)
-       
-
-        #events
-
--- a/frontends/wix/chat.py	Thu May 13 16:27:48 2010 +0930
+++ b/frontends/wix/chat.py	Sun May 23 16:39:05 2010 +0930
@@ -108,11 +108,11 @@
             self.__createMenus_O2O()
             self.historyPrint(profile=self.host.profile)
     
-    def startGame(self, game_type, players):
+    def startGame(self, game_type, referee, players):
         """Configure the chat window to start a game"""
         if game_type=="Tarot":
             debug (_("configure chat window for Tarot game"))
-            self.tarot_panel = CardPanel(self, players, self.nick)
+            self.tarot_panel = CardPanel(self, referee, players, self.nick)
             self.sizer.Prepend(self.tarot_panel, 0, flag=wx.EXPAND)
             self.sizer.Layout()
             self.Fit()
@@ -248,4 +248,4 @@
             err_dlg = wx.MessageDialog(self, _("You need to be exactly 4 peoples in the room to start a Tarot game"), _("Can't start game"), style = wx.OK | wx.ICON_ERROR) #FIXME: gof: temporary only, need to choose the people with who the game has to be started
             err_dlg.ShowModal()
         else:
-            self.host.bridge.createTarotGame(self.id, list(self.occupants), self.host.profile)
+            self.host.bridge.tarotGameCreate(self.id, list(self.occupants), self.host.profile)
--- a/frontends/wix/main_window.py	Thu May 13 16:27:48 2010 +0930
+++ b/frontends/wix/main_window.py	Sun May 23 16:39:05 2010 +0930
@@ -26,7 +26,6 @@
 import wx
 from contact_list import ContactList
 from chat import Chat
-from card_game import CardGame
 from param import Param
 from form import Form
 from gateways import GatewaysManager
--- a/plugins/plugin_misc_tarot.py	Thu May 13 16:27:48 2010 +0930
+++ b/plugins/plugin_misc_tarot.py	Sun May 23 16:39:05 2010 +0930
@@ -42,6 +42,10 @@
 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",
@@ -50,7 +54,7 @@
 "protocols": [],
 "dependencies": ["XEP_0045"],
 "main": "Tarot",
-"handler": "no",
+"handler": "yes",
 "description": _("""Implementation of Tarot card game""")
 }
 
@@ -60,9 +64,10 @@
         info(_("Plugin Tarot initialization"))
         self.host = host
         self.games={}
-        host.bridge.addMethod("createTarotGame", ".communication", in_sign='sass', out_sign='', method=self.createGame)
-        host.bridge.addSignal("tarotGameStarted", ".communication", signature='sass') #args: room_id, players, profile
-        host.bridge.addSignal("tarotGameNew", ".communication", signature='sa(ss)s') #args: room_id, hand, profile
+        host.bridge.addMethod("tarotGameCreate", ".communication", in_sign='sass', out_sign='', method=self.createGame) #args: room_jid, players, profile
+        host.bridge.addMethod("tarotGameReady", ".communication", in_sign='sss', out_sign='', method=self.newPlayerReady) #args: user, referee, profile
+        host.bridge.addSignal("tarotGameStarted", ".communication", signature='ssass') #args: room_jid, referee, players, profile
+        host.bridge.addSignal("tarotGameNew", ".communication", signature='sa(ss)s') #args: room_jid, hand, profile
         self.deck_ordered = []
         for value in map(str,range(1,22))+['excuse']:
             self.deck_ordered.append(("atout",value))
@@ -70,19 +75,72 @@
             for value in map(str,range(1,11))+["valet","cavalier","dame","roi"]:
                 self.deck_ordered.append((family, value))
 
-    def createGame(self, room_jid, players, profile_key='@DEFAULT@'):
+    def createGameElt(self, to_jid):
+        elt = domish.Element(('jabber:client','message'))
+        elt["to"] = to_jid.full()
+        elt.addElement((NS_CG, CG_TAG))
+        return elt
+
+    def __hand_to_xml(self, hand):
+        """Convert a hand (list of tuples) to domish element"""
+        hand_elt = domish.Element(('','hand'))
+        for family, value in hand:
+            card_elt = domish.Element(('','card'))
+            card_elt['family'] = family
+            card_elt['value'] = value
+            hand_elt.addChild(card_elt) 
+        return hand_elt
+
+    def __xml_to_hand(self, hand_elt):
+        """Convert a hand domish element to a list of tuples"""
+        hand = []
+        assert (hand_elt.name == 'hand')
+        for card in hand_elt.elements():
+            hand.append((card['family'], card['value']))
+        return hand
+
+    def __create_started_elt(self, players):
+        """Create a game_started domish element"""
+        started_elt = domish.Element(('','started'))
+        idx = 0
+        for player in players:
+            player_elt = domish.Element(('','player'))
+            player_elt.addContent(player)
+            player_elt['index'] = str(idx)
+            idx+=1
+            started_elt.addChild(player_elt)
+        return started_elt
+
+    def createGame(self, room_jid_param, players, profile_key='@DEFAULT@'):
         """Create a new game"""
         debug (_("Creating Tarot game"))
+        room_jid = jid.JID(room_jid_param)
         profile = self.host.memory.getProfileName(profile_key)
         if not profile:
             error (_("profile %s is unknown") % profile_key)
             return
         if False: #gof: self.games.has_key(room_jid):
-            warning (_("Tarot game already started in room %s") % room_jid)
+            warning (_("Tarot game already started in room %s") % room_jid.userhost())
         else:
-            self.games[room_jid] = {'players':players, 'profile':profile, 'hand_size':18, 'player_start':0}
-            self.host.bridge.tarotGameStarted(room_jid, players, profile)
-            self.newGame(room_jid)
+            status = {}
+            for player in players:
+                status[player] = "init"
+            self.games[room_jid.userhost()] = {'players':players, 'status':status, 'profile':profile, 'hand_size':18, 'player_start':0}
+            for player in players:
+                mess = self.createGameElt(jid.JID(room_jid.userhost()+'/'+player))
+                mess.firstChildElement().addChild(self.__create_started_elt(players))
+                self.host.profiles[profile].xmlstream.send(mess)
+
+    def newPlayerReady(self, user, referee, profile_key='@DEFAULT@'):
+        """Must be called when player is ready to start a new game"""
+        profile = self.host.memory.getProfileName(profile_key)
+        if not profile:
+            error (_("profile %s is unknown") % profile_key)
+            return
+        debug ('new player ready: %s' % profile)
+        mess = self.createGameElt(jid.JID(referee))
+        mess.firstChildElement().addElement('player_ready', content=user)
+        self.host.profiles[profile].xmlstream.send(mess)
 
 
     def newGame(self, room_jid):
@@ -90,11 +148,11 @@
         debug (_('new Tarot game'))
         deck = self.deck_ordered[:]
         random.shuffle(deck)
-        profile = self.games[room_jid]['profile']
-        players = self.games[room_jid]['players']
-        hand = self.games[room_jid]['hand'] = {}
-        hand_size = self.games[room_jid]['hand_size']
-        chien = self.games[room_jid]['chien'] = []
+        profile = self.games[room_jid.userhost()]['profile']
+        players = self.games[room_jid.userhost()]['players']
+        hand = self.games[room_jid.userhost()]['hand'] = {}
+        hand_size = self.games[room_jid.userhost()]['hand_size']
+        chien = self.games[room_jid.userhost()]['chien'] = []
         for i in range(4): #TODO: distribute according to real Tarot rules (3 by 3 counter-clockwise, 1 card at once to chien)
             hand[players[i]] = deck[0:hand_size]
             del deck[0:hand_size]
@@ -102,8 +160,54 @@
         del(deck[:])
 
         for player in players:
-            self.host.sendMessage(room_jid+"/"+player, "/hand: %s" % str(hand[player]))
+            to_jid = jid.JID(room_jid.userhost()+"/"+player) #FIXME: gof:
+            mess = self.createGameElt(to_jid)
+            mess.firstChildElement().addChild(self.__hand_to_xml(hand[player]))
+            self.host.profiles[profile].xmlstream.send(mess)
+            
+
+    def card_game_cmd(self, mess_elt, profile):
+        print "\n\nCARD GAME command received (profile=%s): %s" % (profile, mess_elt.toXml())
+        room_jid = jid.JID(mess_elt['from'])
+        game_elt = mess_elt.firstChildElement()
+        for elt in game_elt.elements(): #new game created
+            if elt.name == 'started':
+                players = []
+                for player in elt.elements():
+                    players.append(unicode(player))
+                self.host.bridge.tarotGameStarted(room_jid.userhost(), room_jid.full(), players, profile)
+            elif elt.name == 'player_ready':
+                player = unicode(elt)
+                status = self.games[room_jid.userhost()]['status']
+                nb_players = len(self.games[room_jid.userhost()]['players'])
+                status[player] = 'ready'
+                debug (_('Player %(player)s is ready to start [status: %(status)s]') % {'player':player, 'status':status})
+                if status.values().count('ready') == 2: #gof: nb_players: #everybody is ready, we can start the game
+                    self.newGame(room_jid)
             
-        self.host.bridge.tarotGameNew(room_jid, hand[players[0]], profile)
+            elif elt.name == 'hand': #a new hand has been received
+                self.host.bridge.tarotGameNew(room_jid.userhost(), self.__xml_to_hand(elt), profile)
+            
+
+    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):
+        print "gof: ajout d'observer", CG_REQUEST
+        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_CB)]
+
+    def getDiscoItems(self, requestor, target, nodeIdentifier=''):
+        return []
+
--- a/sat.tac	Thu May 13 16:27:48 2010 +0930
+++ b/sat.tac	Sun May 23 16:39:05 2010 +0930
@@ -578,7 +578,6 @@
     ## jabber methods ##
     
     def sendMessage(self,to,msg,type='chat', profile_key='@DEFAULT@'):
-        print "sendtype=", type  #gof
         #FIXME: check validity of recipient
         profile = self.memory.getProfileName(profile_key)
         assert(profile)