Repository: hsahovic/poke-env

Issue:
toxic status counter
Hey,

It seems to me that the status counter for toxic was off, so I added a reset to the counter under def _switch_in(self, details=None)
and I think solved the problem. Let me know if this is correct :)

Discussion:

Patch:
diff --git a/src/poke_env/data.py b/src/poke_env/data.py
index e04eb89..950ee16 100644
--- a/src/poke_env/data.py
+++ b/src/poke_env/data.py
@@ -38,7 +38,7 @@ def _compute_type_chart(chart_path: str) -> Dict[str, Dict[str, float]]:
     return type_chart
 
 
-@lru_cache(2 ** 13)  # pyre-ignore
+@lru_cache(2 ** 13)
 def to_id_str(name: str) -> str:
     """Converts a full-name to its corresponding id string.
     :param name: The name to convert.
diff --git a/src/poke_env/environment/abstract_battle.py b/src/poke_env/environment/abstract_battle.py
index de6a02c..5b37b8f 100644
--- a/src/poke_env/environment/abstract_battle.py
+++ b/src/poke_env/environment/abstract_battle.py
@@ -46,7 +46,6 @@ class AbstractBattle(ABC):
         "askreg",
         "debug",
         "c",
-        "cant",
         "crit",
         "deinit",
         "gametype",
diff --git a/src/poke_env/environment/pokemon.py b/src/poke_env/environment/pokemon.py
index 1ca9dfb..0cbc57d 100644
--- a/src/poke_env/environment/pokemon.py
+++ b/src/poke_env/environment/pokemon.py
@@ -304,6 +304,9 @@ class Pokemon:
         self._preparing = False
         self._protect_counter = 0
 
+        if self._status == Status.TOX:
+            self._status_counter = 0
+
     def _transform(self, into):
         current_hp = self.current_hp
         self._update_from_pokedex(into.species, store_species=False)


Test patch:
diff --git a/unit_tests/environment/test_battle.py b/unit_tests/environment/test_battle.py
index b8563bd..6ab90be 100644
--- a/unit_tests/environment/test_battle.py
+++ b/unit_tests/environment/test_battle.py
@@ -431,3 +431,82 @@ def test_end_illusion():
     assert battle.opponent_active_pokemon.species == "kingdra"
     assert battle.get_pokemon("p2: Zoroark").boosts == non_empty_boosts
     assert battle.get_pokemon("p2: Celebi").boosts == empty_boosts
+
+
+def test_toxic_counter(example_request):
+    logger = MagicMock()
+    battle = Battle("tag", "username", logger)
+
+    battle._parse_request(example_request)
+    battle._parse_message(["", "-status", "p2a: Venusaur", "tox"])
+    assert battle.active_pokemon.status == Status.TOX
+    assert battle.active_pokemon.status_counter == 0
+
+    battle.end_turn(2)
+    assert battle.active_pokemon.status == Status.TOX
+    assert battle.active_pokemon.status_counter == 1
+
+    battle.end_turn(3)
+    assert battle.active_pokemon.status == Status.TOX
+    assert battle.active_pokemon.status_counter == 2
+
+    battle._switch("p2a: Unfezant", "Unfezant, L86, M", "100/100")
+    assert battle.active_pokemon.status is None
+    assert battle.active_pokemon.status_counter == 0
+
+    battle.end_turn(4)
+    assert battle.active_pokemon.status is None
+    assert battle.active_pokemon.status_counter == 0
+
+    battle._switch("p2a: Venusaur", "Venusaur, L82, M", "100/100 tox")
+    assert battle.active_pokemon.status == Status.TOX
+    assert battle.active_pokemon.status_counter == 0
+
+    battle.end_turn(5)
+    assert battle.active_pokemon.status == Status.TOX
+    assert battle.active_pokemon.status_counter == 1
+
+    battle.end_turn(6)
+    assert battle.active_pokemon.status == Status.TOX
+    assert battle.active_pokemon.status_counter == 2
+
+
+def test_sleep_counter(example_request):
+    logger = MagicMock()
+    battle = Battle("tag", "username", logger)
+
+    battle._parse_request(example_request)
+    battle._parse_message(["", "-status", "p2a: Venusaur", "slp"])
+    assert battle.active_pokemon.status == Status.SLP
+    assert battle.active_pokemon.status_counter == 0
+
+    battle.end_turn(2)
+    battle._parse_message(["", "cant", "p2a: Venusaur", ""])
+    assert battle.active_pokemon.status == Status.SLP
+    assert battle.active_pokemon.status_counter == 1
+
+    battle.end_turn(3)
+    assert battle.active_pokemon.status == Status.SLP
+    assert battle.active_pokemon.status_counter == 1
+
+    battle._switch("p2a: Unfezant", "Unfezant, L86, M", "100/100")
+    assert battle.active_pokemon.status is None
+    assert battle.active_pokemon.status_counter == 0
+
+    battle.end_turn(4)
+    assert battle.active_pokemon.status is None
+    assert battle.active_pokemon.status_counter == 0
+
+    battle._switch("p2a: Venusaur", "Venusaur, L82, M", "100/100 slp")
+    assert battle.active_pokemon.status == Status.SLP
+    assert battle.active_pokemon.status_counter == 1
+
+    battle.end_turn(5)
+    battle._parse_message(["", "cant", "p2a: Venusaur", ""])
+    assert battle.active_pokemon.status == Status.SLP
+    assert battle.active_pokemon.status_counter == 2
+
+    battle.end_turn(6)
+    battle._parse_message(["", "cant", "p2a: Venusaur", ""])
+    assert battle.active_pokemon.status == Status.SLP
+    assert battle.active_pokemon.status_counter == 3