Skip to content

Commit

Permalink
Merge pull request #13 from hsahovic/fix-move-storing-bug
Browse files Browse the repository at this point in the history
Fix move storing bug
  • Loading branch information
hsahovic authored Feb 6, 2020
2 parents d9ecd08 + 34042ae commit 58fe606
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 31 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
EMAIL = "contact@sahovic.fr"
AUTHOR = "Haris Sahovic"
REQUIRES_PYTHON = ">=3.6.0"
VERSION = "0.0.3"
VERSION = "0.0.4"

# What packages are required for this module to be executed?
with open("requirements.txt") as requirements:
Expand Down
12 changes: 8 additions & 4 deletions src/poke_env/environment/move.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,13 @@ def use(self) -> None:
self._current_pp -= 1

@staticmethod
def should_be_stored(move_id) -> bool:
if move_id not in special_moves:
return True
else:
def should_be_stored(move_id: str) -> bool:
if move_id in special_moves:
return False
move = Move(move_id)
if move.is_z:
return False
return True

@property
def accuracy(self) -> float:
Expand Down Expand Up @@ -270,6 +272,8 @@ def is_z(self) -> bool:
:return: Whether the move is a z move.
:rtype: bool
"""
if self.id.startswith("z") and self.id[1:] in MOVES:
return True
return "isZ" in self.entry

@property
Expand Down
33 changes: 21 additions & 12 deletions src/poke_env/environment/pokemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,18 @@ def __str__(self) -> str:
return f"{self._species} (pokemon object) "
"[Active: {self._active}, Status: {self._status}]"

def _add_move(self, move_id: str, use: bool = False) -> None:
"""Store the move if applicable."""
id_ = Move.retrieve_id(move_id)
if Move.should_be_stored(id_):
move = Move(id_)
if move.id not in self._moves:
if len(self._moves) >= 4:
self._moves = {}
self._moves[move.id] = Move(id_)
if use:
self.moves[move.id].use()

def _boost(self, stat, amount):
self._boosts[stat] += int(amount)
if self._boosts[stat] > 6:
Expand Down Expand Up @@ -137,14 +149,7 @@ def _mega_evolve(self, stone):
def _moved(self, move):
self._must_recharge = False
self._preparing = False

id_ = Move.retrieve_id(move)

if Move.should_be_stored(id_):
if id_ not in self._moves:
self._moves[id_] = Move(id_)

self.moves[id_].use()
self._add_move(move, use=True)

def _prepare(self, move, target):
self._preparing = (move, target)
Expand Down Expand Up @@ -264,11 +269,15 @@ def _update_from_request(self, request_pokemon: Dict[str, Any]) -> None:
self._gender = gender
self._level = int(level)

# This might cause some unnecessary resets with special moves, such as
# hiddenpower
all_moves = set(self._moves.keys()).union(request_pokemon["moves"])
if len(all_moves) > 4:
for move in list(self._moves):
if move not in request_pokemon["moves"]:
self._moves.pop(move)
for move in request_pokemon["moves"]:
move_id = Move.retrieve_id(move)
if move_id not in self._moves:
self._moves[move_id] = Move(move_id=move_id)

self._add_move(move)
ident = request_pokemon["ident"].split(": ")

if len(ident) == 2:
Expand Down
24 changes: 14 additions & 10 deletions src/poke_env/player/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,17 @@ async def accept_challenges(
break
await self._battle_count_queue.join()

@abstractmethod
def choose_move(self, battle: Battle) -> str:
"""Abstract async method to choose a move in a battle.
:param battle: The battle.
:type battle: Battle
:return: The move order.
:rtype: str
"""
pass

def choose_random_move(self, battle: Battle) -> str:
"""Returns a random legal move from battle.
Expand Down Expand Up @@ -369,16 +380,9 @@ def create_order(
" (should be a Pokemon or Move object)" % order
)

@abstractmethod
def choose_move(self, battle: Battle) -> str:
"""Abstract method to choose a move in a battle.
:param battle: The battle.
:type battle: Battle
:return: The move order.
:rtype: str
"""
pass
@property
def battles(self) -> Dict[str, Battle]:
return self._battles

@property
def n_finished_battles(self) -> int:
Expand Down
8 changes: 4 additions & 4 deletions src/poke_env/player/player_network_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ async def _handle_message(self, message: str) -> None:
else:
self.logger.critical("Unhandled message: %s", message)
raise NotImplementedError("Unhandled message: %s" % message)
except CancelledError:
pass
except CancelledError as e:
self.logger.critical("CancelledError intercepted. %s", e)

async def _log_in(self, split_message: List[str]) -> None:
"""Log the player with specified username and password.
Expand Down Expand Up @@ -220,8 +220,8 @@ async def listen(self) -> None:
self.logger.warning(
"Websocket connection with %s closed", self.websocket_url
)
except (CancelledError, RuntimeError):
pass
except (CancelledError, RuntimeError) as e:
self.logger.critical("Listen interrupted by %s", e)
finally:
for coroutine in coroutines:
coroutine.cancel()
Expand Down

0 comments on commit 58fe606

Please sign in to comment.