feat: improves and initilize player pipeline

This commit is contained in:
2026-03-05 00:33:48 -03:00
parent 06ae9b41f0
commit 53eb34a83e
13 changed files with 248 additions and 60 deletions

View File

@@ -1,31 +1,37 @@
import pickle
from pathlib import Path
from diceplayer.config import PlayerConfig
from diceplayer.environment import System
from diceplayer.logger import logger
from diceplayer.state.state_model import StateModel
import pickle
from pathlib import Path
class StateHandler:
def __init__(self, sim_dir: Path, state_file: str = "state.pkl"):
if not sim_dir.exists():
sim_dir.mkdir(parents=True, exist_ok=True)
self._state_file = sim_dir / state_file
def get_state(self, config: PlayerConfig) -> StateModel | None:
def get(self, config: PlayerConfig, force=False) -> StateModel | None:
if not self._state_file.exists():
return None
with self._state_file.open(mode="r") as f:
data = pickle.load(f)
with open(self._state_file, mode="rb") as file:
data = pickle.load(file)
model = StateModel.model_validate(data)
if hash(model.config) != hash(config):
logger.warning("The configuration in the state file does not match the provided configuration.")
if config != model.config and not force:
logger.warning(
"The configuration in the state file does not match the provided configuration."
)
return None
return model
def save_state(self, state: StateModel) -> None:
def save(self, state: StateModel) -> None:
with self._state_file.open(mode="wb") as f:
pickle.dump(state.model_dump(), f)
pickle.dump(state.model_dump(), f)
def delete(self) -> None:
if self._state_file.exists():
self._state_file.unlink()

View File

@@ -1,10 +1,19 @@
from pydantic import BaseModel
from diceplayer.config import PlayerConfig
from diceplayer.environment import System
from pydantic import BaseModel
from typing_extensions import Self
class StateModel(BaseModel):
config: PlayerConfig
system: System
current_cycle: int
current_cycle: int
@classmethod
def from_config(cls, config: PlayerConfig) -> Self:
return cls(
config=config,
system=System(),
current_cycle=0,
)