[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    [kajongg/sid] src: deduplicate some code
From:       Wolfgang Rohdewald <wolfgang () rohdewald ! de>
Date:       2013-10-31 21:21:13
Message-ID: E1Vbzg1-0002lj-Bk () scm ! kde ! org
[Download RAW message or body]

Git commit 7b38ce384ae330627ad4ac22c5199f2c1b58bce4 by Wolfgang Rohdewald.
Committed on 31/10/2013 at 12:50.
Pushed by wrohdewald into branch 'sid'.

deduplicate some code

mostly by introducing mixin class VisiblePlayer

M  +2    -0    src/game.py
M  +21   -52   src/playfield.py
M  +42   -40   src/visible.py

http://commits.kde.org/kajongg/7b38ce384ae330627ad4ac22c5199f2c1b58bce4

diff --git a/src/game.py b/src/game.py
index 4890054..6c3f250 100644
--- a/src/game.py
+++ b/src/game.py
@@ -168,6 +168,8 @@ class Game(object):
         self._scanGameOption()
         if self.shouldSave:
             self.saveStartTime()
+        for player in self.players:
+            player.clearHand()
 
     def clearHand(self):
         """empty all data"""
diff --git a/src/playfield.py b/src/playfield.py
index e9af0e3..193b53f 100644
--- a/src/playfield.py
+++ b/src/playfield.py
@@ -82,6 +82,7 @@ try:
     from uiwall import UIWall
     from animation import animate, afterCurrentAnimationDo, Animated
     from player import Player, Players
+    from visible import VisiblePlayer
     from game import Game
     from chat import ChatWindow
 
@@ -242,21 +243,16 @@ class SelectPlayers(SelectRuleset):
         self.names = list(unicode(cbName.currentText()) for cbName in \
self.nameWidgets)  assert len(set(self.names)) == 4
 
-class ScoringPlayer(Player):
+class ScoringPlayer(VisiblePlayer, Player):
     """Player in a scoring game"""
     # pylint: disable=too-many-public-methods
     def __init__(self, game):
         self.handBoard = None # because Player.init calls clearHand()
         self.manualRuleBoxes = []
         Player.__init__(self, game)
-        self.__front = self.game.wall[self.idx] # need front before setting \
handBoard +        VisiblePlayer.__init__(self)
         self.handBoard = ScoringHandBoard(self)
 
-    def hide(self):
-        """clear visible data and hide"""
-        self.clearHand()
-        self.handBoard.hide()
-
     def clearHand(self):
         """clears attributes related to current hand"""
         Player.clearHand(self)
@@ -269,26 +265,6 @@ class ScoringPlayer(Player):
         self.manualRuleBoxes = []
 
     @property
-    def idx(self):
-        """our index in the player list"""
-        if not self in self.game.players:
-            # we will be added next
-            return len(self.game.players)
-        return self.game.players.index(self)
-
-    @property
-    def front(self):
-        """front"""
-        return self.__front
-
-    @front.setter
-    def front(self, value):
-        """also assign handBoard to front"""
-        self.__front = value
-        if value and self.handBoard:
-            self.handBoard.setParentItem(value)
-
-    @property
     def handTotal(self):
         """the hand total of this player"""
         if self.hasManualScore():
@@ -413,10 +389,6 @@ class ScoringPlayer(Player):
                 logDebug('      exposed: %s' % self._exposedMelds)
         self._hand = None
 
-    def syncHandBoard(self, adding=None):
-        """update display of handBoard. Set Focus to tileName."""
-        self.handBoard.sync()
-
 class ScoringGame(Game):
     """we play manually on a real table with real tiles and use
     kajongg only for scoring"""
@@ -428,31 +400,10 @@ class ScoringGame(Game):
         field.selectorBoard.load(self)
         self.prepareHand()
         self.initHand()
-        for player in self.players:
-            player.clearHand()
         Internal.field.adjustView()
         Internal.field.updateGUI()
         self.wall.decorate()
 
-    def close(self):
-        """log off from the server and return a Deferred"""
-        field = Internal.field
-        if isAlive(field):
-            field.setWindowTitle('Kajongg')
-        if field:
-            field.selectorBoard.uiTiles = []
-            field.selectorBoard.allSelectorTiles = []
-            if isAlive(field.centralScene):
-                field.centralScene.removeTiles()
-            for player in self.players:
-                player.hide()
-            if self.wall:
-                self.wall.hide()
-            field.game = None
-            field.updateGUI()
-            field.scoringDialog = None
-        return Game.close(self)
-
     def prepareHand(self):
         """prepare a scoring game hand"""
         Game.prepareHand(self)
@@ -462,6 +413,22 @@ class ScoringGame(Game):
             selector.hasFocus = True
             self.wall.build()
 
+    def close(self):
+        """log off from the server and return a Deferred"""
+        field = Internal.field
+        field.selectorBoard.uiTiles = []
+        field.selectorBoard.allSelectorTiles = []
+        if isAlive(field.centralScene):
+            field.centralScene.removeTiles()
+        for player in self.players:
+            player.hide()
+        if self.wall:
+            self.wall.hide()
+        field.game = None
+        field.updateGUI()
+        field.scoringDialog = None
+        return Game.close(self)
+
     @staticmethod
     def isScoringGame():
         """are we scoring a manual game?"""
@@ -1081,6 +1048,8 @@ class PlayField(KXmlGuiWindow):
             title = ', '.join('{name}/{url}'.format(name=x.username, url=x.url) for \
x in connections)  if title:
                 self.setWindowTitle('%s - Kajongg' % title)
+            else:
+                self.setWindowTitle('Kajongg')
         for action in [self.actionScoreGame, self.actionPlayGame]:
             action.setEnabled(not bool(game))
         self.actionAbortGame.setEnabled(bool(game))
diff --git a/src/visible.py b/src/visible.py
index 0b4bb0f..5ab5bf8 100644
--- a/src/visible.py
+++ b/src/visible.py
@@ -24,30 +24,17 @@ from PyQt4.QtGui import QBrush, QColor
 from util import m18nc
 from message import Message
 from common import Internal, isAlive
-from player import PlayingPlayer
+from player import Player, PlayingPlayer
 from game import PlayingGame
 from handboard import PlayingHandBoard
 from animation import Animated
 
-class VisiblePlayingPlayer(PlayingPlayer):
-    """this player instance has a visual representation"""
-    # pylint: disable=too-many-public-methods
-    def __init__(self, game):
-        assert game
-        self.handBoard = None # because Player.init calls clearHand()
-        PlayingPlayer.__init__(self, game)
-        self.__front = self.game.wall[self.idx] # need front before setting \
                handBoard
-        self.handBoard = PlayingHandBoard(self)
-        self.voice = None
+class VisiblePlayer(Player):
+    """Mixin for VisiblePlayingPlayer and ScoringPlayer"""
 
-    def clearHand(self):
-        """clears attributes related to current hand"""
-        super(VisiblePlayingPlayer, self).clearHand()
-        if self.game and self.game.wall:
-            # is None while __del__
-            self.front = self.game.wall[self.idx]
-        if self.handBoard:
-            self.handBoard.setEnabled(self.game and self.game.belongsToHumanPlayer() \
and self == self.game.myself) +    def __init__(self):
+        # pylint: disable=super-init-not-called
+        self.__front = self.game.wall[self.idx]
 
     def hide(self):
         """clear visible data and hide"""
@@ -74,6 +61,30 @@ class VisiblePlayingPlayer(PlayingPlayer):
         if value and self.handBoard:
             self.handBoard.setParentItem(value)
 
+    def syncHandBoard(self, adding=None):
+        """update display of handBoard. Set Focus to tileName."""
+        self.handBoard.sync(adding)
+
+class VisiblePlayingPlayer(VisiblePlayer, PlayingPlayer):
+    """this player instance has a visual representation"""
+    # pylint: disable=too-many-public-methods
+    def __init__(self, game):
+        assert game
+        self.handBoard = None # because Player.init calls clearHand()
+        PlayingPlayer.__init__(self, game)
+        VisiblePlayer.__init__(self)
+        self.handBoard = PlayingHandBoard(self)
+        self.voice = None
+
+    def clearHand(self):
+        """clears attributes related to current hand"""
+        super(VisiblePlayingPlayer, self).clearHand()
+        if self.game and self.game.wall:
+            # is None while __del__
+            self.front = self.game.wall[self.idx]
+        if self.handBoard:
+            self.handBoard.setEnabled(self.game and self.game.belongsToHumanPlayer() \
and self == self.game.myself) +
     def handTotalForWall(self):
         """returns the totale for the new hand. Same as current unless we need to \
                discard.
         In that case, make an educated guess about the discard. For \
player==game.myself, use @@ -93,10 +104,6 @@ class \
VisiblePlayingPlayer(PlayingPlayer):  assert not hand.lenOffset
         return hand.total()
 
-    def syncHandBoard(self, adding=None):
-        """update display of handBoard. Set Focus to tileName."""
-        self.handBoard.sync(adding)
-
     def colorizeName(self):
         """set the color to be used for showing the player name on the wall"""
         if not isAlive(self.front.nameLabel):
@@ -180,8 +187,6 @@ class VisiblePlayingGame(PlayingGame):
             client=None, playOpen=False, autoPlay=False):
         PlayingGame.__init__(self, names, ruleset, gameid, wantedGame=wantedGame, \
shouldSave=shouldSave,  client=client, playOpen=playOpen, autoPlay=autoPlay)
-        for player in self.players:
-            player.clearHand()
         Internal.field.adjustView()
         Internal.field.updateGUI()
         self.wall.decorate()
@@ -189,19 +194,16 @@ class VisiblePlayingGame(PlayingGame):
     def close(self):
         """close the game"""
         field = Internal.field
-        if isAlive(field):
-            field.setWindowTitle('Kajongg')
-        if field:
-            field.discardBoard.hide()
-            if isAlive(field.centralScene):
-                field.centralScene.removeTiles()
-            field.clientDialog = None
-            for player in self.players:
-                player.hide()
-            if self.wall:
-                self.wall.hide()
-            field.actionAutoPlay.setChecked(False)
-            field.startingGame = False
-            field.game = None
-            field.updateGUI()
+        field.discardBoard.hide()
+        if isAlive(field.centralScene):
+            field.centralScene.removeTiles()
+        field.clientDialog = None
+        for player in self.players:
+            player.hide()
+        if self.wall:
+            self.wall.hide()
+        field.actionAutoPlay.setChecked(False)
+        field.startingGame = False
+        field.game = None
+        field.updateGUI()
         return PlayingGame.close(self)


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic