Skip to content

Commit

Permalink
Add player and server configuration namedtuples
Browse files Browse the repository at this point in the history
  • Loading branch information
hsahovic committed Sep 22, 2019
1 parent 470f9e0 commit 9afeddc
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 33 deletions.
16 changes: 8 additions & 8 deletions src/pokemon_showdown_env/player/player_network_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
from typing import List, Optional

from pokemon_showdown_env.exceptions import ShowdownException
from pokemon_showdown_env.player_configuration import PlayerConfiguration
from pokemon_showdown_env.server_configuration import ServerConfiguration


class PlayerNetwork(ABC):
Expand All @@ -25,13 +27,11 @@ class PlayerNetwork(ABC):

def __init__(
self,
username: str,
password: str,
player_configuration: PlayerConfiguration,
*,
avatar: Optional[int] = None,
authentication_url: str,
log_level: Optional[int] = None,
server_url: str,
server_configuration: ServerConfiguration,
) -> None:
"""
:param username: Player username.
Expand All @@ -47,12 +47,12 @@ def __init__(
:param server_url: Server URL.
:type server_url: str
"""
self._authentication_url = authentication_url
self._authentication_url = server_configuration.authentication_url
self._avatar = avatar
self._lock = Lock()
self._password = password
self._username = username
self._server_url = server_url
self._password = player_configuration.password
self._username = player_configuration.username
self._server_url = server_configuration.server_url

self._logged_in: bool = False

Expand Down
4 changes: 4 additions & 0 deletions src/pokemon_showdown_env/player_configuration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# -*- coding: utf-8 -*-
from collections import namedtuple

PlayerConfiguration = namedtuple("PlayerConfiguration", ["username", "password"])
14 changes: 14 additions & 0 deletions src/pokemon_showdown_env/server_configuration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# -*- coding: utf-8 -*-
from collections import namedtuple


ServerConfiguration = namedtuple(
"ServerConfiguration", ["server_url", "authentication_url"]
)

LocalhostServerConfiguration = ServerConfiguration(
"localhost:8000", "https://play.pokemonshowdown.com/action.php?"
)
ShowdownServerConfiguration = ServerConfiguration(
"https://play.pokemonshowdown.com/", "https://play.pokemonshowdown.com/action.php?"
)
43 changes: 18 additions & 25 deletions test/player/test_player_network_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@
from mock import MagicMock
from pokemon_showdown_env.exceptions import ShowdownException
from pokemon_showdown_env.player.player_network_interface import PlayerNetwork
from pokemon_showdown_env.player_configuration import PlayerConfiguration
from pokemon_showdown_env.server_configuration import ServerConfiguration


player_configuration = PlayerConfiguration("username", "password")
requests_tuple = namedtuple("requests_tuple", ["text"])
server_configuration = ServerConfiguration("server.url", "auth.url")


class PlayerNetworkChild(PlayerNetwork):
Expand All @@ -27,7 +31,8 @@ def update_challenges(self):

def test_init_and_properties():
player = PlayerNetworkChild(
"username", "password", authentication_url="auth.url", server_url="server.url"
player_configuration=player_configuration,
server_configuration=server_configuration,
)

assert player.username == "username"
Expand All @@ -36,10 +41,8 @@ def test_init_and_properties():

def test_create_player_logger():
player = PlayerNetworkChild(
"username",
"password",
authentication_url="auth.url",
server_url="server.url",
player_configuration=player_configuration,
server_configuration=server_configuration,
log_level=38,
)

Expand All @@ -59,11 +62,9 @@ def test_create_player_logger():
)
async def test_log_in(post_mock):
player = PlayerNetworkChild(
"username",
"password",
player_configuration=player_configuration,
avatar=12,
authentication_url="auth.url",
server_url="server.url",
server_configuration=server_configuration,
log_level=38,
)

Expand All @@ -80,11 +81,9 @@ async def test_log_in(post_mock):
@pytest.mark.asyncio
async def test_change_avatar():
player = PlayerNetworkChild(
"username",
"password",
player_configuration=player_configuration,
avatar=12,
authentication_url="auth.url",
server_url="server.url",
server_configuration=server_configuration,
)

player.send_message = CoroutineMock()
Expand All @@ -97,11 +96,9 @@ async def test_change_avatar():
@pytest.mark.asyncio
async def test_handle_message():
player = PlayerNetworkChild(
"username",
"password",
player_configuration=player_configuration,
avatar=12,
authentication_url="auth.url",
server_url="server.url",
server_configuration=server_configuration,
)
player._log_in = CoroutineMock()

Expand Down Expand Up @@ -137,11 +134,9 @@ async def test_handle_message():
@pytest.mark.asyncio
async def test_listen():
player = PlayerNetworkChild(
"username",
"password",
player_configuration=player_configuration,
avatar=12,
authentication_url="auth.url",
server_url="server.url",
server_configuration=server_configuration,
)

type(player).websocket_url = PropertyMock(return_value="ws://localhost:8899")
Expand Down Expand Up @@ -170,11 +165,9 @@ async def showdown_server_mock(websocket, path):
@pytest.mark.asyncio
async def test_send_message():
player = PlayerNetworkChild(
"username",
"password",
player_configuration=player_configuration,
avatar=12,
authentication_url="auth.url",
server_url="server.url",
server_configuration=server_configuration,
)
player._websocket = CoroutineMock()
player._websocket.send = CoroutineMock()
Expand Down

0 comments on commit 9afeddc

Please sign in to comment.