Mercurial > libervia-backend
annotate frontends/wix/card_game.py @ 95:be206a3d1a9b
Tarot game: score calculation
- plugin tarot: score validity is now checked (some of attackers and defender must equal 91)
- plugin tarot: new signal tarotGameScore to give the scores and winners/loosers
- wix: score are now displayed at the end of the game
- xml_tools: 'fixed' type is now managed in dataForm2xml
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 02 Jun 2010 15:57:23 +0930 |
parents | 1eb5ccead43c |
children | 01d7bd6f0e86 |
rev | line source |
---|---|
81 | 1 #!/usr/bin/python |
2 # -*- coding: utf-8 -*- | |
3 | |
4 """ | |
5 wix: 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 | |
23 | |
24 import wx | |
25 import os.path, glob | |
26 import pdb | |
27 from logging import debug, info, error | |
28 from tools.jid import JID | |
91 | 29 from form import Form |
81 | 30 |
83 | 31 CARD_WIDTH = 74 |
32 CARD_HEIGHT = 136 | |
86
4b5f2d55b6ac
wix: Tarot panel now appear on top of groupchat window when a Tarot game is started
Goffi <goffi@goffi.org>
parents:
83
diff
changeset
|
33 MIN_WIDTH = 950 #Minimum size of the panel |
4b5f2d55b6ac
wix: Tarot panel now appear on top of groupchat window when a Tarot game is started
Goffi <goffi@goffi.org>
parents:
83
diff
changeset
|
34 MIN_HEIGHT = 500 |
83 | 35 |
94 | 36 suits_order = ['pique', 'coeur', 'trefle', 'carreau', 'atout'] #I have swith the usual order 'trefle' and 'carreau' because card are more easy to see if suit colour change (black, red, black, red) |
87 | 37 values_order = map(str,range(1,11))+["valet","cavalier","dame","roi"] |
38 | |
81 | 39 class Card(): |
40 """This class is used to represent a card, graphically and logically""" | |
41 | |
42 def __init__(self, file): | |
43 """@param file: path of the PNG file""" | |
44 self.bitmap = wx.Image(file).ConvertToBitmap() | |
45 root_name = os.path.splitext(os.path.basename(file))[0] | |
92 | 46 self.suit,self.value=root_name.split('_') |
47 #gof: self.bout = True if self.suit=="atout" and self.value in ["1","21","excuse"] else False | |
81 | 48 |
92 | 49 print "Carte:",self.suit, self.value #, self.bout |
81 | 50 |
87 | 51 def __cmp__(self, other): |
52 if other == None: | |
53 return 1 | |
92 | 54 if self.suit != other.suit: |
55 idx1 = suits_order.index(self.suit) | |
56 idx2 = suits_order.index(other.suit) | |
87 | 57 return idx1.__cmp__(idx2) |
92 | 58 if self.suit == 'atout': |
87 | 59 if self.value == other.value == 'excuse': |
60 return 0 | |
61 if self.value == 'excuse': | |
62 return -1 | |
63 if other.value == 'excuse': | |
64 return 1 | |
65 return int(self.value).__cmp__(int(other.value)) | |
92 | 66 #at this point we have the same suit which is not 'atout' |
87 | 67 idx1 = values_order.index(self.value) |
68 idx2 = values_order.index(other.value) | |
69 return idx1.__cmp__(idx2) | |
70 | |
71 def __str__(self): | |
92 | 72 return "[%s,%s]" % (self.suit, self.value) |
83 | 73 |
81 | 74 def draw(self, dc, x, y): |
75 """Draw the card on the device context | |
76 @param dc: device context | |
77 @param x: abscissa | |
78 @param y: ordinate""" | |
79 dc.DrawBitmap(self.bitmap, x, y, True) | |
80 | |
81 | |
82 class CardPanel(wx.Panel): | |
83 """This class is used to display the cards""" | |
84 | |
92 | 85 def __init__(self, parent, referee, players, player_nick): |
81 | 86 wx.Panel.__init__(self, parent) |
90 | 87 self.parent = parent |
88 self.referee = referee | |
87 | 89 self.players = players |
93 | 90 self.played = {} |
91 for player in players: | |
92 self.played[player] = None | |
92 | 93 self.player_nick = player_nick |
94 self.bottom_nick = self.player_nick | |
95 idx = self.players.index(self.player_nick) | |
87 | 96 idx = (idx + 1) % len(self.players) |
97 self.right_nick = self.players[idx] | |
98 idx = (idx + 1) % len(self.players) | |
99 self.top_nick = self.players[idx] | |
100 idx = (idx + 1) % len(self.players) | |
101 self.left_nick = self.players[idx] | |
93 | 102 self.bottom_nick = player_nick |
86
4b5f2d55b6ac
wix: Tarot panel now appear on top of groupchat window when a Tarot game is started
Goffi <goffi@goffi.org>
parents:
83
diff
changeset
|
103 self.SetMinSize(wx.Size(MIN_WIDTH, MIN_HEIGHT)) |
83 | 104 self.load_cards("/home/goffi/dev/divers/images/cards/") |
92 | 105 self.mouse_over_card = None #contain the card to highlight |
106 self.selected = [] #Card choosed by the player (e.g. during ecart) | |
83 | 107 self.hand_size = 13 #number of cards in a hand |
108 self.visible_size = CARD_WIDTH/2 #number of pixels visible for cards | |
87 | 109 self.hand = [] |
92 | 110 self.to_show = [] |
111 self.state = None | |
81 | 112 self.SetBackgroundColour(wx.GREEN) |
83 | 113 self.Bind(wx.EVT_SIZE, self.onResize) |
81 | 114 self.Bind(wx.EVT_PAINT, self.onPaint) |
83 | 115 self.Bind(wx.EVT_MOTION, self.onMouseMove) |
116 self.Bind(wx.EVT_LEFT_UP, self.onMouseClick) | |
92 | 117 self.parent.host.bridge.tarotGameReady(player_nick, referee, profile_key = self.parent.host.profile) |
81 | 118 |
119 def load_cards(self, dir): | |
120 """Load all the cards in memory | |
121 @param dir: directory where the PNG files are""" | |
122 self.cards={} | |
123 self.deck=[] | |
124 self.cards["atout"]={} #As Tarot is a french game, it's more handy & logical to keep french names | |
125 self.cards["pique"]={} #spade | |
126 self.cards["coeur"]={} #heart | |
127 self.cards["carreau"]={} #diamond | |
128 self.cards["trefle"]={} #club | |
129 for file in glob.glob(dir+'/*_*.png'): | |
130 card = Card(file) | |
92 | 131 self.cards[card.suit, card.value]=card |
81 | 132 self.deck.append(card) |
133 """for value in map(str,range(1,22))+['excuse']: | |
134 self.idx_cards.append(self.cards["atout",value]) | |
92 | 135 for suit in ["pique", "coeur", "carreau", "trefle"]: |
81 | 136 for value in map(str,range(1,11))+["valet","cavalier","dame","roi"]: |
92 | 137 self.idx_cards.append(self.cards[suit, value])""" #XXX: no need to sort the cards ! |
81 | 138 |
87 | 139 def newGame(self, hand): |
140 """Start a new game, with given hand""" | |
141 assert (len(self.hand) == 0) | |
92 | 142 for suit, value in hand: |
143 self.hand.append(self.cards[suit, value]) | |
87 | 144 self.hand.sort() |
92 | 145 self.state = "init" |
146 self._recalc_ori() | |
147 self.Refresh() | |
87 | 148 |
91 | 149 def contratSelected(self, data): |
150 """Called when the contrat has been choosed | |
151 @param data: form result""" | |
152 debug (_("Contrat choosed")) | |
153 contrat = data[0][1] | |
92 | 154 self.parent.host.bridge.tarotGameContratChoosed(self.player_nick, self.referee, contrat or 'Passe', self.parent.host.profile) |
91 | 155 |
156 def chooseContrat(self, xml_data): | |
157 """Called when the player as to select hist contrat | |
158 @param xml_data: SàT xml representation of the form""" | |
159 misc = {'callback': self.contratSelected} | |
160 form = Form(self.parent.host, xml_data, title = _('Please choose your contrat'), options = ['NO_CANCEL'], misc = misc) | |
161 | |
92 | 162 def showCards(self, game_stage, cards, data): |
163 """Display cards in the middle of the game (to show for e.g. chien ou poignée)""" | |
164 self.to_show = [] | |
165 for suit, value in cards: | |
166 self.to_show.append(self.cards[suit, value]) | |
167 if game_stage == "chien" and data['attaquant'] == self.player_nick: | |
168 self.state = "wait_for_ecart" | |
169 else: | |
170 self.state = "chien" | |
171 | |
172 def MyTurn(self): | |
173 """Called when we have to play :)""" | |
174 if self.state == "chien": | |
175 self.to_show = [] | |
93 | 176 self.state = "play" |
95 | 177 |
178 #tmp gof: FIXME | |
179 card = self.hand[-1] | |
180 self.parent.host.bridge.tarotGamePlayCards(self.player_nick, self.referee, [(card.suit, card.value)], profile_key = self.parent.host.profile) | |
181 del self.hand[-1] | |
182 self.state = "wait" | |
183 self._recalc_ori() | |
184 self.Refresh() | |
93 | 185 |
95 | 186 def showScores(self, xml_data, winners, loosers): |
187 """Called when the player as to select hist contrat | |
188 @param xml_data: SàT xml representation of the form""" | |
189 form = Form(self.parent.host, xml_data, title = _('You win \o/') if self.player_nick in winners else _('You loose :('), options = ['NO_CANCEL']) | |
190 | |
93 | 191 def cardsPlayed(self, player, cards): |
192 """A card has been played by player""" | |
193 if self.to_show: | |
194 self.to_show = [] | |
195 pl_cards = [] | |
196 if self.played[player] != None: #gof: à supprimer | |
197 for pl in self.played: | |
198 self.played[pl] = None | |
199 for suit, value in cards: | |
200 pl_cards.append(self.cards[suit, value]) | |
201 self.played[player] = pl_cards[0] | |
202 self.Refresh() | |
203 | |
91 | 204 |
83 | 205 def _is_on_hand(self, pos_x, pos_y): |
206 """Return True if the coordinate are on the hand cards""" | |
207 if pos_x > self.orig_x and pos_y > self.orig_y \ | |
208 and pos_x < self.orig_x + (len(self.hand)+1) * self.visible_size \ | |
209 and pos_y < self.end_y: | |
210 return True | |
211 return False | |
212 | |
213 def onResize(self, event): | |
214 self._recalc_ori() | |
215 | |
216 def _recalc_ori(self): | |
92 | 217 """Recalculate origins of hand, must be call when hand size change""" |
83 | 218 self.orig_x = (self.GetSizeTuple()[0]-(len(self.hand)+1)*self.visible_size)/2 #where we start to draw cards |
219 self.orig_y = self.GetSizeTuple()[1] - CARD_HEIGHT - 20 | |
220 self.end_y = self.orig_y + CARD_HEIGHT | |
221 | |
81 | 222 def onPaint(self, event): |
223 dc = wx.PaintDC(self) | |
87 | 224 |
225 #We print the names to know who play where TODO: print avatars when available | |
226 max_x, max_y = self.GetSize() | |
227 border = 10 #border between nick and end of panel | |
228 right_y = left_y = 200 | |
229 right_width, right_height = dc.GetTextExtent(self.right_nick) | |
230 right_x = max_x - right_width - border | |
231 left_x = border | |
232 top_width, top_height = dc.GetTextExtent(self.top_nick) | |
233 top_x = (max_x - top_width) / 2 | |
234 top_y = border | |
235 dc.DrawText(self.right_nick, right_x, right_y) | |
236 dc.DrawText(self.top_nick, top_x, top_y) | |
237 dc.DrawText(self.left_nick, left_x, left_y) | |
238 | |
93 | 239 #We draw the played cards: |
240 center_y = 200 #ordinate used as center point | |
241 left_x = (max_x - CARD_WIDTH)/2 - CARD_WIDTH - 5 | |
242 right_x = (max_x/2) + (CARD_WIDTH/2) + 5 | |
243 left_y = right_y = center_y - CARD_HEIGHT/2 | |
244 top_x = bottom_x = (max_x - CARD_WIDTH)/2 | |
245 top_y = center_y - CARD_HEIGHT - 5 | |
246 bottom_y = center_y + 5 | |
247 for side in ['left', 'top', 'right', 'bottom']: | |
248 card = self.played[getattr(self, side+'_nick')] | |
249 if card != None: | |
250 card.draw(dc,locals()[side+'_x'], locals()[side+'_y']) | |
251 | |
83 | 252 x=self.orig_x |
253 for card in self.hand: | |
92 | 254 if (self.state == "play" or self.state == "ecart") and card == self.mouse_over_card \ |
255 or self.state == "ecart" and card in self.selected: | |
256 y = self.orig_y - 30 | |
257 else: | |
258 y = self.orig_y | |
259 | |
260 card.draw(dc,x,y) | |
83 | 261 x+=self.visible_size |
262 | |
92 | 263 if self.to_show: |
264 """There are cards to display in the middle""" | |
265 size = len(self.to_show)*(CARD_WIDTH+10)-10 | |
266 x = (max_x - size)/2 | |
267 for card in self.to_show: | |
268 card.draw(dc, x, 150) | |
269 x+=CARD_WIDTH+10 | |
270 | |
83 | 271 def onMouseMove(self, event): |
272 pos_x,pos_y = event.GetPosition() | |
273 if self._is_on_hand(pos_x, pos_y): | |
274 try: | |
92 | 275 self.mouse_over_card = self.hand[(pos_x-self.orig_x)/self.visible_size] |
83 | 276 except IndexError: |
92 | 277 self.mouse_over_card = self.hand[-1] |
83 | 278 self.Refresh() |
279 else: | |
92 | 280 self.mouse_over_card = None |
83 | 281 self.Refresh() |
282 | |
283 def onMouseClick(self, event): | |
284 print "mouse click:",event.GetPosition() | |
285 pos_x,pos_y = event.GetPosition() | |
92 | 286 |
287 if self.state == "chien": | |
288 self.to_show = [] | |
289 self.state = "wait" | |
290 return | |
291 elif self.state == "wait_for_ecart": | |
292 self.state = "ecart" | |
293 self.hand.extend(self.to_show) | |
294 self.hand.sort() | |
295 self.to_show = [] | |
296 self._recalc_ori() | |
297 self.Refresh() | |
298 return | |
299 | |
83 | 300 if self._is_on_hand(pos_x, pos_y): |
301 idx = (pos_x-self.orig_x)/self.visible_size | |
302 if idx == len(self.hand): | |
303 idx-=1 | |
92 | 304 if self.hand[idx] == self.mouse_over_card: |
305 if self.state == "ecart": | |
306 if self.hand[idx] in self.selected: | |
307 self.selected.remove(self.hand[idx]) | |
308 else: | |
309 self.selected.append(self.hand[idx]) | |
310 if len(self.selected) == 6: #TODO: use variable here, as chien len can change with variants | |
311 dlg = wx.MessageDialog(self, _("Do you put these cards in chien ?"), _(u"Écart"), wx.YES_NO | wx.ICON_QUESTION) | |
312 answer = dlg.ShowModal() | |
313 if answer == wx.ID_YES: | |
314 ecart = [] | |
315 for card in self.selected: | |
316 ecart.append((card.suit, card.value)) | |
317 self.hand.remove(card) | |
318 print "gof: Cartes envoyes au chien:", ecart | |
319 del self.selected[:] | |
320 self.parent.host.bridge.tarotGamePlayCards(self.player_nick, self.referee, ecart, profile_key = self.parent.host.profile) | |
321 self.state = "wait" | |
322 | |
323 self._recalc_ori() | |
324 self.Refresh() | |
93 | 325 if self.state == "play": |
326 card = self.hand[idx] | |
327 self.parent.host.bridge.tarotGamePlayCards(self.player_nick, self.referee, [(card.suit, card.value)], profile_key = self.parent.host.profile) | |
328 del self.hand[idx] | |
329 self.state = "wait" | |
330 self._recalc_ori() | |
331 self.Refresh() | |
332 | |
333 |