Mercurial > libervia-backend
annotate frontends/src/primitivus/card_game.py @ 686:ae95b0327412
plugin card_game: better PEP-8 compliance
author | souliane <souliane@mailoo.org> |
---|---|
date | Mon, 28 Oct 2013 18:49:01 +0100 |
parents | 2805fa3f4bdf |
children | af0d08a84cc6 |
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 |
682
2805fa3f4bdf
tools: moved src/tools/games.py to src/tools/frontends/
souliane <souliane@mailoo.org>
parents:
680
diff
changeset
|
22 from sat.tools.frontends.games import TarotCard |
225
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 |
686
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
26 |
150 | 27 class CardDisplayer(urwid.Text): |
28 """Show a card""" | |
29 signals = ['click'] | |
30 | |
31 def __init__(self, card): | |
32 self.__selected = False | |
33 self.card = card | |
34 urwid.Text.__init__(self, card.getAttrText()) | |
35 | |
36 def selectable(self): | |
37 return True | |
38 | |
39 def keypress(self, size, key): | |
40 if key == ' ': | |
41 self.select(not self.__selected) | |
42 self._emit('click') | |
43 return key | |
44 | |
159
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
153
diff
changeset
|
45 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
|
46 if urwid.is_mouse_event(event) and button == 1: |
159
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
153
diff
changeset
|
47 self.select(not self.__selected) |
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
153
diff
changeset
|
48 self._emit('click') |
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
153
diff
changeset
|
49 return True |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
50 |
159
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
153
diff
changeset
|
51 return False |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
52 |
150 | 53 def select(self, state=True): |
54 self.__selected = state | |
686
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
55 attr, txt = self.card.getAttrText() |
150 | 56 if self.__selected: |
686
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
57 attr += '_selected' |
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
58 self.set_text((attr, txt)) |
150 | 59 self._invalidate() |
60 | |
61 def isSelected(self): | |
62 return self.__selected | |
63 | |
64 def getCard(self): | |
65 return self.card | |
66 | |
67 def render(self, size, focus=False): | |
68 canvas = urwid.CompositeCanvas(urwid.Text.render(self, size, focus)) | |
69 if focus: | |
686
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
70 canvas.set_cursor((0, 0)) |
150 | 71 return canvas |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
72 |
686
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
73 |
144
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 |
686
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
78 def __init__(self, hand=[], selectable=False, on_click=None, user_data=None): |
150 | 79 """@param hand: list of Card""" |
80 self.__selectable = selectable | |
686
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
81 self.columns = urwid.Columns([], dividechars=1) |
150 | 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]: | |
686
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
94 return self.columns.keypress(size, key) |
150 | 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) | |
686
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
118 self.columns.column_types.append(('fixed', 3)) |
150 | 119 urwid.connect_signal(widget, 'click', self.__onClick) |
686
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
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 |
686
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
123 def __onClick(self, card_wid): |
150 | 124 self._emit('click', card_wid) |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
125 |
686
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
126 |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
127 class Card(TarotCard): |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
128 """This class is used to represent a card, logically |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
129 and give a text representation with attributes""" |
686
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
130 SIZE = 3 # size of a displayed card |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
131 |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
132 def __init__(self, suit, value): |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
133 """@param file: path of the PNG file""" |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
134 TarotCard.__init__(self, (suit, value)) |
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 getAttrText(self): |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
137 """return text representation of the card with attributes""" |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
138 try: |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
139 value = "%02i" % int(self.value) |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
140 except ValueError: |
686
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
141 value = self.value[0].upper() + self.value[1] |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
142 if self.suit == "atout": |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
143 if self.value == "excuse": |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
144 suit = 'c' |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
145 else: |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
146 suit = 'A' |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
147 color = 'neutral' |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
148 elif self.suit == "pique": |
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 == "trefle": |
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 = 'black' |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
154 elif self.suit == "coeur": |
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 elif self.suit == "carreau": |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
158 suit = u'♦' |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
159 color = 'red' |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
160 if self.bout: |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
161 color = 'special' |
686
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
162 return ('card_%s' % color, u"%s%s" % (value, suit)) |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
163 |
150 | 164 def getWidget(self): |
165 """Return a widget representing the card""" | |
166 return CardDisplayer(self) | |
167 | |
686
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
168 |
150 | 169 class Table(urwid.FlowWidget): |
170 """Represent the cards currently on the table""" | |
171 | |
172 def __init__(self): | |
173 self.top = self.left = self.bottom = self.right = None | |
174 | |
175 def putCard(self, location, card): | |
176 """Put a card on the table | |
177 @param location: where to put the card (top, left, bottom or right) | |
178 @param card: Card to play or None""" | |
686
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
179 assert location in ['top', 'left', 'bottom', 'right'] |
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
180 assert isinstance(card, Card) or card == None |
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
181 if [getattr(self, place) for place in ['top', 'left', 'bottom', 'right']].count(None) == 0: |
150 | 182 #If the table is full of card, we remove them |
183 self.top = self.left = self.bottom = self.right = None | |
184 setattr(self, location, card) | |
185 self._invalidate() | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
186 |
686
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
187 def rows(self, size, focus=False): |
150 | 188 return self.display_widget(size, focus).rows(size, focus) |
189 | |
190 def render(self, size, focus=False): | |
191 return self.display_widget(size, focus).render(size, focus) | |
192 | |
193 def display_widget(self, size, focus): | |
686
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
194 cards = {} |
150 | 195 max_col, = size |
196 separator = " - " | |
686
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
197 margin = max((max_col - Card.SIZE) / 2, 0) * ' ' |
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
198 margin_center = max((max_col - Card.SIZE * 2 - len(separator)) / 2, 0) * ' ' |
150 | 199 for location in ['top', 'left', 'bottom', 'right']: |
686
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
200 card = getattr(self, location) |
150 | 201 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
|
202 render_wid = [urwid.Text([margin, cards['top']]), |
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
203 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
|
204 urwid.Text([margin, cards['bottom']])] |
150 | 205 return urwid.Pile(render_wid) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
206 |
150 | 207 |
686
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
208 class CardGame(QuickCardGame, urwid.WidgetWrap): |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
209 """Widget for card games""" |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
210 |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
211 def __init__(self, parent, referee, players, player_nick): |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
212 QuickCardGame.__init__(self, parent, referee, players, player_nick) |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
213 self.loadCards() |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
214 self.top = urwid.Pile([urwid.Padding(urwid.Text(self.top_nick), 'center')]) |
150 | 215 #self.parent.host.debug() |
216 self.table = Table() | |
686
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
217 self.center = urwid.Columns([('fixed', len(self.left_nick), urwid.Filler(urwid.Text(self.left_nick))), |
150 | 218 urwid.Filler(self.table), |
686
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
219 ('fixed', len(self.right_nick), urwid.Filler(urwid.Text(self.right_nick))) |
150 | 220 ]) |
221 """urwid.Pile([urwid.Padding(self.top_card_wid,'center'), | |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
222 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
|
223 urwid.Padding(self.center_cards_wid,'center'), |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
224 ('fixed',len(self.right_nick),urwid.Text(self.right_nick)) |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
225 ]), |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
226 urwid.Padding(self.bottom_card_wid,'center') |
150 | 227 ])""" |
686
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
228 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
|
229 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
|
230 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
|
231 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
|
232 |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
233 def loadCards(self): |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
234 """Load all the cards in memory""" |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
235 QuickCardGame.loadCards(self) |
686
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
236 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
|
237 card = Card('atout', value) |
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
238 self.cards[card.suit, card.value] = card |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
239 self.deck.append(card) |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
240 for suit in ["pique", "coeur", "carreau", "trefle"]: |
686
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
241 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
|
242 card = Card(suit, value) |
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
243 self.cards[card.suit, card.value] = card |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
244 self.deck.append(card) |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
245 |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
246 def newGame(self, hand): |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
247 """Start a new game, with given hand""" |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
248 QuickCardGame.newGame(self, hand) |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
249 self.hand_wid.update(self.hand) |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
250 self.parent.host.redraw() |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
251 |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
252 def contratSelected(self, data): |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
253 """Called when the contrat has been choosed |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
254 @param data: form result""" |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
255 contrat = data[0][1] |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
256 QuickCardGame.contratSelected(self, contrat) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
257 |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
258 def chooseContrat(self, xml_data): |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
259 """Called when the player as to select his contrat |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
260 @param xml_data: SàT xml representation of the form""" |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
261 misc = {'callback': self.contratSelected} |
686
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
262 form = XMLUI(self.parent.host, xml_data, title=_('Please choose your contrat'), options=['NO_CANCEL'], misc=misc) |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
263 form.show() |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
264 |
150 | 265 def showCards(self, game_stage, cards, data): |
266 """Display cards in the middle of the game (to show for e.g. chien ou poignée)""" | |
267 QuickCardGame.showCards(self, game_stage, cards, data) | |
268 self.center.widget_list[1] = urwid.Filler(Hand(self.to_show)) | |
269 self.parent.host.redraw() | |
270 | |
271 def myTurn(self): | |
272 QuickCardGame.myTurn(self) | |
273 | |
274 def showScores(self, xml_data, winners, loosers): | |
275 """Called when the player as to select hist contrat | |
276 @param xml_data: SàT xml representation of the form""" | |
328 | 277 def _new_game(ignore): |
278 self.resetRound() | |
686
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
279 for location in ['top', 'left', 'bottom', 'right']: |
328 | 280 self.table.putCard(location, None) |
281 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
|
282 self.parent.host.bridge.tarotGameReady(self.player_nick, self.referee, self.parent.host.profile) |
328 | 283 if not winners and not loosers: |
284 title = _("Draw game") | |
285 else: | |
286 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
|
287 form = XMLUI(self.parent.host, xml_data, title=title, options=['NO_CANCEL'], misc={'callback': _new_game}) |
162 | 288 form.show() |
150 | 289 |
290 def invalidCards(self, phase, played_cards, invalid_cards): | |
291 """Invalid cards have been played | |
292 @param phase: phase of the game | |
293 @param played_cards: all the cards played | |
294 @param invalid_cards: cards which are invalid""" | |
295 QuickCardGame.invalidCards(self, phase, played_cards, invalid_cards) | |
296 self.hand_wid.update(self.hand) | |
686
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
297 if self._autoplay == None: # No dialog if there is autoplay |
162 | 298 self.parent.host.notify(_('Cards played are invalid !')) |
150 | 299 self.parent.host.redraw() |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
300 |
150 | 301 def cardsPlayed(self, player, cards): |
302 """A card has been played by player""" | |
303 QuickCardGame.cardsPlayed(self, player, cards) | |
686
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
304 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
|
305 self._checkState() |
150 | 306 self.parent.host.redraw() |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
307 |
323
7bc1b3401ecb
primitivus: fixed state state change in tarot game
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
308 def _checkState(self): |
686
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
309 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
|
310 self.center.widget_list[1] = urwid.Filler(self.table) # we show again the table |
150 | 311 if self.state == "chien": |
312 self.to_show = [] | |
313 self.state = "wait" | |
314 elif self.state == "wait_for_ecart": | |
315 self.state = "ecart" | |
316 self.hand.extend(self.to_show) | |
317 self.hand.sort() | |
318 self.to_show = [] | |
319 self.hand_wid.update(self.hand) | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
320 |
323
7bc1b3401ecb
primitivus: fixed state state change in tarot game
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
321 ##EVENTS## |
7bc1b3401ecb
primitivus: fixed state state change in tarot game
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
322 def onClick(self, hand, card_wid): |
7bc1b3401ecb
primitivus: fixed state state change in tarot game
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
323 """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
|
324 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
|
325 #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
|
326 card_wid.select(False) |
7bc1b3401ecb
primitivus: fixed state state change in tarot game
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
327 return |
7bc1b3401ecb
primitivus: fixed state state change in tarot game
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
328 self._checkState() |
686
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
329 if self.state == "ecart": |
150 | 330 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
|
331 pop_up_widget = sat_widgets.ConfirmDialog(_("Do you put these cards in chien ?"), yes_cb=self.onEcartDone, no_cb=self.parent.host.removePopUp) |
150 | 332 self.parent.host.showPopUp(pop_up_widget) |
333 elif self.state == "play": | |
334 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
|
335 self.parent.host.bridge.tarotGamePlayCards(self.player_nick, self.referee, [(card.suit, card.value)], self.parent.host.profile) |
150 | 336 self.hand.remove(card) |
337 self.hand_wid.update(self.hand) | |
338 self.state = "wait" | |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
339 |
686
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
340 def onEcartDone(self, button): |
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
341 """Called when player has finished his écart""" |
150 | 342 ecart = [] |
343 for card in self.hand_wid.getSelected(): | |
344 ecart.append((card.suit, card.value)) | |
345 self.hand.remove(card) | |
346 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
|
347 self.parent.host.bridge.tarotGamePlayCards(self.player_nick, self.referee, ecart, self.parent.host.profile) |
150 | 348 self.state = "wait" |
349 self.parent.host.removePopUp() |