Skip to content

Commit

Permalink
Refactor ps client (#405)
Browse files Browse the repository at this point in the history
* Factor PSClient out of player, server_configuration and player_configuration

* Group concurrency control and loop logic in poke_env.concurrency

* Explicitely set team in ps client methods that can start battles

* Refactor unit tests

* Fix pyre issues
  • Loading branch information
hsahovic committed Aug 13, 2023
1 parent 553d352 commit 75b27ff
Show file tree
Hide file tree
Showing 42 changed files with 650 additions and 601 deletions.
10 changes: 5 additions & 5 deletions diagnostic_tools/anything_goes_gen_7_random_battles_and_teams.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

from poke_env import (
POKEDEX,
AccountConfiguration,
LocalhostServerConfiguration,
PlayerConfiguration,
to_id_str,
)
from poke_env.player import RandomPlayer
Expand Down Expand Up @@ -89,19 +89,19 @@ def yield_team(self):


async def main():
player_1_configuration = PlayerConfiguration("Player 1", None)
player_2_configuration = PlayerConfiguration("Player 2", None)
player_1_configuration = AccountConfiguration("Player 1", None)
player_2_configuration = AccountConfiguration("Player 2", None)

p1 = RandomPlayer(
player_configuration=player_1_configuration,
account_configuration=player_1_configuration,
battle_format="gen7anythinggoes",
server_configuration=LocalhostServerConfiguration,
team=RandomTeambuilder(),
max_concurrent_battles=int(sys.argv[3]),
log_level=int(sys.argv[2]),
)
p2 = RandomPlayer(
player_configuration=player_2_configuration,
account_configuration=player_2_configuration,
battle_format="gen7anythinggoes",
server_configuration=LocalhostServerConfiguration,
team=RandomTeambuilder(),
Expand Down
10 changes: 5 additions & 5 deletions diagnostic_tools/anything_goes_gen_8_random_battles_and_teams.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

from poke_env import (
POKEDEX,
AccountConfiguration,
LocalhostServerConfiguration,
PlayerConfiguration,
to_id_str,
)
from poke_env.player import RandomPlayer
Expand Down Expand Up @@ -89,19 +89,19 @@ def yield_team(self):


async def main():
player_1_configuration = PlayerConfiguration("Player 1", None)
player_2_configuration = PlayerConfiguration("Player 2", None)
player_1_configuration = AccountConfiguration("Player 1", None)
player_2_configuration = AccountConfiguration("Player 2", None)

p1 = RandomPlayer(
player_configuration=player_1_configuration,
account_configuration=player_1_configuration,
battle_format="gen8nationaldexag",
server_configuration=LocalhostServerConfiguration,
team=RandomTeambuilder(),
max_concurrent_battles=int(sys.argv[3]),
log_level=int(sys.argv[2]),
)
p2 = RandomPlayer(
player_configuration=player_2_configuration,
account_configuration=player_2_configuration,
battle_format="gen8nationaldexag",
server_configuration=LocalhostServerConfiguration,
team=RandomTeambuilder(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

from poke_env import (
POKEDEX,
AccountConfiguration,
LocalhostServerConfiguration,
PlayerConfiguration,
to_id_str,
)
from poke_env.player import RandomPlayer
Expand Down Expand Up @@ -89,19 +89,19 @@ def yield_team(self):


async def main():
player_1_configuration = PlayerConfiguration("Player 1", None)
player_2_configuration = PlayerConfiguration("Player 2", None)
player_1_configuration = AccountConfiguration("Player 1", None)
player_2_configuration = AccountConfiguration("Player 2", None)

p1 = RandomPlayer(
player_configuration=player_1_configuration,
account_configuration=player_1_configuration,
battle_format="gen8doublescustomgame",
server_configuration=LocalhostServerConfiguration,
team=RandomTeambuilder(),
max_concurrent_battles=int(sys.argv[3]),
log_level=int(sys.argv[2]),
)
p2 = RandomPlayer(
player_configuration=player_2_configuration,
account_configuration=player_2_configuration,
battle_format="gen8doublescustomgame",
server_configuration=LocalhostServerConfiguration,
team=RandomTeambuilder(),
Expand Down
10 changes: 5 additions & 5 deletions diagnostic_tools/anything_goes_gen_9_random_battles_and_teams.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import orjson
from tqdm import tqdm

from poke_env import LocalhostServerConfiguration, PlayerConfiguration, to_id_str
from poke_env import AccountConfiguration, LocalhostServerConfiguration, to_id_str
from poke_env.data import GenData
from poke_env.player import RandomPlayer
from poke_env.teambuilder import Teambuilder, TeambuilderPokemon
Expand Down Expand Up @@ -88,19 +88,19 @@ def yield_team(self):


async def main():
player_1_configuration = PlayerConfiguration("Player 1", None)
player_2_configuration = PlayerConfiguration("Player 2", None)
player_1_configuration = AccountConfiguration("Player 1", None)
player_2_configuration = AccountConfiguration("Player 2", None)

p1 = RandomPlayer(
player_configuration=player_1_configuration,
account_configuration=player_1_configuration,
battle_format="gen9nationaldexag",
server_configuration=LocalhostServerConfiguration,
team=RandomTeambuilder(),
max_concurrent_battles=int(sys.argv[3]),
log_level=int(sys.argv[2]),
)
p2 = RandomPlayer(
player_configuration=player_2_configuration,
account_configuration=player_2_configuration,
battle_format="gen9nationaldexag",
server_configuration=LocalhostServerConfiguration,
team=RandomTeambuilder(),
Expand Down
6 changes: 3 additions & 3 deletions diagnostic_tools/parse-game-messages.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import asyncio
import json

from poke_env import PlayerConfiguration
from poke_env import AccountConfiguration
from poke_env.player import RandomPlayer

with open("msgs.json", "r") as f:
msgs = json.load(f)


class FakePlayer(RandomPlayer):
async def _send_message(self, *args, **kwargs):
async def send_message(self, *args, **kwargs):
pass


p = FakePlayer(
start_listening=False,
player_configuration=PlayerConfiguration("StoragePlayer 1", None),
account_configuration=AccountConfiguration("StoragePlayer 1", None),
)
p._logged_in.set()

Expand Down
10 changes: 5 additions & 5 deletions docs/source/connecting_to_showdown_and_challenging_humans.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ Connecting your agent to showdown

To connect an agent to a showdown server hosted online, you must specify a matching server configuration.

A configuration pointing towards `play.pokemonshowdown.com <https://play.pokemonshowdown.com/>`__ is available in ``poke_env.server_configuration`` and can be used directly. To specify a different server, see :ref:`configuring a showdown server`.
A configuration pointing towards `play.pokemonshowdown.com <https://play.pokemonshowdown.com/>`__ is available in ``poke_env.ps_client.server_configuration`` and can be used directly. To specify a different server, see :ref:`configuring a showdown server`.

To connect to `play.pokemonshowdown.com <https://play.pokemonshowdown.com/>`__, you also need an account for your agent to use. The following snippets assumes that the account ``bot_username`` exists, and can be accessed with ``bot_password``.

.. code-block:: python
from poke_env.player import RandomPlayer
from poke_env import PlayerConfiguration, ShowdownServerConfiguration
from poke_env import AccountConfiguration, ShowdownServerConfiguration
# We create a random player
player = RandomPlayer(
player_configuration=PlayerConfiguration("bot_username", "bot_password"),
player_configuration=AccountConfiguration("bot_username", "bot_password"),
server_configuration=ShowdownServerConfiguration,
)
Expand Down Expand Up @@ -78,13 +78,13 @@ A complete example source code is:
import asyncio
from poke_env.player import RandomPlayer
from poke_env import PlayerConfiguration, ShowdownServerConfiguration
from poke_env import AccountConfiguration, ShowdownServerConfiguration
async def main():
# We create a random player
player = RandomPlayer(
player_configuration=PlayerConfiguration("bot_username", "bot_password")
player_configuration=AccountConfiguration("bot_username", "bot_password")
server_configuration=ShowdownServerConfiguration,
)
Expand Down
10 changes: 5 additions & 5 deletions docs/source/getting_started.rst
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ Configuring showdown players

``Player`` instances need a player configuration corresponding to showdown accounts. By default, such configurations are automatically generated for each ``Player``. These automatically generated configurations are compatible with servers bypassing authentication, such as the recommended fork mentionned above.

You can create custom configurations, for instance to use existing showdown accounts. To do so, use the ``player_configuration`` argument of ``Player`` constructors: you can pass in a ``PlayerConfiguration``, which are named tuples with two arguments: an username and a password.
You can create custom configurations, for instance to use existing showdown accounts. To do so, use the ``player_configuration`` argument of ``Player`` constructors: you can pass in a ``AccountConfiguration``, which are named tuples with two arguments: an username and a password.

Users without authentication
----------------------------
Expand All @@ -91,11 +91,11 @@ If your showdown configuration does not require authentication, you can use any

.. code-block:: python
from poke_env import PlayerConfiguration
from poke_env import AccountConfiguration
# This will work on servers that do not require authentication, which is the
# case of the server launched in our 'Getting Started' section
my_player_config = PlayerConfiguration("my_username", None)
my_player_config = AccountConfiguration("my_username", None)
Users with authentication
--------------------------
Expand All @@ -104,11 +104,11 @@ If your showdown configuration uses authentication, the values of each ``player_

.. code-block:: python
from poke_env import PlayerConfiguration
from poke_env import AccountConfiguration
# This object can be used with a player connecting to a server using authentication
# The user 'my_username' must exist and have 'super-secret-password' as his password
my_player_config = PlayerConfiguration("my_username", "super-secret-password")
my_player_config = AccountConfiguration("my_username", "super-secret-password")
Connecting your bots to showdown
================================
Expand Down
2 changes: 1 addition & 1 deletion docs/source/player.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Random Player
Player network interface
************************

.. automodule:: poke_env.player.player_network_interface
.. automodule:: poke_env.ps_client
:members:
:undoc-members:
:show-inheritance:
4 changes: 2 additions & 2 deletions docs/source/top_level_modules.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ Utils
Player configuration
********************

.. automodule:: poke_env.player_configuration
.. automodule:: poke_env.ps_client
:members:
:undoc-members:
:show-inheritance:

Server configuration
********************

.. automodule:: poke_env.server_configuration
.. automodule:: poke_env.ps_client.server_configuration
:members:
:undoc-members:
:show-inheritance:
4 changes: 2 additions & 2 deletions examples/connecting_an_agent_to_showdown.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import asyncio

from poke_env import PlayerConfiguration, ShowdownServerConfiguration
from poke_env import AccountConfiguration, ShowdownServerConfiguration
from poke_env.player import RandomPlayer


async def main():
# We create a random player
player = RandomPlayer(
player_configuration=PlayerConfiguration("bot_username", "bot_password"),
account_configuration=AccountConfiguration("bot_username", "bot_password"),
server_configuration=ShowdownServerConfiguration,
)

Expand Down
3 changes: 1 addition & 2 deletions examples/experimental-self-play.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
from threading import Thread

import numpy as np
from tabulate import tabulate

from poke_env import to_id_str
from poke_env.player import Gen8EnvSinglePlayer, RandomPlayer, cross_evaluate
from poke_env.player import Gen8EnvSinglePlayer


class RandomGen8EnvPlayer(Gen8EnvSinglePlayer):
Expand Down
10 changes: 5 additions & 5 deletions examples/gen7/cross_evaluate_random_players.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@

from tabulate import tabulate

from poke_env import LocalhostServerConfiguration, PlayerConfiguration
from poke_env import AccountConfiguration, LocalhostServerConfiguration
from poke_env.player import RandomPlayer, cross_evaluate


async def main():
# First, we define three player configurations.
player_1_configuration = PlayerConfiguration("Player 1", None)
player_2_configuration = PlayerConfiguration("Player 2", None)
player_3_configuration = PlayerConfiguration("Player 3", None)
player_1_configuration = AccountConfiguration("Player 1", None)
player_2_configuration = AccountConfiguration("Player 2", None)
player_3_configuration = AccountConfiguration("Player 3", None)

# Then, we create the corresponding players.
players = [
RandomPlayer(
player_configuration=player_config,
account_configuration=player_config,
battle_format="gen7randombattle",
server_configuration=LocalhostServerConfiguration,
max_concurrent_battles=10,
Expand Down
10 changes: 5 additions & 5 deletions examples/gen7/custom_teambuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import numpy as np

from poke_env import LocalhostServerConfiguration, PlayerConfiguration
from poke_env import AccountConfiguration, LocalhostServerConfiguration
from poke_env.player import RandomPlayer, cross_evaluate
from poke_env.teambuilder import Teambuilder

Expand Down Expand Up @@ -139,19 +139,19 @@ def yield_team(self):

async def main():
# We define two player configurations.
player_1_configuration = PlayerConfiguration("Random player 1", None)
player_2_configuration = PlayerConfiguration("Random player 2", None)
player_1_configuration = AccountConfiguration("Random player 1", None)
player_2_configuration = AccountConfiguration("Random player 2", None)

# We create the corresponding players.
player_1 = RandomPlayer(
player_configuration=player_1_configuration,
account_configuration=player_1_configuration,
battle_format="gen7ou",
server_configuration=LocalhostServerConfiguration,
team=custom_builder,
max_concurrent_battles=10,
)
player_2 = RandomPlayer(
player_configuration=player_2_configuration,
account_configuration=player_2_configuration,
battle_format="gen7ou",
server_configuration=LocalhostServerConfiguration,
team=custom_builder,
Expand Down
10 changes: 5 additions & 5 deletions examples/gen7/max_damage_player.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import asyncio
import time

from poke_env import LocalhostServerConfiguration, PlayerConfiguration
from poke_env import AccountConfiguration, LocalhostServerConfiguration
from poke_env.player import Player, RandomPlayer, cross_evaluate


Expand All @@ -22,17 +22,17 @@ async def main():
start = time.time()

# We define two player configurations.
player_1_configuration = PlayerConfiguration("Random player", None)
player_2_configuration = PlayerConfiguration("Max damage player", None)
player_1_configuration = AccountConfiguration("Random player", None)
player_2_configuration = AccountConfiguration("Max damage player", None)

# We create the corresponding players.
random_player = RandomPlayer(
player_configuration=player_1_configuration,
account_configuration=player_1_configuration,
battle_format="gen7randombattle",
server_configuration=LocalhostServerConfiguration,
)
max_damage_player = MaxDamagePlayer(
player_configuration=player_2_configuration,
account_configuration=player_2_configuration,
battle_format="gen7randombattle",
server_configuration=LocalhostServerConfiguration,
)
Expand Down

0 comments on commit 75b27ff

Please sign in to comment.