Mercurial > libervia-backend
annotate frontends/primitivus/card_game.py @ 157:13888bdb72b6
primitivus: button are now working with XMLUI
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 04 Aug 2010 12:01:07 +0800 |
parents | f197b52796ee |
children | 2fa58703f1b7 |
rev | line source |
---|---|
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1 #!/usr/bin/python |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
2 # -*- coding: utf-8 -*- |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
4 """ |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
5 Primitivus: a SAT frontend |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 Copyright (C) 2009, 2010 Jérôme Poisson (goffi@goffi.org) |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
8 This program is free software: you can redistribute it and/or modify |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
9 it under the terms of the GNU General Public License as published by |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
10 the Free Software Foundation, either version 3 of the License, or |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 (at your option) any later version. |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
13 This program is distributed in the hope that it will be useful, |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 but WITHOUT ANY WARRANTY; without even the implied warranty of |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 GNU General Public License for more details. |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
18 You should have received a copy of the GNU General Public License |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
19 along with this program. If not, see <http://www.gnu.org/licenses/>. |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
20 """ |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
21 |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
22 import urwid |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
23 from tools.games import TarotCard |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
24 from quick_frontend.quick_card_game import QuickCardGame |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
25 from xmlui import XMLUI |
150 | 26 import custom_widgets |
27 | |
28 class CardDisplayer(urwid.Text): | |
29 """Show a card""" | |
30 signals = ['click'] | |
31 | |
32 def __init__(self, card): | |
33 self.__selected = False | |
34 self.card = card | |
35 urwid.Text.__init__(self, card.getAttrText()) | |
36 | |
37 def selectable(self): | |
38 return True | |
39 | |
40 def keypress(self, size, key): | |
41 if key == ' ': | |
42 self.select(not self.__selected) | |
43 self._emit('click') | |
44 return key | |
45 | |
46 def select(self, state=True): | |
47 self.__selected = state | |
48 attr,txt = self.card.getAttrText() | |
49 if self.__selected: | |
50 attr+='_selected' | |
51 self.set_text((attr,txt)) | |
52 self._invalidate() | |
53 | |
54 def isSelected(self): | |
55 return self.__selected | |
56 | |
57 def getCard(self): | |
58 return self.card | |
59 | |
60 def render(self, size, focus=False): | |
61 canvas = urwid.CompositeCanvas(urwid.Text.render(self, size, focus)) | |
62 if focus: | |
63 canvas.set_cursor((0,0)) | |
64 return canvas | |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
65 |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
66 class Hand(urwid.WidgetWrap): |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
67 """Used to display several cards, and manage a hand""" |
150 | 68 signals = ['click'] |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
69 |
150 | 70 def __init__(self, hand=[], selectable = False, on_click=None, user_data=None): |
71 """@param hand: list of Card""" | |
72 self.__selectable = selectable | |
73 self.columns = urwid.Columns([],dividechars=1) | |
74 if on_click: | |
75 urwid.connect_signal(self, 'click', on_click, user_data) | |
76 if hand: | |
77 self.update(hand) | |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
78 urwid.WidgetWrap.__init__(self, self.columns) |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
79 |
150 | 80 def selectable(self): |
81 return self.__selectable | |
82 | |
83 def keypress(self, size, key): | |
84 | |
85 if CardDisplayer in [wid.__class__ for wid in self.columns.widget_list]: | |
86 return self.columns.keypress(size,key) | |
87 else: | |
88 #No card displayed, we still have to manage the clicks | |
89 if key == ' ': | |
90 self._emit('click', None) | |
91 return key | |
92 | |
93 def getSelected(self): | |
94 """Return a list of selected cards""" | |
95 _selected = [] | |
96 for wid in self.columns.widget_list: | |
153
f197b52796ee
Primitivus: begining of management for actionResult
Goffi <goffi@goffi.org>
parents:
150
diff
changeset
|
97 if isinstance(wid, CardDisplayer) and wid.isSelected(): |
150 | 98 _selected.append(wid.getCard()) |
99 return _selected | |
100 | |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
101 def update(self, hand): |
150 | 102 """Update the hand displayed in this widget |
103 @param hand: list of Card""" | |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
104 del self.columns.widget_list[:] |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
105 del self.columns.column_types[:] |
150 | 106 self.columns.widget_list.append(urwid.Text('')) |
107 self.columns.column_types.append(('weight',1)) | |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
108 for card in hand: |
150 | 109 widget = CardDisplayer(card) |
110 self.columns.widget_list.append(widget) | |
111 self.columns.column_types.append(('fixed',3)) | |
112 urwid.connect_signal(widget, 'click', self.__onClick) | |
113 self.columns.widget_list.append(urwid.Text('')) | |
114 self.columns.column_types.append(('weight',1)) | |
115 self.columns.set_focus(1) | |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
116 |
150 | 117 def __onClick(self,card_wid): |
118 self._emit('click', card_wid) | |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
119 |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
120 class Card(TarotCard): |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
121 """This class is used to represent a card, logically |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
122 and give a text representation with attributes""" |
150 | 123 SIZE = 3 #size of a displayed card |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
124 |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
125 def __init__(self, suit, value): |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
126 """@param file: path of the PNG file""" |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
127 TarotCard.__init__(self, (suit, value)) |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
128 |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
129 def getAttrText(self): |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
130 """return text representation of the card with attributes""" |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
131 try: |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
132 value = "%02i" % int(self.value) |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
133 except ValueError: |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
134 value = self.value[0].upper()+self.value[1] |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
135 if self.suit == "atout": |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
136 if self.value == "excuse": |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
137 suit = 'c' |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
138 else: |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
139 suit = 'A' |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
140 color = 'neutral' |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
141 elif self.suit == "pique": |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
142 suit = u'♠' |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
143 color = 'black' |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
144 elif self.suit == "trefle": |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
145 suit = u'♣' |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
146 color = 'black' |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
147 elif self.suit == "coeur": |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
148 suit = u'♥' |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
149 color = 'red' |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
150 elif self.suit == "carreau": |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
151 suit = u'♦' |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
152 color = 'red' |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
153 if self.bout: |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
154 color = 'special' |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
155 return ('card_%s' % color,u"%s%s" % (value,suit)) |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
156 |
150 | 157 def getWidget(self): |
158 """Return a widget representing the card""" | |
159 return CardDisplayer(self) | |
160 | |
161 class Table(urwid.FlowWidget): | |
162 """Represent the cards currently on the table""" | |
163 | |
164 def __init__(self): | |
165 self.top = self.left = self.bottom = self.right = None | |
166 | |
167 def putCard(self, location, card): | |
168 """Put a card on the table | |
169 @param location: where to put the card (top, left, bottom or right) | |
170 @param card: Card to play or None""" | |
171 assert location in ['top','left','bottom','right'] | |
153
f197b52796ee
Primitivus: begining of management for actionResult
Goffi <goffi@goffi.org>
parents:
150
diff
changeset
|
172 assert isinstance(card,Card) or card == None |
150 | 173 if [getattr(self, place) for place in ['top','left','bottom','right']].count(None) == 0: |
174 #If the table is full of card, we remove them | |
175 self.top = self.left = self.bottom = self.right = None | |
176 setattr(self, location, card) | |
177 self._invalidate() | |
178 | |
179 def rows(self,size,focus=False): | |
180 return self.display_widget(size, focus).rows(size, focus) | |
181 | |
182 def render(self, size, focus=False): | |
183 return self.display_widget(size, focus).render(size, focus) | |
184 | |
185 def display_widget(self, size, focus): | |
186 cards={} | |
187 max_col, = size | |
188 separator = " - " | |
189 margin = max((max_col-Card.SIZE)/2,0) * ' ' | |
190 margin_center = max((max_col-Card.SIZE*2-len(separator))/2,0) * ' ' | |
191 for location in ['top', 'left', 'bottom', 'right']: | |
192 card = getattr(self,location) | |
193 cards[location] = card.getAttrText() if card else Card.SIZE * ' ' | |
194 render_wid = [urwid.Text([margin,cards['top']]), | |
195 urwid.Text([margin_center,cards['left'],separator,cards['right']]), | |
196 urwid.Text([margin,cards['bottom']])] | |
197 return urwid.Pile(render_wid) | |
198 | |
199 | |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
200 class CardGame(QuickCardGame,urwid.WidgetWrap): |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
201 """Widget for card games""" |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
202 |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
203 def __init__(self, parent, referee, players, player_nick): |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
204 QuickCardGame.__init__(self, parent, referee, players, player_nick) |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
205 self.loadCards() |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
206 self.top = urwid.Pile([urwid.Padding(urwid.Text(self.top_nick), 'center')]) |
150 | 207 #self.parent.host.debug() |
208 self.table = Table() | |
209 self.center = urwid.Columns([('fixed',len(self.left_nick),urwid.Filler(urwid.Text(self.left_nick))), | |
210 urwid.Filler(self.table), | |
211 ('fixed',len(self.right_nick),urwid.Filler(urwid.Text(self.right_nick))) | |
212 ]) | |
213 """urwid.Pile([urwid.Padding(self.top_card_wid,'center'), | |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
214 urwid.Columns([('fixed',len(self.left_nick),urwid.Text(self.left_nick)), |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
215 urwid.Padding(self.center_cards_wid,'center'), |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
216 ('fixed',len(self.right_nick),urwid.Text(self.right_nick)) |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
217 ]), |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
218 urwid.Padding(self.bottom_card_wid,'center') |
150 | 219 ])""" |
220 self.hand_wid = Hand(selectable = True, on_click = self.onClick) | |
221 self.main_frame = urwid.Frame(self.center,header=self.top, footer=self.hand_wid, focus_part='footer') | |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
222 urwid.WidgetWrap.__init__(self,self.main_frame) |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
223 self.parent.host.bridge.tarotGameReady(player_nick, referee, profile_key = self.parent.host.profile) |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
224 |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
225 def loadCards(self): |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
226 """Load all the cards in memory""" |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
227 QuickCardGame.loadCards(self) |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
228 for value in map(str,range(1,22))+['excuse']: |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
229 card = Card('atout',value) |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
230 self.cards[card.suit, card.value]=card |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
231 self.deck.append(card) |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
232 for suit in ["pique", "coeur", "carreau", "trefle"]: |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
233 for value in map(str,range(1,11))+["valet","cavalier","dame","roi"]: |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
234 card = Card(suit,value) |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
235 self.cards[card.suit, card.value]=card |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
236 self.deck.append(card) |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
237 |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
238 def newGame(self, hand): |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
239 """Start a new game, with given hand""" |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
240 QuickCardGame.newGame(self, hand) |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
241 self.hand_wid.update(self.hand) |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
242 self.parent.host.redraw() |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
243 |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
244 def contratSelected(self, data): |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
245 """Called when the contrat has been choosed |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
246 @param data: form result""" |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
247 contrat = data[0][1] |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
248 QuickCardGame.contratSelected(self, contrat) |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
249 |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
250 def chooseContrat(self, xml_data): |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
251 """Called when the player as to select his contrat |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
252 @param xml_data: SàT xml representation of the form""" |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
253 misc = {'callback': self.contratSelected} |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
254 form = XMLUI(self.parent.host, xml_data, title = _('Please choose your contrat'), options = ['NO_CANCEL'], misc = misc) |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
255 form.show() |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
256 |
150 | 257 def showCards(self, game_stage, cards, data): |
258 """Display cards in the middle of the game (to show for e.g. chien ou poignée)""" | |
259 QuickCardGame.showCards(self, game_stage, cards, data) | |
260 self.center.widget_list[1] = urwid.Filler(Hand(self.to_show)) | |
261 self.parent.host.redraw() | |
262 | |
263 def myTurn(self): | |
264 QuickCardGame.myTurn(self) | |
265 | |
266 def showScores(self, xml_data, winners, loosers): | |
267 """Called when the player as to select hist contrat | |
268 @param xml_data: SàT xml representation of the form""" | |
269 form = XMLUI(self.parent.host, xml_data, title = _('You win \o/') if self.player_nick in winners else _('You loose :('), options = ['NO_CANCEL']) | |
270 | |
271 def invalidCards(self, phase, played_cards, invalid_cards): | |
272 """Invalid cards have been played | |
273 @param phase: phase of the game | |
274 @param played_cards: all the cards played | |
275 @param invalid_cards: cards which are invalid""" | |
276 QuickCardGame.invalidCards(self, phase, played_cards, invalid_cards) | |
277 self.hand_wid.update(self.hand) | |
278 self.parent.host.redraw() | |
279 | |
280 def cardsPlayed(self, player, cards): | |
281 """A card has been played by player""" | |
282 QuickCardGame.cardsPlayed(self, player, cards) | |
283 self.table.putCard(self.getPlayerLocation(player),self.played[player]) | |
284 self.parent.host.redraw() | |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
285 |
150 | 286 ##EVENTS## |
287 def onClick(self, hand, card_wid): | |
288 """Called when user do an action on the hand""" | |
289 if not self.state in ['play','ecart','wait_for_ecart']: | |
290 #it's not our turn, we ignore the click | |
291 card_wid.select(False) | |
292 return | |
153
f197b52796ee
Primitivus: begining of management for actionResult
Goffi <goffi@goffi.org>
parents:
150
diff
changeset
|
293 if isinstance(self.center.widget_list[1].original_widget, Hand): #if we have a hand displayed |
150 | 294 self.center.widget_list[1] = urwid.Filler(self.table) #we show again the table |
295 if self.state == "chien": | |
296 self.to_show = [] | |
297 self.state = "wait" | |
298 elif self.state == "wait_for_ecart": | |
299 self.state = "ecart" | |
300 self.hand.extend(self.to_show) | |
301 self.hand.sort() | |
302 self.to_show = [] | |
303 self.hand_wid.update(self.hand) | |
304 if self.state == "ecart": | |
305 if len(self.hand_wid.getSelected()) == 6: | |
306 pop_up_widget = custom_widgets.ConfirmDialog(_("Do you put these cards in chien ?"), yes_cb=self.onEcartDone, no_cb=self.parent.host.removePopUp) | |
307 self.parent.host.showPopUp(pop_up_widget) | |
308 elif self.state == "play": | |
309 card = card_wid.getCard() | |
310 self.parent.host.bridge.tarotGamePlayCards(self.player_nick, self.referee, [(card.suit, card.value)], profile_key = self.parent.host.profile) | |
311 self.hand.remove(card) | |
312 self.hand_wid.update(self.hand) | |
313 self.state = "wait" | |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
314 |
150 | 315 def onEcartDone(self,button): |
316 """Called when player has finished is écart""" | |
317 ecart = [] | |
318 for card in self.hand_wid.getSelected(): | |
319 ecart.append((card.suit, card.value)) | |
320 self.hand.remove(card) | |
321 self.hand_wid.update(self.hand) | |
322 self.parent.host.bridge.tarotGamePlayCards(self.player_nick, self.referee, ecart, profile_key = self.parent.host.profile) | |
323 self.state = "wait" | |
324 self.parent.host.removePopUp() |