How to use the hearthstone.hslog.tokens function in hearthstone

To help you get started, we’ve selected a few hearthstone examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github HearthSim / python-hearthstone / hearthstone / hslog / parser.py View on Github external
def parse_entity_id(entity):
	if entity.isdigit():
		return int(entity)

	if entity == tokens.GAME_ENTITY:
		# GameEntity is always 1
		return 1

	sre = tokens.ENTITY_RE.match(entity)
	if sre:
		id = sre.groups()[0]
		return int(id)
github HearthSim / python-hearthstone / hearthstone / hslog / parser.py View on Github external
def process_spectator_mode(self, line):
		if line == tokens.SPECTATOR_MODE_BEGIN_GAME:
			self.set_spectating(True)
		elif line == tokens.SPECTATOR_MODE_BEGIN_FIRST:
			self.set_spectating(True, False)
		elif line == tokens.SPECTATOR_MODE_BEGIN_SECOND:
			self.set_spectating(True, True)
		elif line == tokens.SPECTATOR_MODE_END_MODE:
			self.set_spectating(False, False)
		elif line == tokens.SPECTATOR_MODE_END_GAME:
			self.set_spectating(False, False)
		else:
			raise NotImplementedError("Unhandled spectator mode: %r" % (line))
github HearthSim / python-hearthstone / hearthstone / hslog / parser.py View on Github external
return
		elif opcode in ("ACTION_END", "BLOCK_END"):
			regex, callback = tokens.BLOCK_END_RE, self.block_end
		elif opcode == "FULL_ENTITY":
			if data.startswith("FULL_ENTITY - Updating"):
				regex, callback = tokens.FULL_ENTITY_UPDATE_RE, self.full_entity_update
			else:
				regex, callback = tokens.FULL_ENTITY_CREATE_RE, self.full_entity
		elif opcode == "SHOW_ENTITY":
			regex, callback = tokens.SHOW_ENTITY_RE, self.show_entity
		elif opcode == "HIDE_ENTITY":
			regex, callback = tokens.HIDE_ENTITY_RE, self.hide_entity
		elif opcode == "CHANGE_ENTITY":
			regex, callback = tokens.CHANGE_ENTITY_RE, self.change_entity
		elif opcode == "TAG_CHANGE":
			regex, callback = tokens.TAG_CHANGE_RE, self.tag_change
		elif opcode == "META_DATA":
			regex, callback = tokens.META_DATA_RE, self.meta_data
		else:
			raise NotImplementedError(data)

		sre = regex.match(data)
		if not sre:
			logging.warning("Could not correctly parse %r", data)
			return
		return callback(ts, *sre.groups())
github HearthSim / python-hearthstone / hearthstone / hslog / parser.py View on Github external
def handle_entities_chosen(self, ts, data):
		if data.startswith("id="):
			sre = tokens.ENTITIES_CHOSEN_RE.match(data)
			if not sre:
				raise RegexParsingError(data)
			id, player, count = sre.groups()
			id = int(id)
			player = self.parse_entity_or_player(player)
			self._chosen_packet_count = int(count)
			self._chosen_packet = packets.ChosenEntities(ts, player, id)
			self.current_block.packets.append(self._chosen_packet)
			return self._chosen_packet
		elif data.startswith("Entities["):
			sre = tokens.ENTITIES_CHOSEN_ENTITIES_RE.match(data)
			if not sre:
				raise RegexParsingError(data)
			idx, entity = sre.groups()
			id = self.parse_entity_or_player(entity)
			if not id:
				raise ParsingError("Missing entity chosen %r (%r)" % (id, entity))
			if not self._chosen_packet:
				raise ParsingError("Entity Chosen outside of choice packet: %r" % (data))
			self._chosen_packet.choices.append(id)
			if len(self._chosen_packet.choices) > self._chosen_packet_count:
				raise ParsingError("Too many choices (expected %r)" % (self._chosen_packet_count))
			return id
		raise NotImplementedError("Unhandled entities chosen: %r" % (data))
github HearthSim / python-hearthstone / hearthstone / hslog / parser.py View on Github external
def read_line(self, line):
		sre = tokens.TIMESTAMP_RE.match(line)
		if not sre:
			raise RegexParsingError(line)
		level, ts, line = sre.groups()
		if line.startswith(tokens.SPECTATOR_MODE_TOKEN):
			line = line.replace(tokens.SPECTATOR_MODE_TOKEN, "").strip()
			return self.process_spectator_mode(line)

		sre = self.line_regex.match(line)
		if not sre:
			return
		method, msg = sre.groups()
		msg = msg.strip()
		if not self.current_block and "CREATE_GAME" not in msg:
			# Ignore messages before the first CREATE_GAME packet
			return
