"""Per-process cache of running games to avoid repeated lookups per turn.""" from collections import OrderedDict from dataclasses import dataclass, field from .records import PointsState @dataclass class RunningGame: gid:int snake_ids:dict[str, int] = field(default_factory=dict) points:PointsState|None = None class RunningGameCache: def __init__(self, max_games:int=512): self._games:OrderedDict[str, RunningGame] = OrderedDict() self._max_games = max(1, max_games) def get(self, game_id:str) -> RunningGame|None: game = self._games.get(game_id) if game is not None: self._games.move_to_end(game_id) return game def put(self, game_id:str, game:RunningGame) -> RunningGame: self._games[game_id] = game self._games.move_to_end(game_id) while len(self._games) > self._max_games: self._games.popitem(last=False) return game def drop(self, game_id:str) -> None: self._games.pop(game_id, None)