From c782c3a79baf40203e293a1718f85b453f089ee7 Mon Sep 17 00:00:00 2001 From: Trysdyn Black Date: Wed, 9 Oct 2024 15:47:15 -0700 Subject: [PATCH] Make character command data consistent typing Previously a character's command data could be a list or a dict depending on if command randomization was on. This resulted in having to inspect the object and determine its type in tools using the output. This change guarantees character command data will be a dict of name->description. The description is pulled from the COMMANDS section of the spoiler log. If it doesn't exist (or the command is not present, which should never happen), the description will simply be the command name again. --- main.py | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/main.py b/main.py index 18891a3..7d0774d 100644 --- a/main.py +++ b/main.py @@ -2,7 +2,7 @@ """Parse BCEX (or BCCE) logs into json objects.""" -__version__ = "0.3" +__version__ = "0.4.0" __author__ = "Trysdyn Black" import json @@ -126,9 +126,14 @@ def parse_CHARACTERS(data: str) -> dict[str, dict]: # noqa: C901 spell_level, spell_name = line.split("-", 1) info["spells"][spell_name.strip()] = int(spell_level.strip().split(" ")[1]) - # Special k=v strings with comma-delimited lists + # Command list + # Commands: is just a CSV list of things like "fight" "magic" etc. However if command + # randomization is on, these will be exotic things like "KitMerton" that need additional + # info provided from the COMMANDS section later. + # As such, we begin with a dehydrated hash of command_name=None, then the COMMANDS section + # will provide hydration data for the values. elif line.startswith("Commands:"): - info["commands"] = [command.strip() for command in line.split(":")[1].split(",")] + info["commands"] = {command.strip(): None for command in line.split(":")[1].split(",")} elif line.startswith("Notable"): info["equipment"] = [eq.strip() for eq in line.split(":")[1].split(",")] @@ -288,17 +293,13 @@ if __name__ == "__main__": except KeyError: continue - # Subkey CHARACTERS commands with COMMANDS data - # This turns lists of commands each character has into hashes where - # Command name => Textual desc of command - # Certain flags don't shuffle commands like this so we have to check - if "COMMANDS" in data: - for c_data in data["CHARACTERS"].values(): - new_commands = {} - - for command in c_data["commands"]: - new_commands[command] = data["COMMANDS"].get(command, command) - c_data["commands"] = new_commands + # Hydrate each character's command list with descriptions of the commands + # This uses the COMMANDS section, but if one doesn't exist just repeat the command + # name because it should be simple things like "fight" and "magic" + command_info = data.get("COMMANDS", {}) + for c_data in data["CHARACTERS"].values(): + for command in c_data["commands"]: + c_data["commands"][command] = command_info.get(command, command) # If we have a STATS block, snap it into CHARACTER data # BCCE broke this out into its own section