github HearthSim / python-hearthstone / hearthstone / hslog / parser.py View on Github external
def handle_entity_choices_old(self, ts, data):
		if data.startswith("id="):
			sre = tokens.CHOICES_CHOICE_OLD_1_RE.match(data)
			if sre:
				self.register_choices_old_1(ts, *sre.groups())
			else:
				sre = tokens.CHOICES_CHOICE_OLD_2_RE.match(data)
				if not sre:
					raise RegexParsingError(data)
				self.register_choices_old_2(ts, *sre.groups())
		else:
			return self.handle_entity_choices(ts, data)
github HearthSim / python-hearthstone / hearthstone / hslog / parser.py View on Github external
def handle_options(self, ts, data):
		if data.startswith("id="):
			sre = tokens.OPTIONS_ENTITY_RE.match(data)
			if not sre:
				raise RegexParsingError(data)
			id, = sre.groups()
			id = int(id)
			self._options_packet = packets.Options(ts, id)
			self.current_block.packets.append(self._options_packet)
		elif data.startswith("option "):
			return self._parse_option_packet(ts, data)
		elif data.startswith(("subOption ", "target ")):
			return self._parse_suboption_packet(ts, data)
github HearthSim / python-hearthstone / hearthstone / hslog / parser.py View on Github external
def _parse_option_packet(self, ts, data):
		if " errorParam=" in data:
			sre = tokens.OPTIONS_OPTION_ERROR_RE.match(data)
			optype, id, type, entity, error, error_param = sre.groups()
			if not sre:
				raise RegexParsingError(data)
			error, error_param = clean_option_errors(error, error_param)
		else:
			sre = tokens.OPTIONS_OPTION_RE.match(data)
			if not sre:
				raise RegexParsingError(data)
			optype, id, type, entity = sre.groups()
			error, error_param = None, None

		id = int(id)
		type = parse_enum(enums.OptionType, type)
		if entity:
			entity = self.parse_entity_or_player(entity)
		self._option_packet = packets.Option(ts, entity, id, type, optype, error, error_param)
		if not self._options_packet:
			raise ParsingError("Option without a parent option group: %r" % (data))

		self._options_packet.options.append(self._option_packet)
		self._suboption_packet = None
github HearthSim / python-hearthstone / hearthstone / hslog / parser.py View on Github external
index = None
			self.block_start(ts, entity, type, index, effectid, effectindex, target)
			return
		elif opcode in ("ACTION_END", "BLOCK_END"):
			regex, callback = tokens.BLOCK_END_RE, self.block_end
		elif opcode == "FULL_ENTITY":
			if data.startswith("FULL_ENTITY - Updating"):
				regex, callback = tokens.FULL_ENTITY_UPDATE_RE, self.full_entity_update
			else:
				regex, callback = tokens.FULL_ENTITY_CREATE_RE, self.full_entity
		elif opcode == "SHOW_ENTITY":
			regex, callback = tokens.SHOW_ENTITY_RE, self.show_entity
		elif opcode == "HIDE_ENTITY":
			regex, callback = tokens.HIDE_ENTITY_RE, self.hide_entity
		elif opcode == "CHANGE_ENTITY":
			regex, callback = tokens.CHANGE_ENTITY_RE, self.change_entity
		elif opcode == "TAG_CHANGE":
			regex, callback = tokens.TAG_CHANGE_RE, self.tag_change
		elif opcode == "META_DATA":
			regex, callback = tokens.META_DATA_RE, self.meta_data
		else:
			raise NotImplementedError(data)

		sre = regex.match(data)
		if not sre:
			logging.warning("Could not correctly parse %r", data)
			return
		return callback(ts, *sre.groups())
github HearthSim / python-hearthstone / hearthstone / hslog / parser.py View on Github external
else:
				type, entity, effectid, effectindex, target = sre.groups()
				index = None
			self.block_start(ts, entity, type, index, effectid, effectindex, target)
			return
		elif opcode in ("ACTION_END", "BLOCK_END"):
			regex, callback = tokens.BLOCK_END_RE, self.block_end
		elif opcode == "FULL_ENTITY":
			if data.startswith("FULL_ENTITY - Updating"):
				regex, callback = tokens.FULL_ENTITY_UPDATE_RE, self.full_entity_update
			else:
				regex, callback = tokens.FULL_ENTITY_CREATE_RE, self.full_entity
		elif opcode == "SHOW_ENTITY":
			regex, callback = tokens.SHOW_ENTITY_RE, self.show_entity
		elif opcode == "HIDE_ENTITY":
			regex, callback = tokens.HIDE_ENTITY_RE, self.hide_entity
		elif opcode == "CHANGE_ENTITY":
			regex, callback = tokens.CHANGE_ENTITY_RE, self.change_entity
		elif opcode == "TAG_CHANGE":
			regex, callback = tokens.TAG_CHANGE_RE, self.tag_change
		elif opcode == "META_DATA":
			regex, callback = tokens.META_DATA_RE, self.meta_data
		else:
			raise NotImplementedError(data)

		sre = regex.match(data)
		if not sre:
			logging.warning("Could not correctly parse %r", data)
			return
		return callback(ts, *sre.groups())