Mercurial > libervia-backend
annotate frontends/src/wix/card_game.py @ 849:c5a8f602662b
plugin room_game, radiocol: RoomGame.send returns a Deferred.
author | souliane <souliane@mailoo.org> |
---|---|
date | Tue, 18 Feb 2014 17:49:33 +0100 |
parents | 1fe00f0c9a91 |
children | 36c6495d86b0 |
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 |
811 | 5 # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 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 | |
771 | 22 from sat.core.i18n import _ |
81 | 23 import wx |
24 import os.path, glob | |
25 import pdb | |
26 from logging import debug, info, error | |
225
fd9b7834d98a
distutils installation script, draft
Goffi <goffi@goffi.org>
parents:
223
diff
changeset
|
27 from sat.tools.jid import JID |
719
56aa0e98c92e
frontends tools: moved src/tools/frontends to frontends/src/tools
souliane <souliane@mailoo.org>
parents:
686
diff
changeset
|
28 from sat_frontends.tools.games import TarotCard |
227 | 29 from sat_frontends.quick_frontend.quick_card_game import QuickCardGame |
30 from sat_frontends.wix.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 |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
141
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] | |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
141
diff
changeset
|
44 suit,value = root_name.split('_') |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
141
diff
changeset
|
45 TarotCard.__init__(self, (suit, value)) |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
141
diff
changeset
|
46 print "Carte:",suit, 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 | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
51 @param x: abscissa |
81 | 52 @param y: ordinate""" |
53 dc.DrawBitmap(self.bitmap, x, y, True) | |
54 | |
55 | |
686
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
56 class CardPanel(QuickCardGame, wx.Panel): |
81 | 57 """This class is used to display the cards""" |
58 | |
92 | 59 def __init__(self, parent, referee, players, player_nick): |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
141
diff
changeset
|
60 QuickCardGame.__init__(self, parent, referee, players, player_nick) |
81 | 61 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
|
62 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
|
63 self.loadCards(os.path.join(self.parent.host.media_dir, 'games/cards/tarot')) |
92 | 64 self.mouse_over_card = None #contain the card to highlight |
83 | 65 self.visible_size = CARD_WIDTH/2 #number of pixels visible for cards |
87 | 66 self.hand = [] |
92 | 67 self.to_show = [] |
68 self.state = None | |
81 | 69 self.SetBackgroundColour(wx.GREEN) |
83 | 70 self.Bind(wx.EVT_SIZE, self.onResize) |
81 | 71 self.Bind(wx.EVT_PAINT, self.onPaint) |
83 | 72 self.Bind(wx.EVT_MOTION, self.onMouseMove) |
73 self.Bind(wx.EVT_LEFT_UP, self.onMouseClick) | |
680
8281587eb528
primitivus, wix: fixed bridge methods calls for plugins radiocol and card game
souliane <souliane@mailoo.org>
parents:
609
diff
changeset
|
74 |
8281587eb528
primitivus, wix: fixed bridge methods calls for plugins radiocol and card game
souliane <souliane@mailoo.org>
parents:
609
diff
changeset
|
75 self.parent.host.bridge.tarotGameReady(player_nick, referee, self.parent.host.profile) |
81 | 76 |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
141
diff
changeset
|
77 def loadCards(self, dir): |
81 | 78 """Load all the cards in memory |
79 @param dir: directory where the PNG files are""" | |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
141
diff
changeset
|
80 QuickCardGame.loadCards(self) |
81 | 81 for file in glob.glob(dir+'/*_*.png'): |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
141
diff
changeset
|
82 card = WxCard(file) |
92 | 83 self.cards[card.suit, card.value]=card |
81 | 84 self.deck.append(card) |
85 | |
87 | 86 def newGame(self, hand): |
87 """Start a new game, with given hand""" | |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
141
diff
changeset
|
88 QuickCardGame.newGame(self, hand) |
92 | 89 self._recalc_ori() |
90 self.Refresh() | |
87 | 91 |
91 | 92 def contratSelected(self, data): |
93 """Called when the contrat has been choosed | |
94 @param data: form result""" | |
95 debug (_("Contrat choosed")) | |
96 contrat = data[0][1] | |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
141
diff
changeset
|
97 QuickCardGame.contratSelected(self, contrat) |
91 | 98 |
99 def chooseContrat(self, xml_data): | |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
141
diff
changeset
|
100 """Called when the player as to select his contrat |
91 | 101 @param xml_data: SàT xml representation of the form""" |
102 misc = {'callback': self.contratSelected} | |
133
d998adb62d1a
wix: fixed wrong class Name in card_game
Goffi <goffi@goffi.org>
parents:
103
diff
changeset
|
103 form = XMLUI(self.parent.host, xml_data, title = _('Please choose your contrat'), options = ['NO_CANCEL'], misc = misc) |
91 | 104 |
95 | 105 def showScores(self, xml_data, winners, loosers): |
106 """Called when the player as to select hist contrat | |
107 @param xml_data: SàT xml representation of the form""" | |
328 | 108 def _new_game(ignore): |
109 self.resetRound() | |
110 self.Refresh() | |
680
8281587eb528
primitivus, wix: fixed bridge methods calls for plugins radiocol and card game
souliane <souliane@mailoo.org>
parents:
609
diff
changeset
|
111 self.parent.host.bridge.tarotGameReady(self.player_nick, self.referee, self.parent.host.profile) |
328 | 112 |
113 if not winners and not loosers: | |
114 title = _("Draw game") | |
115 else: | |
116 title = _('You win \o/') if self.player_nick in winners else _('You loose :(') | |
117 form = XMLUI(self.parent.host, xml_data, title = title, options = ['NO_CANCEL'], misc={'callback':_new_game}) | |
95 | 118 |
93 | 119 def cardsPlayed(self, player, cards): |
120 """A card has been played by player""" | |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
141
diff
changeset
|
121 QuickCardGame.cardsPlayed(self, player, cards) |
93 | 122 self.Refresh() |
123 | |
99
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
97
diff
changeset
|
124 def invalidCards(self, phase, played_cards, invalid_cards): |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
97
diff
changeset
|
125 """Invalid cards have been played |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
97
diff
changeset
|
126 @param phase: phase of the game |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
97
diff
changeset
|
127 @param played_cards: all the cards played |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
97
diff
changeset
|
128 @param invalid_cards: cards which are invalid""" |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
141
diff
changeset
|
129 QuickCardGame.invalidCards(self, phase, played_cards, invalid_cards) |
99
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
97
diff
changeset
|
130 |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
97
diff
changeset
|
131 self._recalc_ori() |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
97
diff
changeset
|
132 self.Refresh() |
162 | 133 if self._autoplay==None: #No dialog if there is autoplay |
134 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
|
135 |
83 | 136 def _is_on_hand(self, pos_x, pos_y): |
137 """Return True if the coordinate are on the hand cards""" | |
138 if pos_x > self.orig_x and pos_y > self.orig_y \ | |
139 and pos_x < self.orig_x + (len(self.hand)+1) * self.visible_size \ | |
140 and pos_y < self.end_y: | |
141 return True | |
142 return False | |
143 | |
144 def onResize(self, event): | |
145 self._recalc_ori() | |
146 | |
147 def _recalc_ori(self): | |
92 | 148 """Recalculate origins of hand, must be call when hand size change""" |
83 | 149 self.orig_x = (self.GetSizeTuple()[0]-(len(self.hand)+1)*self.visible_size)/2 #where we start to draw cards |
150 self.orig_y = self.GetSizeTuple()[1] - CARD_HEIGHT - 20 | |
151 self.end_y = self.orig_y + CARD_HEIGHT | |
152 | |
81 | 153 def onPaint(self, event): |
154 dc = wx.PaintDC(self) | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
155 |
87 | 156 #We print the names to know who play where TODO: print avatars when available |
157 max_x, max_y = self.GetSize() | |
158 border = 10 #border between nick and end of panel | |
159 right_y = left_y = 200 | |
160 right_width, right_height = dc.GetTextExtent(self.right_nick) | |
161 right_x = max_x - right_width - border | |
162 left_x = border | |
163 top_width, top_height = dc.GetTextExtent(self.top_nick) | |
164 top_x = (max_x - top_width) / 2 | |
165 top_y = border | |
166 dc.DrawText(self.right_nick, right_x, right_y) | |
167 dc.DrawText(self.top_nick, top_x, top_y) | |
168 dc.DrawText(self.left_nick, left_x, left_y) | |
169 | |
93 | 170 #We draw the played cards: |
171 center_y = 200 #ordinate used as center point | |
172 left_x = (max_x - CARD_WIDTH)/2 - CARD_WIDTH - 5 | |
173 right_x = (max_x/2) + (CARD_WIDTH/2) + 5 | |
174 left_y = right_y = center_y - CARD_HEIGHT/2 | |
175 top_x = bottom_x = (max_x - CARD_WIDTH)/2 | |
176 top_y = center_y - CARD_HEIGHT - 5 | |
177 bottom_y = center_y + 5 | |
178 for side in ['left', 'top', 'right', 'bottom']: | |
179 card = self.played[getattr(self, side+'_nick')] | |
180 if card != None: | |
181 card.draw(dc,locals()[side+'_x'], locals()[side+'_y']) | |
182 | |
83 | 183 x=self.orig_x |
184 for card in self.hand: | |
92 | 185 if (self.state == "play" or self.state == "ecart") and card == self.mouse_over_card \ |
186 or self.state == "ecart" and card in self.selected: | |
187 y = self.orig_y - 30 | |
188 else: | |
189 y = self.orig_y | |
190 | |
191 card.draw(dc,x,y) | |
83 | 192 x+=self.visible_size |
193 | |
92 | 194 if self.to_show: |
195 """There are cards to display in the middle""" | |
196 size = len(self.to_show)*(CARD_WIDTH+10)-10 | |
197 x = (max_x - size)/2 | |
198 for card in self.to_show: | |
199 card.draw(dc, x, 150) | |
200 x+=CARD_WIDTH+10 | |
201 | |
83 | 202 def onMouseMove(self, event): |
203 pos_x,pos_y = event.GetPosition() | |
204 if self._is_on_hand(pos_x, pos_y): | |
205 try: | |
92 | 206 self.mouse_over_card = self.hand[(pos_x-self.orig_x)/self.visible_size] |
83 | 207 except IndexError: |
92 | 208 self.mouse_over_card = self.hand[-1] |
83 | 209 self.Refresh() |
210 else: | |
92 | 211 self.mouse_over_card = None |
83 | 212 self.Refresh() |
213 | |
214 def onMouseClick(self, event): | |
215 print "mouse click:",event.GetPosition() | |
216 pos_x,pos_y = event.GetPosition() | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
217 |
92 | 218 if self.state == "chien": |
219 self.to_show = [] | |
220 self.state = "wait" | |
221 return | |
222 elif self.state == "wait_for_ecart": | |
223 self.state = "ecart" | |
224 self.hand.extend(self.to_show) | |
225 self.hand.sort() | |
226 self.to_show = [] | |
227 self._recalc_ori() | |
228 self.Refresh() | |
229 return | |
230 | |
83 | 231 if self._is_on_hand(pos_x, pos_y): |
232 idx = (pos_x-self.orig_x)/self.visible_size | |
233 if idx == len(self.hand): | |
234 idx-=1 | |
92 | 235 if self.hand[idx] == self.mouse_over_card: |
236 if self.state == "ecart": | |
237 if self.hand[idx] in self.selected: | |
238 self.selected.remove(self.hand[idx]) | |
239 else: | |
240 self.selected.append(self.hand[idx]) | |
241 if len(self.selected) == 6: #TODO: use variable here, as chien len can change with variants | |
242 dlg = wx.MessageDialog(self, _("Do you put these cards in chien ?"), _(u"Écart"), wx.YES_NO | wx.ICON_QUESTION) | |
243 answer = dlg.ShowModal() | |
244 if answer == wx.ID_YES: | |
245 ecart = [] | |
246 for card in self.selected: | |
247 ecart.append((card.suit, card.value)) | |
248 self.hand.remove(card) | |
249 del self.selected[:] | |
680
8281587eb528
primitivus, wix: fixed bridge methods calls for plugins radiocol and card game
souliane <souliane@mailoo.org>
parents:
609
diff
changeset
|
250 self.parent.host.bridge.tarotGamePlayCards(self.player_nick, self.referee, ecart, self.parent.host.profile) |
92 | 251 self.state = "wait" |
252 | |
253 self._recalc_ori() | |
254 self.Refresh() | |
93 | 255 if self.state == "play": |
256 card = self.hand[idx] | |
680
8281587eb528
primitivus, wix: fixed bridge methods calls for plugins radiocol and card game
souliane <souliane@mailoo.org>
parents:
609
diff
changeset
|
257 self.parent.host.bridge.tarotGamePlayCards(self.player_nick, self.referee, [(card.suit, card.value)], self.parent.host.profile) |
93 | 258 del self.hand[idx] |
259 self.state = "wait" | |
260 self._recalc_ori() | |
261 self.Refresh() | |
262 | |
263 |