This commit is contained in:
@@ -95,7 +95,7 @@ def _trade_to_schema(trade: TradeOffer) -> TradeOfferSchema:
|
||||
|
||||
|
||||
def _build_state(game: Game, runtime) -> GameStateSchema:
|
||||
if game.status != "running" or runtime is None:
|
||||
if game.status != "running" or runtime is None or runtime.env is None:
|
||||
return GameStateSchema(
|
||||
id=game.id,
|
||||
name=game.name,
|
||||
|
||||
@@ -17,7 +17,7 @@ from services.game.models import Game, GameEvent, TradeOffer
|
||||
@dataclass
|
||||
class GameRuntime:
|
||||
game: Game
|
||||
env: CatanEnv
|
||||
env: Optional[CatanEnv] = None
|
||||
action_index: int = 0
|
||||
|
||||
def next_action_index(self) -> int:
|
||||
@@ -47,14 +47,19 @@ class GameRuntimeManager:
|
||||
slots = game.slots.get("slots", [])
|
||||
names = [slot.get("name") for slot in slots if slot.get("name")]
|
||||
colors = [slot.get("color", "player") for slot in slots if slot.get("name")]
|
||||
config = GameConfig(player_names=names, colors=colors, seed=game.seed)
|
||||
env = CatanEnv(config)
|
||||
events = session.exec(
|
||||
select(GameEvent).where(GameEvent.game_id == game_id, GameEvent.applied == True).order_by(GameEvent.idx)
|
||||
).all()
|
||||
for event in events:
|
||||
action = Action(ActionType(event.action_type), event.payload)
|
||||
env.step(action)
|
||||
if game.status == "running" and len(names) >= 2:
|
||||
config = GameConfig(player_names=names, colors=colors, seed=game.seed)
|
||||
env = CatanEnv(config)
|
||||
events = session.exec(
|
||||
select(GameEvent).where(GameEvent.game_id == game_id, GameEvent.applied == True).order_by(GameEvent.idx)
|
||||
).all()
|
||||
else:
|
||||
env = None
|
||||
events = []
|
||||
if env is not None:
|
||||
for event in events:
|
||||
action = Action(ActionType(event.action_type), event.payload)
|
||||
env.step(action)
|
||||
action_index = events[-1].idx if events else 0
|
||||
return GameRuntime(game=game, env=env, action_index=action_index)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user