Skip to content

Commit

Permalink
weird hack to make unique names for players that use the name of the …
Browse files Browse the repository at this point in the history
…function they're in
  • Loading branch information
phinate committed Jun 24, 2023
1 parent c3277d0 commit fd061c8
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 35 deletions.
16 changes: 10 additions & 6 deletions src/p2lab/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ def parse_args():
default=10,
)
parser.add_argument(
"--teamsize", help="Number of pokemon per team (max 6)", type=int, default=2
"--team-size", help="Number of pokemon per team (max 6)", type=int, default=2
)
parser.add_argument(
"--numteams",
"--teams",
help="Number of teams i.e., individuals per generation",
type=int,
default=30,
Expand All @@ -73,12 +73,16 @@ def parse_args():

def main():
args = parse_args()

if args["s"] is not None:
np.random.seed(args["s"])
if args["seed"] is not None:
np.random.seed(args["seed"])

asyncio.get_event_loop().run_until_complete(
main_loop(args["n"], args["t"], args["g"], args["u"])
main_loop(
num_teams=args["teams"],
team_size=args["team_size"],
num_generations=args["generations"],
unique=args["unique"],
)
)


Expand Down
76 changes: 47 additions & 29 deletions tests/test_battles.py
Original file line number Diff line number Diff line change
@@ -1,83 +1,101 @@
from __future__ import annotations

import inspect

import pytest
from poke_env import PlayerConfiguration
from poke_env.player import SimpleHeuristicsPlayer

from p2lab.pokemon import pokefactory
from p2lab.pokemon.battles import run_battles
from p2lab.pokemon.teams import Team

pytest_plugins = ("pytest_asyncio",)


@pytest.mark.asyncio()
async def test_battle_eevee_pikachu_pokes():
p = pokefactory.PokeFactory()
eevee = p.make_pokemon(133, moves=["tackle", "growl"], level=5)
pikachu = p.make_pokemon(25, moves=["thundershock", "growl"], level=5)
def test_battle_eevee_pikachu_pokes(event_loop, default_factory):
eevee = default_factory.make_pokemon(133, moves=["tackle", "growl"], level=5)
pikachu = default_factory.make_pokemon(25, moves=["thundershock", "growl"], level=5)
team1 = Team([eevee])
team2 = Team([pikachu])
teams = [team1, team2]
matches = [[0, 1]]

player_1 = SimpleHeuristicsPlayer(
PlayerConfiguration("Player 1", None), battle_format="gen7anythinggoes"
PlayerConfiguration(inspect.stack()[0][3].split("test_")[1][:15] + " P1", None),
battle_format="gen7anythinggoes",
)
player_2 = SimpleHeuristicsPlayer(
PlayerConfiguration("Player 2", None), battle_format="gen7anythinggoes"
PlayerConfiguration(inspect.stack()[0][3].split("test_")[1][:15] + " P2", None),
battle_format="gen7anythinggoes",
)
res = event_loop.run_until_complete(
run_battles(matches, teams, player_1, player_2, battles_per_match=1)
)
res = await run_battles(matches, teams, player_1, player_2, battles_per_match=1)

assert res is not None


@pytest.mark.asyncio()
async def test_battle_mewtwo_obliterates_eevee():
p = pokefactory.PokeFactory()
eevee = p.make_pokemon(133, moves=["tackle", "growl"], level=5)
mewtwo = p.make_pokemon(150, moves=["psychic"], level=100)
def test_battle_mewtwo_obliterates_eevee(event_loop, default_factory):
eevee = default_factory.make_pokemon(133, moves=["tackle", "growl"], level=5)
mewtwo = default_factory.make_pokemon(150, moves=["psychic"], level=100)
team1 = Team([eevee])
team2 = Team([mewtwo])
teams = [team1, team2]
matches = [[0, 1]]
player_1 = SimpleHeuristicsPlayer(
PlayerConfiguration("Player 1", None), battle_format="gen7anythinggoes"
PlayerConfiguration(inspect.stack()[0][3].split("test_")[1][:15] + " P1", None),
battle_format="gen7anythinggoes",
)
player_2 = SimpleHeuristicsPlayer(
PlayerConfiguration("Player 2", None), battle_format="gen7anythinggoes"
PlayerConfiguration(inspect.stack()[0][3].split("test_")[1][:15] + " P2", None),
battle_format="gen7anythinggoes",
)
res = event_loop.run_until_complete(
run_battles(matches, teams, player_1, player_2, battles_per_match=1)
)
res = await run_battles(matches, teams, player_1, player_2, battles_per_match=10)
mewtwo_wins = res[0][1]
eevee_wins = res[0][0]
assert mewtwo_wins > eevee_wins


@pytest.mark.asyncio()
@pytest.mark.parametrize(
"battle_format",
[
"gen4anythinggoes",
"gen6anythinggoes",
"gen7anythinggoes",
"gen8anythinggoes",
("gen4anythinggoes"),
("gen6anythinggoes"),
("gen7anythinggoes"),
("gen8anythinggoes"),
],
)
async def test_battle_eevee_pikachu_formats(battle_format):
p = pokefactory.PokeFactory()
eevee = p.make_pokemon(133, moves=["tackle", "growl"], level=5)
pikachu = p.make_pokemon(25, moves=["thundershock", "growl"], level=5)
def test_battle_eevee_pikachu_formats(event_loop, battle_format, default_factory):
eevee = default_factory.make_pokemon(133, moves=["tackle", "growl"], level=5)
pikachu = default_factory.make_pokemon(25, moves=["thundershock", "growl"], level=5)
team1 = Team([eevee])
team2 = Team([pikachu])
teams = [team1, team2]
matches = [[0, 1]]

player_1 = SimpleHeuristicsPlayer(
PlayerConfiguration("Player 1", None),
PlayerConfiguration(
inspect.stack()[0][3].split("test_")[1][:10]
+ "-"
+ battle_format[:4]
+ " P1",
None,
),
battle_format=battle_format,
)
player_2 = SimpleHeuristicsPlayer(
PlayerConfiguration("Player 2", None),
PlayerConfiguration(
inspect.stack()[0][3].split("test_")[1][:10]
+ "-"
+ battle_format[:4]
+ " P2",
None,
),
battle_format=battle_format,
)
res = await run_battles(matches, teams, player_1, player_2, battles_per_match=1)
res = event_loop.run_until_complete(
run_battles(matches, teams, player_1, player_2, battles_per_match=1)
)
assert res is not None

1 comment on commit fd061c8

@AoifeHughes
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@phinate does this work for you? My tests won't run in test_battles.py without @pytest.mark.asyncio() and await.

Please sign in to comment.