Mercurial > libervia-backend
diff frontends/src/primitivus/card_game.py @ 686:ae95b0327412
plugin card_game: better PEP-8 compliance
author | souliane <souliane@mailoo.org> |
---|---|
date | Mon, 28 Oct 2013 18:49:01 +0100 |
parents | 2805fa3f4bdf |
children | af0d08a84cc6 |
line wrap: on
line diff
--- a/frontends/src/primitivus/card_game.py Mon Oct 28 18:29:34 2013 +0100 +++ b/frontends/src/primitivus/card_game.py Mon Oct 28 18:49:01 2013 +0100 @@ -23,6 +23,7 @@ from sat_frontends.quick_frontend.quick_card_game import QuickCardGame from sat_frontends.primitivus.xmlui import XMLUI + class CardDisplayer(urwid.Text): """Show a card""" signals = ['click'] @@ -42,7 +43,7 @@ return key def mouse_event(self, size, event, button, x, y, focus): - if urwid.is_mouse_press(event) and button == 1: + if urwid.is_mouse_event(event) and button == 1: self.select(not self.__selected) self._emit('click') return True @@ -51,10 +52,10 @@ def select(self, state=True): self.__selected = state - attr,txt = self.card.getAttrText() + attr, txt = self.card.getAttrText() if self.__selected: - attr+='_selected' - self.set_text((attr,txt)) + attr += '_selected' + self.set_text((attr, txt)) self._invalidate() def isSelected(self): @@ -66,17 +67,18 @@ def render(self, size, focus=False): canvas = urwid.CompositeCanvas(urwid.Text.render(self, size, focus)) if focus: - canvas.set_cursor((0,0)) + canvas.set_cursor((0, 0)) return canvas + class Hand(urwid.WidgetWrap): """Used to display several cards, and manage a hand""" signals = ['click'] - def __init__(self, hand=[], selectable = False, on_click=None, user_data=None): + 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) + self.columns = urwid.Columns([], dividechars=1) if on_click: urwid.connect_signal(self, 'click', on_click, user_data) if hand: @@ -89,7 +91,7 @@ def keypress(self, size, key): if CardDisplayer in [wid.__class__ for wid in self.columns.widget_list]: - return self.columns.keypress(size,key) + return self.columns.keypress(size, key) else: #No card displayed, we still have to manage the clicks if key == ' ': @@ -113,18 +115,19 @@ for card in hand: widget = CardDisplayer(card) self.columns.widget_list.append(widget) - self.columns.column_types.append(('fixed',3)) + 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.contents.append((urwid.Text(''), ('weight', 1, False))) self.columns.set_focus(1) - def __onClick(self,card_wid): + def __onClick(self, 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 + SIZE = 3 # size of a displayed card def __init__(self, suit, value): """@param file: path of the PNG file""" @@ -135,7 +138,7 @@ try: value = "%02i" % int(self.value) except ValueError: - value = self.value[0].upper()+self.value[1] + value = self.value[0].upper() + self.value[1] if self.suit == "atout": if self.value == "excuse": suit = 'c' @@ -156,12 +159,13 @@ color = 'red' if self.bout: color = 'special' - return ('card_%s' % color,u"%s%s" % (value,suit)) + return ('card_%s' % color, u"%s%s" % (value, suit)) def getWidget(self): """Return a widget representing the card""" return CardDisplayer(self) + class Table(urwid.FlowWidget): """Represent the cards currently on the table""" @@ -172,36 +176,36 @@ """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 isinstance(card,Card) or card == None - if [getattr(self, place) for place in ['top','left','bottom','right']].count(None) == 0: + 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 self.top = self.left = self.bottom = self.right = None setattr(self, location, card) self._invalidate() - def rows(self,size,focus=False): + def rows(self, size, focus=False): return self.display_widget(size, focus).rows(size, focus) def render(self, size, focus=False): return self.display_widget(size, focus).render(size, focus) def display_widget(self, size, focus): - cards={} + 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) * ' ' + 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) + 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']])] + 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) -class CardGame(QuickCardGame,urwid.WidgetWrap): +class CardGame(QuickCardGame, urwid.WidgetWrap): """Widget for card games""" def __init__(self, parent, referee, players, player_nick): @@ -210,9 +214,9 @@ 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))), + 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))) + ('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)), @@ -221,22 +225,22 @@ ]), 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') - urwid.WidgetWrap.__init__(self,self.main_frame) + 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') + urwid.WidgetWrap.__init__(self, self.main_frame) self.parent.host.bridge.tarotGameReady(player_nick, referee, self.parent.host.profile) def loadCards(self): """Load all the cards in memory""" QuickCardGame.loadCards(self) - for value in map(str,range(1,22))+['excuse']: - card = Card('atout',value) - self.cards[card.suit, card.value]=card + 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"]: - for value in map(str,range(1,11))+["valet","cavalier","dame","roi"]: - card = Card(suit,value) - self.cards[card.suit, card.value]=card + for value in map(str, range(1, 11)) + ["valet", "cavalier", "dame", "roi"]: + card = Card(suit, value) + self.cards[card.suit, card.value] = card self.deck.append(card) def newGame(self, hand): @@ -255,7 +259,7 @@ """Called when the player as to select his contrat @param xml_data: SàT xml representation of the form""" misc = {'callback': self.contratSelected} - form = XMLUI(self.parent.host, xml_data, title = _('Please choose your contrat'), options = ['NO_CANCEL'], misc = misc) + form = XMLUI(self.parent.host, xml_data, title=_('Please choose your contrat'), options=['NO_CANCEL'], misc=misc) form.show() def showCards(self, game_stage, cards, data): @@ -272,7 +276,7 @@ @param xml_data: SàT xml representation of the form""" def _new_game(ignore): 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.host.profile) @@ -280,7 +284,7 @@ title = _("Draw game") else: title = _('You win \o/') if self.player_nick in winners else _('You loose :(') - form = XMLUI(self.parent.host, xml_data, title = title, options = ['NO_CANCEL'], misc={'callback':_new_game}) + form = XMLUI(self.parent.host, xml_data, title=title, options=['NO_CANCEL'], misc={'callback': _new_game}) form.show() def invalidCards(self, phase, played_cards, invalid_cards): @@ -290,20 +294,20 @@ @param invalid_cards: cards which are invalid""" QuickCardGame.invalidCards(self, phase, played_cards, invalid_cards) self.hand_wid.update(self.hand) - if self._autoplay==None: #No dialog if there is autoplay + if self._autoplay == None: # No dialog if there is autoplay self.parent.host.notify(_('Cards played are invalid !')) self.parent.host.redraw() def cardsPlayed(self, player, cards): """A card has been played by player""" QuickCardGame.cardsPlayed(self, player, cards) - self.table.putCard(self.getPlayerLocation(player),self.played[player]) + self.table.putCard(self.getPlayerLocation(player), self.played[player]) self._checkState() 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" @@ -314,16 +318,15 @@ self.to_show = [] self.hand_wid.update(self.hand) - ##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']: + 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 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) self.parent.host.showPopUp(pop_up_widget) @@ -334,8 +337,8 @@ self.hand_wid.update(self.hand) self.state = "wait" - def onEcartDone(self,button): - """Called when player has finished is écart""" + def onEcartDone(self, button): + """Called when player has finished his écart""" ecart = [] for card in self.hand_wid.getSelected(): ecart.append((card.suit, card.value))