Mercurial > libervia-backend
comparison sat_frontends/primitivus/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 |
---|---|
32 signals = ["click"] | 32 signals = ["click"] |
33 | 33 |
34 def __init__(self, card): | 34 def __init__(self, card): |
35 self.__selected = False | 35 self.__selected = False |
36 self.card = card | 36 self.card = card |
37 urwid.Text.__init__(self, card.getAttrText()) | 37 urwid.Text.__init__(self, card.get_attr_text()) |
38 | 38 |
39 def selectable(self): | 39 def selectable(self): |
40 return True | 40 return True |
41 | 41 |
42 def keypress(self, size, key): | 42 def keypress(self, size, key): |
53 | 53 |
54 return False | 54 return False |
55 | 55 |
56 def select(self, state=True): | 56 def select(self, state=True): |
57 self.__selected = state | 57 self.__selected = state |
58 attr, txt = self.card.getAttrText() | 58 attr, txt = self.card.get_attr_text() |
59 if self.__selected: | 59 if self.__selected: |
60 attr += "_selected" | 60 attr += "_selected" |
61 self.set_text((attr, txt)) | 61 self.set_text((attr, txt)) |
62 self._invalidate() | 62 self._invalidate() |
63 | 63 |
64 def isSelected(self): | 64 def is_selected(self): |
65 return self.__selected | 65 return self.__selected |
66 | 66 |
67 def getCard(self): | 67 def get_card(self): |
68 return self.card | 68 return self.card |
69 | 69 |
70 def render(self, size, focus=False): | 70 def render(self, size, focus=False): |
71 canvas = urwid.CompositeCanvas(urwid.Text.render(self, size, focus)) | 71 canvas = urwid.CompositeCanvas(urwid.Text.render(self, size, focus)) |
72 if focus: | 72 if focus: |
100 # No card displayed, we still have to manage the clicks | 100 # No card displayed, we still have to manage the clicks |
101 if key == a_key["CARD_SELECT"]: | 101 if key == a_key["CARD_SELECT"]: |
102 self._emit("click", None) | 102 self._emit("click", None) |
103 return key | 103 return key |
104 | 104 |
105 def getSelected(self): | 105 def get_selected(self): |
106 """Return a list of selected cards""" | 106 """Return a list of selected cards""" |
107 _selected = [] | 107 _selected = [] |
108 for wid in self.columns.widget_list: | 108 for wid in self.columns.widget_list: |
109 if isinstance(wid, CardDisplayer) and wid.isSelected(): | 109 if isinstance(wid, CardDisplayer) and wid.is_selected(): |
110 _selected.append(wid.getCard()) | 110 _selected.append(wid.get_card()) |
111 return _selected | 111 return _selected |
112 | 112 |
113 def update(self, hand): | 113 def update(self, hand): |
114 """Update the hand displayed in this widget | 114 """Update the hand displayed in this widget |
115 @param hand: list of Card""" | 115 @param hand: list of Card""" |
121 self.columns.contents.append((urwid.Text(""), ("weight", 1, False))) | 121 self.columns.contents.append((urwid.Text(""), ("weight", 1, False))) |
122 for card in hand: | 122 for card in hand: |
123 widget = CardDisplayer(card) | 123 widget = CardDisplayer(card) |
124 self.columns.widget_list.append(widget) | 124 self.columns.widget_list.append(widget) |
125 self.columns.column_types.append(("fixed", 3)) | 125 self.columns.column_types.append(("fixed", 3)) |
126 urwid.connect_signal(widget, "click", self.__onClick) | 126 urwid.connect_signal(widget, "click", self.__on_click) |
127 self.columns.contents.append((urwid.Text(""), ("weight", 1, False))) | 127 self.columns.contents.append((urwid.Text(""), ("weight", 1, False))) |
128 self.columns.focus_position = 1 | 128 self.columns.focus_position = 1 |
129 | 129 |
130 def __onClick(self, card_wid): | 130 def __on_click(self, card_wid): |
131 self._emit("click", card_wid) | 131 self._emit("click", card_wid) |
132 | 132 |
133 | 133 |
134 class Card(TarotCard): | 134 class Card(TarotCard): |
135 """This class is used to represent a card, logically | 135 """This class is used to represent a card, logically |
139 | 139 |
140 def __init__(self, suit, value): | 140 def __init__(self, suit, value): |
141 """@param file: path of the PNG file""" | 141 """@param file: path of the PNG file""" |
142 TarotCard.__init__(self, (suit, value)) | 142 TarotCard.__init__(self, (suit, value)) |
143 | 143 |
144 def getAttrText(self): | 144 def get_attr_text(self): |
145 """return text representation of the card with attributes""" | 145 """return text representation of the card with attributes""" |
146 try: | 146 try: |
147 value = "%02i" % int(self.value) | 147 value = "%02i" % int(self.value) |
148 except ValueError: | 148 except ValueError: |
149 value = self.value[0].upper() + self.value[1] | 149 value = self.value[0].upper() + self.value[1] |
167 color = "red" | 167 color = "red" |
168 if self.bout: | 168 if self.bout: |
169 color = "special" | 169 color = "special" |
170 return ("card_%s" % color, "%s%s" % (value, suit)) | 170 return ("card_%s" % color, "%s%s" % (value, suit)) |
171 | 171 |
172 def getWidget(self): | 172 def get_widget(self): |
173 """Return a widget representing the card""" | 173 """Return a widget representing the card""" |
174 return CardDisplayer(self) | 174 return CardDisplayer(self) |
175 | 175 |
176 | 176 |
177 class Table(urwid.FlowWidget): | 177 class Table(urwid.FlowWidget): |
178 """Represent the cards currently on the table""" | 178 """Represent the cards currently on the table""" |
179 | 179 |
180 def __init__(self): | 180 def __init__(self): |
181 self.top = self.left = self.bottom = self.right = None | 181 self.top = self.left = self.bottom = self.right = None |
182 | 182 |
183 def putCard(self, location, card): | 183 def put_card(self, location, card): |
184 """Put a card on the table | 184 """Put a card on the table |
185 @param location: where to put the card (top, left, bottom or right) | 185 @param location: where to put the card (top, left, bottom or right) |
186 @param card: Card to play or None""" | 186 @param card: Card to play or None""" |
187 assert location in ["top", "left", "bottom", "right"] | 187 assert location in ["top", "left", "bottom", "right"] |
188 assert isinstance(card, Card) or card == None | 188 assert isinstance(card, Card) or card == None |
206 separator = " - " | 206 separator = " - " |
207 margin = max((max_col - Card.SIZE) / 2, 0) * " " | 207 margin = max((max_col - Card.SIZE) / 2, 0) * " " |
208 margin_center = max((max_col - Card.SIZE * 2 - len(separator)) / 2, 0) * " " | 208 margin_center = max((max_col - Card.SIZE * 2 - len(separator)) / 2, 0) * " " |
209 for location in ["top", "left", "bottom", "right"]: | 209 for location in ["top", "left", "bottom", "right"]: |
210 card = getattr(self, location) | 210 card = getattr(self, location) |
211 cards[location] = card.getAttrText() if card else Card.SIZE * " " | 211 cards[location] = card.get_attr_text() if card else Card.SIZE * " " |
212 render_wid = [ | 212 render_wid = [ |
213 urwid.Text([margin, cards["top"]]), | 213 urwid.Text([margin, cards["top"]]), |
214 urwid.Text([margin_center, cards["left"], separator, cards["right"]]), | 214 urwid.Text([margin_center, cards["left"], separator, cards["right"]]), |
215 urwid.Text([margin, cards["bottom"]]), | 215 urwid.Text([margin, cards["bottom"]]), |
216 ] | 216 ] |
220 class TarotGame(QuickTarotGame, urwid.WidgetWrap): | 220 class TarotGame(QuickTarotGame, urwid.WidgetWrap): |
221 """Widget for card games""" | 221 """Widget for card games""" |
222 | 222 |
223 def __init__(self, parent, referee, players): | 223 def __init__(self, parent, referee, players): |
224 QuickTarotGame.__init__(self, parent, referee, players) | 224 QuickTarotGame.__init__(self, parent, referee, players) |
225 self.loadCards() | 225 self.load_cards() |
226 self.top = urwid.Pile([urwid.Padding(urwid.Text(self.top_nick), "center")]) | 226 self.top = urwid.Pile([urwid.Padding(urwid.Text(self.top_nick), "center")]) |
227 # self.parent.host.debug() | 227 # self.parent.host.debug() |
228 self.table = Table() | 228 self.table = Table() |
229 self.center = urwid.Columns( | 229 self.center = urwid.Columns( |
230 [ | 230 [ |
242 urwid.Padding(self.center_cards_wid,'center'), | 242 urwid.Padding(self.center_cards_wid,'center'), |
243 ('fixed',len(self.right_nick),urwid.Text(self.right_nick)) | 243 ('fixed',len(self.right_nick),urwid.Text(self.right_nick)) |
244 ]), | 244 ]), |
245 urwid.Padding(self.bottom_card_wid,'center') | 245 urwid.Padding(self.bottom_card_wid,'center') |
246 ])""" | 246 ])""" |
247 self.hand_wid = Hand(selectable=True, on_click=self.onClick) | 247 self.hand_wid = Hand(selectable=True, on_click=self.on_click) |
248 self.main_frame = urwid.Frame( | 248 self.main_frame = urwid.Frame( |
249 self.center, header=self.top, footer=self.hand_wid, focus_part="footer" | 249 self.center, header=self.top, footer=self.hand_wid, focus_part="footer" |
250 ) | 250 ) |
251 urwid.WidgetWrap.__init__(self, self.main_frame) | 251 urwid.WidgetWrap.__init__(self, self.main_frame) |
252 self.parent.host.bridge.tarotGameReady( | 252 self.parent.host.bridge.tarot_game_ready( |
253 self.player_nick, referee, self.parent.profile | 253 self.player_nick, referee, self.parent.profile |
254 ) | 254 ) |
255 | 255 |
256 def loadCards(self): | 256 def load_cards(self): |
257 """Load all the cards in memory""" | 257 """Load all the cards in memory""" |
258 QuickTarotGame.loadCards(self) | 258 QuickTarotGame.load_cards(self) |
259 for value in list(map(str, list(range(1, 22)))) + ["excuse"]: | 259 for value in list(map(str, list(range(1, 22)))) + ["excuse"]: |
260 card = Card("atout", value) | 260 card = Card("atout", value) |
261 self.cards[card.suit, card.value] = card | 261 self.cards[card.suit, card.value] = card |
262 self.deck.append(card) | 262 self.deck.append(card) |
263 for suit in ["pique", "coeur", "carreau", "trefle"]: | 263 for suit in ["pique", "coeur", "carreau", "trefle"]: |
264 for value in list(map(str, list(range(1, 11)))) + ["valet", "cavalier", "dame", "roi"]: | 264 for value in list(map(str, list(range(1, 11)))) + ["valet", "cavalier", "dame", "roi"]: |
265 card = Card(suit, value) | 265 card = Card(suit, value) |
266 self.cards[card.suit, card.value] = card | 266 self.cards[card.suit, card.value] = card |
267 self.deck.append(card) | 267 self.deck.append(card) |
268 | 268 |
269 def tarotGameNewHandler(self, hand): | 269 def tarot_game_new_handler(self, hand): |
270 """Start a new game, with given hand""" | 270 """Start a new game, with given hand""" |
271 if hand is []: # reset the display after the scores have been showed | 271 if hand is []: # reset the display after the scores have been showed |
272 self.resetRound() | 272 self.reset_round() |
273 for location in ["top", "left", "bottom", "right"]: | 273 for location in ["top", "left", "bottom", "right"]: |
274 self.table.putCard(location, None) | 274 self.table.put_card(location, None) |
275 self.parent.host.redraw() | 275 self.parent.host.redraw() |
276 self.parent.host.bridge.tarotGameReady( | 276 self.parent.host.bridge.tarot_game_ready( |
277 self.player_nick, self.referee, self.parent.profile | 277 self.player_nick, self.referee, self.parent.profile |
278 ) | 278 ) |
279 return | 279 return |
280 QuickTarotGame.tarotGameNewHandler(self, hand) | 280 QuickTarotGame.tarot_game_new_handler(self, hand) |
281 self.hand_wid.update(self.hand) | 281 self.hand_wid.update(self.hand) |
282 self.parent.host.redraw() | 282 self.parent.host.redraw() |
283 | 283 |
284 def tarotGameChooseContratHandler(self, xml_data): | 284 def tarot_game_choose_contrat_handler(self, xml_data): |
285 """Called when the player has to select his contrat | 285 """Called when the player has to select his contrat |
286 @param xml_data: SàT xml representation of the form""" | 286 @param xml_data: SàT xml representation of the form""" |
287 form = xmlui.create( | 287 form = xmlui.create( |
288 self.parent.host, | 288 self.parent.host, |
289 xml_data, | 289 xml_data, |
291 flags=["NO_CANCEL"], | 291 flags=["NO_CANCEL"], |
292 profile=self.parent.profile, | 292 profile=self.parent.profile, |
293 ) | 293 ) |
294 form.show(valign="top") | 294 form.show(valign="top") |
295 | 295 |
296 def tarotGameShowCardsHandler(self, game_stage, cards, data): | 296 def tarot_game_show_cards_handler(self, game_stage, cards, data): |
297 """Display cards in the middle of the game (to show for e.g. chien ou poignée)""" | 297 """Display cards in the middle of the game (to show for e.g. chien ou poignée)""" |
298 QuickTarotGame.tarotGameShowCardsHandler(self, game_stage, cards, data) | 298 QuickTarotGame.tarot_game_show_cards_handler(self, game_stage, cards, data) |
299 self.center.widget_list[1] = urwid.Filler(Hand(self.to_show)) | 299 self.center.widget_list[1] = urwid.Filler(Hand(self.to_show)) |
300 self.parent.host.redraw() | 300 self.parent.host.redraw() |
301 | 301 |
302 def tarotGameYourTurnHandler(self): | 302 def tarot_game_your_turn_handler(self): |
303 QuickTarotGame.tarotGameYourTurnHandler(self) | 303 QuickTarotGame.tarot_game_your_turn_handler(self) |
304 | 304 |
305 def tarotGameScoreHandler(self, xml_data, winners, loosers): | 305 def tarot_game_score_handler(self, xml_data, winners, loosers): |
306 """Called when the round is over, display the scores | 306 """Called when the round is over, display the scores |
307 @param xml_data: SàT xml representation of the form""" | 307 @param xml_data: SàT xml representation of the form""" |
308 if not winners and not loosers: | 308 if not winners and not loosers: |
309 title = _("Draw game") | 309 title = _("Draw game") |
310 else: | 310 else: |
316 flags=["NO_CANCEL"], | 316 flags=["NO_CANCEL"], |
317 profile=self.parent.profile, | 317 profile=self.parent.profile, |
318 ) | 318 ) |
319 form.show() | 319 form.show() |
320 | 320 |
321 def tarotGameInvalidCardsHandler(self, phase, played_cards, invalid_cards): | 321 def tarot_game_invalid_cards_handler(self, phase, played_cards, invalid_cards): |
322 """Invalid cards have been played | 322 """Invalid cards have been played |
323 @param phase: phase of the game | 323 @param phase: phase of the game |
324 @param played_cards: all the cards played | 324 @param played_cards: all the cards played |
325 @param invalid_cards: cards which are invalid""" | 325 @param invalid_cards: cards which are invalid""" |
326 QuickTarotGame.tarotGameInvalidCardsHandler( | 326 QuickTarotGame.tarot_game_invalid_cards_handler( |
327 self, phase, played_cards, invalid_cards | 327 self, phase, played_cards, invalid_cards |
328 ) | 328 ) |
329 self.hand_wid.update(self.hand) | 329 self.hand_wid.update(self.hand) |
330 if self._autoplay == None: # No dialog if there is autoplay | 330 if self._autoplay == None: # No dialog if there is autoplay |
331 self.parent.host.barNotify(_("Cards played are invalid !")) | 331 self.parent.host.bar_notify(_("Cards played are invalid !")) |
332 self.parent.host.redraw() | 332 self.parent.host.redraw() |
333 | 333 |
334 def tarotGameCardsPlayedHandler(self, player, cards): | 334 def tarot_game_cards_played_handler(self, player, cards): |
335 """A card has been played by player""" | 335 """A card has been played by player""" |
336 QuickTarotGame.tarotGameCardsPlayedHandler(self, player, cards) | 336 QuickTarotGame.tarot_game_cards_played_handler(self, player, cards) |
337 self.table.putCard(self.getPlayerLocation(player), self.played[player]) | 337 self.table.put_card(self.get_player_location(player), self.played[player]) |
338 self._checkState() | 338 self._checkState() |
339 self.parent.host.redraw() | 339 self.parent.host.redraw() |
340 | 340 |
341 def _checkState(self): | 341 def _checkState(self): |
342 if isinstance( | 342 if isinstance( |
354 self.hand.sort() | 354 self.hand.sort() |
355 self.to_show = [] | 355 self.to_show = [] |
356 self.hand_wid.update(self.hand) | 356 self.hand_wid.update(self.hand) |
357 | 357 |
358 ##EVENTS## | 358 ##EVENTS## |
359 def onClick(self, hand, card_wid): | 359 def on_click(self, hand, card_wid): |
360 """Called when user do an action on the hand""" | 360 """Called when user do an action on the hand""" |
361 if not self.state in ["play", "ecart", "wait_for_ecart"]: | 361 if not self.state in ["play", "ecart", "wait_for_ecart"]: |
362 # it's not our turn, we ignore the click | 362 # it's not our turn, we ignore the click |
363 card_wid.select(False) | 363 card_wid.select(False) |
364 return | 364 return |
365 self._checkState() | 365 self._checkState() |
366 if self.state == "ecart": | 366 if self.state == "ecart": |
367 if len(self.hand_wid.getSelected()) == 6: | 367 if len(self.hand_wid.get_selected()) == 6: |
368 pop_up_widget = sat_widgets.ConfirmDialog( | 368 pop_up_widget = sat_widgets.ConfirmDialog( |
369 _("Do you put these cards in chien ?"), | 369 _("Do you put these cards in chien ?"), |
370 yes_cb=self.onEcartDone, | 370 yes_cb=self.on_ecart_done, |
371 no_cb=self.parent.host.removePopUp, | 371 no_cb=self.parent.host.remove_pop_up, |
372 ) | 372 ) |
373 self.parent.host.showPopUp(pop_up_widget) | 373 self.parent.host.show_pop_up(pop_up_widget) |
374 elif self.state == "play": | 374 elif self.state == "play": |
375 card = card_wid.getCard() | 375 card = card_wid.get_card() |
376 self.parent.host.bridge.tarotGamePlayCards( | 376 self.parent.host.bridge.tarot_game_play_cards( |
377 self.player_nick, | 377 self.player_nick, |
378 self.referee, | 378 self.referee, |
379 [(card.suit, card.value)], | 379 [(card.suit, card.value)], |
380 self.parent.profile, | 380 self.parent.profile, |
381 ) | 381 ) |
382 self.hand.remove(card) | 382 self.hand.remove(card) |
383 self.hand_wid.update(self.hand) | 383 self.hand_wid.update(self.hand) |
384 self.state = "wait" | 384 self.state = "wait" |
385 | 385 |
386 def onEcartDone(self, button): | 386 def on_ecart_done(self, button): |
387 """Called when player has finished his écart""" | 387 """Called when player has finished his écart""" |
388 ecart = [] | 388 ecart = [] |
389 for card in self.hand_wid.getSelected(): | 389 for card in self.hand_wid.get_selected(): |
390 ecart.append((card.suit, card.value)) | 390 ecart.append((card.suit, card.value)) |
391 self.hand.remove(card) | 391 self.hand.remove(card) |
392 self.hand_wid.update(self.hand) | 392 self.hand_wid.update(self.hand) |
393 self.parent.host.bridge.tarotGamePlayCards( | 393 self.parent.host.bridge.tarot_game_play_cards( |
394 self.player_nick, self.referee, ecart, self.parent.profile | 394 self.player_nick, self.referee, ecart, self.parent.profile |
395 ) | 395 ) |
396 self.state = "wait" | 396 self.state = "wait" |
397 self.parent.host.removePopUp() | 397 self.parent.host.remove_pop_up() |