diff browser_side/card_game.py @ 243:63e9b680d3e7

browser_side, blog: better PEP8 compliance
author souliane <souliane@mailoo.org>
date Tue, 15 Oct 2013 17:19:03 +0200
parents dec76d4536ad
children 8fdd20399a06
line wrap: on
line diff
--- a/browser_side/card_game.py	Fri Oct 18 11:14:55 2013 +0200
+++ b/browser_side/card_game.py	Tue Oct 15 17:19:03 2013 +0200
@@ -19,7 +19,7 @@
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 """
 
-import pyjd # this is dummy in pyjs
+import pyjd  # this is dummy in pyjs
 from pyjamas.ui.AbsolutePanel import AbsolutePanel
 from pyjamas.ui.VerticalPanel import VerticalPanel
 from pyjamas.ui.HorizontalPanel import HorizontalPanel
@@ -43,16 +43,17 @@
 from datetime import datetime
 from time import time
 from games import TarotCard
+from logging import error
 import re
 
 
-
 CARD_WIDTH = 74
 CARD_HEIGHT = 136
 CARD_DELTA_Y = 30
-MIN_WIDTH = 950 #Minimum size of the panel
+MIN_WIDTH = 950  # Minimum size of the panel
 MIN_HEIGHT = 500
 
+
 class ContratChooser(DialogBox):
 
     def __init__(self, parent):
@@ -78,21 +79,22 @@
         button_panel.add(self.choose_button)
         content.add(button_panel)
         self.setHTML("Please select your contrat")
-        self.setWidget(content)	
+        self.setWidget(content)
 
     def onChoose(self, button):
         self.hide()
         self._parent.contratSelected(self.contrats_list.getSelectedItemText()[0])
 
+
 class CardWidget(TarotCard, Image, MouseHandler):
     """This class is used to represent a card, graphically and logically"""
 
-    def __init__(self, parent, file):
+    def __init__(self, parent, file_):
         """@param file: path of the PNG file"""
         self._parent = parent
-        Image.__init__(self,file)
-        root_name = file[file.rfind("/")+1:-4]
-        suit,value = root_name.split('_')
+        Image.__init__(self, file_)
+        root_name = file_[file_.rfind("/") + 1:-4]
+        suit, value = root_name.split('_')
         TarotCard.__init__(self, (suit, value))
         MouseHandler.__init__(self)
         self.addMouseListener(self)
@@ -104,7 +106,7 @@
     def onMouseLeave(self, sender):
         if not self in self._parent.hand:
             return
-        if not self in list(self._parent.selected): #FIXME: Workaround pyjs bug, must report it
+        if not self in list(self._parent.selected):  # FIXME: Workaround pyjs bug, must report it
             DOM.setStyleAttribute(self.getElement(), "top", "%dpx" % CARD_DELTA_Y)
 
     def onMouseUp(self, sender, x, y):
@@ -116,13 +118,14 @@
         elif self._parent.state == "play":
             self._parent.playCard(self)
 
+
 class CardPanel(DockPanel, ClickHandler):
 
     def __init__(self, parent, referee, players, player_nick):
         DockPanel.__init__(self)
         ClickHandler.__init__(self)
         self._parent = parent
-        self._autoplay = None #XXX: use 0 to activate fake play, None else
+        self._autoplay = None  # XXX: use 0 to activate fake play, None else
         self.referee = referee
         self.players = players
         self.player_nick = player_nick
@@ -135,42 +138,38 @@
         idx = (idx + 1) % len(self.players)
         self.left_nick = self.players[idx]
         self.bottom_nick = player_nick
-        self.selected = set() #Card choosed by the player (e.g. during ecart)
-        self.hand_size = 13 #number of cards in a hand
+        self.selected = set()  # Card choosed by the player (e.g. during ecart)
+        self.hand_size = 13  # number of cards in a hand
         self.hand = []
         self.to_show = []
         self.state = None
         self.setSize("%dpx" % MIN_WIDTH, "%dpx" % MIN_HEIGHT)
         self.setStyleName("cardPanel")
-        
+
         # Now we set up the layout
         _label = Label(self.top_nick)
         _label.setStyleName('cardGamePlayerNick')
         self.add(_label, DockPanel.NORTH)
         self.setCellWidth(_label, '100%')
-        self.setCellHorizontalAlignment(_label, HasAlignment.ALIGN_CENTER) 
-        
-        
+        self.setCellHorizontalAlignment(_label, HasAlignment.ALIGN_CENTER)
+
         self.hand_panel = AbsolutePanel()
         self.add(self.hand_panel, DockPanel.SOUTH)
         self.setCellWidth(self.hand_panel, '100%')
         self.setCellHorizontalAlignment(self.hand_panel, HasAlignment.ALIGN_CENTER)
 
-
         _label = Label(self.left_nick)
         _label.setStyleName('cardGamePlayerNick')
         self.add(_label, DockPanel.WEST)
         self.setCellHeight(_label, '100%')
-        self.setCellVerticalAlignment(_label, HasAlignment.ALIGN_MIDDLE) 
-
+        self.setCellVerticalAlignment(_label, HasAlignment.ALIGN_MIDDLE)
 
         _label = Label(self.right_nick)
         _label.setStyleName('cardGamePlayerNick')
         self.add(_label, DockPanel.EAST)
         self.setCellHeight(_label, '100%')
-        self.setCellHorizontalAlignment(_label, HasAlignment.ALIGN_RIGHT) 
-        self.setCellVerticalAlignment(_label, HasAlignment.ALIGN_MIDDLE) 
-        
+        self.setCellHorizontalAlignment(_label, HasAlignment.ALIGN_RIGHT)
+        self.setCellVerticalAlignment(_label, HasAlignment.ALIGN_MIDDLE)
 
         self.center_panel = DockPanel()
         self.inner_left = SimplePanel()
@@ -185,19 +184,19 @@
         self.center_panel.add(self.inner_right, DockPanel.EAST)
         self.center_panel.setCellHeight(self.inner_right, '100%')
         self.center_panel.setCellVerticalAlignment(self.inner_right, HasAlignment.ALIGN_MIDDLE)
-        
+
         self.inner_top = SimplePanel()
         self.inner_top.setSize("%dpx" % CARD_WIDTH, "%dpx" % CARD_HEIGHT)
         self.center_panel.add(self.inner_top, DockPanel.NORTH)
         self.center_panel.setCellHorizontalAlignment(self.inner_top, HasAlignment.ALIGN_CENTER)
         self.center_panel.setCellVerticalAlignment(self.inner_top, HasAlignment.ALIGN_BOTTOM)
-        
+
         self.inner_bottom = SimplePanel()
         self.inner_bottom.setSize("%dpx" % CARD_WIDTH, "%dpx" % CARD_HEIGHT)
         self.center_panel.add(self.inner_bottom, DockPanel.SOUTH)
         self.center_panel.setCellHorizontalAlignment(self.inner_bottom, HasAlignment.ALIGN_CENTER)
         self.center_panel.setCellVerticalAlignment(self.inner_bottom, HasAlignment.ALIGN_TOP)
-        
+
         self.inner_center = SimplePanel()
         self.center_panel.add(self.inner_center, DockPanel.CENTER)
         self.center_panel.setCellHorizontalAlignment(self.inner_center, HasAlignment.ALIGN_CENTER)
@@ -210,29 +209,28 @@
         self.setCellHorizontalAlignment(self.center_panel, HasAlignment.ALIGN_CENTER)
 
         self.loadCards()
-        self.mouse_over_card = None #contain the card to highlight
-        self.visible_size = CARD_WIDTH/2 #number of pixels visible for cards
+        self.mouse_over_card = None  # contain the card to highlight
+        self.visible_size = CARD_WIDTH / 2  # number of pixels visible for cards
         self.addClickListener(self)
 
-
     def loadCards(self):
         """Load all the cards in memory"""
         def _getTarotCardsPathsCb(paths):
             print "_getTarotCardsPathsCb"
-            for file in paths:
-                print "path:", file
-                card = CardWidget(self, file)
+            for file_ in paths:
+                print "path:", file_
+                card = CardWidget(self, file_)
                 print "card:", card
-                self.cards[(card.suit, card.value)]=card
+                self.cards[(card.suit, card.value)] = card
                 self.deck.append(card)
             self._parent.host.bridge.call('tarotGameReady', None, self.player_nick, self.referee)
-        self.cards={}
-        self.deck=[]
-        self.cards["atout"]={} #As Tarot is a french game, it's more handy & logical to keep french names
-        self.cards["pique"]={} #spade
-        self.cards["coeur"]={} #heart
-        self.cards["carreau"]={} #diamond
-        self.cards["trefle"]={} #club
+        self.cards = {}
+        self.deck = []
+        self.cards["atout"] = {}  # As Tarot is a french game, it's more handy & logical to keep french names
+        self.cards["pique"] = {}  # spade
+        self.cards["coeur"] = {}  # heart
+        self.cards["carreau"] = {}  # diamond
+        self.cards["trefle"] = {}  # club
         self._parent.host.bridge.call('getTarotCardsPaths', _getTarotCardsPathsCb)
 
     def onClick(self, sender):
@@ -259,18 +257,18 @@
     def updateHand(self):
         """Show the cards in the hand in the hand_panel (SOUTH panel)"""
         self.hand_panel.clear()
-        self.hand_panel.setSize("%dpx" % (self.visible_size * (len(self.hand)+1)), "%dpx" % (CARD_HEIGHT + CARD_DELTA_Y + 10))
+        self.hand_panel.setSize("%dpx" % (self.visible_size * (len(self.hand) + 1)), "%dpx" % (CARD_HEIGHT + CARD_DELTA_Y + 10))
         x_pos = 0
         y_pos = CARD_DELTA_Y
         for card in self.hand:
             self.hand_panel.add(card, x_pos, y_pos)
-            x_pos+=self.visible_size
-   
+            x_pos += self.visible_size
+
     def updateToShow(self):
         """Show cards in the center panel"""
         if not self.to_show:
             _widget = self.inner_center.getWidget()
-            if _widget: 
+            if _widget:
                 self.inner_center.remove(_widget)
             return
         panel = AbsolutePanel()
@@ -279,7 +277,7 @@
         y_pos = 0
         for card in self.to_show:
             panel.add(card, x_pos, y_pos)
-            x_pos+=CARD_WIDTH + 5
+            x_pos += CARD_WIDTH + 5
         self.inner_center.setWidget(panel)
 
     def _ecartConfirm(self, confirm):
@@ -293,7 +291,7 @@
         self._parent.host.bridge.call('tarotGamePlayCards', None, self.player_nick, self.referee, ecart)
         self.state = "wait"
         self.updateHand()
-    
+
     def addToSelection(self, card):
         self.selected.add(card)
         if len(self.selected) == 6:
@@ -310,11 +308,11 @@
         elif phase == "ecart":
             self.state = "ecart"
         else:
-            error ('INTERNAL ERROR: unmanaged game phase')
-        
+            error('INTERNAL ERROR: unmanaged game phase')
+
         for suit, value in played_cards:
             self.hand.append(self.cards[(suit, value)])
-        
+
         self.hand.sort()
         self.updateHand()
         Window.alert('Cards played are invalid !')
@@ -350,8 +348,8 @@
 
     def getPlayerLocation(self, nick):
         """return player location (top,bottom,left or right)"""
-        for location in ['top','left','bottom','right']:
-            if getattr(self,'%s_nick' % location) == nick:
+        for location in ['top', 'left', 'bottom', 'right']:
+            if getattr(self, '%s_nick' % location) == nick:
                 return location
         print ("ERROR: This line should not be reached")
 
@@ -371,13 +369,13 @@
 
         if player_panel.getWidget() != None:
             #We have already cards on the table, we remove them
-            for pos in ['top','left','bottom','right']:
+            for pos in ['top', 'left', 'bottom', 'right']:
                 getattr(self, "inner_%s" % pos).setWidget(None)
-       
+
         card = self.cards[(suit, value)]
         DOM.setElemAttribute(card.getElement(), "style", "")
         player_panel.setWidget(card)
-    
+
     def tarotGameYourTurn(self):
         """Called when we have to play :)"""
         if self.state == "chien":
@@ -404,7 +402,7 @@
             self.updateHand()
             #nothing on the table
             self.updateToShow()
-            for pos in ['top','left','bottom','right']:
+            for pos in ['top', 'left', 'bottom', 'right']:
                 getattr(self, "inner_%s" % pos).setWidget(None)
             self._parent.host.bridge.call('tarotGameReady', None, self.player_nick, self.referee)
 
@@ -415,5 +413,5 @@
                 title = "You <b>win</b> !"
             else:
                 title = "You <b>loose</b> :("
-        body = re.sub(r'<.*?>',lambda x:'<br />' if '/elem' in x.group(0) else '', xml_data) #Q&D conversion to simple HTML text
+        body = re.sub(r'<.*?>', lambda x: '<br />' if '/elem' in x.group(0) else '', xml_data)  # Q&D conversion to simple HTML text
         InfoDialog(title, body, _new_game).show()