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