add types to move tracking

This commit is contained in:
xeals 2023-04-04 22:39:02 +10:00
parent e8149e7c3b
commit 87114b1d1d
Signed by: xeals
GPG Key ID: A498C7AF27EC6B5C

41
main.py
View File

@ -108,6 +108,10 @@ PokemonSpecie = t.NewType("PokemonSpecie", str)
TaggedPokemon = t.NewType("TaggedPokemon", str)
def tag(tagged: TaggedPlayer | TaggedPokemon) -> PlayerTag:
return PlayerTag(tagged[0:1])
TEAMS: dict[Player, Player] = {}
_logged_teams: list[Player] = []
@ -129,16 +133,15 @@ class LogParser:
players: dict[PlayerTag, Player] = {}
hp: dict[TaggedPokemon, int] = {}
# ("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]]
# Memorises the user of the move that causes environment setting or status,
# its target, and the move name (for debugging).
last_move: t.Optional[tuple[TaggedPokemon, TaggedPokemon, str]] = None
# ("p1", "Spikes") => "p2a: Frosslas"
last_env_set: dict[tuple[str, str], str] = {}
# Memorises the last hazard set against a player and the causing user.
last_env_set: dict[tuple[PlayerTag, str], TaggedPokemon] = {}
# ("p1a: Meteo", "brn") => "p2a: Edward"
last_status_set: dict[tuple[str, str], str] = {}
# Memorises statuses set on a pokemon and the causing user.
last_status_set: dict[tuple[TaggedPokemon, str], TaggedPokemon] = {}
def __init__(self, game: str, into: sqlite3.Connection):
self.game = game
@ -236,7 +239,7 @@ class LogParser:
user = TaggedPokemon(user_)
target = TaggedPokemon(target_)
last_move = (user, target)
last_move = (user, target, move)
player, _ = self.split_pokemon(user)
self.conn.execute(
"""
@ -310,8 +313,8 @@ class LogParser:
# resolve to damage source
if last_move[1] != pokemon:
LOG.warning(
f"{pokemon} took direct damage but last move was not"
" targeted at them"
f"{pokemon} took direct damage but last move"
f" {last_move[2]} was not targeted at them"
)
continue
damage_source = last_move[0]
@ -348,7 +351,7 @@ class LogParser:
source: TaggedPokemon | str | None = None
source_is_pokemon = True
test_hazard = self.last_env_set.get((pokemon[0:1], reason))
test_hazard = self.last_env_set.get((tag(pokemon), reason))
if test_hazard:
source = test_hazard
LOG.debug(f"identified hazard source {source}")
@ -412,24 +415,28 @@ class LogParser:
)
# t.Literal, TaggedPlayer, str
case ["-sidestart", side, env]:
case ["-sidestart", side_, env]:
side = TaggedPlayer(side_)
if not last_move:
LOG.warning(f"missing previous move for {line}")
continue
LOG.debug(f"{line} <- {last_move}")
self.last_env_set[
(side[0:1], env.replace("move: ", ""))
(tag(side), env.replace("move: ", ""))
] = last_move[0]
# t.Literal, TaggedPokemon, str
case ["-status", mon, cond]:
if not last_move or last_move[1] != mon:
case ["-status", pokemon_, cond]:
pokemon = TaggedPokemon(pokemon_)
if not last_move or last_move[1] != pokemon:
LOG.warning(f"missing previous move for {line}")
continue
LOG.debug(f"{line} <- {last_move}")
self.last_status_set[(mon, cond)] = last_move[0]
self.last_status_set[(pokemon, cond)] = last_move[0]
case _:
# LOG.debug(f"unhandled message {chunks[0]}")