Mercurial > libervia-backend
annotate frontends/src/primitivus/card_game.py @ 1316:8adcdf2cdfe1 frontends_multi_profiles
core: added a "profileConnecting" method check:
if a plugin has a profileConnecting method, it will be called when the profile is being connected but before the client stream is started. That can be usefull if things need to be done before the communication is started: e.g. putting triggers, loading PersistentDict, etc.
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 09 Feb 2015 21:39:51 +0100 |
parents | c0f15e52695a |
children |
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 |
1106
e2e1e27a3680
frontends: XMLUI refactoring + dialogs:
Goffi <goffi@goffi.org>
parents:
911
diff
changeset
|
25 from sat_frontends.primitivus import xmlui |
1158
c0f15e52695a
primitivus: use of new keys modules from Urwid SàText
Goffi <goffi@goffi.org>
parents:
1106
diff
changeset
|
26 from sat_frontends.primitivus.keys import action_key_map as a_key |
150 | 27 |
686
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
28 |
150 | 29 class CardDisplayer(urwid.Text): |
30 """Show a card""" | |
31 signals = ['click'] | |
32 | |
33 def __init__(self, card): | |
34 self.__selected = False | |
35 self.card = card | |
36 urwid.Text.__init__(self, card.getAttrText()) | |
37 | |
38 def selectable(self): | |
39 return True | |
40 | |
41 def keypress(self, size, key): | |
1158
c0f15e52695a
primitivus: use of new keys modules from Urwid SàText
Goffi <goffi@goffi.org>
parents:
1106
diff
changeset
|
42 if key == a_key['CARD_SELECT']: |
150 | 43 self.select(not self.__selected) |
44 self._emit('click') | |
45 return key | |
46 | |
159
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
153
diff
changeset
|
47 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
|
48 if urwid.is_mouse_event(event) and button == 1: |
159
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
153
diff
changeset
|
49 self.select(not self.__selected) |
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
153
diff
changeset
|
50 self._emit('click') |
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
153
diff
changeset
|
51 return True |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
52 |
159
2fa58703f1b7
Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents:
153
diff
changeset
|
53 return False |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
54 |
150 | 55 def select(self, state=True): |
56 self.__selected = state | |
686
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
57 attr, txt = self.card.getAttrText() |
150 | 58 if self.__selected: |
686
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
59 attr += '_selected' |
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
60 self.set_text((attr, txt)) |
150 | 61 self._invalidate() |
62 | |
63 def isSelected(self): | |
64 return self.__selected | |
65 | |
66 def getCard(self): | |
67 return self.card | |
68 | |
69 def render(self, size, focus=False): | |
70 canvas = urwid.CompositeCanvas(urwid.Text.render(self, size, focus)) | |
71 if focus: | |
686
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
72 canvas.set_cursor((0, 0)) |
150 | 73 return canvas |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
74 |
686
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
75 |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
76 class Hand(urwid.WidgetWrap): |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
77 """Used to display several cards, and manage a hand""" |
150 | 78 signals = ['click'] |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
79 |
686
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
80 def __init__(self, hand=[], selectable=False, on_click=None, user_data=None): |
150 | 81 """@param hand: list of Card""" |
82 self.__selectable = selectable | |
686
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
83 self.columns = urwid.Columns([], dividechars=1) |
150 | 84 if on_click: |
85 urwid.connect_signal(self, 'click', on_click, user_data) | |
86 if hand: | |
87 self.update(hand) | |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
88 urwid.WidgetWrap.__init__(self, self.columns) |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
89 |
150 | 90 def selectable(self): |
91 return self.__selectable | |
92 | |
93 def keypress(self, size, key): | |
94 | |
95 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
|
96 return self.columns.keypress(size, key) |
150 | 97 else: |
98 #No card displayed, we still have to manage the clicks | |
1158
c0f15e52695a
primitivus: use of new keys modules from Urwid SàText
Goffi <goffi@goffi.org>
parents:
1106
diff
changeset
|
99 if key == a_key['CARD_SELECT']: |
150 | 100 self._emit('click', None) |
101 return key | |
102 | |
103 def getSelected(self): | |
104 """Return a list of selected cards""" | |
105 _selected = [] | |
106 for wid in self.columns.widget_list: | |
153
f197b52796ee
Primitivus: begining of management for actionResult
Goffi <goffi@goffi.org>
parents:
150
diff
changeset
|
107 if isinstance(wid, CardDisplayer) and wid.isSelected(): |
150 | 108 _selected.append(wid.getCard()) |
109 return _selected | |
110 | |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
111 def update(self, hand): |
150 | 112 """Update the hand displayed in this widget |
113 @param hand: list of Card""" | |
687
af0d08a84cc6
primitivus card_game: bug fix and improvement
souliane <souliane@mailoo.org>
parents:
686
diff
changeset
|
114 try: |
af0d08a84cc6
primitivus card_game: bug fix and improvement
souliane <souliane@mailoo.org>
parents:
686
diff
changeset
|
115 del self.columns.widget_list[:] |
af0d08a84cc6
primitivus card_game: bug fix and improvement
souliane <souliane@mailoo.org>
parents:
686
diff
changeset
|
116 del self.columns.column_types[:] |
af0d08a84cc6
primitivus card_game: bug fix and improvement
souliane <souliane@mailoo.org>
parents:
686
diff
changeset
|
117 except IndexError: |
af0d08a84cc6
primitivus card_game: bug fix and improvement
souliane <souliane@mailoo.org>
parents:
686
diff
changeset
|
118 pass |
af0d08a84cc6
primitivus card_game: bug fix and improvement
souliane <souliane@mailoo.org>
parents:
686
diff
changeset
|
119 self.columns.contents.append((urwid.Text(''), ('weight', 1, False))) |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
120 for card in hand: |
150 | 121 widget = CardDisplayer(card) |
122 self.columns.widget_list.append(widget) | |
686
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
123 self.columns.column_types.append(('fixed', 3)) |
150 | 124 urwid.connect_signal(widget, 'click', self.__onClick) |
686
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
125 self.columns.contents.append((urwid.Text(''), ('weight', 1, False))) |
911
b12706d164d7
primitivus: removed deprecated use of set_focus
Goffi <goffi@goffi.org>
parents:
878
diff
changeset
|
126 self.columns.focus_position = 1 |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
127 |
686
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
128 def __onClick(self, card_wid): |
150 | 129 self._emit('click', card_wid) |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
130 |
686
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
131 |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
132 class Card(TarotCard): |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
133 """This class is used to represent a card, logically |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
134 and give a text representation with attributes""" |
686
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
135 SIZE = 3 # size of a displayed card |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
136 |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
137 def __init__(self, suit, value): |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
138 """@param file: path of the PNG file""" |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
139 TarotCard.__init__(self, (suit, value)) |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
140 |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
141 def getAttrText(self): |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
142 """return text representation of the card with attributes""" |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
143 try: |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
144 value = "%02i" % int(self.value) |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
145 except ValueError: |
686
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
146 value = self.value[0].upper() + self.value[1] |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
147 if self.suit == "atout": |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
148 if self.value == "excuse": |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
149 suit = 'c' |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
150 else: |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
151 suit = 'A' |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
152 color = 'neutral' |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
153 elif self.suit == "pique": |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
154 suit = u'♠' |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
155 color = 'black' |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
156 elif self.suit == "trefle": |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
157 suit = u'♣' |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
158 color = 'black' |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
159 elif self.suit == "coeur": |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
160 suit = u'♥' |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
161 color = 'red' |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
162 elif self.suit == "carreau": |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
163 suit = u'♦' |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
164 color = 'red' |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
165 if self.bout: |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
166 color = 'special' |
686
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
167 return ('card_%s' % color, u"%s%s" % (value, suit)) |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
168 |
150 | 169 def getWidget(self): |
170 """Return a widget representing the card""" | |
171 return CardDisplayer(self) | |
172 | |
686
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
173 |
150 | 174 class Table(urwid.FlowWidget): |
175 """Represent the cards currently on the table""" | |
176 | |
177 def __init__(self): | |
178 self.top = self.left = self.bottom = self.right = None | |
179 | |
180 def putCard(self, location, card): | |
181 """Put a card on the table | |
182 @param location: where to put the card (top, left, bottom or right) | |
183 @param card: Card to play or None""" | |
686
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
184 assert location in ['top', 'left', 'bottom', 'right'] |
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
185 assert isinstance(card, Card) or card == None |
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
186 if [getattr(self, place) for place in ['top', 'left', 'bottom', 'right']].count(None) == 0: |
150 | 187 #If the table is full of card, we remove them |
188 self.top = self.left = self.bottom = self.right = None | |
189 setattr(self, location, card) | |
190 self._invalidate() | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
191 |
686
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
192 def rows(self, size, focus=False): |
150 | 193 return self.display_widget(size, focus).rows(size, focus) |
194 | |
195 def render(self, size, focus=False): | |
196 return self.display_widget(size, focus).render(size, focus) | |
197 | |
198 def display_widget(self, size, focus): | |
686
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
199 cards = {} |
150 | 200 max_col, = size |
201 separator = " - " | |
686
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
202 margin = max((max_col - Card.SIZE) / 2, 0) * ' ' |
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
203 margin_center = max((max_col - Card.SIZE * 2 - len(separator)) / 2, 0) * ' ' |
150 | 204 for location in ['top', 'left', 'bottom', 'right']: |
686
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
205 card = getattr(self, location) |
150 | 206 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
|
207 render_wid = [urwid.Text([margin, cards['top']]), |
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
208 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
|
209 urwid.Text([margin, cards['bottom']])] |
150 | 210 return urwid.Pile(render_wid) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
211 |
150 | 212 |
686
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
213 class CardGame(QuickCardGame, urwid.WidgetWrap): |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
214 """Widget for card games""" |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
215 |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
216 def __init__(self, parent, referee, players, player_nick): |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
217 QuickCardGame.__init__(self, parent, referee, players, player_nick) |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
218 self.loadCards() |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
219 self.top = urwid.Pile([urwid.Padding(urwid.Text(self.top_nick), 'center')]) |
150 | 220 #self.parent.host.debug() |
221 self.table = Table() | |
686
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
222 self.center = urwid.Columns([('fixed', len(self.left_nick), urwid.Filler(urwid.Text(self.left_nick))), |
150 | 223 urwid.Filler(self.table), |
686
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
224 ('fixed', len(self.right_nick), urwid.Filler(urwid.Text(self.right_nick))) |
150 | 225 ]) |
226 """urwid.Pile([urwid.Padding(self.top_card_wid,'center'), | |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
227 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
|
228 urwid.Padding(self.center_cards_wid,'center'), |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
229 ('fixed',len(self.right_nick),urwid.Text(self.right_nick)) |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
230 ]), |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
231 urwid.Padding(self.bottom_card_wid,'center') |
150 | 232 ])""" |
686
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
233 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
|
234 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
|
235 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
|
236 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
|
237 |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
238 def loadCards(self): |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
239 """Load all the cards in memory""" |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
240 QuickCardGame.loadCards(self) |
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, 22)) + ['excuse']: |
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
242 card = Card('atout', 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 for suit in ["pique", "coeur", "carreau", "trefle"]: |
686
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
246 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
|
247 card = Card(suit, value) |
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
248 self.cards[card.suit, card.value] = card |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
249 self.deck.append(card) |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
250 |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
251 def newGame(self, hand): |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
252 """Start a new game, with given hand""" |
878
36c6495d86b0
plugin card_game: update to use the new XMLUI mechanism:
souliane <souliane@mailoo.org>
parents:
811
diff
changeset
|
253 if hand is []: # reset the display after the scores have been showed |
36c6495d86b0
plugin card_game: update to use the new XMLUI mechanism:
souliane <souliane@mailoo.org>
parents:
811
diff
changeset
|
254 self.resetRound() |
36c6495d86b0
plugin card_game: update to use the new XMLUI mechanism:
souliane <souliane@mailoo.org>
parents:
811
diff
changeset
|
255 for location in ['top', 'left', 'bottom', 'right']: |
36c6495d86b0
plugin card_game: update to use the new XMLUI mechanism:
souliane <souliane@mailoo.org>
parents:
811
diff
changeset
|
256 self.table.putCard(location, None) |
36c6495d86b0
plugin card_game: update to use the new XMLUI mechanism:
souliane <souliane@mailoo.org>
parents:
811
diff
changeset
|
257 self.parent.host.redraw() |
36c6495d86b0
plugin card_game: update to use the new XMLUI mechanism:
souliane <souliane@mailoo.org>
parents:
811
diff
changeset
|
258 self.parent.host.bridge.tarotGameReady(self.player_nick, self.referee, self.parent.host.profile) |
36c6495d86b0
plugin card_game: update to use the new XMLUI mechanism:
souliane <souliane@mailoo.org>
parents:
811
diff
changeset
|
259 return |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
260 QuickCardGame.newGame(self, hand) |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
261 self.hand_wid.update(self.hand) |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
262 self.parent.host.redraw() |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
263 |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
264 def contratSelected(self, data): |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
265 """Called when the contrat has been choosed |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
266 @param data: form result""" |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
267 contrat = data[0][1] |
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
268 QuickCardGame.contratSelected(self, contrat) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
269 |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
270 def chooseContrat(self, xml_data): |
878
36c6495d86b0
plugin card_game: update to use the new XMLUI mechanism:
souliane <souliane@mailoo.org>
parents:
811
diff
changeset
|
271 """Called when the player has to select his contrat |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
272 @param xml_data: SàT xml representation of the form""" |
1106
e2e1e27a3680
frontends: XMLUI refactoring + dialogs:
Goffi <goffi@goffi.org>
parents:
911
diff
changeset
|
273 form = xmlui.create(self.parent.host, xml_data, title=_('Please choose your contrat'), flags=['NO_CANCEL']) |
687
af0d08a84cc6
primitivus card_game: bug fix and improvement
souliane <souliane@mailoo.org>
parents:
686
diff
changeset
|
274 form.show(valign='top') |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
275 |
150 | 276 def showCards(self, game_stage, cards, data): |
277 """Display cards in the middle of the game (to show for e.g. chien ou poignée)""" | |
278 QuickCardGame.showCards(self, game_stage, cards, data) | |
279 self.center.widget_list[1] = urwid.Filler(Hand(self.to_show)) | |
280 self.parent.host.redraw() | |
281 | |
282 def myTurn(self): | |
283 QuickCardGame.myTurn(self) | |
284 | |
285 def showScores(self, xml_data, winners, loosers): | |
878
36c6495d86b0
plugin card_game: update to use the new XMLUI mechanism:
souliane <souliane@mailoo.org>
parents:
811
diff
changeset
|
286 """Called when the round is over, display the scores |
150 | 287 @param xml_data: SàT xml representation of the form""" |
328 | 288 if not winners and not loosers: |
289 title = _("Draw game") | |
290 else: | |
291 title = _('You win \o/') if self.player_nick in winners else _('You loose :(') | |
1106
e2e1e27a3680
frontends: XMLUI refactoring + dialogs:
Goffi <goffi@goffi.org>
parents:
911
diff
changeset
|
292 form = xmlui.create(self.parent.host, xml_data, title=title, flags=['NO_CANCEL']) |
162 | 293 form.show() |
150 | 294 |
295 def invalidCards(self, phase, played_cards, invalid_cards): | |
296 """Invalid cards have been played | |
297 @param phase: phase of the game | |
298 @param played_cards: all the cards played | |
299 @param invalid_cards: cards which are invalid""" | |
300 QuickCardGame.invalidCards(self, phase, played_cards, invalid_cards) | |
301 self.hand_wid.update(self.hand) | |
686
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
302 if self._autoplay == None: # No dialog if there is autoplay |
162 | 303 self.parent.host.notify(_('Cards played are invalid !')) |
150 | 304 self.parent.host.redraw() |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
305 |
150 | 306 def cardsPlayed(self, player, cards): |
307 """A card has been played by player""" | |
308 QuickCardGame.cardsPlayed(self, player, cards) | |
686
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
309 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
|
310 self._checkState() |
150 | 311 self.parent.host.redraw() |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
312 |
323
7bc1b3401ecb
primitivus: fixed state state change in tarot game
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
313 def _checkState(self): |
686
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
314 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
|
315 self.center.widget_list[1] = urwid.Filler(self.table) # we show again the table |
150 | 316 if self.state == "chien": |
317 self.to_show = [] | |
318 self.state = "wait" | |
319 elif self.state == "wait_for_ecart": | |
320 self.state = "ecart" | |
321 self.hand.extend(self.to_show) | |
322 self.hand.sort() | |
323 self.to_show = [] | |
324 self.hand_wid.update(self.hand) | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
325 |
323
7bc1b3401ecb
primitivus: fixed state state change in tarot game
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
326 ##EVENTS## |
7bc1b3401ecb
primitivus: fixed state state change in tarot game
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
327 def onClick(self, hand, card_wid): |
7bc1b3401ecb
primitivus: fixed state state change in tarot game
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
328 """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
|
329 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
|
330 #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
|
331 card_wid.select(False) |
7bc1b3401ecb
primitivus: fixed state state change in tarot game
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
332 return |
7bc1b3401ecb
primitivus: fixed state state change in tarot game
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
333 self._checkState() |
686
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
334 if self.state == "ecart": |
150 | 335 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
|
336 pop_up_widget = sat_widgets.ConfirmDialog(_("Do you put these cards in chien ?"), yes_cb=self.onEcartDone, no_cb=self.parent.host.removePopUp) |
150 | 337 self.parent.host.showPopUp(pop_up_widget) |
338 elif self.state == "play": | |
339 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
|
340 self.parent.host.bridge.tarotGamePlayCards(self.player_nick, self.referee, [(card.suit, card.value)], self.parent.host.profile) |
150 | 341 self.hand.remove(card) |
342 self.hand_wid.update(self.hand) | |
343 self.state = "wait" | |
144
80661755ea8d
Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
344 |
686
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
345 def onEcartDone(self, button): |
ae95b0327412
plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
346 """Called when player has finished his écart""" |
150 | 347 ecart = [] |
348 for card in self.hand_wid.getSelected(): | |
349 ecart.append((card.suit, card.value)) | |
350 self.hand.remove(card) | |
351 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
|
352 self.parent.host.bridge.tarotGamePlayCards(self.player_nick, self.referee, ecart, self.parent.host.profile) |
150 | 353 self.state = "wait" |
354 self.parent.host.removePopUp() |