diff plugins/plugin_misc_tarot.py @ 93:2f87651a5ad8

Tarot game: basic trick - plugin xep-0045: new method getRoomNick - plugin tarot: basic trick - wix: played card are shown in CardPanel
author Goffi <goffi@goffi.org>
date Sun, 30 May 2010 15:33:08 +0930
parents 2503de7fb4c7
children 1eb5ccead43c
line wrap: on
line diff
--- a/plugins/plugin_misc_tarot.py	Sat May 29 20:53:03 2010 +0930
+++ b/plugins/plugin_misc_tarot.py	Sun May 30 15:33:08 2010 +0930
@@ -74,6 +74,7 @@
         host.bridge.addSignal("tarotGameNew", ".communication", signature='sa(ss)s') #args: room_jid, hand, profile
         host.bridge.addSignal("tarotGameChooseContrat", ".communication", signature='sss') #args: room_jid, xml_data, profile
         host.bridge.addSignal("tarotGameShowCards", ".communication", signature='ssa(ss)a{ss}s') #args: room_jid, type ["chien", "poignée",...], cards, data[dict], profile
+        host.bridge.addSignal("tarotGameCardsPlayed", ".communication", signature='ssa(ss)s') #args: room_jid, player, type ["chien", "poignée",...], cards, data[dict], profile
         host.bridge.addSignal("tarotGameYourTurn", ".communication", signature='ss') #args: room_jid, profile
         self.deck_ordered = []
         for value in ['excuse']+map(str,range(1,22)):
@@ -145,12 +146,17 @@
         if False: #gof: self.games.has_key(room_jid):
             warning (_("Tarot game already started in room %s") % room_jid.userhost())
         else:
+            room_nick = self.host.plugins["XEP_0045"].getRoomNick(room_jid.userhost(), profile)
+            if not room_nick:
+                error ('Internal error')
+                return
+            referee = room_jid.userhost() + '/' + room_nick
             status = {}
             players_data = {}
             for player in players:
                 players_data[player] = {}
                 status[player] = "init"
-            self.games[room_jid.userhost()] = {'players':players, 'status':status, 'players_data':players_data, 'hand_size':18, 'init_player':0, 'current_player': None, 'stage': None}
+            self.games[room_jid.userhost()] = {'referee':referee, 'players':players, 'status':status, 'players_data':players_data, 'hand_size':18, 'init_player':0, 'current_player': None, 'stage': None}
             for player in players:
                 mess = self.createGameElt(jid.JID(room_jid.userhost()+'/'+player))
                 mess.firstChildElement().addChild(self.__create_started_elt(players))
@@ -239,7 +245,8 @@
 
     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'])
+        from_jid = jid.JID(mess_elt['from']) 
+        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']
@@ -250,7 +257,7 @@
                 players = []
                 for player in elt.elements():
                     players.append(unicode(player))
-                self.host.bridge.tarotGameStarted(room_jid.userhost(), room_jid.full(), players, profile)
+                self.host.bridge.tarotGameStarted(room_jid.userhost(), from_jid.full(), players, profile)
             
             elif elt.name == 'player_ready': #ready to play
                 player = elt['player']
@@ -323,9 +330,34 @@
                     next_player = game_data['players'][next_player_idx]
                     to_jid = jid.JID(room_jid.userhost()+"/"+next_player) #FIXME: gof:
                     mess = self.createGameElt(to_jid)
-                    self.host.profiles[profile].xmlstream.send(mess)
                     yourturn_elt = mess.firstChildElement().addElement('your_turn')
                     self.host.profiles[profile].xmlstream.send(mess)
+                elif game_data['stage'] == "play":
+                    current_player = game_data['players'][game_data['current_player']]
+                    #assert (elt['player'] == current_player) #TODO: throw xml error here
+                    cards = self.__xml_to_list(elt)
+                    #TODO: check card validity and send error mess if necessary
+                    if mess_elt['type'] != 'groupchat':
+                        #the card played is ok, we forward it to everybody
+                        #first we remove it from the hand
+                        game_data['hand'][current_player].remove(cards[0])
+
+                        #then we forward the message
+                        mess = self.createGameElt(room_jid)
+                        playcard_elt = mess.firstChildElement().addChild(elt)
+                        self.host.profiles[profile].xmlstream.send(mess)
+                    
+                        #finally, we tell to the next player to play
+                        next_player = self.__next_player(game_data)
+                        to_jid = jid.JID(room_jid.userhost()+"/"+next_player) #FIXME: gof:
+                        mess = self.createGameElt(to_jid)
+                        yourturn_elt = mess.firstChildElement().addElement('your_turn')
+                        self.host.profiles[profile].xmlstream.send(mess)
+                    else:
+                        self.host.bridge.tarotGameCardsPlayed(room_jid.userhost(), elt['player'], self.__xml_to_list(elt), profile)
+
+
+                    
             
             elif elt.name == 'your_turn':
                 self.host.bridge.tarotGameYourTurn(room_jid.userhost(), profile)