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