comparison libervia/frontends/quick_frontend/quick_game_tarot.py @ 4074:26b7ed2817da

refactoring: rename `sat_frontends` to `libervia.frontends`
author Goffi <goffi@goffi.org>
date Fri, 02 Jun 2023 14:12:38 +0200
parents sat_frontends/quick_frontend/quick_game_tarot.py@4b842c1fb686
children 0d7bb4df2343
comparison
equal deleted inserted replaced
4073:7c5654c54fed 4074:26b7ed2817da
1 #!/usr/bin/env python3
2
3
4 # helper class for making a SAT frontend
5 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
6
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
16
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20 from libervia.backend.core.log import getLogger
21
22 log = getLogger(__name__)
23 from libervia.frontends.tools.jid import JID
24
25
26 class QuickTarotGame(object):
27 def __init__(self, parent, referee, players):
28 self._autoplay = None # XXX: use 0 to activate fake play, None else
29 self.parent = parent
30 self.referee = referee
31 self.players = players
32 self.played = {}
33 for player in players:
34 self.played[player] = None
35 self.player_nick = parent.nick
36 self.bottom_nick = str(self.player_nick)
37 idx = self.players.index(self.player_nick)
38 idx = (idx + 1) % len(self.players)
39 self.right_nick = str(self.players[idx])
40 idx = (idx + 1) % len(self.players)
41 self.top_nick = str(self.players[idx])
42 idx = (idx + 1) % len(self.players)
43 self.left_nick = str(self.players[idx])
44 self.bottom_nick = str(self.player_nick)
45 self.selected = [] # Card choosed by the player (e.g. during ecart)
46 self.hand_size = 13 # number of cards in a hand
47 self.hand = []
48 self.to_show = []
49 self.state = None
50
51 def reset_round(self):
52 """Reset the game's variables to be reatty to start the next round"""
53 del self.selected[:]
54 del self.hand[:]
55 del self.to_show[:]
56 self.state = None
57 for pl in self.played:
58 self.played[pl] = None
59
60 def get_player_location(self, nick):
61 """return player location (top,bottom,left or right)"""
62 for location in ["top", "left", "bottom", "right"]:
63 if getattr(self, "%s_nick" % location) == nick:
64 return location
65 assert False
66
67 def load_cards(self):
68 """Load all the cards in memory
69 @param dir: directory where the PNG files are"""
70 self.cards = {}
71 self.deck = []
72 self.cards[
73 "atout"
74 ] = {} # As Tarot is a french game, it's more handy & logical to keep french names
75 self.cards["pique"] = {} # spade
76 self.cards["coeur"] = {} # heart
77 self.cards["carreau"] = {} # diamond
78 self.cards["trefle"] = {} # club
79
80 def tarot_game_new_handler(self, hand):
81 """Start a new game, with given hand"""
82 assert len(self.hand) == 0
83 for suit, value in hand:
84 self.hand.append(self.cards[suit, value])
85 self.hand.sort()
86 self.state = "init"
87
88 def tarot_game_choose_contrat_handler(self, xml_data):
89 """Called when the player as to select his contrat
90 @param xml_data: SàT xml representation of the form"""
91 raise NotImplementedError
92
93 def tarot_game_show_cards_handler(self, game_stage, cards, data):
94 """Display cards in the middle of the game (to show for e.g. chien ou poignée)"""
95 self.to_show = []
96 for suit, value in cards:
97 self.to_show.append(self.cards[suit, value])
98 if game_stage == "chien" and data["attaquant"] == self.player_nick:
99 self.state = "wait_for_ecart"
100 else:
101 self.state = "chien"
102
103 def tarot_game_your_turn_handler(self):
104 """Called when we have to play :)"""
105 if self.state == "chien":
106 self.to_show = []
107 self.state = "play"
108 self.__fake_play()
109
110 def __fake_play(self):
111 """Convenience method for stupid autoplay
112 /!\ don't forgot to comment any interactive dialog for invalid card"""
113 if self._autoplay == None:
114 return
115 if self._autoplay >= len(self.hand):
116 self._autoplay = 0
117 card = self.hand[self._autoplay]
118 self.parent.host.bridge.tarot_game_play_cards(
119 self.player_nick, self.referee, [(card.suit, card.value)], self.parent.profile
120 )
121 del self.hand[self._autoplay]
122 self.state = "wait"
123 self._autoplay += 1
124
125 def tarot_game_score_handler(self, xml_data, winners, loosers):
126 """Called at the end of a game
127 @param xml_data: SàT xml representation of the scores
128 @param winners: list of winners' nicks
129 @param loosers: list of loosers' nicks"""
130 raise NotImplementedError
131
132 def tarot_game_cards_played_handler(self, player, cards):
133 """A card has been played by player"""
134 if self.to_show:
135 self.to_show = []
136 pl_cards = []
137 if self.played[player] != None: # FIXME
138 for pl in self.played:
139 self.played[pl] = None
140 for suit, value in cards:
141 pl_cards.append(self.cards[suit, value])
142 self.played[player] = pl_cards[0]
143
144 def tarot_game_invalid_cards_handler(self, phase, played_cards, invalid_cards):
145 """Invalid cards have been played
146 @param phase: phase of the game
147 @param played_cards: all the cards played
148 @param invalid_cards: cards which are invalid"""
149
150 if phase == "play":
151 self.state = "play"
152 elif phase == "ecart":
153 self.state = "ecart"
154 else:
155 log.error("INTERNAL ERROR: unmanaged game phase")
156
157 for suit, value in played_cards:
158 self.hand.append(self.cards[suit, value])
159
160 self.hand.sort()
161 self.__fake_play()