track indirect knockouts

This commit is contained in:
xeals 2023-03-27 23:07:38 +11:00
parent 693029fd21
commit b162f63ab2
Signed by: xeals
GPG Key ID: A498C7AF27EC6B5C

63
main.py
View File

@ -70,6 +70,10 @@ def _init_db(conn: sqlite3.Connection):
game, turn, player, name, game, turn, player, name,
UNIQUE(game, turn, player) UNIQUE(game, turn, player)
); );
CREATE TABLE IF NOT EXISTS indirect_knockouts(
game, turn, player, name, source, source_user,
UNIQUE(game, turn, player)
);
CREATE TABLE IF NOT EXISTS games( CREATE TABLE IF NOT EXISTS games(
id, p1, p2, format, uploadtime, id, p1, p2, format, uploadtime,
UNIQUE(id) UNIQUE(id)
@ -84,6 +88,17 @@ def parse_log(game: str, log: str, into: sqlite3.Connection):
turn = 0 turn = 0
players = {} players = {}
# ("p2a: Edward", "p1a: Meteo")
# memorises the user of the move that causes environment setting or status,
# and its target
last_move: t.Optional[tuple[str, str]]
# ("p1", "Spikes") => "p2a: Frosslas"
last_env_set: dict[tuple[str, str], str] = {}
# ("p1a: Meteo", "brn") => "p2a: Edward"
last_status_set: dict[tuple[str, str], str] = {}
def resolve_mon(user: str) -> tuple[str, str]: def resolve_mon(user: str) -> tuple[str, str]:
[player, name] = user.split(": ") [player, name] = user.split(": ")
return players[player.strip("ab")], name return players[player.strip("ab")], name
@ -99,6 +114,7 @@ def parse_log(game: str, log: str, into: sqlite3.Connection):
case ["turn", turn]: case ["turn", turn]:
turn = int(turn) turn = int(turn)
case ["move", user, move, target]: case ["move", user, move, target]:
last_move = (user, target)
player, user = resolve_mon(user) player, user = resolve_mon(user)
_, target = resolve_mon(target) _, target = resolve_mon(target)
conn.execute( conn.execute(
@ -137,6 +153,53 @@ def parse_log(game: str, log: str, into: sqlite3.Connection):
""", """,
(game, turn, player, mon), (game, turn, player, mon),
) )
case ["-sidestart", side, env]:
if not last_move:
LOG.warning(f"missing previous move for {line}")
continue
LOG.debug(f"{line} <- {last_move}")
last_env_set[(side[0:1], env.replace("move: ", ""))] = last_move[0]
case ["-status", mon, cond]:
if not last_move or last_move[1] != mon:
LOG.warning(f"missing previous move for {line}")
continue
LOG.debug(f"{line} <- {last_move}")
last_status_set[(mon, cond)] = last_move[0]
case ["-damage", mon, *rest]:
# rest is new_hp and sometimes a source (if not from a move)
# in a knockout, new_hp is "0 fnt"
if rest[0] == "0 fnt" and len(rest) > 1:
LOG.debug(f"tracing source for {line}")
source = rest[1].replace("[from] ", "")
source_user = None
if source == "Recoil" or source.startswith("item: "):
source_user = source.replace("item: ", "")
if source_user:
LOG.debug(f"identified special source {source_user}")
else:
source_user = last_env_set.get((mon[0:1], source))
if source_user:
LOG.debug(f"identified hazard source {source_user}")
else:
source_user = last_status_set.get((mon, source))
if source_user:
LOG.debug(f"identified move source {source_user}")
else:
LOG.error(f"missing source for {line}")
continue
conn.execute(
"""
INSERT INTO indirect_knockouts(game, turn, player, name, source, source_user)
VALUES(?, ?, ?, ?, ?, ?)
ON CONFLICT DO NOTHING
""",
(game, turn, player, mon, source, source_user),
)
case _: case _:
# LOG.debug(f"unhandled message {chunks[0]}") # LOG.debug(f"unhandled message {chunks[0]}")
pass pass