diff sat_frontends/primitivus/game_tarot.py @ 2624:56f94936df1e

code style reformatting using black
author Goffi <goffi@goffi.org>
date Wed, 27 Jun 2018 20:14:46 +0200
parents 26edcf3a30eb
children 003b8b4b56a7
line wrap: on
line diff
--- a/sat_frontends/primitivus/game_tarot.py	Wed Jun 27 07:51:29 2018 +0200
+++ b/sat_frontends/primitivus/game_tarot.py	Wed Jun 27 20:14:46 2018 +0200
@@ -28,7 +28,8 @@
 
 class CardDisplayer(urwid.Text):
     """Show a card"""
-    signals = ['click']
+
+    signals = ["click"]
 
     def __init__(self, card):
         self.__selected = False
@@ -39,15 +40,15 @@
         return True
 
     def keypress(self, size, key):
-        if key == a_key['CARD_SELECT']:
+        if key == a_key["CARD_SELECT"]:
             self.select(not self.__selected)
-            self._emit('click')
+            self._emit("click")
         return key
 
     def mouse_event(self, size, event, button, x, y, focus):
         if urwid.is_mouse_event(event) and button == 1:
             self.select(not self.__selected)
-            self._emit('click')
+            self._emit("click")
             return True
 
         return False
@@ -56,7 +57,7 @@
         self.__selected = state
         attr, txt = self.card.getAttrText()
         if self.__selected:
-            attr += '_selected'
+            attr += "_selected"
         self.set_text((attr, txt))
         self._invalidate()
 
@@ -75,14 +76,15 @@
 
 class Hand(urwid.WidgetWrap):
     """Used to display several cards, and manage a hand"""
-    signals = ['click']
+
+    signals = ["click"]
 
     def __init__(self, hand=[], selectable=False, on_click=None, user_data=None):
         """@param hand: list of Card"""
         self.__selectable = selectable
         self.columns = urwid.Columns([], dividechars=1)
         if on_click:
-            urwid.connect_signal(self, 'click', on_click, user_data)
+            urwid.connect_signal(self, "click", on_click, user_data)
         if hand:
             self.update(hand)
         urwid.WidgetWrap.__init__(self, self.columns)
@@ -95,9 +97,9 @@
         if CardDisplayer in [wid.__class__ for wid in self.columns.widget_list]:
             return self.columns.keypress(size, key)
         else:
-            #No card displayed, we still have to manage the clicks
-            if key == a_key['CARD_SELECT']:
-                self._emit('click', None)
+            # No card displayed, we still have to manage the clicks
+            if key == a_key["CARD_SELECT"]:
+                self._emit("click", None)
             return key
 
     def getSelected(self):
@@ -116,22 +118,23 @@
             del self.columns.column_types[:]
         except IndexError:
             pass
-        self.columns.contents.append((urwid.Text(''), ('weight', 1, False)))
+        self.columns.contents.append((urwid.Text(""), ("weight", 1, False)))
         for card in hand:
             widget = CardDisplayer(card)
             self.columns.widget_list.append(widget)
-            self.columns.column_types.append(('fixed', 3))
-            urwid.connect_signal(widget, 'click', self.__onClick)
-        self.columns.contents.append((urwid.Text(''), ('weight', 1, False)))
+            self.columns.column_types.append(("fixed", 3))
+            urwid.connect_signal(widget, "click", self.__onClick)
+        self.columns.contents.append((urwid.Text(""), ("weight", 1, False)))
         self.columns.focus_position = 1
 
     def __onClick(self, card_wid):
-        self._emit('click', card_wid)
+        self._emit("click", card_wid)
 
 
 class Card(TarotCard):
     """This class is used to represent a card, logically
     and give a text representation with attributes"""
+
     SIZE = 3  # size of a displayed card
 
     def __init__(self, suit, value):
@@ -146,25 +149,25 @@
             value = self.value[0].upper() + self.value[1]
         if self.suit == "atout":
             if self.value == "excuse":
-                suit = 'c'
+                suit = "c"
             else:
-                suit = 'A'
-            color = 'neutral'
+                suit = "A"
+            color = "neutral"
         elif self.suit == "pique":
-            suit = u'♠'
-            color = 'black'
+            suit = u"♠"
+            color = "black"
         elif self.suit == "trefle":
-            suit = u'♣'
-            color = 'black'
+            suit = u"♣"
+            color = "black"
         elif self.suit == "coeur":
-            suit = u'♥'
-            color = 'red'
+            suit = u"♥"
+            color = "red"
         elif self.suit == "carreau":
-            suit = u'♦'
-            color = 'red'
+            suit = u"♦"
+            color = "red"
         if self.bout:
-            color = 'special'
-        return ('card_%s' % color, u"%s%s" % (value, suit))
+            color = "special"
+        return ("card_%s" % color, u"%s%s" % (value, suit))
 
     def getWidget(self):
         """Return a widget representing the card"""
