Example #1
0
File: hand.py Project: KDE/kajongg
    def __init__(self, player, string, prevHand=None):
        """evaluate string for player. rules are to be applied in any case"""
        if hasattr(self, 'string'):
            # I am from cache
            return

        # shortcuts for speed:
        self._player = weakref.ref(player)
        self.ruleset = player.game.ruleset
        self.intelligence = player.intelligence if player else AIDefault()
        self.string = string
        self.__robbedTile = Tile.unknown
        self.prevHand = prevHand
        self.__won = None
        self.__score = None
        self.__callingHands = None
        self.__mjRule = None
        self.ruleCache = {}
        self.__lastTile = None
        self.__lastSource = TileSource.Unknown
        self.__announcements = set()
        self.__lastMeld = 0
        self.__lastMelds = MeldList()
        self.tiles = None
        self.melds = MeldList()
        self.bonusMelds = MeldList()
        self.usedRules = []
        self.__rest = TileList()
        self.__arranged = None

        self.__parseString(string)
        self.__won = self.lenOffset == 1 and player.mayWin

        if Debug.hand or (Debug.mahJongg and self.lenOffset == 1):
            self.debug(fmt('{callers}',
                           callers=callers(exclude=['__init__'])))
            Hand.indent += 1
            _hideString = string
            self.debug(fmt('New Hand {_hideString} {self.lenOffset}'))

        try:
            self.__arrange()
            self.__calculate()
            self.__arranged = True
        except Hand.__NotWon as notwon:
            if Debug.mahJongg:
                self.debug(fmt(str(notwon)))
            self.__won = False
            self.__score = Score()
        finally:
            self._fixed = True
            if Debug.hand or (Debug.mahJongg and self.lenOffset == 1):
                _hideSelf = str(self)
                _hideScore = str(self.score)
                self.debug(fmt(
                    'Fixing {_hideSelf} {self.won} {_hideScore}'))
            Hand.indent -= 1
Example #2
0
    def __init__(self, player, string, prevHand=None):
        """evaluate string for player. rules are to be applied in any case"""
        if hasattr(self, 'string'):
            # I am from cache
            return

        # shortcuts for speed:
        self._player = weakref.ref(player)
        self.ruleset = player.game.ruleset
        self.intelligence = player.intelligence if player else AIDefault()
        self.string = string
        self.__robbedTile = Tile.unknown
        self.prevHand = prevHand
        self.__won = None
        self.__score = None
        self.__callingHands = None
        self.__mjRule = None
        self.ruleCache = {}
        self.__lastTile = None
        self.__lastSource = TileSource.Unknown
        self.__announcements = set()
        self.__lastMeld = 0
        self.__lastMelds = MeldList()
        self.tiles = None
        self.melds = MeldList()
        self.bonusMelds = MeldList()
        self.usedRules = []
        self.__rest = TileList()
        self.__arranged = None

        self.__parseString(string)
        self.__won = self.lenOffset == 1 and player.mayWin

        if Debug.hand or (Debug.mahJongg and self.lenOffset == 1):
            self.debug(fmt('{callers}',
                           callers=callers(exclude=['__init__'])))
            Hand.indent += 1
            self.debug('New Hand {} {}'.format(string, self.lenOffset))

        try:
            self.__arrange()
            self.__calculate()
            self.__arranged = True
        except Hand.__NotWon as notwon:
            if Debug.mahJongg:
                self.debug(fmt(str(notwon)))
            self.__won = False
            self.__score = Score()
        finally:
            self._fixed = True
            if Debug.hand or (Debug.mahJongg and self.lenOffset == 1):
                self.debug('Fixing {} {} {}'.format(self, self.won, self.score))
            Hand.indent -= 1
Example #3
0
File: hand.py Project: KDE/kajongg
 def __maybeMahjongg(self):
     """check if this is a mah jongg hand.
     Return a sorted list of matching MJ rules, highest
     total first. If no rule matches, return None"""
     if self.lenOffset == 1 and self.player.mayWin:
         matchingMJRules = [x for x in self.ruleset.mjRules
                            if x.appliesToHand(self)]
         if matchingMJRules:
             if self.robbedTile and self.robbedTile.isConcealed:
                 # Millington 58: robbing hidden kong is only
                 # allowed for 13 orphans
                 matchingMJRules = [
                     x for x in matchingMJRules
                     if 'mayrobhiddenkong' in x.options]
             result = sorted(matchingMJRules, key=lambda x: -x.score.total())
             if Debug.mahJongg:
                 self.debug(fmt('{callers} Found {matchingMJRules}',
                                callers=callers()))
             return result
Example #4
0
 def __maybeMahjongg(self):
     """check if this is a mah jongg hand.
     Return a sorted list of matching MJ rules, highest
     total first. If no rule matches, return None"""
     if self.lenOffset == 1 and self.player.mayWin:
         matchingMJRules = [x for x in self.ruleset.mjRules
                            if x.appliesToHand(self)]
         if matchingMJRules:
             if self.robbedTile and self.robbedTile.isConcealed:
                 # Millington 58: robbing hidden kong is only
                 # allowed for 13 orphans
                 matchingMJRules = [
                     x for x in matchingMJRules
                     if 'mayrobhiddenkong' in x.options]
             result = sorted(matchingMJRules, key=lambda x: -x.score.total())
             if Debug.mahJongg:
                 self.debug(fmt('{callers} Found {matchingMJRules}',
                                callers=callers()))
             return result
Example #5
0
def logMessage(msg, prio, showDialog, showStack=False, withGamePrefix=True):
    """writes info message to log and to stdout"""
    # pylint: disable=R0912
    if isinstance(msg, Exception):
        msg = __exceptionToString(msg)
    msg = str(msg)
    msg = translateServerMessage(msg)
    __logUnicodeMessage(prio, __enrichMessage(msg, withGamePrefix))
    if showStack:
        if showStack is True:
            lower = 2
        else:
            lower = -showStack - 3
        for line in traceback.format_stack()[lower:-3]:
            if 'logException' not in line:
                __logUnicodeMessage(prio, '  ' + line.strip())
    if int(Debug.callers):
        __logUnicodeMessage(prio, callers(int(Debug.callers)))
    if showDialog and not Internal.isServer:
        if prio == logging.INFO:
            return Information(msg)
        else:
            return Sorry(msg, always=True)
    return NoPrompt(msg)
Example #6
0
 def __exit__(self, exc_type, exc_value, trback):
     if Debug.random:
         self.rnd.game.debug(
             '{} out of {} calls to random by {} from {}'.format(
                 CountingRandom.count - self.oldCount, CountingRandom.count,
                 self.what, callers()))