Spaces:
Sleeping
Sleeping
active battle tracking
Browse files- utils/pokemon_utils.py +31 -4
utils/pokemon_utils.py
CHANGED
|
@@ -49,7 +49,7 @@ class MCPPokemonAgent(LLMAgentBase):
|
|
| 49 |
|
| 50 |
# Update active battle state
|
| 51 |
if battle.battle_tag in active_battles:
|
| 52 |
-
active_battles[battle.battle_tag]['battle_state'] =
|
| 53 |
active_battles[battle.battle_tag]['waiting_for_move'] = True
|
| 54 |
active_battles[battle.battle_tag]['available_moves'] = [move.id for move in battle.available_moves]
|
| 55 |
active_battles[battle.battle_tag]['available_switches'] = [pkmn.species for pkmn in battle.available_switches]
|
|
@@ -112,6 +112,26 @@ def register_player_instance(base_username: str, player: MCPPokemonAgent) -> Non
|
|
| 112 |
player_instances[base_username][player.username] = player
|
| 113 |
player_instances_by_unique[player.username] = player
|
| 114 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
def normalize_name(name: str) -> str:
|
| 116 |
"""Lowercase and remove non-alphanumeric characters."""
|
| 117 |
return "".join(filter(str.isalnum, name)).lower()
|
|
@@ -403,7 +423,9 @@ async def submit_move_for_battle(battle_id: str, move_name: str = None, pokemon_
|
|
| 403 |
str: Result message
|
| 404 |
"""
|
| 405 |
if battle_id not in active_battles:
|
| 406 |
-
|
|
|
|
|
|
|
| 407 |
|
| 408 |
battle_info = active_battles[battle_id]
|
| 409 |
unique_username = battle_info.get('player_unique_username') or battle_info.get('player_username')
|
|
@@ -442,7 +464,10 @@ def get_battle_state(battle_id: str) -> Dict[str, Any]:
|
|
| 442 |
dict: Current battle state
|
| 443 |
"""
|
| 444 |
if battle_id not in active_battles:
|
| 445 |
-
|
|
|
|
|
|
|
|
|
|
| 446 |
|
| 447 |
return active_battles[battle_id]
|
| 448 |
|
|
@@ -530,7 +555,9 @@ async def download_battle_replay(battle_id: str) -> str:
|
|
| 530 |
str: Path to the replay file or replay content
|
| 531 |
"""
|
| 532 |
if battle_id not in active_battles:
|
| 533 |
-
|
|
|
|
|
|
|
| 534 |
|
| 535 |
battle_info = active_battles[battle_id]
|
| 536 |
unique_username = battle_info.get('player_unique_username') or battle_info.get('player_username')
|
|
|
|
| 49 |
|
| 50 |
# Update active battle state
|
| 51 |
if battle.battle_tag in active_battles:
|
| 52 |
+
active_battles[battle.battle_tag]['battle_state'] = format_battle_state(battle)
|
| 53 |
active_battles[battle.battle_tag]['waiting_for_move'] = True
|
| 54 |
active_battles[battle.battle_tag]['available_moves'] = [move.id for move in battle.available_moves]
|
| 55 |
active_battles[battle.battle_tag]['available_switches'] = [pkmn.species for pkmn in battle.available_switches]
|
|
|
|
| 112 |
player_instances[base_username][player.username] = player
|
| 113 |
player_instances_by_unique[player.username] = player
|
| 114 |
|
| 115 |
+
# Helper to ensure an untracked battle becomes tracked by scanning all players
|
| 116 |
+
def ensure_battle_tracked(battle_id: str) -> bool:
|
| 117 |
+
for player in player_instances_by_unique.values():
|
| 118 |
+
for battle_obj in player.battles.values():
|
| 119 |
+
if battle_obj.battle_tag == battle_id:
|
| 120 |
+
battle_url = f"https://jofthomas.com/play.pokemonshowdown.com/testclient.html#battle-{battle_id}"
|
| 121 |
+
active_battles[battle_id] = {
|
| 122 |
+
'type': 'recovered',
|
| 123 |
+
'player_base_username': getattr(player, 'base_username', player.username),
|
| 124 |
+
'player_unique_username': player.username,
|
| 125 |
+
'player_username': getattr(player, 'base_username', player.username),
|
| 126 |
+
'opponent': getattr(battle_obj, 'opponent_username', 'unknown'),
|
| 127 |
+
'battle_state': format_battle_state(battle_obj),
|
| 128 |
+
'waiting_for_move': False,
|
| 129 |
+
'completed': False,
|
| 130 |
+
'battle_url': battle_url
|
| 131 |
+
}
|
| 132 |
+
return True
|
| 133 |
+
return False
|
| 134 |
+
|
| 135 |
def normalize_name(name: str) -> str:
|
| 136 |
"""Lowercase and remove non-alphanumeric characters."""
|
| 137 |
return "".join(filter(str.isalnum, name)).lower()
|
|
|
|
| 423 |
str: Result message
|
| 424 |
"""
|
| 425 |
if battle_id not in active_battles:
|
| 426 |
+
# Attempt to recover by scanning players
|
| 427 |
+
if not ensure_battle_tracked(battle_id):
|
| 428 |
+
raise ValueError(f"Battle {battle_id} not found")
|
| 429 |
|
| 430 |
battle_info = active_battles[battle_id]
|
| 431 |
unique_username = battle_info.get('player_unique_username') or battle_info.get('player_username')
|
|
|
|
| 464 |
dict: Current battle state
|
| 465 |
"""
|
| 466 |
if battle_id not in active_battles:
|
| 467 |
+
# Attempt to recover by scanning players
|
| 468 |
+
recovered = ensure_battle_tracked(battle_id)
|
| 469 |
+
if not recovered:
|
| 470 |
+
raise ValueError(f"Battle {battle_id} not found")
|
| 471 |
|
| 472 |
return active_battles[battle_id]
|
| 473 |
|
|
|
|
| 555 |
str: Path to the replay file or replay content
|
| 556 |
"""
|
| 557 |
if battle_id not in active_battles:
|
| 558 |
+
# Attempt to recover by scanning players
|
| 559 |
+
if not ensure_battle_tracked(battle_id):
|
| 560 |
+
raise ValueError(f"Battle {battle_id} not found")
|
| 561 |
|
| 562 |
battle_info = active_battles[battle_id]
|
| 563 |
unique_username = battle_info.get('player_unique_username') or battle_info.get('player_username')
|