コード例 #1
0
ファイル: Parser.py プロジェクト: jbzdarkid/HearthstonePro
def parseFile(line_generator, config, *args):
    '''
    Main parsing function.
    line_generator can be a tail for live execution,
    or a file object for testing.
    '''
    lineNo = 0
    from re import match
    showEntity = None
    for line in line_generator(*args):
        lineNo += 1
        line_parts = match('^D \d{2}:\d{2}:\d{2}\.\d{7} ([a-zA-Z]*\.[a-zA-Z]*\(\)) -\s*([A-Z_]{2,}|)(.*)', line)
        if line_parts is None: # Any of the error messages won't match, but it's not safe to use them
            continue
        source = line_parts.group(1)
        type = line_parts.group(2)
        data = parse(line_parts.group(3))

        if source == 'GameState.DebugPrintEntityChoices()':
            if 'ChoiceType' in data and data['ChoiceType'] == 'MULLIGAN':
                if data['Player'] == config['username']:
                    logging.debug('You are player id %s' % data['id'])
                    Utilities.us = data['id']
                else:
                    logging.debug('Opponent is player id %s' % data['id'])
                    Utilities.them = data['id']
        if source == 'GameState.DebugPrintEntitiesChosen()':
            # Cards that were not mulliganed
            if data.keys()[0][:8] == 'Entities': # Entities[0], e.g.
                if data.values()[0]['zone'] == 'HAND':
                    Hand.keep(data.values()[0])
        if showEntity is not None:
            if type:
                showEntity = None
            elif 'tag' in data and data['tag'] == 'ZONE' and data['value'] == 'GRAVEYARD':
                Hand.discard(showEntity)
        if source == 'PowerTaskList.DebugPrintPower()':
            if type == 'BLOCK_END':
                Legendaries.blockEnd()
            elif type == 'BLOCK_START':
                if data['BlockType'] == 'TRIGGER':
                    if 'zone' in data['Entity']:
                        if data['Entity']['zone'] == 'GRAVEYARD':
                            Cards.die(data['Entity'])
                            Legendaries.die(data['Entity'])
                        elif data['Entity']['zone'] == 'PLAY':
                            Cards.trigger(data['Entity'])
                elif data['BlockType'] == 'POWER': # When a card actually hits the board
                    if 'Target' in data and isinstance(data['Target'], dict):
                        Cards.play3(data['Entity'], data['Target']) # A card targets another card.
                        Legendaries.play3(data['Entity'], data['Target'])
                    else:
                        Cards.play2(data['Entity'])
                        Legendaries.play2(data['Entity'])
            elif type == 'SHOW_ENTITY': # Start of a SHOW_ENTITY block of data
                showEntity = data['Entity']
            elif type == 'TAG_CHANGE':
                if data['tag'] == 'FIRST_PLAYER':
                    logging.warning('New game started')
                    Utilities.wentFirst(data['Entity'] == config['username'])
                if data['tag'] == 'JUST_PLAYED':
                    if data['Entity']['zone'] == 'HAND':
                        Hand.play(data['Entity']) # When a card is removed from a player's hand
                elif data['tag'] == 'RESOURCES':
                    if data['Entity'] != config['username']:
                        Utilities.resources = data['value']
                elif data['tag'] == 'STEP':
                    if data['value'] == 'FINAL_GAMEOVER':
                        Hand.reset()
                        Utilities.reset()
                        Legendaries.reset()
                        print 'Game Over'
                    if data['value'] == 'MAIN_READY':
                        if Utilities.ourTurn():
                            Hand.turnover()
                            Cards.turnover()
                            Legendaries.turnover()
                elif data['tag'] == 'TURN':
                    Utilities.turn = int(data['value'])
                elif data['tag'] == 'ZONE_POSITION':
                    if 'zone' in data['Entity'] and data['Entity']['zone'] == 'DECK':
                        Hand.draw(data['Entity'], int(data['value'])-1)