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