Mercurial > libervia-backend
annotate frontends/src/wix/card_game.py @ 655:56f8a9c99194
core, primitivus: better support for parameter of type list
author | souliane <souliane@mailoo.org> |
---|---|
date | Wed, 02 Oct 2013 17:38:29 +0200 |
parents | 84a6e83157c2 |
children | 8281587eb528 |
rev | line source |
---|---|
81 | 1 #!/usr/bin/python |
2 # -*- coding: utf-8 -*- | |
3 | |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
587
diff
changeset
|
4 # wix: a SAT frontend |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
587
diff
changeset
|
5 # Copyright (C) 2009, 2010, 2011, 2012, 2013 Jérôme Poisson (goffi@goffi.org) |
81 | 6 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
587
diff
changeset
|
7 # This program is free software: you can redistribute it and/or modify |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
587
diff
changeset
|
8 # it under the terms of the GNU Affero General Public License as published by |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
587
diff
changeset
|
9 # the Free Software Foundation, either version 3 of the License, or |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
587
diff
changeset
|
10 # (at your option) any later version. |
81 | 11 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
587
diff
changeset
|
12 # This program is distributed in the hope that it will be useful, |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
587
diff
changeset
|
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
587
diff
changeset
|
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
587
diff
changeset
|
15 # GNU Affero General Public License for more details. |
81 | 16 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
587
diff
changeset
|
17 # You should have received a copy of the GNU Affero General Public License |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
587
diff
changeset
|
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
81 | 19 |
20 | |
21 | |
22 import wx | |
23 import os.path, glob | |
24 import pdb | |
25 from logging import debug, info, error | |
225
fd9b7834d98a
distutils installation script, draft
Goffi <goffi@goffi.org>
parents:
223
diff
changeset
|
26 from sat.tools.jid import JID |
227 | 27 from sat.tools.games import TarotCard |
28 from sat_frontends.quick_frontend.quick_card_game import QuickCardGame | |
29 from sat_frontends.wix.xmlui import XMLUI | |
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 |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
141
diff
changeset
|
36 class WxCard(TarotCard): |
81 | 37 """This class is used to represent a card, graphically and logically""" |
38 | |
39 def __init__(self, file): | |
40 """@param file: path of the PNG file""" | |
41 self.bitmap = wx.Image(file).ConvertToBitmap() | |
42 root_name = os.path.splitext(os.path.basename(file))[0] | |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
141
diff
changeset
|
43 suit,value = root_name.split('_') |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
141
diff
changeset
|
44 TarotCard.__init__(self, (suit, value)) |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
141
diff
changeset
|
45 print "Carte:",suit, value #, self.bout |
81 | 46 |
47 def draw(self, dc, x, y): | |
48 """Draw the card on the device context | |
49 @param dc: device context | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
50 @param x: abscissa |
81 | 51 @param y: ordinate""" |
52 dc.DrawBitmap(self.bitmap, x, y, True) | |
53 | |
54 | |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
141
diff
changeset
|
55 class CardPanel(QuickCardGame,wx.Panel): |
81 | 56 """This class is used to display the cards""" |
57 | |
92 | 58 def __init__(self, parent, referee, players, player_nick): |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
141
diff
changeset
|
59 QuickCardGame.__init__(self, parent, referee, players, player_nick) |
81 | 60 wx.Panel.__init__(self, parent) |
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
|
61 self.SetMinSize(wx.Size(MIN_WIDTH, MIN_HEIGHT)) |
366
0806a65a5fa9
wix: updated paths to use media_dir
Goffi <goffi@goffi.org>
parents:
328
diff
changeset
|
62 self.loadCards(os.path.join(self.parent.host.media_dir, 'games/cards/tarot')) |
92 | 63 self.mouse_over_card = None #contain the card to highlight |
83 | 64 self.visible_size = CARD_WIDTH/2 #number of pixels visible for cards |
87 | 65 self.hand = [] |
92 | 66 self.to_show = [] |
67 self.state = None | |
81 | 68 self.SetBackgroundColour(wx.GREEN) |
83 | 69 self.Bind(wx.EVT_SIZE, self.onResize) |
81 | 70 self.Bind(wx.EVT_PAINT, self.onPaint) |
83 | 71 self.Bind(wx.EVT_MOTION, self.onMouseMove) |
72 self.Bind(wx.EVT_LEFT_UP, self.onMouseClick) | |
92 | 73 self.parent.host.bridge.tarotGameReady(player_nick, referee, profile_key = self.parent.host.profile) |
81 | 74 |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
141
diff
changeset
|
75 def loadCards(self, dir): |
81 | 76 """Load all the cards in memory |
77 @param dir: directory where the PNG files are""" | |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
141
diff
changeset
|
78 QuickCardGame.loadCards(self) |
81 | 79 for file in glob.glob(dir+'/*_*.png'): |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
141
diff
changeset
|
80 card = WxCard(file) |
92 | 81 self.cards[card.suit, card.value]=card |
81 | 82 self.deck.append(card) |
83 | |
87 | 84 def newGame(self, hand): |
85 """Start a new game, with given hand""" | |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
141
diff
changeset
|
86 QuickCardGame.newGame(self, hand) |
92 | 87 self._recalc_ori() |
88 self.Refresh() | |
87 | 89 |
91 | 90 def contratSelected(self, data): |
91 """Called when the contrat has been choosed | |
92 @param data: form result""" | |
93 debug (_("Contrat choosed")) | |
94 contrat = data[0][1] | |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
141
diff
changeset
|
95 QuickCardGame.contratSelected(self, contrat) |
91 | 96 |
97 def chooseContrat(self, xml_data): | |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
141
diff
changeset
|
98 """Called when the player as to select his contrat |
91 | 99 @param xml_data: SàT xml representation of the form""" |
100 misc = {'callback': self.contratSelected} | |
133
d998adb62d1a
wix: fixed wrong class Name in card_game
Goffi <goffi@goffi.org>
parents:
103
diff
changeset
|
101 form = XMLUI(self.parent.host, xml_data, title = _('Please choose your contrat'), options = ['NO_CANCEL'], misc = misc) |
91 | 102 |
95 | 103 def showScores(self, xml_data, winners, loosers): |
104 """Called when the player as to select hist contrat | |
105 @param xml_data: SàT xml representation of the form""" | |
328 | 106 def _new_game(ignore): |
107 self.resetRound() | |
108 self.Refresh() | |
109 self.parent.host.bridge.tarotGameReady(self.player_nick, self.referee, profile_key = self.parent.host.profile) | |
110 | |
111 if not winners and not loosers: | |
112 title = _("Draw game") | |
113 else: | |
114 title = _('You win \o/') if self.player_nick in winners else _('You loose :(') | |
115 form = XMLUI(self.parent.host, xml_data, title = title, options = ['NO_CANCEL'], misc={'callback':_new_game}) | |
95 | 116 |
93 | 117 def cardsPlayed(self, player, cards): |
118 """A card has been played by player""" | |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
141
diff
changeset
|
119 QuickCardGame.cardsPlayed(self, player, cards) |
93 | 120 self.Refresh() |
121 | |
99
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
97
diff
changeset
|
122 def invalidCards(self, phase, played_cards, invalid_cards): |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
97
diff
changeset
|
123 """Invalid cards have been played |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
97
diff
changeset
|
124 @param phase: phase of the game |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
97
diff
changeset
|
125 @param played_cards: all the cards played |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
97
diff
changeset
|
126 @param invalid_cards: cards which are invalid""" |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
141
diff
changeset
|
127 QuickCardGame.invalidCards(self, phase, played_cards, invalid_cards) |
99
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
97
diff
changeset
|
128 |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
97
diff
changeset
|
129 self._recalc_ori() |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
97
diff
changeset
|
130 self.Refresh() |
162 | 131 if self._autoplay==None: #No dialog if there is autoplay |
132 wx.MessageDialog(self, _("Cards played are invalid !"), _("Error"), wx.OK | wx.ICON_ERROR).ShowModal() | |
99
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
97
diff
changeset
|
133 |
83 | 134 def _is_on_hand(self, pos_x, pos_y): |
135 """Return True if the coordinate are on the hand cards""" | |
136 if pos_x > self.orig_x and pos_y > self.orig_y \ | |
137 and pos_x < self.orig_x + (len(self.hand)+1) * self.visible_size \ | |
138 and pos_y < self.end_y: | |
139 return True | |
140 return False | |
141 | |
142 def onResize(self, event): | |
143 self._recalc_ori() | |
144 | |
145 def _recalc_ori(self): | |
92 | 146 """Recalculate origins of hand, must be call when hand size change""" |
83 | 147 self.orig_x = (self.GetSizeTuple()[0]-(len(self.hand)+1)*self.visible_size)/2 #where we start to draw cards |
148 self.orig_y = self.GetSizeTuple()[1] - CARD_HEIGHT - 20 | |
149 self.end_y = self.orig_y + CARD_HEIGHT | |
150 | |
81 | 151 def onPaint(self, event): |
152 dc = wx.PaintDC(self) | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
153 |
87 | 154 #We print the names to know who play where TODO: print avatars when available |
155 max_x, max_y = self.GetSize() | |
156 border = 10 #border between nick and end of panel | |
157 right_y = left_y = 200 | |
158 right_width, right_height = dc.GetTextExtent(self.right_nick) | |
159 right_x = max_x - right_width - border | |
160 left_x = border | |
161 top_width, top_height = dc.GetTextExtent(self.top_nick) | |
162 top_x = (max_x - top_width) / 2 | |
163 top_y = border | |
164 dc.DrawText(self.right_nick, right_x, right_y) | |
165 dc.DrawText(self.top_nick, top_x, top_y) | |
166 dc.DrawText(self.left_nick, left_x, left_y) | |
167 | |
93 | 168 #We draw the played cards: |
169 center_y = 200 #ordinate used as center point | |
170 left_x = (max_x - CARD_WIDTH)/2 - CARD_WIDTH - 5 | |
171 right_x = (max_x/2) + (CARD_WIDTH/2) + 5 | |
172 left_y = right_y = center_y - CARD_HEIGHT/2 | |
173 top_x = bottom_x = (max_x - CARD_WIDTH)/2 | |
174 top_y = center_y - CARD_HEIGHT - 5 | |
175 bottom_y = center_y + 5 | |
176 for side in ['left', 'top', 'right', 'bottom']: | |
177 card = self.played[getattr(self, side+'_nick')] | |
178 if card != None: | |
179 card.draw(dc,locals()[side+'_x'], locals()[side+'_y']) | |
180 | |
83 | 181 x=self.orig_x |
182 for card in self.hand: | |
92 | 183 if (self.state == "play" or self.state == "ecart") and card == self.mouse_over_card \ |
184 or self.state == "ecart" and card in self.selected: | |
185 y = self.orig_y - 30 | |
186 else: | |
187 y = self.orig_y | |
188 | |
189 card.draw(dc,x,y) | |
83 | 190 x+=self.visible_size |
191 | |
92 | 192 if self.to_show: |
193 """There are cards to display in the middle""" | |
194 size = len(self.to_show)*(CARD_WIDTH+10)-10 | |
195 x = (max_x - size)/2 | |
196 for card in self.to_show: | |
197 card.draw(dc, x, 150) | |
198 x+=CARD_WIDTH+10 | |
199 | |
83 | 200 def onMouseMove(self, event): |
201 pos_x,pos_y = event.GetPosition() | |
202 if self._is_on_hand(pos_x, pos_y): | |
203 try: | |
92 | 204 self.mouse_over_card = self.hand[(pos_x-self.orig_x)/self.visible_size] |
83 | 205 except IndexError: |
92 | 206 self.mouse_over_card = self.hand[-1] |
83 | 207 self.Refresh() |
208 else: | |
92 | 209 self.mouse_over_card = None |
83 | 210 self.Refresh() |
211 | |
212 def onMouseClick(self, event): | |
213 print "mouse click:",event.GetPosition() | |
214 pos_x,pos_y = event.GetPosition() | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
215 |
92 | 216 if self.state == "chien": |
217 self.to_show = [] | |
218 self.state = "wait" | |
219 return | |
220 elif self.state == "wait_for_ecart": | |
221 self.state = "ecart" | |
222 self.hand.extend(self.to_show) | |
223 self.hand.sort() | |
224 self.to_show = [] | |
225 self._recalc_ori() | |
226 self.Refresh() | |
227 return | |
228 | |
83 | 229 if self._is_on_hand(pos_x, pos_y): |
230 idx = (pos_x-self.orig_x)/self.visible_size | |
231 if idx == len(self.hand): | |
232 idx-=1 | |
92 | 233 if self.hand[idx] == self.mouse_over_card: |
234 if self.state == "ecart": | |
235 if self.hand[idx] in self.selected: | |
236 self.selected.remove(self.hand[idx]) | |
237 else: | |
238 self.selected.append(self.hand[idx]) | |
239 if len(self.selected) == 6: #TODO: use variable here, as chien len can change with variants | |
240 dlg = wx.MessageDialog(self, _("Do you put these cards in chien ?"), _(u"Écart"), wx.YES_NO | wx.ICON_QUESTION) | |
241 answer = dlg.ShowModal() | |
242 if answer == wx.ID_YES: | |
243 ecart = [] | |
244 for card in self.selected: | |
245 ecart.append((card.suit, card.value)) | |
246 self.hand.remove(card) | |
247 del self.selected[:] | |
248 self.parent.host.bridge.tarotGamePlayCards(self.player_nick, self.referee, ecart, profile_key = self.parent.host.profile) | |
249 self.state = "wait" | |
250 | |
251 self._recalc_ori() | |
252 self.Refresh() | |
93 | 253 if self.state == "play": |
254 card = self.hand[idx] | |
255 self.parent.host.bridge.tarotGamePlayCards(self.player_nick, self.referee, [(card.suit, card.value)], profile_key = self.parent.host.profile) | |
256 del self.hand[idx] | |
257 self.state = "wait" | |
258 self._recalc_ori() | |
259 self.Refresh() | |
260 | |
261 |