comparison sat_frontends/quick_frontend/quick_game_tarot.py @ 4037:524856bd7b19

massive refactoring to switch from camelCase to snake_case: historically, Libervia (SàT before) was using camelCase as allowed by PEP8 when using a pre-PEP8 code, to use the same coding style as in Twisted. However, snake_case is more readable and it's better to follow PEP8 best practices, so it has been decided to move on full snake_case. Because Libervia has a huge codebase, this ended with a ugly mix of camelCase and snake_case. To fix that, this patch does a big refactoring by renaming every function and method (including bridge) that are not coming from Twisted or Wokkel, to use fully snake_case. This is a massive change, and may result in some bugs.
author Goffi <goffi@goffi.org>
date Sat, 08 Apr 2023 13:54:42 +0200
parents be6d91572633
children 4b842c1fb686
comparison
equal deleted inserted replaced
4036:c4464d7ae97b 4037:524856bd7b19
46 self.hand_size = 13 # number of cards in a hand 46 self.hand_size = 13 # number of cards in a hand
47 self.hand = [] 47 self.hand = []
48 self.to_show = [] 48 self.to_show = []
49 self.state = None 49 self.state = None
50 50
51 def resetRound(self): 51 def reset_round(self):
52 """Reset the game's variables to be reatty to start the next round""" 52 """Reset the game's variables to be reatty to start the next round"""
53 del self.selected[:] 53 del self.selected[:]
54 del self.hand[:] 54 del self.hand[:]
55 del self.to_show[:] 55 del self.to_show[:]
56 self.state = None 56 self.state = None
57 for pl in self.played: 57 for pl in self.played:
58 self.played[pl] = None 58 self.played[pl] = None
59 59
60 def getPlayerLocation(self, nick): 60 def get_player_location(self, nick):
61 """return player location (top,bottom,left or right)""" 61 """return player location (top,bottom,left or right)"""
62 for location in ["top", "left", "bottom", "right"]: 62 for location in ["top", "left", "bottom", "right"]:
63 if getattr(self, "%s_nick" % location) == nick: 63 if getattr(self, "%s_nick" % location) == nick:
64 return location 64 return location
65 assert False 65 assert False
66 66
67 def loadCards(self): 67 def load_cards(self):
68 """Load all the cards in memory 68 """Load all the cards in memory
69 @param dir: directory where the PNG files are""" 69 @param dir: directory where the PNG files are"""
70 self.cards = {} 70 self.cards = {}
71 self.deck = [] 71 self.deck = []
72 self.cards[ 72 self.cards[
75 self.cards["pique"] = {} # spade 75 self.cards["pique"] = {} # spade
76 self.cards["coeur"] = {} # heart 76 self.cards["coeur"] = {} # heart
77 self.cards["carreau"] = {} # diamond 77 self.cards["carreau"] = {} # diamond
78 self.cards["trefle"] = {} # club 78 self.cards["trefle"] = {} # club
79 79
80 def tarotGameNewHandler(self, hand): 80 def tarot_game_new_handler(self, hand):
81 """Start a new game, with given hand""" 81 """Start a new game, with given hand"""
82 assert len(self.hand) == 0 82 assert len(self.hand) == 0
83 for suit, value in hand: 83 for suit, value in hand:
84 self.hand.append(self.cards[suit, value]) 84 self.hand.append(self.cards[suit, value])
85 self.hand.sort() 85 self.hand.sort()
86 self.state = "init" 86 self.state = "init"
87 87
88 def tarotGameChooseContratHandler(self, xml_data): 88 def tarot_game_choose_contrat_handler(self, xml_data):
89 """Called when the player as to select his contrat 89 """Called when the player as to select his contrat
90 @param xml_data: SàT xml representation of the form""" 90 @param xml_data: SàT xml representation of the form"""
91 raise NotImplementedError 91 raise NotImplementedError
92 92
93 def tarotGameShowCardsHandler(self, game_stage, cards, data): 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)""" 94 """Display cards in the middle of the game (to show for e.g. chien ou poignée)"""
95 self.to_show = [] 95 self.to_show = []
96 for suit, value in cards: 96 for suit, value in cards:
97 self.to_show.append(self.cards[suit, value]) 97 self.to_show.append(self.cards[suit, value])
98 if game_stage == "chien" and data["attaquant"] == self.player_nick: 98 if game_stage == "chien" and data["attaquant"] == self.player_nick:
99 self.state = "wait_for_ecart" 99 self.state = "wait_for_ecart"
100 else: 100 else:
101 self.state = "chien" 101 self.state = "chien"
102 102
103 def tarotGameYourTurnHandler(self): 103 def tarot_game_your_turn_handler(self):
104 """Called when we have to play :)""" 104 """Called when we have to play :)"""
105 if self.state == "chien": 105 if self.state == "chien":
106 self.to_show = [] 106 self.to_show = []
107 self.state = "play" 107 self.state = "play"
108 self.__fakePlay() 108 self.__fake_play()
109 109
110 def __fakePlay(self): 110 def __fake_play(self):
111 """Convenience method for stupid autoplay 111 """Convenience method for stupid autoplay
112 /!\ don't forgot to comment any interactive dialog for invalid card""" 112 /!\ don't forgot to comment any interactive dialog for invalid card"""
113 if self._autoplay == None: 113 if self._autoplay == None:
114 return 114 return
115 if self._autoplay >= len(self.hand): 115 if self._autoplay >= len(self.hand):
116 self._autoplay = 0 116 self._autoplay = 0
117 card = self.hand[self._autoplay] 117 card = self.hand[self._autoplay]
118 self.parent.host.bridge.tarotGamePlayCards( 118 self.parent.host.bridge.tarot_game_play_cards(
119 self.player_nick, self.referee, [(card.suit, card.value)], self.parent.profile 119 self.player_nick, self.referee, [(card.suit, card.value)], self.parent.profile
120 ) 120 )
121 del self.hand[self._autoplay] 121 del self.hand[self._autoplay]
122 self.state = "wait" 122 self.state = "wait"
123 self._autoplay += 1 123 self._autoplay += 1
124 124
125 def tarotGameScoreHandler(self, xml_data, winners, loosers): 125 def tarot_game_score_handler(self, xml_data, winners, loosers):
126 """Called at the end of a game 126 """Called at the end of a game
127 @param xml_data: SàT xml representation of the scores 127 @param xml_data: SàT xml representation of the scores
128 @param winners: list of winners' nicks 128 @param winners: list of winners' nicks
129 @param loosers: list of loosers' nicks""" 129 @param loosers: list of loosers' nicks"""
130 raise NotImplementedError 130 raise NotImplementedError
131 131
132 def tarotGameCardsPlayedHandler(self, player, cards): 132 def tarot_game_cards_played_handler(self, player, cards):
133 """A card has been played by player""" 133 """A card has been played by player"""
134 if self.to_show: 134 if self.to_show:
135 self.to_show = [] 135 self.to_show = []
136 pl_cards = [] 136 pl_cards = []
137 if self.played[player] != None: # FIXME 137 if self.played[player] != None: # FIXME
139 self.played[pl] = None 139 self.played[pl] = None
140 for suit, value in cards: 140 for suit, value in cards:
141 pl_cards.append(self.cards[suit, value]) 141 pl_cards.append(self.cards[suit, value])
142 self.played[player] = pl_cards[0] 142 self.played[player] = pl_cards[0]
143 143
144 def tarotGameInvalidCardsHandler(self, phase, played_cards, invalid_cards): 144 def tarot_game_invalid_cards_handler(self, phase, played_cards, invalid_cards):
145 """Invalid cards have been played 145 """Invalid cards have been played
146 @param phase: phase of the game 146 @param phase: phase of the game
147 @param played_cards: all the cards played 147 @param played_cards: all the cards played
148 @param invalid_cards: cards which are invalid""" 148 @param invalid_cards: cards which are invalid"""
149 149
156 156
157 for suit, value in played_cards: 157 for suit, value in played_cards:
158 self.hand.append(self.cards[suit, value]) 158 self.hand.append(self.cards[suit, value])
159 159
160 self.hand.sort() 160 self.hand.sort()
161 self.__fakePlay() 161 self.__fake_play()