Compare commits
No commits in common. "262a0513569e251937751e962375460a9dd68136" and "641ddef2095d3d454b168e2a53036b598d2104d0" have entirely different histories.
262a051356
...
641ddef209
131
main.py
131
main.py
@ -7,7 +7,6 @@ from pathlib import Path
|
|||||||
import argparse
|
import argparse
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import re
|
|
||||||
import requests
|
import requests
|
||||||
import sqlite3
|
import sqlite3
|
||||||
import sys
|
import sys
|
||||||
@ -54,7 +53,7 @@ def team(player: str) -> str:
|
|||||||
if player in TEAMS:
|
if player in TEAMS:
|
||||||
return TEAMS[player]
|
return TEAMS[player]
|
||||||
else:
|
else:
|
||||||
if not player in _logged_teams and player:
|
if not player in _logged_teams:
|
||||||
LOG.warning(f"missing team mapping for {player}")
|
LOG.warning(f"missing team mapping for {player}")
|
||||||
_logged_teams.append(player)
|
_logged_teams.append(player)
|
||||||
return player
|
return player
|
||||||
@ -101,12 +100,7 @@ def _init_db(conn: sqlite3.Connection):
|
|||||||
CREATE TABLE IF NOT EXISTS games(
|
CREATE TABLE IF NOT EXISTS games(
|
||||||
id, p1, p2, format, uploadtime, winner,
|
id, p1, p2, format, uploadtime, winner,
|
||||||
UNIQUE(id)
|
UNIQUE(id)
|
||||||
);
|
)
|
||||||
-- No good way to ensure idempotence for damage; just re-build it.
|
|
||||||
DROP TABLE IF EXISTS damage;
|
|
||||||
CREATE TABLE damage(game, player, name, value);
|
|
||||||
DROP TABLE IF EXISTS indirect_damage;
|
|
||||||
CREATE TABLE indirect_damage(game, player, name, value);
|
|
||||||
"""
|
"""
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -116,7 +110,6 @@ def parse_log(game: str, log: str, into: sqlite3.Connection):
|
|||||||
|
|
||||||
turn = 0
|
turn = 0
|
||||||
players = {}
|
players = {}
|
||||||
hp = {}
|
|
||||||
|
|
||||||
# ("p2a: Edward", "p1a: Meteo")
|
# ("p2a: Edward", "p1a: Meteo")
|
||||||
# memorises the user of the move that causes environment setting or status,
|
# memorises the user of the move that causes environment setting or status,
|
||||||
@ -143,10 +136,8 @@ def parse_log(game: str, log: str, into: sqlite3.Connection):
|
|||||||
match chunks:
|
match chunks:
|
||||||
case ["player", id, username, *rest]:
|
case ["player", id, username, *rest]:
|
||||||
players[id] = username
|
players[id] = username
|
||||||
|
|
||||||
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)
|
last_move = (user, target)
|
||||||
player, user = resolve_mon(user)
|
player, user = resolve_mon(user)
|
||||||
@ -159,13 +150,7 @@ def parse_log(game: str, log: str, into: sqlite3.Connection):
|
|||||||
""",
|
""",
|
||||||
(game, turn, team(player), move, user, target),
|
(game, turn, team(player), move, user, target),
|
||||||
)
|
)
|
||||||
|
case ["switch", name, specie, *rest]:
|
||||||
case ["drag", name, specie, status, *rest]:
|
|
||||||
hp[name] = int(status.split("/")[0])
|
|
||||||
|
|
||||||
case ["switch", name, specie, status, *rest]:
|
|
||||||
hp[name] = int(status.split("/")[0])
|
|
||||||
|
|
||||||
player, name = resolve_mon(name)
|
player, name = resolve_mon(name)
|
||||||
conn.execute(
|
conn.execute(
|
||||||
"""
|
"""
|
||||||
@ -183,7 +168,6 @@ def parse_log(game: str, log: str, into: sqlite3.Connection):
|
|||||||
""",
|
""",
|
||||||
(game, team(player), name, specie.split(", ")[0]),
|
(game, team(player), name, specie.split(", ")[0]),
|
||||||
)
|
)
|
||||||
|
|
||||||
case ["faint", mon]:
|
case ["faint", mon]:
|
||||||
player, mon = resolve_mon(mon)
|
player, mon = resolve_mon(mon)
|
||||||
conn.execute(
|
conn.execute(
|
||||||
@ -194,7 +178,6 @@ def parse_log(game: str, log: str, into: sqlite3.Connection):
|
|||||||
""",
|
""",
|
||||||
(game, turn, team(player), mon),
|
(game, turn, team(player), mon),
|
||||||
)
|
)
|
||||||
|
|
||||||
case ["win", player]:
|
case ["win", player]:
|
||||||
conn.execute(
|
conn.execute(
|
||||||
"""
|
"""
|
||||||
@ -204,97 +187,51 @@ def parse_log(game: str, log: str, into: sqlite3.Connection):
|
|||||||
""",
|
""",
|
||||||
(team(player), game),
|
(team(player), game),
|
||||||
)
|
)
|
||||||
|
|
||||||
case ["-sidestart", side, env]:
|
case ["-sidestart", side, env]:
|
||||||
if not last_move:
|
if not last_move:
|
||||||
LOG.warning(f"missing previous move for {line}")
|
LOG.warning(f"missing previous move for {line}")
|
||||||
continue
|
continue
|
||||||
LOG.debug(f"{line} <- {last_move}")
|
LOG.debug(f"{line} <- {last_move}")
|
||||||
last_env_set[(side[0:1], env.replace("move: ", ""))] = last_move[0]
|
last_env_set[(side[0:1], env.replace("move: ", ""))] = last_move[0]
|
||||||
|
|
||||||
case ["-status", mon, cond]:
|
case ["-status", mon, cond]:
|
||||||
if not last_move or last_move[1] != mon:
|
if not last_move or last_move[1] != mon:
|
||||||
LOG.warning(f"missing previous move for {line}")
|
LOG.warning(f"missing previous move for {line}")
|
||||||
continue
|
continue
|
||||||
LOG.debug(f"{line} <- {last_move}")
|
LOG.debug(f"{line} <- {last_move}")
|
||||||
last_status_set[(mon, cond)] = last_move[0]
|
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
|
||||||
|
|
||||||
case ["-damage", mon, status]:
|
test_hazard = last_env_set.get((mon[0:1], source))
|
||||||
# mon takes direct (non-hazard/condition) damage
|
if test_hazard:
|
||||||
# status can be a percentage 70/100 with or without condition,
|
source_user = test_hazard
|
||||||
# or "0 fnt"
|
LOG.debug(f"identified hazard source {source_user}")
|
||||||
new_hp = int(re.split("[/ ]", status)[0])
|
|
||||||
LOG.debug(f"{mon} dropped to {new_hp} from {hp[mon]}")
|
|
||||||
LOG.debug(f"source: {last_move}")
|
|
||||||
|
|
||||||
# resolve to damage source
|
test_status = last_status_set.get((mon, source))
|
||||||
if last_move[1] != mon:
|
if test_status:
|
||||||
LOG.warn(
|
source_user = test_status
|
||||||
f"{mon} took direct damage but last move was not targeted at them"
|
LOG.debug(f"identified move source {source_user}")
|
||||||
)
|
|
||||||
continue
|
|
||||||
user = last_move[0]
|
|
||||||
source_player, source_mon = resolve_mon(user)
|
|
||||||
|
|
||||||
conn.execute(
|
if source == "Recoil" or source.startswith("item: "):
|
||||||
"""
|
LOG.debug(f"identified special source {source}")
|
||||||
INSERT INTO damage(game, player, name, value)
|
source = source.replace("item: ", "")
|
||||||
VALUES(?, ?, ?, ?)
|
source_user = "self"
|
||||||
ON CONFLICT DO NOTHING
|
|
||||||
""",
|
|
||||||
(game, team(source_player), source_mon, hp[mon] - new_hp),
|
|
||||||
)
|
|
||||||
|
|
||||||
hp[mon] = new_hp
|
if not source_user:
|
||||||
|
LOG.error(f"missing source for {line}")
|
||||||
|
continue
|
||||||
|
|
||||||
case ["-damage", mon, status, from_]:
|
player, mon = resolve_mon(mon)
|
||||||
# mon takes indirect damage
|
if source_user.startswith("p1") or source_user.startswith("p2"):
|
||||||
# status can be a percentage 70/100 with or without condition,
|
source_player, source_user = resolve_mon(source_user)
|
||||||
# or "0 fnt"
|
else:
|
||||||
# mon has fainted from an indirect damage source
|
source_player = None
|
||||||
#
|
|
||||||
new_hp = int(re.split("[/ ]", status)[0])
|
|
||||||
LOG.debug(f"{mon} dropped to {new_hp} from {from_}")
|
|
||||||
|
|
||||||
LOG.debug(f"tracing source for {line}")
|
|
||||||
source = from_.replace("[from] ", "")
|
|
||||||
source_user = None
|
|
||||||
|
|
||||||
test_hazard = last_env_set.get((mon[0:1], source))
|
|
||||||
if test_hazard:
|
|
||||||
source_user = test_hazard
|
|
||||||
LOG.debug(f"identified hazard source {source_user}")
|
|
||||||
|
|
||||||
test_status = last_status_set.get((mon, source))
|
|
||||||
if test_status:
|
|
||||||
source_user = test_status
|
|
||||||
LOG.debug(f"identified move source {source_user}")
|
|
||||||
|
|
||||||
if source == "Recoil" or source.startswith("item: "):
|
|
||||||
LOG.debug(f"identified special source {source}")
|
|
||||||
source = source.replace("item: ", "")
|
|
||||||
source_user = "self"
|
|
||||||
|
|
||||||
if not source_user:
|
|
||||||
LOG.error(f"missing source for {line}")
|
|
||||||
continue
|
|
||||||
|
|
||||||
player, pkmn = resolve_mon(mon)
|
|
||||||
if source_user.startswith("p1") or source_user.startswith("p2"):
|
|
||||||
source_player, source_mon = resolve_mon(source_user)
|
|
||||||
else:
|
|
||||||
source_player = None
|
|
||||||
|
|
||||||
if source_player:
|
|
||||||
conn.execute(
|
|
||||||
"""
|
|
||||||
INSERT INTO indirect_damage(game, player, name, value)
|
|
||||||
VALUES(?, ?, ?, ?)
|
|
||||||
""",
|
|
||||||
(game, team(source_player), source_mon, hp[mon] - new_hp),
|
|
||||||
)
|
|
||||||
|
|
||||||
if status == "0 fnt":
|
|
||||||
conn.execute(
|
conn.execute(
|
||||||
"""
|
"""
|
||||||
INSERT INTO indirect_knockouts(game, turn, player, name, source, source_user, source_player)
|
INSERT INTO indirect_knockouts(game, turn, player, name, source, source_user, source_player)
|
||||||
@ -305,16 +242,12 @@ def parse_log(game: str, log: str, into: sqlite3.Connection):
|
|||||||
game,
|
game,
|
||||||
turn,
|
turn,
|
||||||
team(player),
|
team(player),
|
||||||
pkmn,
|
mon,
|
||||||
source,
|
source,
|
||||||
source_mon,
|
source_user,
|
||||||
team(source_player),
|
team(source_player),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
case ["-heal", mon, status, *rest]:
|
|
||||||
hp[mon] = int(status.split("/")[0])
|
|
||||||
|
|
||||||
case _:
|
case _:
|
||||||
# LOG.debug(f"unhandled message {chunks[0]}")
|
# LOG.debug(f"unhandled message {chunks[0]}")
|
||||||
pass
|
pass
|
||||||
|
Loading…
Reference in New Issue
Block a user