changeset 328:809733b8d9be

Tarot game: - draw game managed - we can now play continuously \o/
author Goffi <goffi@goffi.org>
date Mon, 23 May 2011 00:46:51 +0200
parents 7c9784658163
children be9f682c53a5
files frontends/src/primitivus/card_game.py frontends/src/quick_frontend/quick_card_game.py frontends/src/wix/card_game.py src/plugins/plugin_misc_tarot.py
diffstat 4 files changed, 63 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/frontends/src/primitivus/card_game.py	Sun May 22 12:59:25 2011 +0200
+++ b/frontends/src/primitivus/card_game.py	Mon May 23 00:46:51 2011 +0200
@@ -274,7 +274,17 @@
     def showScores(self, xml_data, winners, loosers):
         """Called when the player as to select hist contrat
         @param xml_data: SàT xml representation of the form"""
-        form = XMLUI(self.parent.host, xml_data, title = _('You win \o/') if self.player_nick in winners else _('You loose :('), options = ['NO_CANCEL'])
+        def _new_game(ignore):
+            self.resetRound()
+            for location in ['top','left','bottom','right']:
+                self.table.putCard(location, None)
+            self.parent.host.redraw()
+            self.parent.host.bridge.tarotGameReady(self.player_nick, self.referee, profile_key = self.parent.host.profile)
+        if not winners and not loosers:
+            title = _("Draw game")
+        else:
+            title = _('You win \o/') if self.player_nick in winners else _('You loose :(')
+        form = XMLUI(self.parent.host, xml_data, title = title, options = ['NO_CANCEL'], misc={'callback':_new_game})
         form.show()
 
     def invalidCards(self, phase, played_cards, invalid_cards):
--- a/frontends/src/quick_frontend/quick_card_game.py	Sun May 22 12:59:25 2011 +0200
+++ b/frontends/src/quick_frontend/quick_card_game.py	Mon May 23 00:46:51 2011 +0200
@@ -27,7 +27,7 @@
 class QuickCardGame():
     
     def __init__(self, parent, referee, players, player_nick):
-        self._autoplay = None #XXX: use 0 to activate fake play, None else
+        self._autoplay = 0 #None #XXX: use 0 to activate fake play, None else
         self.parent = parent
         self.referee = referee
         self.players = players
@@ -50,6 +50,15 @@
         self.to_show = []
         self.state = None
 
+    def resetRound(self):
+        """Reset the game's variables to be reatty to start the next round"""
+        del self.selected[:]
+        del self.hand[:]
+        del self.to_show[:]
+        self.state = None
+        for pl in self.played:
+            self.played[pl] = None
+        
     def getPlayerLocation(self, nick):
         """return player location (top,bottom,left or right)"""
         for location in ['top','left','bottom','right']:
@@ -91,10 +100,10 @@
         self.to_show = []
         for suit, value in cards:
             self.to_show.append(self.cards[suit, value])
-            if game_stage == "chien" and data['attaquant'] == self.player_nick:
-                self.state = "wait_for_ecart"
-            else:
-                self.state = "chien"
+        if game_stage == "chien" and data['attaquant'] == self.player_nick:
+            self.state = "wait_for_ecart"
+        else:
+            self.state = "chien"
 
     def myTurn(self):
         """Called when we have to play :)"""
--- a/frontends/src/wix/card_game.py	Sun May 22 12:59:25 2011 +0200
+++ b/frontends/src/wix/card_game.py	Mon May 23 00:46:51 2011 +0200
@@ -105,7 +105,16 @@
     def showScores(self, xml_data, winners, loosers):
         """Called when the player as to select hist contrat
         @param xml_data: SàT xml representation of the form"""
-        form = XMLUI(self.parent.host, xml_data, title = _('You win \o/') if self.player_nick in winners else _('You loose :('), options = ['NO_CANCEL'])
+        def _new_game(ignore):
+            self.resetRound()
+            self.Refresh()
+            self.parent.host.bridge.tarotGameReady(self.player_nick, self.referee, profile_key = self.parent.host.profile)
+
+        if not winners and not loosers:
+            title = _("Draw game")
+        else:
+            title = _('You win \o/') if self.player_nick in winners else _('You loose :(')
+        form = XMLUI(self.parent.host, xml_data, title = title, options = ['NO_CANCEL'], misc={'callback':_new_game})
 
     def cardsPlayed(self, player, cards):
         """A card has been played by player"""
