Mercurial > libervia-backend
annotate frontends/src/primitivus/card_game.py @ 607:c123dddaea6b
primitivus: fixed urwid issues with recent urwid versions
fix bug 18
author | Goffi <goffi@goffi.org> |
---|---|
date | Sun, 24 Feb 2013 13:57:37 +0100 |
parents | 952322b1d490 |
children | 84a6e83157c2 |
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 |
572 | 6 Copyright (C) 2009, 2010, 2011, 2012, 2013 Jérôme Poisson (goffi@goffi.org) |
144
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 |
480
2a072735e459
Licence modification: the full project is now under AGPL v3+ instead of GPL v3+
Goffi <goffi@goffi.org>
parents:
459
diff
changeset
|
9 it under the terms of the GNU Affero General Public License as published by |
144
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 |
480
2a072735e459
Licence modification: the full project is now under AGPL v3+ instead of GPL v3+
Goffi <goffi@goffi.org>
parents:
459
diff
changeset
|
16 GNU Affero General Public License for more details. |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 |
480
2a072735e459
Licence modification: the full project is now under AGPL v3+ instead of GPL v3+
Goffi <goffi@goffi.org>
parents:
459
diff
changeset
|
18 You should have received a copy of the GNU Affero General Public License |
144
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 |
222
3198bfd66daa
primitivus: refactoring to use urwid-satext which is now a separate project
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
23 from urwid_satext import sat_widgets |
225
fd9b7834d98a
distutils installation script, draft
Goffi <goffi@goffi.org>
parents:
223
diff
changeset
|
24 from sat.tools.games import TarotCard |
fd9b7834d98a
distutils installation script, draft
Goffi <goffi@goffi.org>
parents:
223
diff
changeset
|
25 from sat_frontends.quick_frontend.quick_card_game import QuickCardGame |
fd9b7834d98a
distutils installation script, draft
Goffi <goffi@goffi.org>
parents:
223
diff
changeset
|
26 from sat_frontends.primitivus.xmlui import XMLUI |
150 | 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 | |
159
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
153
diff
changeset
|
46 def mouse_event(self, size, event, button, x, y, focus): |
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
153
diff
changeset
|
47 if urwid.is_mouse_press(event) and button == 1: |
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
153
diff
changeset
|
48 self.select(not self.__selected) |
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
153
diff
changeset
|
49 self._emit('click') |
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
153
diff
changeset
|
50 return True |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
51 |
159
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
153
diff
changeset
|
52 return False |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
53 |
150 | 54 def select(self, state=True): |
55 self.__selected = state | |
56 attr,txt = self.card.getAttrText() | |
57 if self.__selected: | |
58 attr+='_selected' | |
59 self.set_text((attr,txt)) | |
60 self._invalidate() | |
61 | |
62 def isSelected(self): | |
63 return self.__selected | |
64 | |
65 def getCard(self): | |
66 return self.card | |
67 | |
68 def render(self, size, focus=False): | |
69 canvas = urwid.CompositeCanvas(urwid.Text.render(self, size, focus)) | |
70 if focus: | |
71 canvas.set_cursor((0,0)) | |
72 return canvas | |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
73 |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
74 class Hand(urwid.WidgetWrap): |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
75 """Used to display several cards, and manage a hand""" |
150 | 76 signals = ['click'] |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
77 |
150 | 78 def __init__(self, hand=[], selectable = False, on_click=None, user_data=None): |
79 """@param hand: list of Card""" | |
80 self.__selectable = selectable | |
81 self.columns = urwid.Columns([],dividechars=1) | |
82 if on_click: | |
83 urwid.connect_signal(self, 'click', on_click, user_data) | |
84 if hand: | |
85 self.update(hand) | |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
86 urwid.WidgetWrap.__init__(self, self.columns) |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
87 |
150 | 88 def selectable(self): |
89 return self.__selectable | |
90 | |
91 def keypress(self, size, key): | |
92 | |
93 if CardDisplayer in [wid.__class__ for wid in self.columns.widget_list]: | |
94 return self.columns.keypress(size,key) | |
95 else: | |
96 #No card displayed, we still have to manage the clicks | |
97 if key == ' ': | |
98 self._emit('click', None) | |
99 return key | |
100 | |
101 def getSelected(self): | |
102 """Return a list of selected cards""" | |
103 _selected = [] | |
104 for wid in self.columns.widget_list: | |
153
f197b52796ee
Primitivus: begining of management for actionResult
Goffi <goffi@goffi.org>
parents:
150
diff
changeset
|
105 if isinstance(wid, CardDisplayer) and wid.isSelected(): |
150 | 106 _selected.append(wid.getCard()) |
107 return _selected | |
108 | |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
109 def update(self, hand): |
150 | 110 """Update the hand displayed in this widget |
111 @param hand: list of Card""" | |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
112 del self.columns.widget_list[:] |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
113 del self.columns.column_types[:] |
607
c123dddaea6b
primitivus: fixed urwid issues with recent urwid versions
Goffi <goffi@goffi.org>
parents:
587
diff
changeset
|
114 self.columns.contents.append((urwid.Text(''),('weight',1, False))) |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
115 for card in hand: |
150 | 116 widget = CardDisplayer(card) |
117 self.columns.widget_list.append(widget) | |
118 self.columns.column_types.append(('fixed',3)) | |
119 urwid.connect_signal(widget, 'click', self.__onClick) | |
607
c123dddaea6b
primitivus: fixed urwid issues with recent urwid versions
Goffi <goffi@goffi.org>
parents:
587
diff
changeset
|
120 self.columns.contents.append((urwid.Text(''),('weight',1, False))) |
150 | 121 self.columns.set_focus(1) |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
122 |
150 | 123 def __onClick(self,card_wid): |
124 self._emit('click', card_wid) | |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
125 |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
126 class Card(TarotCard): |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
127 """This class is used to represent a card, logically |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
128 and give a text representation with attributes""" |
150 | 129 SIZE = 3 #size of a displayed card |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
130 |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
131 def __init__(self, suit, value): |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
132 """@param file: path of the PNG file""" |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
133 TarotCard.__init__(self, (suit, value)) |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
134 |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
135 def getAttrText(self): |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
136 """return text representation of the card with attributes""" |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
137 try: |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
138 value = "%02i" % int(self.value) |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
139 except ValueError: |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
140 value = self.value[0].upper()+self.value[1] |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
141 if self.suit == "atout": |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
142 if self.value == "excuse": |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
143 suit = 'c' |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
144 else: |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
145 suit = 'A' |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
146 color = 'neutral' |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
147 elif self.suit == "pique": |
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 = 'black' |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
150 elif self.suit == "trefle": |
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 = 'black' |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
153 elif self.suit == "coeur": |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
154 suit = u'♥' |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
155 color = 'red' |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
156 elif self.suit == "carreau": |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
157 suit = u'♦' |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
158 color = 'red' |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
159 if self.bout: |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
160 color = 'special' |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
161 return ('card_%s' % color,u"%s%s" % (value,suit)) |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
162 |
150 | 163 def getWidget(self): |
164 """Return a widget representing the card""" | |
165 return CardDisplayer(self) | |
166 | |
167 class Table(urwid.FlowWidget): | |
168 """Represent the cards currently on the table""" | |
169 | |
170 def __init__(self): | |
171 self.top = self.left = self.bottom = self.right = None | |
172 | |
173 def putCard(self, location, card): | |
174 """Put a card on the table | |
175 @param location: where to put the card (top, left, bottom or right) | |
176 @param card: Card to play or None""" | |
177 assert location in ['top','left','bottom','right'] | |
153
f197b52796ee
Primitivus: begining of management for actionResult
Goffi <goffi@goffi.org>
parents:
150
diff
changeset
|
178 assert isinstance(card,Card) or card == None |
150 | 179 if [getattr(self, place) for place in ['top','left','bottom','right']].count(None) == 0: |
180 #If the table is full of card, we remove them | |
181 self.top = self.left = self.bottom = self.right = None | |
182 setattr(self, location, card) | |
183 self._invalidate() | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
184 |
150 | 185 def rows(self,size,focus=False): |
186 return self.display_widget(size, focus).rows(size, focus) | |
187 | |
188 def render(self, size, focus=False): | |
189 return self.display_widget(size, focus).render(size, focus) | |
190 | |
191 def display_widget(self, size, focus): | |
192 cards={} | |
193 max_col, = size | |
194 separator = " - " | |
195 margin = max((max_col-Card.SIZE)/2,0) * ' ' | |
196 margin_center = max((max_col-Card.SIZE*2-len(separator))/2,0) * ' ' | |
197 for location in ['top', 'left', 'bottom', 'right']: | |
198 card = getattr(self,location) | |
199 cards[location] = card.getAttrText() if card else Card.SIZE * ' ' | |
200 render_wid = [urwid.Text([margin,cards['top']]), | |
201 urwid.Text([margin_center,cards['left'],separator,cards['right']]), | |
202 urwid.Text([margin,cards['bottom']])] | |
203 return urwid.Pile(render_wid) | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
204 |
150 | 205 |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
206 class CardGame(QuickCardGame,urwid.WidgetWrap): |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
207 """Widget for card games""" |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
208 |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
209 def __init__(self, parent, referee, players, player_nick): |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
210 QuickCardGame.__init__(self, parent, referee, players, player_nick) |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
211 self.loadCards() |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
212 self.top = urwid.Pile([urwid.Padding(urwid.Text(self.top_nick), 'center')]) |
150 | 213 #self.parent.host.debug() |
214 self.table = Table() | |
215 self.center = urwid.Columns([('fixed',len(self.left_nick),urwid.Filler(urwid.Text(self.left_nick))), | |
216 urwid.Filler(self.table), | |
217 ('fixed',len(self.right_nick),urwid.Filler(urwid.Text(self.right_nick))) | |
218 ]) | |
219 """urwid.Pile([urwid.Padding(self.top_card_wid,'center'), | |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
220 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
|
221 urwid.Padding(self.center_cards_wid,'center'), |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
222 ('fixed',len(self.right_nick),urwid.Text(self.right_nick)) |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
223 ]), |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
224 urwid.Padding(self.bottom_card_wid,'center') |
150 | 225 ])""" |
226 self.hand_wid = Hand(selectable = True, on_click = self.onClick) | |
227 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
|
228 urwid.WidgetWrap.__init__(self,self.main_frame) |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
229 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
|
230 |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
231 def loadCards(self): |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
232 """Load all the cards in memory""" |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
233 QuickCardGame.loadCards(self) |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
234 for value in map(str,range(1,22))+['excuse']: |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
235 card = Card('atout',value) |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
236 self.cards[card.suit, card.value]=card |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
237 self.deck.append(card) |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
238 for suit in ["pique", "coeur", "carreau", "trefle"]: |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
239 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
|
240 card = Card(suit,value) |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
241 self.cards[card.suit, card.value]=card |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
242 self.deck.append(card) |
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 newGame(self, hand): |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
245 """Start a new game, with given hand""" |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
246 QuickCardGame.newGame(self, hand) |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
247 self.hand_wid.update(self.hand) |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
248 self.parent.host.redraw() |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
249 |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
250 def contratSelected(self, data): |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
251 """Called when the contrat has been choosed |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
252 @param data: form result""" |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
253 contrat = data[0][1] |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
254 QuickCardGame.contratSelected(self, contrat) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
255 |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
256 def chooseContrat(self, xml_data): |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
257 """Called when the player as to select his contrat |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
258 @param xml_data: SàT xml representation of the form""" |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
259 misc = {'callback': self.contratSelected} |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
260 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
|
261 form.show() |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
262 |
150 | 263 def showCards(self, game_stage, cards, data): |
264 """Display cards in the middle of the game (to show for e.g. chien ou poignée)""" | |
265 QuickCardGame.showCards(self, game_stage, cards, data) | |
266 self.center.widget_list[1] = urwid.Filler(Hand(self.to_show)) | |
267 self.parent.host.redraw() | |
268 | |
269 def myTurn(self): | |
270 QuickCardGame.myTurn(self) | |
271 | |
272 def showScores(self, xml_data, winners, loosers): | |
273 """Called when the player as to select hist contrat | |
274 @param xml_data: SàT xml representation of the form""" | |
328 | 275 def _new_game(ignore): |
276 self.resetRound() | |
277 for location in ['top','left','bottom','right']: | |
278 self.table.putCard(location, None) | |
279 self.parent.host.redraw() | |
280 self.parent.host.bridge.tarotGameReady(self.player_nick, self.referee, profile_key = self.parent.host.profile) | |
281 if not winners and not loosers: | |
282 title = _("Draw game") | |
283 else: | |
284 title = _('You win \o/') if self.player_nick in winners else _('You loose :(') | |
285 form = XMLUI(self.parent.host, xml_data, title = title, options = ['NO_CANCEL'], misc={'callback':_new_game}) | |
162 | 286 form.show() |
150 | 287 |
288 def invalidCards(self, phase, played_cards, invalid_cards): | |
289 """Invalid cards have been played | |
290 @param phase: phase of the game | |
291 @param played_cards: all the cards played | |
292 @param invalid_cards: cards which are invalid""" | |
293 QuickCardGame.invalidCards(self, phase, played_cards, invalid_cards) | |
294 self.hand_wid.update(self.hand) | |
162 | 295 if self._autoplay==None: #No dialog if there is autoplay |
296 self.parent.host.notify(_('Cards played are invalid !')) | |
150 | 297 self.parent.host.redraw() |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
298 |
150 | 299 def cardsPlayed(self, player, cards): |
300 """A card has been played by player""" | |
301 QuickCardGame.cardsPlayed(self, player, cards) | |
302 self.table.putCard(self.getPlayerLocation(player),self.played[player]) | |
323
7bc1b3401ecb
primitivus: fixed state state change in tarot game
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
303 self._checkState() |
150 | 304 self.parent.host.redraw() |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
305 |
323
7bc1b3401ecb
primitivus: fixed state state change in tarot game
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
306 def _checkState(self): |
153
f197b52796ee
Primitivus: begining of management for actionResult
Goffi <goffi@goffi.org>
parents:
150
diff
changeset
|
307 if isinstance(self.center.widget_list[1].original_widget, Hand): #if we have a hand displayed |
150 | 308 self.center.widget_list[1] = urwid.Filler(self.table) #we show again the table |
309 if self.state == "chien": | |
310 self.to_show = [] | |
311 self.state = "wait" | |
312 elif self.state == "wait_for_ecart": | |
313 self.state = "ecart" | |
314 self.hand.extend(self.to_show) | |
315 self.hand.sort() | |
316 self.to_show = [] | |
317 self.hand_wid.update(self.hand) | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
318 |
323
7bc1b3401ecb
primitivus: fixed state state change in tarot game
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
319 |
7bc1b3401ecb
primitivus: fixed state state change in tarot game
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
320 ##EVENTS## |
7bc1b3401ecb
primitivus: fixed state state change in tarot game
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
321 def onClick(self, hand, card_wid): |
7bc1b3401ecb
primitivus: fixed state state change in tarot game
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
322 """Called when user do an action on the hand""" |
7bc1b3401ecb
primitivus: fixed state state change in tarot game
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
323 if not self.state in ['play','ecart','wait_for_ecart']: |
7bc1b3401ecb
primitivus: fixed state state change in tarot game
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
324 #it's not our turn, we ignore the click |
7bc1b3401ecb
primitivus: fixed state state change in tarot game
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
325 card_wid.select(False) |
7bc1b3401ecb
primitivus: fixed state state change in tarot game
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
326 return |
7bc1b3401ecb
primitivus: fixed state state change in tarot game
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
327 self._checkState() |
150 | 328 if self.state == "ecart": |
329 if len(self.hand_wid.getSelected()) == 6: | |
222
3198bfd66daa
primitivus: refactoring to use urwid-satext which is now a separate project
Goffi <goffi@goffi.org>
parents:
162
diff
changeset
|
330 pop_up_widget = sat_widgets.ConfirmDialog(_("Do you put these cards in chien ?"), yes_cb=self.onEcartDone, no_cb=self.parent.host.removePopUp) |
150 | 331 self.parent.host.showPopUp(pop_up_widget) |
332 elif self.state == "play": | |
333 card = card_wid.getCard() | |
334 self.parent.host.bridge.tarotGamePlayCards(self.player_nick, self.referee, [(card.suit, card.value)], profile_key = self.parent.host.profile) | |
335 self.hand.remove(card) | |
336 self.hand_wid.update(self.hand) | |
337 self.state = "wait" | |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
338 |
150 | 339 def onEcartDone(self,button): |
340 """Called when player has finished is écart""" | |
341 ecart = [] | |
342 for card in self.hand_wid.getSelected(): | |
343 ecart.append((card.suit, card.value)) | |
344 self.hand.remove(card) | |
345 self.hand_wid.update(self.hand) | |
346 self.parent.host.bridge.tarotGamePlayCards(self.player_nick, self.referee, ecart, profile_key = self.parent.host.profile) | |
347 self.state = "wait" | |
348 self.parent.host.removePopUp() |