comparison frontends/src/quick_frontend/quick_card_game.py @ 223:86d249b6d9b7

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