@@ -181,10 +184,12 @@
         """Put a card on the table
         @param location: where to put the card (top, left, bottom or right)
         @param card: Card to play or None"""
-        assert location in ['top', 'left', 'bottom', 'right']
+        assert location in ["top", "left", "bottom", "right"]
         assert isinstance(card, Card) or card == None
-        if [getattr(self, place) for place in ['top', 'left', 'bottom', 'right']].count(None) == 0:
-            #If the table is full of card, we remove them
+        if [getattr(self, place) for place in ["top", "left", "bottom", "right"]].count(
+            None
+        ) == 0:
+            # If the table is full of card, we remove them
             self.top = self.left = self.bottom = self.right = None
         setattr(self, location, card)
         self._invalidate()
@@ -199,14 +204,16 @@
         cards = {}
         max_col, = size
         separator = " - "
-        margin = max((max_col - Card.SIZE) / 2, 0) * ' '
-        margin_center = max((max_col - Card.SIZE * 2 - len(separator)) / 2, 0) * ' '
-        for location in ['top', 'left', 'bottom', 'right']:
+        margin = max((max_col - Card.SIZE) / 2, 0) * " "
+        margin_center = max((max_col - Card.SIZE * 2 - len(separator)) / 2, 0) * " "
+        for location in ["top", "left", "bottom", "right"]:
             card = getattr(self, location)
-            cards[location] = card.getAttrText() if card else Card.SIZE * ' '
-        render_wid = [urwid.Text([margin, cards['top']]),
-                      urwid.Text([margin_center, cards['left'], separator, cards['right']]),
-                      urwid.Text([margin, cards['bottom']])]
+            cards[location] = card.getAttrText() if card else Card.SIZE * " "
+        render_wid = [
+            urwid.Text([margin, cards["top"]]),
+            urwid.Text([margin_center, cards["left"], separator, cards["right"]]),
+            urwid.Text([margin, cards["bottom"]]),
+        ]
         return urwid.Pile(render_wid)
 
 
@@ -216,13 +223,20 @@
     def __init__(self, parent, referee, players):
         QuickTarotGame.__init__(self, parent, referee, players)
         self.loadCards()
-        self.top = urwid.Pile([urwid.Padding(urwid.Text(self.top_nick), 'center')])
-        #self.parent.host.debug()
+        self.top = urwid.Pile([urwid.Padding(urwid.Text(self.top_nick), "center")])
+        # self.parent.host.debug()
         self.table = Table()
-        self.center = urwid.Columns([('fixed', len(self.left_nick), urwid.Filler(urwid.Text(self.left_nick))),
-                                urwid.Filler(self.table),
-                                ('fixed', len(self.right_nick), urwid.Filler(urwid.Text(self.right_nick)))
-                               ])
+        self.center = urwid.Columns(
+            [
+                ("fixed", len(self.left_nick), urwid.Filler(urwid.Text(self.left_nick))),
+                urwid.Filler(self.table),
+                (
+                    "fixed",
+                    len(self.right_nick),
+                    urwid.Filler(urwid.Text(self.right_nick)),
+                ),
+            ]
+        )
         """urwid.Pile([urwid.Padding(self.top_card_wid,'center'),
                              urwid.Columns([('fixed',len(self.left_nick),urwid.Text(self.left_nick)),
                                             urwid.Padding(self.center_cards_wid,'center'),
@@ -231,15 +245,19 @@
                              urwid.Padding(self.bottom_card_wid,'center')
                              ])"""
         self.hand_wid = Hand(selectable=True, on_click=self.onClick)
-        self.main_frame = urwid.Frame(self.center, header=self.top, footer=self.hand_wid, focus_part='footer')
+        self.main_frame = urwid.Frame(
+            self.center, header=self.top, footer=self.hand_wid, focus_part="footer"
+        )
         urwid.WidgetWrap.__init__(self, self.main_frame)
-        self.parent.host.bridge.tarotGameReady(self.player_nick, referee, self.parent.profile)
+        self.parent.host.bridge.tarotGameReady(
+            self.player_nick, referee, self.parent.profile
+        )
 
     def loadCards(self):
         """Load all the cards in memory"""
         QuickTarotGame.loadCards(self)
-        for value in map(str, range(1, 22)) + ['excuse']:
-            card = Card('atout', value)
+        for value in map(str, range(1, 22)) + ["excuse"]:
+            card = Card("atout", value)
             self.cards[card.suit, card.value] = card
             self.deck.append(card)
         for suit in ["pique", "coeur", "carreau", "trefle"]:
@@ -252,10 +270,12 @@
         """Start a new game, with given hand"""
         if hand is []:  # reset the display after the scores have been showed
             self.resetRound()
-            for location in ['top', 'left', 'bottom', 'right']:
+            for location in ["top", "left", "bottom", "right"]:
                 self.table.putCard(location, None)
             self.parent.host.redraw()
