changeset 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
files frontends/quick_frontend/quick_app.py frontends/wix/card_game.py plugins/plugin_misc_tarot.py plugins/plugin_xep_0045.py
diffstat 4 files changed, 97 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/frontends/quick_frontend/quick_app.py	Sat May 29 20:53:03 2010 +0930
+++ b/frontends/quick_frontend/quick_app.py	Sun May 30 15:33:08 2010 +0930
@@ -53,6 +53,7 @@
         self.bridge.register("tarotGameChooseContrat", self.tarotChooseContrat)
         self.bridge.register("tarotGameShowCards", self.tarotShowCards)
         self.bridge.register("tarotGameYourTurn", self.tarotMyTurn)
+        self.bridge.register("tarotGameCardsPlayed", self.tarotCardsPlayed)
         self.bridge.register("subscribe", self.subscribe)
         self.bridge.register("paramUpdate", self.paramUpdate)
         self.bridge.register("contactDeleted", self.contactDeleted)
@@ -306,7 +307,14 @@
         debug (_("My turn to play"))
         if self.chat_wins.has_key(room_jid):
             self.chat_wins[room_jid].getGame("Tarot").MyTurn()
-
+    
+    def tarotCardsPlayed(self, room_jid, player, cards, profile):
+        if not self.__check_profile(profile):
+            return
+        debug (_("Card(s) played (%(player)s): %(cards)s") % {"player":player, "cards":cards})
+        if self.chat_wins.has_key(room_jid):
+            self.chat_wins[room_jid].getGame("Tarot").cardsPlayed(player, cards)
+   
     def subscribe(self, type, raw_jid, profile):
         """Called when a subsciption management signal is received"""
         if not self.__check_profile(profile):
--- a/frontends/wix/card_game.py	Sat May 29 20:53:03 2010 +0930
+++ b/frontends/wix/card_game.py	Sun May 30 15:33:08 2010 +0930
@@ -87,6 +87,9 @@
         self.parent = parent
         self.referee = referee
         self.players = players
+        self.played = {}
+        for player in players:
+            self.played[player] = None
         self.player_nick = player_nick
         self.bottom_nick = self.player_nick
         idx = self.players.index(self.player_nick)
@@ -96,6 +99,7 @@
         self.top_nick = self.players[idx]
         idx = (idx + 1) % len(self.players)
         self.left_nick = self.players[idx]
+        self.bottom_nick = player_nick
         self.SetMinSize(wx.Size(MIN_WIDTH, MIN_HEIGHT))
         self.load_cards("/home/goffi/dev/divers/images/cards/")
         self.mouse_over_card = None #contain the card to highlight
@@ -169,7 +173,21 @@
         """Called when we have to play :)"""
         if self.state == "chien":
             self.to_show = []
-        self.state="play"
+        self.state = "play"
+    
+    def cardsPlayed(self, player, cards):
+        """A card has been played by player"""
+        if self.to_show:
+            self.to_show = []
+        pl_cards = []
+        if self.played[player] != None: #gof: à supprimer
+            for pl in self.played:
+                self.played[pl] = None
+        for suit, value in cards:
+            pl_cards.append(self.cards[suit, value])
+        self.played[player] = pl_cards[0]
+        self.Refresh()
+
 
     def _is_on_hand(self, pos_x, pos_y):
         """Return True if the coordinate are on the hand cards"""
@@ -205,6 +223,19 @@
         dc.DrawText(self.top_nick, top_x, top_y)
         dc.DrawText(self.left_nick, left_x, left_y)
 
+        #We draw the played cards:
+        center_y = 200 #ordinate used as center point
+        left_x = (max_x - CARD_WIDTH)/2 - CARD_WIDTH - 5
+        right_x = (max_x/2) + (CARD_WIDTH/2) + 5
+        left_y = right_y = center_y - CARD_HEIGHT/2
+        top_x = bottom_x = (max_x - CARD_WIDTH)/2
+        top_y = center_y - CARD_HEIGHT - 5
+        bottom_y = center_y + 5
+        for side in ['left', 'top', 'right', 'bottom']:
+            card = self.played[getattr(self, side+'_nick')]
+            if card != None:
+                card.draw(dc,locals()[side+'_x'], locals()[side+'_y'])
+
         x=self.orig_x
         for card in self.hand:
             if (self.state == "play" or self.state == "ecart") and card == self.mouse_over_card \
@@ -278,3 +309,12 @@
 
                    self._recalc_ori()
                    self.Refresh()
+               if self.state == "play":
+                   card = self.hand[idx]
+                   self.parent.host.bridge.tarotGamePlayCards(self.player_nick, self.referee, [(card.suit, card.value)], profile_key = self.parent.host.profile)
+                   del self.hand[idx]
+                   self.state = "wait"
+                   self._recalc_ori()
+                   self.Refresh()
+
+
--- 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)
--- a/plugins/plugin_xep_0045.py	Sat May 29 20:53:03 2010 +0930
+++ b/plugins/plugin_xep_0045.py	Sun May 30 15:33:08 2010 +0930
@@ -110,6 +110,17 @@
             result.append((room.roomIdentifier, room.service, [user.nick for user in room.roster.values()], room.nick))
         return result
 
+    def getRoomNick(self, room_jid, profile_key='@DEFAULT@'):
+        """return nick used in room by user
+        @param room_jid: unicode room id
+        @profile_key: profile
+        @return: nick or empty string in case of error"""
+        profile = self.host.memory.getProfileName(profile_key)
+        if not self.__check_profile(profile) or not self.clients[profile].joined_rooms.has_key(room_jid):
+            return ''
+        return self.clients[profile].joined_rooms[room_jid].nick
+
+
     def getRoomSubjects(self, profile_key='@DEFAULT@'):
         """Return received subjects of rooms"""
         profile = self.host.memory.getProfileName(profile_key)