--- a/src/plugins/plugin_misc_tarot.py	Sun May 22 12:59:25 2011 +0200
+++ b/src/plugins/plugin_misc_tarot.py	Mon May 23 00:46:51 2011 +0200
@@ -205,7 +205,6 @@
         @param played: cards currently on the table
         @param winner: nick of the trick winner"""
         #TODO: manage the case where excuse is played on the last trick (and lost)
-        #TODO: gof: manage excuse (fool)
         players_data = game_data['players_data']
         excuse = TarotCard(("atout","excuse"))
 
@@ -260,6 +259,20 @@
             debug(_("%(excuse_owner)s keep the Excuse but has not card to give, %(winner)s is waiting for one") % {'excuse_owner':excuse_player, 'winner':winner})
 
 
+    def __draw_game(self, game_data):
+        """The game is draw, no score change
+        @param game_data: data of the game
+        @return: tuple with (string victory message, list of winners, list of loosers)"""
+        players_data = game_data['players_data']
+        scores_str = _('Draw game')
+        scores_str+='\n'
+        for player in game_data['players']:
+            scores_str+=_("\n--\n%(player)s:\nscore for this game ==> %(score_game)i\ntotal score ==> %(total_score)i") % {'player':player, 'score_game':0, 'total_score': players_data[player]['score']}
+        debug(scores_str)
+ 
+        return (scores_str, [], [])
+
+    
     def __calculate_scores(self, game_data):
         """The game is finished, time to know who won :)
         @param game_data: data of the game
@@ -547,7 +560,7 @@
         hand = game_data['hand'] = {}
         hand_size = game_data['hand_size']
         chien = game_data['chien'] = []
-        for i in range(4): #TODO: distribute according to real Tarot rules (3 by 3 counter-clockwise, 1 card at once to chien)
+        for i in range(4):
             hand[players[i]] = deck[0:hand_size]
             del deck[0:hand_size]
         chien.extend(deck)
@@ -617,7 +630,6 @@
                     mess.firstChildElement().addChild(self.__ask_contrat())
                     self.host.profiles[profile].xmlstream.send(mess)
                 else:
-                    #TODO: gof: manage "everybody pass" case
                     best_contrat = [None, "Passe"]
                     for player in game_data['players']:
                         contrat = players_data[player]['contrat']
@@ -626,6 +638,16 @@
                         if idx_pl > idx_best:
                             best_contrat[0] = player
                             best_contrat[1] = contrat
+                    if best_contrat[1] == "Passe":
+                        debug(_("Everybody is passing, round ended"))
+                        to_jid = jid.JID(room_jid.userhost())
+                        mess = self.createGameElt(to_jid)
+                        mess.firstChildElement().addChild(self.__give_scores(*self.__draw_game(game_data)))
+                        self.host.profiles[profile].xmlstream.send(mess)
+                        game_data['init_player'] = (game_data['init_player'] + 1) % len(game_data['players']) #we change the dealer
+                        for player in game_data['players']:
+                            game_data['status'][player] = "init"
+                        return
                     debug (_("%(player)s win the bid with %(contrat)s") % {'player':best_contrat[0],'contrat':best_contrat[1]})
                     game_data['contrat'] = best_contrat[1]
                     
@@ -718,6 +740,9 @@
                                 mess = self.createGameElt(to_jid)
                                 chien_elt = mess.firstChildElement().addChild(self.__give_scores(*self.__calculate_scores(game_data)))
                                 self.host.profiles[profile].xmlstream.send(mess)
+                                game_data['init_player'] = (game_data['init_player'] + 1) % len(game_data['players']) #we change the dealer
+                                for player in game_data['players']:
+                                    game_data['status'][player] = "init"
                                 return
                             #next player is the winner
                             next_player = game_data['first_player'] = self.__next_player(game_data, winner)