Commit 969267f2 authored by Wolfgang Rohdewald's avatar Wolfgang Rohdewald
Browse files

new class TileSource

parent f259b1ee
......@@ -29,6 +29,7 @@ from log import logDebug, logException, logWarning, m18nc
from message import Message
from common import Internal, Debug, Options, StrMixin
from common import unicodeString, unicode
from tilesource import TileSource
from rule import Ruleset
from game import PlayingGame
from query import Query
......@@ -445,7 +446,7 @@ class Client(pb.Referenceable):
if not self.thatWasMe(move.player) and not self.game.playOpen:
move.player.showConcealedTiles(hadTiles)
move.player.lastTile = calledTile.exposed
move.player.lastSource = 'd'
move.player.lastSource = TileSource.LivingWallDiscard
move.exposedMeld = move.player.exposeMeld(
hadTiles,
calledTile=calledTileItem or calledTile)
......
......@@ -27,6 +27,7 @@ from hashlib import md5
from log import dbgIndent, Fmt, fmt
from tile import Tile, TileList
from tilesource import TileSource
from meld import Meld, MeldList
from rule import Score, UsedRule
from common import Debug, isPython3
......@@ -105,7 +106,7 @@ class Hand(object):
self.__mjRule = None
self.ruleCache = {}
self.__lastTile = None
self.__lastSource = None
self.__lastSource = TileSource.Unknown
self.__announcements = set()
self.__lastMeld = 0
self.__lastMelds = MeldList()
......@@ -152,7 +153,10 @@ class Hand(object):
partId = part[0]
if partId == 'm':
if len(part) > 1:
self.__lastSource = part[1]
try:
self.__lastSource = TileSource.byChar[part[1]]
except KeyError:
raise Exception('{} has unknown lastTile {}'.format(inString, part[1]))
if len(part) > 2:
self.__announcements = set(part[2])
elif partId == 'L':
......@@ -192,7 +196,7 @@ class Hand(object):
if last and not last.isBonus:
assert last in self.tiles, \
'lastTile %s is not in hand %s' % (last, str(self))
if self.__lastSource == 'k':
if self.__lastSource is TileSource.RobbedKong:
assert self.tiles.count(last.exposed) + \
self.tiles.count(last.concealed) == 1, (
'Robbing kong: I cannot have '
......@@ -429,7 +433,7 @@ class Hand(object):
parts.append('R' + ''.join(str(x) for x in sorted(rest)))
if lastSource or announcements:
parts.append('m{}{}'.format(
self.lastSource or '.',
self.lastSource.char,
''.join(self.announcements)))
if lastTile:
parts.append('L{}{}'.format(lastTile, lastMeld if lastMeld else ''))
......
......@@ -27,6 +27,7 @@ from common import StrMixin
from wind import East
from query import Query
from tile import Tile, TileList, elements
from tilesource import TileSource
from meld import Meld, MeldList
from permutations import Permutations
from message import Message
......@@ -141,7 +142,7 @@ class Player(StrMixin):
self.cacheHits = 0
self.cacheMisses = 0
self.clearHand()
self.__lastSource = '1' # no source: blessing from heaven or earth
self.__lastSource = TileSource.East14th
self.handBoard = None
def __lt__(self, other):
......@@ -195,7 +196,7 @@ class Player(StrMixin):
self.newHandContent = None
self.originalCallingHand = None
self.__lastTile = None
self.lastSource = '1'
self.lastSource = TileSource.East14th
self.lastMeld = Meld()
self.__mayWin = True
self.__payment = 0
......@@ -272,10 +273,10 @@ class Player(StrMixin):
def lastSource(self, lastSource):
"""the source of the last tile the player got"""
self.__lastSource = lastSource
if lastSource == 'd' and not self.game.wall.living:
self.__lastSource = 'Z'
if lastSource == 'w' and not self.game.wall.living:
self.__lastSource = 'z'
if lastSource is TileSource.LivingWallDiscard and not self.game.wall.living:
self.__lastSource = TileSource.LivingWallEndDiscard
if lastSource is TileSource.LivingWall and not self.game.wall.living:
self.__lastSource = TileSource.LivingWallEnd
@property
def nameid(self):
......@@ -335,10 +336,10 @@ class Player(StrMixin):
self.lastTile = tile
self.addConcealedTiles([tile])
if deadEnd:
self.lastSource = 'e'
self.lastSource = TileSource.DeadWall
else:
self.game.lastDiscard = None
self.lastSource = 'w'
self.lastSource = TileSource.LivingWall
return self.lastTile
def removeTile(self, tile):
......@@ -381,7 +382,7 @@ class Player(StrMixin):
def mjString(self):
"""compile hand info into a string as needed by the scoring engine"""
announcements = 'a' if self.originalCall else ''
return ''.join(['m', self.lastSource, ''.join(announcements)])
return ''.join(['m', self.lastSource.char, ''.join(announcements)])
def makeTileKnown(self, tileName):
"""used when somebody else discards a tile"""
......@@ -504,8 +505,8 @@ class PlayingPlayer(Player):
PlayingPlayer.addConcealedTiles(
self,
[withDiscard]) # this should NOT invoke syncHandBoard
if self.lastSource != 'k': # robbed the kong
self.lastSource = 'd'
if self.lastSource is not TileSource.RobbedKong:
self.lastSource = TileSource.LivingWallDiscard
# the last claimed meld is exposed
melds.remove(lastMeld)
lastTile = withDiscard.exposed
......@@ -706,7 +707,7 @@ class PlayingPlayer(Player):
def robsTile(self):
"""True if the player is robbing a tile"""
self.lastSource = 'k'
self.lastSource = TileSource.RobbedKong
def scoreMatchesServer(self, score):
"""do we compute the same score as the server does?"""
......
......@@ -19,6 +19,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
"""
from tile import Tile, elements
from tilesource import TileSource
from meld import Meld, MeldList
from common import IntDict
from wind import East
......@@ -917,47 +918,47 @@ class AllGreen(RuleCode):
class LastTileFromWall(RuleCode):
def appliesToHand(hand):
return hand.lastSource == 'w'
return hand.lastSource is TileSource.LivingWall
class LastTileFromDeadWall(RuleCode):
def appliesToHand(hand):
return hand.lastSource == 'e'
return hand.lastSource is TileSource.DeadWall
def selectable(hand):
"""for scoring game"""
return hand.lastSource == 'w'
return hand.lastSource is TileSource.LivingWall
class IsLastTileFromWall(RuleCode):
def appliesToHand(hand):
return hand.lastSource == 'z'
return hand.lastSource is TileSource.LivingWallEnd
def selectable(hand):
"""for scoring game"""
return hand.lastSource == 'w'
return hand.lastSource is TileSource.LivingWall
class IsLastTileFromWallDiscarded(RuleCode):
def appliesToHand(hand):
return hand.lastSource == 'Z'
return hand.lastSource is TileSource.LivingWallEndDiscard
def selectable(hand):
"""for scoring game"""
return hand.lastSource == 'd'
return hand.lastSource is TileSource.LivingWallDiscard
class RobbingKong(RuleCode):
def appliesToHand(hand):
return hand.lastSource == 'k'
return hand.lastSource is TileSource.RobbedKong
def selectable(hand):
"""for scoring game"""
return (hand.lastSource and hand.lastSource in 'kwd'
return (hand.lastSource in (TileSource.RobbedKong, TileSource.LivingWall, TileSource.LivingWallDiscard)
and hand.lastTile and hand.lastTile.group.islower()
and [x.exposed for x in hand.tiles].count(hand.lastTile.exposed) < 2)
......@@ -1265,19 +1266,19 @@ class TwofoldFortune(RuleCode):
class BlessingOfHeaven(RuleCode):
def appliesToHand(hand):
return hand.ownWind is East and hand.lastSource == '1'
return hand.ownWind is East and hand.lastSource is TileSource.East14th
def selectable(hand):
"""for scoring game"""
return (hand.ownWind is East
and hand.lastSource and hand.lastSource in 'wd'
and hand.lastSource in (TileSource.LivingWall, TileSource.LivingWallDiscard)
and not hand.announcements - {'a'})
class BlessingOfEarth(RuleCode):
def appliesToHand(hand):
result = hand.ownWind is not East and hand.lastSource == '1'
result = hand.ownWind is not East and hand.lastSource is TileSource.East14th
if result:
assert hand.lastTile.isExposed, '{}: Blessing of Earth: last tile must be exposed'.format(hand)
return result
......@@ -1285,7 +1286,7 @@ class BlessingOfEarth(RuleCode):
def selectable(hand):
"""for scoring game"""
return (hand.ownWind is not East
and hand.lastSource and hand.lastSource in 'wd'
and hand.lastSource in (TileSource.LivingWall, TileSource.LivingWallDiscard)
and not hand.announcements - {'a'})
......
......@@ -27,6 +27,7 @@ from qt import QPushButton, QMessageBox, QComboBox
from common import Internal, isAlive, unicode
from wind import Wind
from tilesource import TileSource
from animation import animate
from log import logError, logDebug, logWarning, m18n
from query import Query
......@@ -398,15 +399,15 @@ class ScoringPlayer(VisiblePlayer, Player):
def __mjstring(self):
"""compile hand info into a string as needed by the scoring engine"""
if self.lastTile and self.lastTile.isConcealed:
lastSource = 'w'
lastSource = TileSource.LivingWall.char
else:
lastSource = 'd'
lastSource = TileSource.LivingWallDiscard.char
announcements = set()
rules = [x.rule for x in self.manualRuleBoxes if x.isChecked()]
for rule in rules:
options = rule.options
if 'lastsource' in options:
if lastSource != '1':
if lastSource != TileSource.East14th.char:
# this defines precedences for source of last tile
lastSource = options['lastsource']
if 'announcements' in options:
......
......@@ -34,6 +34,7 @@ from twisted.spread import pb
from common import Debug, Internal
from wind import Wind
from tilesource import TileSource
from util import Duration
from message import Message, ChatMessage
from log import logDebug, logError, m18nE, m18n, m18ncE
......@@ -553,7 +554,7 @@ class ServerTable(Table):
block = DeferredBlock(self)
player = msg.player
if dangerousText:
if mustPlayDangerous and player.lastSource not in 'dZ':
if mustPlayDangerous and not player.lastSource.isDiscarded:
if Debug.dangerousGame:
tile = Tile(msg.args[0])
logDebug(u'%s claims no choice. Discarded %s, keeping %s. %s' %
......@@ -702,7 +703,7 @@ class ServerTable(Table):
self.game.activePlayer = player
if lastDiscard:
player.lastTile = lastDiscard.exposed
player.lastSource = 'd'
player.lastSource = TileSource.LivingWallDiscard
player.exposeMeld(hasTiles, lastDiscard)
self.game.lastDiscard = None
block = DeferredBlock(self)
......@@ -781,7 +782,7 @@ class ServerTable(Table):
block = DeferredBlock(self)
if robbedTheKong:
block.tellAll(player, Message.RobbedTheKong, tile=withDiscard)
if (player.lastSource == 'd'
if (player.lastSource is TileSource.LivingWallDiscard
and self.game.dangerousFor(discardingPlayer, player.lastTile)
and discardingPlayer.playedDangerous):
player.usedDangerousFrom = discardingPlayer
......
# -*- coding: utf-8 -*-
"""
Copyright (C) 2008-2014 Wolfgang Rohdewald <wolfgang@rohdewald.de>
Kajongg is free software you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
"""
from __future__ import print_function
class TileSource(object):
"""
some constants
"""
byChar = dict()
def __str__(self):
return str(self.char)
def __repr__(self):
return str(self)
class SourceClass(object):
"""Defines defaults"""
isDiscarded = False
class LivingWallDiscard(SourceClass):
"""Last Tile was discarded"""
char = 'd'
isDiscarded = True
class LivingWall(SourceClass):
"""Last tile comes from wall"""
char = 'w'
class East14th(SourceClass):
"""This is the 14th tile for East"""
char = '1'
class RobbedKong(SourceClass):
"""Last tile comes from robbing a kong"""
char = 'k'
class DeadWall(SourceClass):
"""Last tile comes from dead wall"""
char = 'e'
class LivingWallEnd(SourceClass):
"""Last tile comes from living wall and is the last living wall tile"""
char = 'z'
class LivingWallEndDiscard(SourceClass):
"""like LivingWallEnd but discarded"""
char = 'Z'
isDiscarded = True
class Unknown(SourceClass):
"""Unknown source"""
char = '.'
TileSource.byChar['w'] = TileSource.LivingWall
TileSource.byChar['d'] = TileSource.LivingWallDiscard
TileSource.byChar['z'] = TileSource.LivingWallEnd
TileSource.byChar['e'] = TileSource.DeadWall
TileSource.byChar['Z'] = TileSource.LivingWallEndDiscard
TileSource.byChar['1'] = TileSource.East14th
TileSource.byChar['k'] = TileSource.RobbedKong
TileSource.byChar['.'] = TileSource.Unknown
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment