Skip to content

Commit

Permalink
update with code from hsahovic#660
Browse files Browse the repository at this point in the history
  • Loading branch information
cameronangliss committed Jan 3, 2025
1 parent 2dd59bf commit ca04cc2
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 33 deletions.
76 changes: 67 additions & 9 deletions src/poke_env/environment/abstract_battle.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
from poke_env.data import GenData, to_id_str
from poke_env.data.replay_template import REPLAY_TEMPLATE
from poke_env.environment.field import Field
from poke_env.environment.move import Move
from poke_env.environment.observation import Observation
from poke_env.environment.observed_pokemon import ObservedPokemon
from poke_env.environment.pokemon import Pokemon
from poke_env.environment.pokemon_gender import PokemonGender
from poke_env.environment.pokemon_type import PokemonType
from poke_env.environment.side_condition import STACKABLE_CONDITIONS, SideCondition
from poke_env.environment.weather import Weather

Expand Down Expand Up @@ -215,19 +218,34 @@ def get_pokemon(
if identifier[3] != " ":
identifier = identifier[:2] + identifier[3:]

if identifier in self._team:
return self._team[identifier]
elif identifier in self._opponent_team:
return self._opponent_team[identifier]

player_role = identifier[:2]
name = identifier[3:].strip()
is_mine = player_role == self._player_role
team = self._team if is_mine or force_self_team else self._opponent_team

# if the pokemon has a nickname, this ensures we recognize it
matches = [
i
for i, p in enumerate(team.values())
if p.base_species in to_id_str(details.split(",")[0])
and not (
p.base_species == "mew" and to_id_str(details.split(",")[0]) == "mewtwo"
)
]
assert len(matches) < 2
if identifier not in team and matches:
i = matches[0]
items = list(team.items())
items[i] = (identifier, items[i][1])
items[i][1]._name = identifier[4:]
if is_mine or force_self_team:
self._team = dict(items)
else:
self._opponent_team = dict(items)
team = self._team if is_mine or force_self_team else self._opponent_team

if is_mine or force_self_team:
team: Dict[str, Pokemon] = self._team
else:
team: Dict[str, Pokemon] = self._opponent_team # type: ignore
if identifier in team:
return team[identifier]

if self._team_size and len(team) >= self._team_size[player_role]:
raise ValueError(
Expand Down Expand Up @@ -958,6 +976,46 @@ def _update_team_from_request(self, side: Dict[str, Any]):
pokemon["ident"], force_self_team=True, request=pokemon
)

def _update_team_from_open_sheets(
self, message_dict: Dict[str, List[str]], role: str
):
teampreview_team = (
self.teampreview_team
if role == self.player_role
else self.teampreview_opponent_team
)
team = self._team if role == self.player_role else self._opponent_team
for mon in teampreview_team:
identifier = f"{role}: {mon.base_species.capitalize()}"
if identifier not in team:
team[identifier] = Pokemon(
mon._data.gen,
species=mon.species,
name=mon._data.pokedex[mon.species]["name"],
details=mon._last_details,
)
pokemon = team[identifier]
pokemon_msg = [
msg
for name, msg in message_dict.items()
if mon.base_species in to_id_str(name)
][0]
pokemon._item = to_id_str(pokemon_msg[1])
pokemon._ability = to_id_str(pokemon_msg[2])
pokemon._moves = {
to_id_str(name): Move(to_id_str(name), self.gen)
for name in pokemon_msg[3].split(",")
}
pokemon._gender = (
PokemonGender.from_request_details(pokemon_msg[6])
if pokemon_msg[6]
else PokemonGender.NEUTRAL
)
pokemon._level = int(pokemon_msg[9])
pokemon._terastallized_type = PokemonType.from_name(
pokemon_msg[10].split(",")[-1]
)

def won_by(self, player_name: str):
if player_name == self._player_username:
self._won = True
Expand Down
2 changes: 0 additions & 2 deletions src/poke_env/environment/pokemon_gender.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,4 @@ def from_request_details(gender: str) -> PokemonGender:
return PokemonGender.MALE
elif gender == "F":
return PokemonGender.FEMALE
elif gender == "N":
return PokemonGender.NEUTRAL
raise ShowdownException("Unmanaged request gender: '%s'", gender)
39 changes: 17 additions & 22 deletions src/poke_env/player/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,33 +304,28 @@ async def _handle_battle_message(self, split_messages: List[List[str]]):
will_move = True
battle.move_on_next_request = False
elif split_message[1] == "showteam":
# only need open sheets data for opponent
if split_message[2] == battle.opponent_role:
split_pokemon_messages = [
m.split("|") for m in "|".join(split_message[3:]).split("]")
]
message_dict = {m[0]: m[1:] for m in split_pokemon_messages}
for mon in battle.teampreview_opponent_team:
identifier = (
f"{battle.opponent_role}: {mon.base_species.capitalize()}"
)
if identifier not in battle._opponent_team:
battle._opponent_team[identifier] = Pokemon(
mon._data.gen, species=mon.species
)
pokemon = battle._opponent_team[identifier]
pokemon._item = to_id_str(message_dict[pokemon.name][1])
pokemon._ability = to_id_str(message_dict[pokemon.name][2])
pokemon._moves = {
to_id_str(name): Move(to_id_str(name), battle.gen)
for name in message_dict[pokemon.name][3].split(",")
}
pokemon._gender = PokemonGender.from_request_details(
message_dict[pokemon.name][6] or "N"
)
pokemon._level = int(message_dict[pokemon.name][9])
pokemon._terastallized_type = PokemonType.from_name(
message_dict[pokemon.name][10].split(",")[-1]
)
role = split_message[2]
battle._update_team_from_open_sheets(message_dict, role)
# only handle battle request after all open sheets are processed
if (
battle.team
and battle.opponent_team
and all(
[
p.moves
for p in list(battle.team.values())
+ list(battle.opponent_team.values())
]
)
):
will_move = True
from_teampreview_request = True
elif split_message[1] == "win" or split_message[1] == "tie":
if split_message[1] == "win":
battle.won_by(split_message[2])
Expand Down

0 comments on commit ca04cc2

Please sign in to comment.