From kde-commits Thu Oct 31 21:21:13 2013 From: Wolfgang Rohdewald Date: Thu, 31 Oct 2013 21:21:13 +0000 To: kde-commits Subject: [kajongg/sid] src: deduplicate some code Message-Id: X-MARC-Message: https://marc.info/?l=kde-commits&m=138325490506771 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 =3D list(unicode(cbName.currentText()) for cbName in se= lf.nameWidgets) assert len(set(self.names)) =3D=3D 4 = -class ScoringPlayer(Player): +class ScoringPlayer(VisiblePlayer, Player): """Player in a scoring game""" # pylint: disable=3Dtoo-many-public-methods def __init__(self, game): self.handBoard =3D None # because Player.init calls clearHand() self.manualRuleBoxes =3D [] Player.__init__(self, game) - self.__front =3D self.game.wall[self.idx] # need front before sett= ing handBoard + VisiblePlayer.__init__(self) self.handBoard =3D 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 =3D [] = @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 =3D 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 =3D None = - def syncHandBoard(self, adding=3DNone): - """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 =3D Internal.field - if isAlive(field): - field.setWindowTitle('Kajongg') - if field: - field.selectorBoard.uiTiles =3D [] - field.selectorBoard.allSelectorTiles =3D [] - if isAlive(field.centralScene): - field.centralScene.removeTiles() - for player in self.players: - player.hide() - if self.wall: - self.wall.hide() - field.game =3D None - field.updateGUI() - field.scoringDialog =3D 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 =3D True self.wall.build() = + def close(self): + """log off from the server and return a Deferred""" + field =3D Internal.field + field.selectorBoard.uiTiles =3D [] + field.selectorBoard.allSelectorTiles =3D [] + if isAlive(field.centralScene): + field.centralScene.removeTiles() + for player in self.players: + player.hide() + if self.wall: + self.wall.hide() + field.game =3D None + field.updateGUI() + field.scoringDialog =3D None + return Game.close(self) + @staticmethod def isScoringGame(): """are we scoring a manual game?""" @@ -1081,6 +1048,8 @@ class PlayField(KXmlGuiWindow): title =3D ', '.join('{name}/{url}'.format(name=3Dx.username, u= rl=3Dx.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=3Dtoo-many-public-methods - def __init__(self, game): - assert game - self.handBoard =3D None # because Player.init calls clearHand() - PlayingPlayer.__init__(self, game) - self.__front =3D self.game.wall[self.idx] # need front before sett= ing handBoard - self.handBoard =3D PlayingHandBoard(self) - self.voice =3D 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 =3D self.game.wall[self.idx] - if self.handBoard: - self.handBoard.setEnabled(self.game and self.game.belongsToHum= anPlayer() and self =3D=3D self.game.myself) + def __init__(self): + # pylint: disable=3Dsuper-init-not-called + self.__front =3D 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=3DNone): + """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=3Dtoo-many-public-methods + def __init__(self, game): + assert game + self.handBoard =3D None # because Player.init calls clearHand() + PlayingPlayer.__init__(self, game) + VisiblePlayer.__init__(self) + self.handBoard =3D PlayingHandBoard(self) + self.voice =3D 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 =3D self.game.wall[self.idx] + if self.handBoard: + self.handBoard.setEnabled(self.game and self.game.belongsToHum= anPlayer() and self =3D=3D 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= =3D=3Dgame.myself, use @@ -93,10 +104,6 @@ class VisiblePlayingPlayer(PlayingPlayer): assert not hand.lenOffset return hand.total() = - def syncHandBoard(self, adding=3DNone): - """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 wal= l""" if not isAlive(self.front.nameLabel): @@ -180,8 +187,6 @@ class VisiblePlayingGame(PlayingGame): client=3DNone, playOpen=3DFalse, autoPlay=3DFalse): PlayingGame.__init__(self, names, ruleset, gameid, wantedGame=3Dwa= ntedGame, shouldSave=3DshouldSave, client=3Dclient, playOpen=3DplayOpen, autoPlay=3DautoPlay) - 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 =3D Internal.field - if isAlive(field): - field.setWindowTitle('Kajongg') - if field: - field.discardBoard.hide() - if isAlive(field.centralScene): - field.centralScene.removeTiles() - field.clientDialog =3D None - for player in self.players: - player.hide() - if self.wall: - self.wall.hide() - field.actionAutoPlay.setChecked(False) - field.startingGame =3D False - field.game =3D None - field.updateGUI() + field.discardBoard.hide() + if isAlive(field.centralScene): + field.centralScene.removeTiles() + field.clientDialog =3D None + for player in self.players: + player.hide() + if self.wall: + self.wall.hide() + field.actionAutoPlay.setChecked(False) + field.startingGame =3D False + field.game =3D None + field.updateGUI() return PlayingGame.close(self)