comparison frontends/src/quick_frontend/quick_game_tarot.py @ 1360:8ea8fa13c351 frontends_multi_profiles

frontends (quick_frontend, primitivus): fixes room games: - add quick_frontend.quick_games for registering the signals and registering the UI classes - rename the signals handlers to fit the convention (e.g.: tarotGameScoreHandler) - rename card_game to game_tarot, quick_card_game to quick_game_tarot, CardGame to TarotGame
author souliane <souliane@mailoo.org>
date Wed, 11 Mar 2015 12:43:48 +0100
parents frontends/src/quick_frontend/quick_card_game.py@75025461141f
children 069ad98b360d
comparison
equal deleted inserted replaced
1359:83127a4c89ce 1360:8ea8fa13c351
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # helper class for making a SAT frontend
5 # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 Jérôme Poisson (goffi@goffi.org)
6
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
16
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20 from sat.core.log import getLogger
21 log = getLogger(__name__)
22 from sat_frontends.tools.jid import JID
23
24
25 class QuickTarotGame(object):
26
27 def __init__(self, parent, referee, players):
28 self._autoplay = None #XXX: use 0 to activate fake play, None else
29 self.parent = parent
30 self.referee = referee
31 self.players = players
32 self.played = {}
33 for player in players:
34 self.played[player] = None
35 self.player_nick = parent.nick
36 self.bottom_nick = unicode(self.player_nick)
37 idx = self.players.index(self.player_nick)
38 idx = (idx + 1) % len(self.players)
39 self.right_nick = unicode(self.players[idx])
40 idx = (idx + 1) % len(self.players)
41 self.top_nick = unicode(self.players[idx])
42 idx = (idx + 1) % len(self.players)
43 self.left_nick = unicode(self.players[idx])
44 self.bottom_nick = unicode(self.player_nick)
45 self.selected = [] #Card choosed by the player (e.g. during ecart)
46 self.hand_size = 13 #number of cards in a hand
47 self.hand = []
48 self.to_show = []
49 self.state = None
50
51 def resetRound(self):
52 """Reset the game's variables to be reatty to start the next round"""
53 del self.selected[:]
54 del self.hand[:]
55 del self.to_show[:]
56 self.state = None
57 for pl in self.played:
58 self.played[pl] = None
59
60 def getPlayerLocation(self, nick):
61 """return player location (top,bottom,left or right)"""
62 for location in ['top','left','bottom','right']:
63 if getattr(self,'%s_nick' % location) == nick:
64 return location
65 assert(False)
66
67 def loadCards(self):
68 """Load all the cards in memory
69 @param dir: directory where the PNG files are"""
70 self.cards={}
71 self.deck=[]
72 self.cards["atout"]={} #As Tarot is a french game, it's more handy & logical to keep french names
73 self.cards["pique"]={} #spade
74 self.cards["coeur"]={} #heart
75 self.cards["carreau"]={} #diamond
76 self.cards["trefle"]={} #club
77
78 def tarotGameNewHandler(self, hand):
79 """Start a new game, with given hand"""
80 assert (len(self.hand) == 0)
81 for suit, value in hand:
82 self.hand.append(self.cards[suit, value])
83 self.hand.sort()
84 self.state = "init"
85
86 def tarotGameChooseContratHandler(self, xml_data):
87 """Called when the player as to select his contrat
88 @param xml_data: SàT xml representation of the form"""
89 raise NotImplementedError
90
91 def tarotGameShowCardsHandler(self, game_stage, cards, data):
92 """Display cards in the middle of the game (to show for e.g. chien ou poignée)"""
93 self.to_show = []
94 for suit, value in cards:
95 self.to_show.append(self.cards[suit, value])
96 if game_stage == "chien" and data['attaquant'] == self.player_nick:
97 self.state = "wait_for_ecart"
98 else:
99 self.state = "chien"
100
101 def tarotGameYourTurnHandler(self):
102 """Called when we have to play :)"""
103 if self.state == "chien":
104 self.to_show = []
105 self.state = "play"
106 self.__fakePlay()
107
108 def __fakePlay(self):
109 """Convenience method for stupid autoplay
110 /!\ don't forgot to comment any interactive dialog for invalid card"""
111 if self._autoplay == None:
112 return
113 if self._autoplay >= len(self.hand):
114 self._autoplay = 0
115 card = self.hand[self._autoplay]
116 self.parent.host.bridge.tarotGamePlayCards(self.player_nick, self.referee, [(card.suit, card.value)], self.parent.profile)
117 del self.hand[self._autoplay]
118 self.state = "wait"
119 self._autoplay+=1
120
121 def tarotGameScoreHandler(self, xml_data, winners, loosers):
122 """Called at the end of a game
123 @param xml_data: SàT xml representation of the scores
124 @param winners: list of winners' nicks
125 @param loosers: list of loosers' nicks"""
126 raise NotImplementedError
127
128 def tarotGameCardsPlayedHandler(self, player, cards):
129 """A card has been played by player"""
130 if self.to_show:
131 self.to_show = []
132 pl_cards = []
133 if self.played[player] != None: #FIXME
134 for pl in self.played:
135 self.played[pl] = None
136 for suit, value in cards:
137 pl_cards.append(self.cards[suit, value])
138 self.played[player] = pl_cards[0]
139
140 def tarotGameInvalidCardsHandler(self, phase, played_cards, invalid_cards):
141 """Invalid cards have been played
142 @param phase: phase of the game
143 @param played_cards: all the cards played
144 @param invalid_cards: cards which are invalid"""
145
146 if phase == "play":
147 self.state = "play"
148 elif phase == "ecart":
149 self.state = "ecart"
150 else:
151 log.error ('INTERNAL ERROR: unmanaged game phase')
152
153 for suit, value in played_cards:
154 self.hand.append(self.cards[suit, value])
155
156 self.hand.sort()
157 self.__fakePlay()
158