Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Force Switch Battle Property Tracking Bugfix #478

Merged
merged 4 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ def raw_team_data():
def example_request():
with open(os.path.join(FIXTURE_DIR, "example_request.json")) as f:
return orjson.loads(f.read())


@fixture
def force_switch_example_request():
with open(os.path.join(FIXTURE_DIR, "force_switch_example_request.json")) as f:
return orjson.loads(f.read())


@fixture
Expand Down
145 changes: 145 additions & 0 deletions fixture_data/force_switch_example_request.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
{
"forceSwitch": [
true
],
"side": {
"name": "RandomPlayer 1",
"id": "p1",
"pokemon": [
{
"ident": "p1: Glaceon",
"details": "Glaceon, L88, M",
"condition": "0 fnt",
"active": true,
"stats": {
"atk": 113,
"def": 244,
"spa": 278,
"spd": 217,
"spe": 165
},
"moves": [
"protect",
"wish",
"hiddenpowerground",
"icebeam"
],
"baseAbility": "snowcloak",
"item": "leftovers",
"pokeball": "pokeball"
},
{
"ident": "p1: Hitmonchan",
"details": "Hitmonchan, L87, M",
"condition": "229/229",
"active": false,
"stats": {
"atk": 232,
"def": 187,
"spa": 111,
"spd": 241,
"spe": 182
},
"moves": [
"rapidspin",
"stoneedge",
"icepunch",
"drainpunch"
],
"baseAbility": "ironfist",
"item": "leftovers",
"pokeball": "pokeball"
},
{
"ident": "p1: Raikou",
"details": "Raikou, L76",
"condition": "262/262",
"active": false,
"stats": {
"atk": 135,
"def": 157,
"spa": 219,
"spd": 196,
"spe": 219
},
"moves": [
"aurasphere",
"thunderbolt",
"hiddenpowerice",
"calmmind"
],
"baseAbility": "pressure",
"item": "lifeorb",
"pokeball": "pokeball"
},
{
"ident": "p1: Gyarados",
"details": "Gyarados, L78, F",
"condition": "276/276",
"active": false,
"stats": {
"atk": 240,
"def": 168,
"spa": 139,
"spd": 201,
"spe": 171
},
"moves": [
"dragondance",
"earthquake",
"substitute",
"waterfall"
],
"baseAbility": "intimidate",
"item": "leftovers",
"pokeball": "pokeball"
},
{
"ident": "p1: Magmortar",
"details": "Magmortar, L83, M",
"condition": "129/260",
"active": false,
"stats": {
"atk": 162,
"def": 159,
"spa": 255,
"spd": 205,
"spe": 185
},
"moves": [
"fireblast",
"thunderbolt",
"focusblast",
"substitute"
],
"baseAbility": "flamebody",
"item": "leftovers",
"pokeball": "pokeball"
},
{
"ident": "p1: Meganium",
"details": "Meganium, L90, M",
"condition": "52/290",
"active": false,
"stats": {
"atk": 152,
"def": 231,
"spa": 201,
"spd": 231,
"spe": 195
},
"moves": [
"energyball",
"aromatherapy",
"synthesis",
"leechseed"
],
"baseAbility": "overgrow",
"item": "leftovers",
"pokeball": "pokeball"
}
]
},
"noCancel": true,
"rqid": 20
}
2 changes: 1 addition & 1 deletion src/poke_env/environment/battle.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def parse_request(self, request: Dict[str, Any]) -> None:
[m["reviving"] for m in side.get("pokemon", []) if "reviving" in m]
)
self._trapped = False
self._force_switch = request.get("forceSwitch", False)
self._force_switch = request.get("forceSwitch", [False])[0]

if self._force_switch:
self._move_on_next_request = True
Expand Down
10 changes: 10 additions & 0 deletions unit_tests/environment/test_battle.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ def test_battle_request_parsing(example_request):
battle = Battle("tag", "username", logger, gen=8)

battle.parse_request(example_request)
assert battle.force_switch is False

mon = battle.active_pokemon

Expand Down Expand Up @@ -197,6 +198,15 @@ def test_battle_request_parsing(example_request):
assert team["p2: Necrozma"].status == Status.TOX


def test_battle_request_parsing_with_force_switch(force_switch_example_request):
logger = MagicMock()
battle = Battle("tag", "username", logger, gen=8)

battle.parse_request(force_switch_example_request)

assert battle.force_switch is True


def test_battle_request_and_interactions(example_request):
logger = MagicMock()
battle = Battle("tag", "username", logger, gen=8)
Expand Down
Loading