Skip to content

Commit

Permalink
process protocol, and THEN request
Browse files Browse the repository at this point in the history
  • Loading branch information
cameronangliss committed Feb 1, 2025
1 parent c80d8a2 commit 0ce7f67
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 152 deletions.
12 changes: 6 additions & 6 deletions src/poke_env/environment/abstract_battle.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,8 +449,8 @@ def parse_message(self, split_message: List[str]):
if event[-1].startswith("[anim]"):
event = event[:-1]

if event[-1].startswith("[from]move: "):
override_move = event.pop()[12:]
if event[-1].startswith("[from] move: "):
override_move = event.pop()[13:]

if override_move == "Sleep Talk":
# Sleep talk was used, but also reveals another move
Expand All @@ -461,7 +461,7 @@ def parse_message(self, split_message: List[str]):
override_move = None
elif self.logger is not None:
self.logger.warning(
"Unmanaged [from]move message received - move %s in cleaned up "
"Unmanaged [from] move message received - move %s in cleaned up "
"message %s in battle %s turn %d",
override_move,
event,
Expand All @@ -472,8 +472,8 @@ def parse_message(self, split_message: List[str]):
if event[-1] == "null":
event = event[:-1]

if event[-1].startswith("[from]ability: "):
revealed_ability = event.pop()[15:]
if event[-1].startswith("[from] ability: "):
revealed_ability = event.pop()[16:]
pokemon = event[2]
self.get_pokemon(pokemon).ability = revealed_ability

Expand All @@ -483,7 +483,7 @@ def parse_message(self, split_message: List[str]):
return
elif self.logger is not None:
self.logger.warning(
"Unmanaged [from]ability: message received - ability %s in "
"Unmanaged [from] ability: message received - ability %s in "
"cleaned up message %s in battle %s turn %d",
revealed_ability,
event,
Expand Down
10 changes: 10 additions & 0 deletions src/poke_env/environment/battle.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,16 @@ def parse_request(self, request: Dict[str, Any]) -> None:
self._teampreview = False
self._update_team_from_request(request["side"])

if self.active_pokemon is not None:
active_mon = self.get_pokemon(
request["side"]["pokemon"][0]["ident"],
force_self_team=True,
details=request["side"]["pokemon"][0]["details"],
)
if active_mon != self.active_pokemon:
self.active_pokemon.switch_out()
active_mon.switch_in()

Check warning on line 105 in src/poke_env/environment/battle.py

View check run for this annotation

Codecov / codecov/patch

src/poke_env/environment/battle.py#L104-L105

Added lines #L104 - L105 were not covered by tests

if "active" in request:
active_request = request["active"][0]

Expand Down
42 changes: 7 additions & 35 deletions src/poke_env/environment/double_battle.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,13 @@ def parse_request(self, request: Dict[str, Any]) -> None:
if side["pokemon"]:
self._player_role = side["pokemon"][0]["ident"][:2]
self._update_team_from_request(side)
if self.player_role is not None:
self._active_pokemon[f"{self.player_role}a"] = self.team[
request["side"]["pokemon"][0]["ident"]
]
self._active_pokemon[f"{self.player_role}b"] = self.team[
request["side"]["pokemon"][1]["ident"]
]

if "active" in request:
for active_pokemon_number, active_request in enumerate(request["active"]):
Expand All @@ -139,41 +146,6 @@ def parse_request(self, request: Dict[str, Any]) -> None:
force_self_team=True,
details=pokemon_dict["details"],
)
if self.player_role is not None:
if (
active_pokemon_number == 0
and f"{self.player_role}a" not in self._active_pokemon
):
self._active_pokemon[f"{self.player_role}a"] = active_pokemon
elif f"{self.player_role}b" not in self._active_pokemon:
self._active_pokemon[f"{self.player_role}b"] = active_pokemon
elif (
active_pokemon_number == 0
and self._active_pokemon[f"{self.player_role}a"].fainted
and self._active_pokemon[f"{self.player_role}b"]
== active_pokemon
):
(
self._active_pokemon[f"{self.player_role}a"],
self._active_pokemon[f"{self.player_role}b"],
) = (
self._active_pokemon[f"{self.player_role}b"],
self._active_pokemon[f"{self.player_role}a"],
)
elif (
active_pokemon_number == 1
and self._active_pokemon[f"{self.player_role}b"].fainted
and not active_pokemon.fainted
and self._active_pokemon[f"{self.player_role}a"]
== active_pokemon
):
(
self._active_pokemon[f"{self.player_role}a"],
self._active_pokemon[f"{self.player_role}b"],
) = (
self._active_pokemon[f"{self.player_role}b"],
self._active_pokemon[f"{self.player_role}a"],
)

if active_pokemon.fainted:
continue
Expand Down
1 change: 1 addition & 0 deletions src/poke_env/environment/pokemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ def set_hp_status(self, hp_status: str, store=False):
self.end_effect("yawn")
else:
hp = hp_status
self._status = None

current_hp, max_hp = "".join([c for c in hp if c in "0123456789/"]).split("/")
self._current_hp = int(current_hp)
Expand Down
Loading

0 comments on commit 0ce7f67

Please sign in to comment.