Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_tag_value_parsing():
tag, value = parse_initial_tag("tag=ZONE value=PLAY")
assert tag == GameTag.ZONE
assert value == Zone.PLAY
tag, value = parse_initial_tag("tag=CARDTYPE value=PLAYER")
assert tag == GameTag.CARDTYPE
assert value == CardType.PLAYER
tag, value = parse_initial_tag("tag=1 value=2")
assert tag == 1
assert value == 2
tag, value = parse_initial_tag("tag=9999998 value=123")
assert tag == 9999998
assert value == 123
parser = LogParser()
parser.read(StringIO(data.INITIAL_GAME))
parser.flush()
entity_format = (
"[name=UNKNOWN ENTITY [cardType=INVALID] id=24 zone=DECK zonePos=0 cardId= player=1]"
)
id = parser.parse_entity_id(entity_format)
assert id == 24
line = "TAG_CHANGE Entity=%s tag=ZONE value=HAND" % (entity_format)
packet = parser.handle_power(None, "TAG_CHANGE", line)
assert packet.power_type == PowerType.TAG_CHANGE
assert packet.entity == id
assert packet.tag == GameTag.ZONE
assert packet.value == Zone.HAND
GameTag.PLAYSTATE: PlayState.PLAYING,
GameTag.PLAYER_ID: 1,
GameTag.TEAM_ID: 1,
GameTag.ZONE: Zone.PLAY,
GameTag.CONTROLLER: 1,
GameTag.ENTITY_ID: 2,
GameTag.CARDTYPE: CardType.PLAYER,
}
assert game.players[1].tags == {
GameTag.PLAYSTATE: PlayState.PLAYING,
GameTag.CURRENT_PLAYER: 1,
GameTag.FIRST_PLAYER: 1,
GameTag.PLAYER_ID: 2,
GameTag.TEAM_ID: 2,
GameTag.ZONE: Zone.PLAY,
GameTag.CONTROLLER: 2,
GameTag.ENTITY_ID: 3,
GameTag.CARDTYPE: CardType.PLAYER,
}
# Test that there should be no friendly player
fpe = FriendlyPlayerExporter(packet_tree)
friendly_player = fpe.export()
assert not friendly_player
def zone(self):
return self.tags.get(GameTag.ZONE, Zone.INVALID)
def hero(self):
entity_id = self.tags.get(GameTag.HERO_ENTITY, 0)
if entity_id:
return self.game.find_entity_by_id(entity_id)
else:
# Fallback that should never trigger
for entity in self.in_zone(Zone.PLAY):
if entity.type == CardType.HERO:
return entity
def handle_show_entity(self, packet):
tags = dict(packet.tags)
if GameTag.CONTROLLER in tags:
self._controller_map[packet.entity] = tags[GameTag.CONTROLLER]
if tags.get(GameTag.ZONE) != Zone.HAND:
# Ignore cards already in play (such as enchantments, common in TB)
return
# The first SHOW_ENTITY packet will always be the friendly player's.
self.friendly_player = self._controller_map[packet.entity]
def handle_full_entity(self, packet):
tags = dict(packet.tags)
if GameTag.CONTROLLER in tags:
self._controller_map[packet.entity] = tags[GameTag.CONTROLLER]
# The following logic only works for pre-13619 logs
# The first FULL_ENTITY packet which is in Zone.HAND and does *not*
# have an ID is owned by the friendly player's *opponent*.
if tags[GameTag.ZONE] == Zone.HAND and not packet.card_id:
controller = self._controller_map[packet.entity]
# That controller is the enemy player - return its opponent.
self.friendly_player = controller % 2 + 1
GameTag.COUNTER: Type.BOOL,
GameTag.ARTISTNAME: Type.STRING,
GameTag.LocalizationNotes: Type.STRING,
GameTag.ImmuneToSpellpower: Type.BOOL,
GameTag.ADJACENT_BUFF: Type.BOOL,
GameTag.FLAVORTEXT: Type.LOCSTRING,
GameTag.HealTarget: Type.BOOL,
GameTag.AURA: Type.BOOL,
GameTag.POISONOUS: Type.BOOL,
GameTag.HOW_TO_EARN: Type.LOCSTRING,
GameTag.HOW_TO_EARN_GOLDEN: Type.LOCSTRING,
GameTag.AI_MUST_PLAY: Type.BOOL,
GameTag.AFFECTED_BY_SPELL_POWER: Type.BOOL,
GameTag.SPARE_PART: Type.BOOL,
GameTag.PLAYSTATE: PlayState,
GameTag.ZONE: Zone,
GameTag.STEP: Step,
GameTag.NEXT_STEP: Step,
GameTag.STATE: State,
GameTag.MULLIGAN_STATE: Mulligan,
GameTag.AUTOATTACK: Type.BOOL,
}
LOCALIZED_TAGS = [k for k, v in TAG_TYPES.items() if v == Type.LOCSTRING]
class PuzzleType(IntEnum):
"""TAG_PUZZLE_TYPE"""
INVALID = 0
MIRROR = 1
def __init__(self, id):
self.id = id
self.game = None
self.tags = {}
self.initial_creator = 0
self.initial_zone = Zone.INVALID
self._initial_controller = 0
def initial_deck(self):
for entity in self.game.initial_entities:
# Exclude entities that aren't initially owned by the player
if entity.initial_controller != self:
continue
# Exclude entities that aren't initially in the deck
if entity.initial_zone != Zone.DECK:
continue
# Exclude entity types that cannot be in the deck
if not entity.can_be_in_deck:
continue
# Allow CREATOR=1 because of monster hunt decks.
# Everything else is likely a false positive.
if entity.initial_creator > 1:
continue
yield entity