-            self.parent.host.bridge.tarotGameReady(self.player_nick, self.referee, self.parent.profile)
+            self.parent.host.bridge.tarotGameReady(
+                self.player_nick, self.referee, self.parent.profile
+            )
             return
         QuickTarotGame.tarotGameNewHandler(self, hand)
         self.hand_wid.update(self.hand)
@@ -264,8 +284,14 @@
     def tarotGameChooseContratHandler(self, xml_data):
         """Called when the player has to select his contrat
         @param xml_data: SàT xml representation of the form"""
-        form = xmlui.create(self.parent.host, xml_data, title=_('Please choose your contrat'), flags=['NO_CANCEL'], profile=self.parent.profile)
-        form.show(valign='top')
+        form = xmlui.create(
+            self.parent.host,
+            xml_data,
+            title=_("Please choose your contrat"),
+            flags=["NO_CANCEL"],
+            profile=self.parent.profile,
+        )
+        form.show(valign="top")
 
     def tarotGameShowCardsHandler(self, game_stage, cards, data):
         """Display cards in the middle of the game (to show for e.g. chien ou poignée)"""
@@ -282,8 +308,14 @@
         if not winners and not loosers:
             title = _("Draw game")
         else:
-            title = _('You win \o/') if self.player_nick in winners else _('You loose :(')
-        form = xmlui.create(self.parent.host, xml_data, title=title, flags=['NO_CANCEL'], profile=self.parent.profile)
+            title = _("You win \o/") if self.player_nick in winners else _("You loose :(")
+        form = xmlui.create(
+            self.parent.host,
+            xml_data,
+            title=title,
+            flags=["NO_CANCEL"],
+            profile=self.parent.profile,
+        )
         form.show()
 
     def tarotGameInvalidCardsHandler(self, phase, played_cards, invalid_cards):
@@ -291,10 +323,12 @@
         @param phase: phase of the game
         @param played_cards: all the cards played
         @param invalid_cards: cards which are invalid"""
-        QuickTarotGame.tarotGameInvalidCardsHandler(self, phase, played_cards, invalid_cards)
+        QuickTarotGame.tarotGameInvalidCardsHandler(
+            self, phase, played_cards, invalid_cards
+        )
         self.hand_wid.update(self.hand)
         if self._autoplay == None:  # No dialog if there is autoplay
-            self.parent.host.barNotify(_('Cards played are invalid !'))
+            self.parent.host.barNotify(_("Cards played are invalid !"))
         self.parent.host.redraw()
 
     def tarotGameCardsPlayedHandler(self, player, cards):
@@ -305,8 +339,12 @@
         self.parent.host.redraw()
 
     def _checkState(self):
-        if isinstance(self.center.widget_list[1].original_widget, Hand):  # if we have a hand displayed
-            self.center.widget_list[1] = urwid.Filler(self.table)  # we show again the table
+        if isinstance(
+            self.center.widget_list[1].original_widget, Hand
+        ):  # if we have a hand displayed
+            self.center.widget_list[1] = urwid.Filler(
+                self.table
+            )  # we show again the table
             if self.state == "chien":
                 self.to_show = []
                 self.state = "wait"
@@ -320,18 +358,27 @@
     ##EVENTS##
     def onClick(self, hand, card_wid):
         """Called when user do an action on the hand"""
-        if not self.state in ['play', 'ecart', 'wait_for_ecart']:
-            #it's not our turn, we ignore the click
+        if not self.state in ["play", "ecart", "wait_for_ecart"]:
+            # it's not our turn, we ignore the click
             card_wid.select(False)
             return
         self._checkState()
         if self.state == "ecart":
             if len(self.hand_wid.getSelected()) == 6:
-                pop_up_widget = sat_widgets.ConfirmDialog(_("Do you put these cards in chien ?"), yes_cb=self.onEcartDone, no_cb=self.parent.host.removePopUp)
+                pop_up_widget = sat_widgets.ConfirmDialog(
+                    _("Do you put these cards in chien ?"),
+                    yes_cb=self.onEcartDone,
+                    no_cb=self.parent.host.removePopUp,
+                )
                 self.parent.host.showPopUp(pop_up_widget)
         elif self.state == "play":
             card = card_wid.getCard()
-            self.parent.host.bridge.tarotGamePlayCards(self.player_nick, self.referee, [(card.suit, card.value)], self.parent.profile)
+            self.parent.host.bridge.tarotGamePlayCards(
+                self.player_nick,
+                self.referee,
+                [(card.suit, card.value)],
+                self.parent.profile,
+            )
             self.hand.remove(card)
             self.hand_wid.update(self.hand)
             self.state = "wait"
@@ -343,6 +390,8 @@
             ecart.append((card.suit, card.value))
             self.hand.remove(card)
         self.hand_wid.update(self.hand)
-        self.parent.host.bridge.tarotGamePlayCards(self.player_nick, self.referee, ecart, self.parent.profile)
+        self.parent.host.bridge.tarotGamePlayCards(
+            self.player_nick, self.referee, ecart, self.parent.profile
+        )
         self.state = "wait"
         self.parent.host.removePopUp()