annotate libervia/tui/game_tarot.py @ 4317:055930cc81f9

component email gateway: Add support for XEP-0131 headers: Some email headers (`Keywords` and `Importance` for now) are converted between XMPP and email. rel 451
author Goffi <goffi@goffi.org>
date Sat, 28 Sep 2024 15:59:12 +0200
parents 0d7bb4df2343
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3137
559a625a236b fixed shebangs
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
1 #!/usr/bin/env python3
559a625a236b fixed shebangs
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
2
144
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
3
4076
b620a8e882e1 refactoring: rename `libervia.frontends.primitivus` to `libervia.tui`
Goffi <goffi@goffi.org>
parents: 4074
diff changeset
4 # Libervia TUI
3479
be6d91572633 date update
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
5 # Copyright (C) 2009-2021 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
4071
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
20 from libervia.backend.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
4074
26b7ed2817da refactoring: rename `sat_frontends` to `libervia.frontends`
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
23 from libervia.frontends.tools.games import TarotCard
26b7ed2817da refactoring: rename `sat_frontends` to `libervia.frontends`
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
24 from libervia.frontends.quick_frontend.quick_game_tarot import QuickTarotGame
4076
b620a8e882e1 refactoring: rename `libervia.frontends.primitivus` to `libervia.tui`
Goffi <goffi@goffi.org>
parents: 4074
diff changeset
25 from libervia.tui import xmlui
b620a8e882e1 refactoring: rename `libervia.frontends.primitivus` to `libervia.tui`
Goffi <goffi@goffi.org>
parents: 4074
diff changeset
26 from libervia.tui.keys import action_key_map as a_key
150
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
27
686
ae95b0327412 plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 682
diff changeset
28
150
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
29 class CardDisplayer(urwid.Text):
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
30 """Show a card"""
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
31
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
32 signals = ["click"]
150
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
33
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
34 def __init__(self, card):
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
35 self.__selected = False
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
36 self.card = card
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
37 urwid.Text.__init__(self, card.get_attr_text())
150
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
38
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
39 def selectable(self):
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
40 return True
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
41
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
42 def keypress(self, size, key):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
43 if key == a_key["CARD_SELECT"]:
150
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
44 self.select(not self.__selected)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
45 self._emit("click")
150
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
46 return key
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
47
159
2fa58703f1b7 Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents: 153
diff changeset
48 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
49 if urwid.is_mouse_event(event) and button == 1:
159
2fa58703f1b7 Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents: 153
diff changeset
50 self.select(not self.__selected)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
51 self._emit("click")
159
2fa58703f1b7 Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents: 153
diff changeset
52 return True
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
53
159
2fa58703f1b7 Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents: 153
diff changeset
54 return False
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
55
150
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
56 def select(self, state=True):
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
57 self.__selected = state
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
58 attr, txt = self.card.get_attr_text()
150
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
59 if self.__selected:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
60 attr += "_selected"
686
ae95b0327412 plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 682
diff changeset
61 self.set_text((attr, txt))
150
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
62 self._invalidate()
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
63
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
64 def is_selected(self):
150
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
65 return self.__selected
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
66
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
67 def get_card(self):
150
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
68 return self.card
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
69
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
70 def render(self, size, focus=False):
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
71 canvas = urwid.CompositeCanvas(urwid.Text.render(self, size, focus))
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
72 if focus:
686
ae95b0327412 plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 682
diff changeset
73 canvas.set_cursor((0, 0))
150
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
74 return canvas
144
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
75
686
ae95b0327412 plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 682
diff changeset
76
144
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
77 class Hand(urwid.WidgetWrap):
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
78 """Used to display several cards, and manage a hand"""
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
79
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
80 signals = ["click"]
144
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
81
686
ae95b0327412 plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 682
diff changeset
82 def __init__(self, hand=[], selectable=False, on_click=None, user_data=None):
150
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
83 """@param hand: list of Card"""
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
84 self.__selectable = selectable
686
ae95b0327412 plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 682
diff changeset
85 self.columns = urwid.Columns([], dividechars=1)
150
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
86 if on_click:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
87 urwid.connect_signal(self, "click", on_click, user_data)
150
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
88 if hand:
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
89 self.update(hand)
144
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
90 urwid.WidgetWrap.__init__(self, self.columns)
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
91
150
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
92 def selectable(self):
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
93 return self.__selectable
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
94
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
95 def keypress(self, size, key):
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
96
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
97 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
98 return self.columns.keypress(size, key)
150
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
99 else:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
100 # No card displayed, we still have to manage the clicks
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
101 if key == a_key["CARD_SELECT"]:
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
102 self._emit("click", None)
150
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
103 return key
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
104
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
105 def get_selected(self):
150
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
106 """Return a list of selected cards"""
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
107 _selected = []
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
108 for wid in self.columns.widget_list:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
109 if isinstance(wid, CardDisplayer) and wid.is_selected():
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
110 _selected.append(wid.get_card())
150
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
111 return _selected
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
112
144
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
113 def update(self, hand):
150
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
114 """Update the hand displayed in this widget
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
115 @param hand: list of Card"""
687
af0d08a84cc6 primitivus card_game: bug fix and improvement
souliane <souliane@mailoo.org>
parents: 686
diff changeset
116 try:
af0d08a84cc6 primitivus card_game: bug fix and improvement
souliane <souliane@mailoo.org>
parents: 686
diff changeset
117 del self.columns.widget_list[:]
af0d08a84cc6 primitivus card_game: bug fix and improvement
souliane <souliane@mailoo.org>
parents: 686
diff changeset
118 del self.columns.column_types[:]
af0d08a84cc6 primitivus card_game: bug fix and improvement
souliane <souliane@mailoo.org>
parents: 686
diff changeset
119 except IndexError:
af0d08a84cc6 primitivus card_game: bug fix and improvement
souliane <souliane@mailoo.org>
parents: 686
diff changeset
120 pass
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
121 self.columns.contents.append((urwid.Text(""), ("weight", 1, False)))
144
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
122 for card in hand:
150
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
123 widget = CardDisplayer(card)
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
124 self.columns.widget_list.append(widget)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
125 self.columns.column_types.append(("fixed", 3))
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
126 urwid.connect_signal(widget, "click", self.__on_click)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
127 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
128 self.columns.focus_position = 1
144
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
129
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
130 def __on_click(self, card_wid):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
131 self._emit("click", card_wid)
144
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
132
686
ae95b0327412 plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 682
diff changeset
133
144
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
134 class Card(TarotCard):
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
135 """This class is used to represent a card, logically
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
136 and give a text representation with attributes"""
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
137
686
ae95b0327412 plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 682
diff changeset
138 SIZE = 3 # size of a displayed card
144
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
139
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
140 def __init__(self, suit, value):
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
141 """@param file: path of the PNG file"""
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
142 TarotCard.__init__(self, (suit, value))
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
143
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
144 def get_attr_text(self):
144
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
145 """return text representation of the card with attributes"""
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
146 try:
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
147 value = "%02i" % int(self.value)
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
148 except ValueError:
686
ae95b0327412 plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 682
diff changeset
149 value = self.value[0].upper() + self.value[1]
144
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
150 if self.suit == "atout":
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
151 if self.value == "excuse":
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
152 suit = "c"
144
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
153 else:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
154 suit = "A"
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
155 color = "neutral"
144
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
156 elif self.suit == "pique":
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
157 suit = "♠"
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
158 color = "black"
144
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
159 elif self.suit == "trefle":
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
160 suit = "♣"
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
161 color = "black"
144
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
162 elif self.suit == "coeur":
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
163 suit = "♥"
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
164 color = "red"
144
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
165 elif self.suit == "carreau":
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
166 suit = "♦"
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
167 color = "red"
144
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
168 if self.bout:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
169 color = "special"
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
170 return ("card_%s" % color, "%s%s" % (value, suit))
144
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
171
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
172 def get_widget(self):
150
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
173 """Return a widget representing the card"""
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
174 return CardDisplayer(self)
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
175
686
ae95b0327412 plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 682
diff changeset
176
150
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
177 class Table(urwid.FlowWidget):
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
178 """Represent the cards currently on the table"""
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
179
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
180 def __init__(self):
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
181 self.top = self.left = self.bottom = self.right = None
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
182
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
183 def put_card(self, location, card):
150
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
184 """Put a card on the table
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
185 @param location: where to put the card (top, left, bottom or right)
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
186 @param card: Card to play or None"""
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
187 assert location in ["top", "left", "bottom", "right"]
686
ae95b0327412 plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 682
diff changeset
188 assert isinstance(card, Card) or card == None
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
189 if [getattr(self, place) for place in ["top", "left", "bottom", "right"]].count(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
190 None
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
191 ) == 0:
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
192 # If the table is full of card, we remove them
150
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
193 self.top = self.left = self.bottom = self.right = None
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
194 setattr(self, location, card)
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
195 self._invalidate()
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
196
686
ae95b0327412 plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 682
diff changeset
197 def rows(self, size, focus=False):
150
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
198 return self.display_widget(size, focus).rows(size, focus)
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
199
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
200 def render(self, size, focus=False):
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
201 return self.display_widget(size, focus).render(size, focus)
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
202
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
203 def display_widget(self, size, focus):
686
ae95b0327412 plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 682
diff changeset
204 cards = {}
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4076
diff changeset
205 (max_col,) = size
150
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
206 separator = " - "
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
207 margin = max((max_col - Card.SIZE) / 2, 0) * " "
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
208 margin_center = max((max_col - Card.SIZE * 2 - len(separator)) / 2, 0) * " "
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
209 for location in ["top", "left", "bottom", "right"]:
686
ae95b0327412 plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 682
diff changeset
210 card = getattr(self, location)
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
211 cards[location] = card.get_attr_text() if card else Card.SIZE * " "
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
212 render_wid = [
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
213 urwid.Text([margin, cards["top"]]),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
214 urwid.Text([margin_center, cards["left"], separator, cards["right"]]),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
215 urwid.Text([margin, cards["bottom"]]),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
216 ]
150
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
217 return urwid.Pile(render_wid)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
218
150
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
219
1360
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents: 1158
diff changeset
220 class TarotGame(QuickTarotGame, urwid.WidgetWrap):
144
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
221 """Widget for card games"""
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
222
1360
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents: 1158
diff changeset
223 def __init__(self, parent, referee, players):
8ea8fa13c351 frontends (quick_frontend, primitivus): fixes room games:
souliane <souliane@mailoo.org>
parents: 1158
diff changeset
224 QuickTarotGame.__init__(self, parent, referee, players)
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
225 self.load_cards()
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
226 self.top = urwid.Pile([urwid.Padding(urwid.Text(self.top_nick), "center")])
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
227 # self.parent.host.debug()
150
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
228 self.table = Table()
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
229 self.center = urwid.Columns(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
230 [
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
231 ("fixed", len(self.left_nick), urwid.Filler(urwid.Text(self.left_nick))),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
232 urwid.Filler(self.table),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
233 (
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
234 "fixed",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
235 len(self.right_nick),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
236 urwid.Filler(urwid.Text(self.right_nick)),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
237 ),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
238 ]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
239 )
150
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
240 """urwid.Pile([urwid.Padding(self.top_card_wid,'center'),
144
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
241 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
242 urwid.Padding(self.center_cards_wid,'center'),
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
243 ('fixed',len(self.right_nick),urwid.Text(self.right_nick))
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
244 ]),
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
245 urwid.Padding(self.bottom_card_wid,'center')
150
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
246 ])"""
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
247 self.hand_wid = Hand(selectable=True, on_click=self.on_click)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
248 self.main_frame = urwid.Frame(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
249 self.center, header=self.top, footer=self.hand_wid, focus_part="footer"
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
250 )
686
ae95b0327412 plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 682
diff changeset
251 urwid.WidgetWrap.__init__(self, self.main_frame)
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
252 self.parent.host.bridge.tarot_game_ready(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
253 self.player_nick, referee, self.parent.profile
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
254 )
144
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
255
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
256 def load_cards(self):
144
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
257 """Load all the cards in memory"""
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
258 QuickTarotGame.load_cards(self)
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
259 for value in list(map(str, list(range(1, 22)))) + ["excuse"]:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
260 card = Card("atout", value)
686
ae95b0327412 plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 682
diff changeset
261 self.cards[card.suit, card.value] = card
144
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
262 self.deck.append(card)
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
263 for suit in ["pique", "coeur", "carreau", "trefle"]:
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4076
diff changeset
264 for value in list(map(str, list(range(1, 11)))) + [
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4076
diff changeset
265 "valet",
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4076
diff changeset
266 "cavalier",
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4076
diff changeset
267 "dame",
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4076
diff changeset
268 "roi",
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4076
diff changeset
269 ]:
686
ae95b0327412 plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 682
diff changeset
270 card = Card(suit, value)
ae95b0327412 plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 682
diff changeset
271 self.cards[card.suit, card.value] = card
144
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
272 self.deck.append(card)
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
273
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
274 def tarot_game_new_handler(self, hand):
144
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
275 """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
276 if hand is []: # reset the display after the scores have been showed
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
277 self.reset_round()
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
278 for location in ["top", "left", "bottom", "right"]:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
279 self.table.put_card(location, None)
878
36c6495d86b0 plugin card_game: update to use the new XMLUI mechanism:
souliane <souliane@mailoo.org>
parents: 811
diff changeset
280 self.parent.host.redraw()
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
281 self.parent.host.bridge.tarot_game_ready(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
282 self.player_nick, self.referee, self.parent.profile
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
283 )
878
36c6495d86b0 plugin card_game: update to use the new XMLUI mechanism:
souliane <souliane@mailoo.org>
parents: 811
diff changeset
284 return
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
285 QuickTarotGame.tarot_game_new_handler(self, hand)
144
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
286 self.hand_wid.update(self.hand)
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
287 self.parent.host.redraw()
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
288
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
289 def tarot_game_choose_contrat_handler(self, xml_data):
878
36c6495d86b0 plugin card_game: update to use the new XMLUI mechanism:
souliane <souliane@mailoo.org>
parents: 811
diff changeset
290 """Called when the player has to select his contrat
144
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
291 @param xml_data: SàT xml representation of the form"""
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
292 form = xmlui.create(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
293 self.parent.host,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
294 xml_data,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
295 title=_("Please choose your contrat"),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
296 flags=["NO_CANCEL"],
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
297 profile=self.parent.profile,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
298 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
299 form.show(valign="top")
144
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
300
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
301 def tarot_game_show_cards_handler(self, game_stage, cards, data):
150
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
302 """Display cards in the middle of the game (to show for e.g. chien ou poignée)"""
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
303 QuickTarotGame.tarot_game_show_cards_handler(self, game_stage, cards, data)
150
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
304 self.center.widget_list[1] = urwid.Filler(Hand(self.to_show))
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
305 self.parent.host.redraw()
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
306
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
307 def tarot_game_your_turn_handler(self):
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
308 QuickTarotGame.tarot_game_your_turn_handler(self)
150
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
309
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
310 def tarot_game_score_handler(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
311 """Called when the round is over, display the scores
150
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
312 @param xml_data: SàT xml representation of the form"""
328
809733b8d9be Tarot game:
Goffi <goffi@goffi.org>
parents: 323
diff changeset
313 if not winners and not loosers:
809733b8d9be Tarot game:
Goffi <goffi@goffi.org>
parents: 323
diff changeset
314 title = _("Draw game")
809733b8d9be Tarot game:
Goffi <goffi@goffi.org>
parents: 323
diff changeset
315 else:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
316 title = _("You win \o/") if self.player_nick in winners else _("You loose :(")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
317 form = xmlui.create(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
318 self.parent.host,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
319 xml_data,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
320 title=title,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
321 flags=["NO_CANCEL"],
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
322 profile=self.parent.profile,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
323 )
162
ae50b53ff868 misc Tarot fixes
Goffi <goffi@goffi.org>
parents: 159
diff changeset
324 form.show()
150
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
325
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
326 def tarot_game_invalid_cards_handler(self, phase, played_cards, invalid_cards):
150
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
327 """Invalid cards have been played
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
328 @param phase: phase of the game
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
329 @param played_cards: all the cards played
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
330 @param invalid_cards: cards which are invalid"""
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
331 QuickTarotGame.tarot_game_invalid_cards_handler(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
332 self, phase, played_cards, invalid_cards
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
333 )
150
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
334 self.hand_wid.update(self.hand)
686
ae95b0327412 plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 682
diff changeset
335 if self._autoplay == None: # No dialog if there is autoplay
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
336 self.parent.host.bar_notify(_("Cards played are invalid !"))
150
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
337 self.parent.host.redraw()
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
338
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
339 def tarot_game_cards_played_handler(self, player, cards):
150
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
340 """A card has been played by player"""
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
341 QuickTarotGame.tarot_game_cards_played_handler(self, player, cards)
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
342 self.table.put_card(self.get_player_location(player), self.played[player])
323
7bc1b3401ecb primitivus: fixed state state change in tarot game
Goffi <goffi@goffi.org>
parents: 228
diff changeset
343 self._checkState()
150
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
344 self.parent.host.redraw()
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
345
323
7bc1b3401ecb primitivus: fixed state state change in tarot game
Goffi <goffi@goffi.org>
parents: 228
diff changeset
346 def _checkState(self):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
347 if isinstance(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
348 self.center.widget_list[1].original_widget, Hand
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
349 ): # if we have a hand displayed
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
350 self.center.widget_list[1] = urwid.Filler(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
351 self.table
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
352 ) # we show again the table
150
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
353 if self.state == "chien":
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
354 self.to_show = []
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
355 self.state = "wait"
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
356 elif self.state == "wait_for_ecart":
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
357 self.state = "ecart"
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
358 self.hand.extend(self.to_show)
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
359 self.hand.sort()
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
360 self.to_show = []
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
361 self.hand_wid.update(self.hand)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
362
323
7bc1b3401ecb primitivus: fixed state state change in tarot game
Goffi <goffi@goffi.org>
parents: 228
diff changeset
363 ##EVENTS##
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
364 def on_click(self, hand, card_wid):
323
7bc1b3401ecb primitivus: fixed state state change in tarot game
Goffi <goffi@goffi.org>
parents: 228
diff changeset
365 """Called when user do an action on the hand"""
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
366 if not self.state in ["play", "ecart", "wait_for_ecart"]:
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
367 # it's not our turn, we ignore the click
323
7bc1b3401ecb primitivus: fixed state state change in tarot game
Goffi <goffi@goffi.org>
parents: 228
diff changeset
368 card_wid.select(False)
7bc1b3401ecb primitivus: fixed state state change in tarot game
Goffi <goffi@goffi.org>
parents: 228
diff changeset
369 return
7bc1b3401ecb primitivus: fixed state state change in tarot game
Goffi <goffi@goffi.org>
parents: 228
diff changeset
370 self._checkState()
686
ae95b0327412 plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 682
diff changeset
371 if self.state == "ecart":
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
372 if len(self.hand_wid.get_selected()) == 6:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
373 pop_up_widget = sat_widgets.ConfirmDialog(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
374 _("Do you put these cards in chien ?"),
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
375 yes_cb=self.on_ecart_done,
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
376 no_cb=self.parent.host.remove_pop_up,
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
377 )
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
378 self.parent.host.show_pop_up(pop_up_widget)
150
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
379 elif self.state == "play":
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
380 card = card_wid.get_card()
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
381 self.parent.host.bridge.tarot_game_play_cards(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
382 self.player_nick,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
383 self.referee,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
384 [(card.suit, card.value)],
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
385 self.parent.profile,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
386 )
150
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
387 self.hand.remove(card)
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
388 self.hand_wid.update(self.hand)
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
389 self.state = "wait"
144
80661755ea8d Primitivus: Tarot card game implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
390
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
391 def on_ecart_done(self, button):
686
ae95b0327412 plugin card_game: better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 682
diff changeset
392 """Called when player has finished his écart"""
150
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
393 ecart = []
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
394 for card in self.hand_wid.get_selected():
150
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
395 ecart.append((card.suit, card.value))
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
396 self.hand.remove(card)
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
397 self.hand_wid.update(self.hand)
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
398 self.parent.host.bridge.tarot_game_play_cards(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
399 self.player_nick, self.referee, ecart, self.parent.profile
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
400 )
150
63d20bda5754 Primitivus: Tarot game
Goffi <goffi@goffi.org>
parents: 144
diff changeset
401 self.state = "wait"
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
402 self.parent.host.remove_pop_up()