annotate frontends/src/primitivus/card_game.py @ 405:10b4f577d0c0

MUC update to follow wokkel's MUC branch update - bridge methods have been updated (API change) - plugin XEP-0045 has been updated
author Goffi <goffi@goffi.org>
date Sat, 08 Oct 2011 18:43:17 +0200
parents 809733b8d9be
children cf005701624b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
144
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/python
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
3
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
4 """
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
5 Primitivus: a SAT frontend
228
b1794cbb88e5 2011 copyright upgrade
Goffi <goffi@goffi.org>
parents: 225
diff changeset
6 Copyright (C) 2009, 2010, 2011 Jérôme Poisson (goffi@goffi.org)
144
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
7
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
8 This program is free software: you can redistribute it and/or modify
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
9 it under the terms of the GNU General Public License as published by
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
10 the Free Software Foundation, either version 3 of the License, or
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
11 (at your option) any later version.
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
12
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
13 This program is distributed in the hope that it will be useful,
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
16 GNU General Public License for more details.
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
17
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
18 You should have received a copy of the GNU General Public License
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
20 """
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
21
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
22 import urwid
222
3198bfd66daa primitivus: refactoring to use urwid-satext which is now a separate project
Goffi <goffi@goffi.org>
parents: 162
diff changeset
23 from urwid_satext import sat_widgets
225
fd9b7834d98a distutils installation script, draft
Goffi <goffi@goffi.org>
parents: 223
diff changeset
24 from sat.tools.games import TarotCard
fd9b7834d98a distutils installation script, draft
Goffi <goffi@goffi.org>
parents: 223
diff changeset
25 from sat_frontends.quick_frontend.quick_card_game import QuickCardGame
fd9b7834d98a distutils installation script, draft
Goffi <goffi@goffi.org>
parents: 223
diff changeset
26 from sat_frontends.primitivus.xmlui import XMLUI
150
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
27
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
28 class CardDisplayer(urwid.Text):
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
29 """Show a card"""
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
30 signals = ['click']
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
31
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
32 def __init__(self, card):
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
33 self.__selected = False
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
34 self.card = card
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
35 urwid.Text.__init__(self, card.getAttrText())
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
36
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
37 def selectable(self):
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
38 return True
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
39
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
40 def keypress(self, size, key):
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
41 if key == ' ':
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
42 self.select(not self.__selected)
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
43 self._emit('click')
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
44 return key
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
45
159
2fa58703f1b7 Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents: 153
diff changeset
46 def mouse_event(self, size, event, button, x, y, focus):
2fa58703f1b7 Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents: 153
diff changeset
47 if urwid.is_mouse_press(event) and button == 1:
2fa58703f1b7 Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents: 153
diff changeset
48 self.select(not self.__selected)
2fa58703f1b7 Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents: 153
diff changeset
49 self._emit('click')
2fa58703f1b7 Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents: 153
diff changeset
50 return True
2fa58703f1b7 Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents: 153
diff changeset
51
2fa58703f1b7 Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents: 153
diff changeset
52 return False
2fa58703f1b7 Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents: 153
diff changeset
53
150
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
54 def select(self, state=True):
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
55 self.__selected = state
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
56 attr,txt = self.card.getAttrText()
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
57 if self.__selected:
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
58 attr+='_selected'
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
59 self.set_text((attr,txt))
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
60 self._invalidate()
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
61
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
62 def isSelected(self):
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
63 return self.__selected
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
64
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
65 def getCard(self):
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
66 return self.card
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
67
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
68 def render(self, size, focus=False):
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
69 canvas = urwid.CompositeCanvas(urwid.Text.render(self, size, focus))
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
70 if focus:
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
71 canvas.set_cursor((0,0))
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
72 return canvas
144
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
73
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
74 class Hand(urwid.WidgetWrap):
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
75 """Used to display several cards, and manage a hand"""
150
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
76 signals = ['click']
144
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
77
150
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
78 def __init__(self, hand=[], selectable = False, on_click=None, user_data=None):
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
79 """@param hand: list of Card"""
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
80 self.__selectable = selectable
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
81 self.columns = urwid.Columns([],dividechars=1)
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
82 if on_click:
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
83 urwid.connect_signal(self, 'click', on_click, user_data)
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
84 if hand:
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
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
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
88 def selectable(self):
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
89 return self.__selectable
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
90
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
91 def keypress(self, size, key):
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
92
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
93 if CardDisplayer in [wid.__class__ for wid in self.columns.widget_list]:
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
94 return self.columns.keypress(size,key)
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
95 else:
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
96 #No card displayed, we still have to manage the clicks
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
97 if key == ' ':
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
98 self._emit('click', None)
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
99 return key
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
100
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
101 def getSelected(self):
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
102 """Return a list of selected cards"""
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
103 _selected = []
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
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
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
106 _selected.append(wid.getCard())
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
107 return _selected
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
108
144
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
109 def update(self, hand):
150
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
110 """Update the hand displayed in this widget
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
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[:]
150
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
114 self.columns.widget_list.append(urwid.Text(''))
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
115 self.columns.column_types.append(('weight',1))
144
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
116 for card in hand:
150
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
117 widget = CardDisplayer(card)
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
118 self.columns.widget_list.append(widget)
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
119 self.columns.column_types.append(('fixed',3))
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
120 urwid.connect_signal(widget, 'click', self.__onClick)
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
121 self.columns.widget_list.append(urwid.Text(''))
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
122 self.columns.column_types.append(('weight',1))
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
123 self.columns.set_focus(1)
144
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
124
150
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
125 def __onClick(self,card_wid):
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
126 self._emit('click', card_wid)
144
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
127
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
128 class Card(TarotCard):
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
129 """This class is used to represent a card, logically
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
130 and give a text representation with attributes"""
150
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
131 SIZE = 3 #size of a displayed card
144
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
132
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
133 def __init__(self, suit, value):
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
134 """@param file: path of the PNG file"""
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
135 TarotCard.__init__(self, (suit, value))
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 getAttrText(self):
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
138 """return text representation of the card with attributes"""
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
139 try:
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
140 value = "%02i" % int(self.value)
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
141 except ValueError:
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
142 value = self.value[0].upper()+self.value[1]
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
143 if self.suit == "atout":
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
144 if self.value == "excuse":
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
145 suit = 'c'
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
146 else:
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
147 suit = 'A'
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
148 color = 'neutral'
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
149 elif self.suit == "pique":
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
150 suit = u'♠'
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
151 color = 'black'
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
152 elif self.suit == "trefle":
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 == "coeur":
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 = 'red'
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
158 elif self.suit == "carreau":
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 if self.bout:
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
162 color = 'special'
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
163 return ('card_%s' % color,u"%s%s" % (value,suit))
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
164
150
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
165 def getWidget(self):
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
166 """Return a widget representing the card"""
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
167 return CardDisplayer(self)
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
168
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
169 class Table(urwid.FlowWidget):
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
170 """Represent the cards currently on the table"""
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
171
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
172 def __init__(self):
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
173 self.top = self.left = self.bottom = self.right = None
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
174
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
175 def putCard(self, location, card):
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
176 """Put a card on the table
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
177 @param location: where to put the card (top, left, bottom or right)
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
178 @param card: Card to play or None"""
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
179 assert location in ['top','left','bottom','right']
153
f197b52796ee Primitivus: begining of management for actionResult
Goffi <goffi@goffi.org>
parents: 150
diff changeset
180 assert isinstance(card,Card) or card == None
150
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
181 if [getattr(self, place) for place in ['top','left','bottom','right']].count(None) == 0:
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
182 #If the table is full of card, we remove them
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
183 self.top = self.left = self.bottom = self.right = None
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
184 setattr(self, location, card)
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
185 self._invalidate()
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
186
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
187 def rows(self,size,focus=False):
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
188 return self.display_widget(size, focus).rows(size, focus)
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
189
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
190 def render(self, size, focus=False):
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
191 return self.display_widget(size, focus).render(size, focus)
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
192
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
193 def display_widget(self, size, focus):
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
194 cards={}
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
195 max_col, = size
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
196 separator = " - "
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
197 margin = max((max_col-Card.SIZE)/2,0) * ' '
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
198 margin_center = max((max_col-Card.SIZE*2-len(separator))/2,0) * ' '
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
199 for location in ['top', 'left', 'bottom', 'right']:
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
200 card = getattr(self,location)
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
201 cards[location] = card.getAttrText() if card else Card.SIZE * ' '
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
202 render_wid = [urwid.Text([margin,cards['top']]),
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
203 urwid.Text([margin_center,cards['left'],separator,cards['right']]),
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
204 urwid.Text([margin,cards['bottom']])]
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
205 return urwid.Pile(render_wid)
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
206
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
207
144
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
208 class CardGame(QuickCardGame,urwid.WidgetWrap):
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
209 """Widget for card games"""
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
210
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
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
215 #self.parent.host.debug()
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
216 self.table = Table()
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
217 self.center = urwid.Columns([('fixed',len(self.left_nick),urwid.Filler(urwid.Text(self.left_nick))),
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
218 urwid.Filler(self.table),
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
219 ('fixed',len(self.right_nick),urwid.Filler(urwid.Text(self.right_nick)))
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
220 ])
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
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
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
227 ])"""
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
228 self.hand_wid = Hand(selectable = True, on_click = self.onClick)
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
229 self.main_frame = urwid.Frame(self.center,header=self.top, footer=self.hand_wid, focus_part='footer')
144
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
230 urwid.WidgetWrap.__init__(self,self.main_frame)
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
231 self.parent.host.bridge.tarotGameReady(player_nick, referee, profile_key = self.parent.host.profile)
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
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)
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
236 for value in map(str,range(1,22))+['excuse']:
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
237 card = Card('atout',value)
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
238 self.cards[card.suit, card.value]=card
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"]:
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
241 for value in map(str,range(1,11))+["valet","cavalier","dame","roi"]:
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
242 card = Card(suit,value)
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
243 self.cards[card.suit, card.value]=card
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()
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
251
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)
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
257
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}
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
262 form = XMLUI(self.parent.host, xml_data, title = _('Please choose your contrat'), options = ['NO_CANCEL'], misc = misc)
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
263 form.show()
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
264
150
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
265 def showCards(self, game_stage, cards, data):
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
266 """Display cards in the middle of the game (to show for e.g. chien ou poignée)"""
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
267 QuickCardGame.showCards(self, game_stage, cards, data)
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
268 self.center.widget_list[1] = urwid.Filler(Hand(self.to_show))
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
269 self.parent.host.redraw()
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
270
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
271 def myTurn(self):
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
272 QuickCardGame.myTurn(self)
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
273
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
274 def showScores(self, xml_data, winners, loosers):
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
275 """Called when the player as to select hist contrat
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
276 @param xml_data: SàT xml representation of the form"""
328
809733b8d9be Tarot game:
Goffi <goffi@goffi.org>
parents: 323
diff changeset
277 def _new_game(ignore):
809733b8d9be Tarot game:
Goffi <goffi@goffi.org>
parents: 323
diff changeset
278 self.resetRound()
809733b8d9be Tarot game:
Goffi <goffi@goffi.org>
parents: 323
diff changeset
279 for location in ['top','left','bottom','right']:
809733b8d9be Tarot game:
Goffi <goffi@goffi.org>
parents: 323
diff changeset
280 self.table.putCard(location, None)
809733b8d9be Tarot game:
Goffi <goffi@goffi.org>
parents: 323
diff changeset
281 self.parent.host.redraw()
809733b8d9be Tarot game:
Goffi <goffi@goffi.org>
parents: 323
diff changeset
282 self.parent.host.bridge.tarotGameReady(self.player_nick, self.referee, profile_key = self.parent.host.profile)
809733b8d9be Tarot game:
Goffi <goffi@goffi.org>
parents: 323
diff changeset
283 if not winners and not loosers:
809733b8d9be Tarot game:
Goffi <goffi@goffi.org>
parents: 323
diff changeset
284 title = _("Draw game")
809733b8d9be Tarot game:
Goffi <goffi@goffi.org>
parents: 323
diff changeset
285 else:
809733b8d9be Tarot game:
Goffi <goffi@goffi.org>
parents: 323
diff changeset
286 title = _('You win \o/') if self.player_nick in winners else _('You loose :(')
809733b8d9be Tarot game:
Goffi <goffi@goffi.org>
parents: 323
diff changeset
287 form = XMLUI(self.parent.host, xml_data, title = title, options = ['NO_CANCEL'], misc={'callback':_new_game})
162
ae50b53ff868 misc Tarot fixes
Goffi <goffi@goffi.org>
parents: 159
diff changeset
288 form.show()
150
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
289
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
290 def invalidCards(self, phase, played_cards, invalid_cards):
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
291 """Invalid cards have been played
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
292 @param phase: phase of the game
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
293 @param played_cards: all the cards played
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
294 @param invalid_cards: cards which are invalid"""
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
295 QuickCardGame.invalidCards(self, phase, played_cards, invalid_cards)
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
296 self.hand_wid.update(self.hand)
162
ae50b53ff868 misc Tarot fixes
Goffi <goffi@goffi.org>
parents: 159
diff changeset
297 if self._autoplay==None: #No dialog if there is autoplay
ae50b53ff868 misc Tarot fixes
Goffi <goffi@goffi.org>
parents: 159
diff changeset
298 self.parent.host.notify(_('Cards played are invalid !'))
150
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
299 self.parent.host.redraw()
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
300
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
301 def cardsPlayed(self, player, cards):
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
302 """A card has been played by player"""
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
303 QuickCardGame.cardsPlayed(self, player, cards)
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
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
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
306 self.parent.host.redraw()
323
7bc1b3401ecb primitivus: fixed state state change in tarot game
Goffi <goffi@goffi.org>
parents: 228
diff changeset
307
7bc1b3401ecb primitivus: fixed state state change in tarot game
Goffi <goffi@goffi.org>
parents: 228
diff changeset
308 def _checkState(self):
153
f197b52796ee Primitivus: begining of management for actionResult
Goffi <goffi@goffi.org>
parents: 150
diff changeset
309 if isinstance(self.center.widget_list[1].original_widget, Hand): #if we have a hand displayed
150
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
310 self.center.widget_list[1] = urwid.Filler(self.table) #we show again the table
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
311 if self.state == "chien":
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
312 self.to_show = []
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
313 self.state = "wait"
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
314 elif self.state == "wait_for_ecart":
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
315 self.state = "ecart"
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
316 self.hand.extend(self.to_show)
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
317 self.hand.sort()
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
318 self.to_show = []
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
319 self.hand_wid.update(self.hand)
323
7bc1b3401ecb primitivus: fixed state state change in tarot game
Goffi <goffi@goffi.org>
parents: 228
diff changeset
320
7bc1b3401ecb primitivus: fixed state state change in tarot game
Goffi <goffi@goffi.org>
parents: 228
diff changeset
321
7bc1b3401ecb primitivus: fixed state state change in tarot game
Goffi <goffi@goffi.org>
parents: 228
diff changeset
322 ##EVENTS##
7bc1b3401ecb primitivus: fixed state state change in tarot game
Goffi <goffi@goffi.org>
parents: 228
diff changeset
323 def onClick(self, hand, card_wid):
7bc1b3401ecb primitivus: fixed state state change in tarot game
Goffi <goffi@goffi.org>
parents: 228
diff changeset
324 """Called when user do an action on the hand"""
7bc1b3401ecb primitivus: fixed state state change in tarot game
Goffi <goffi@goffi.org>
parents: 228
diff changeset
325 if not self.state in ['play','ecart','wait_for_ecart']:
7bc1b3401ecb primitivus: fixed state state change in tarot game
Goffi <goffi@goffi.org>
parents: 228
diff changeset
326 #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
327 card_wid.select(False)
7bc1b3401ecb primitivus: fixed state state change in tarot game
Goffi <goffi@goffi.org>
parents: 228
diff changeset
328 return
7bc1b3401ecb primitivus: fixed state state change in tarot game
Goffi <goffi@goffi.org>
parents: 228
diff changeset
329 self._checkState()
150
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
330 if self.state == "ecart":
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
331 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
332 pop_up_widget = sat_widgets.ConfirmDialog(_("Do you put these cards in chien ?"), yes_cb=self.onEcartDone, no_cb=self.parent.host.removePopUp)
150
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
333 self.parent.host.showPopUp(pop_up_widget)
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
334 elif self.state == "play":
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
335 card = card_wid.getCard()
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
336 self.parent.host.bridge.tarotGamePlayCards(self.player_nick, self.referee, [(card.suit, card.value)], profile_key = self.parent.host.profile)
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
337 self.hand.remove(card)
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
338 self.hand_wid.update(self.hand)
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
339 self.state = "wait"
144
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
340
150
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
341 def onEcartDone(self,button):
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
342 """Called when player has finished is écart"""
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
343 ecart = []
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
344 for card in self.hand_wid.getSelected():
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
345 ecart.append((card.suit, card.value))
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
346 self.hand.remove(card)
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
347 self.hand_wid.update(self.hand)
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
348 self.parent.host.bridge.tarotGamePlayCards(self.player_nick, self.referee, ecart, profile_key = self.parent.host.profile)
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
349 self.state = "wait"
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
350 self.parent.host.removePopUp()