Skip to main content

manabrew_engine/
game_runtime.rs

1use crate::agent::PlayerAgent;
2use crate::game::GameState;
3use crate::game_loop::GameLoop;
4use crate::ids::PlayerId;
5
6/// Owns the live state and runtime services for one game session.
7///
8/// This is an additive facade over the existing `GameState` + `GameLoop`
9/// split. It intentionally keeps `GameLoop::run(&mut GameState, ...)`
10/// available so callers can migrate incrementally.
11pub struct GameRuntime {
12    pub game: GameState,
13    pub loop_state: GameLoop,
14    pub agents: Vec<Box<dyn PlayerAgent>>,
15}
16
17impl GameRuntime {
18    pub fn from_parts(
19        game: GameState,
20        loop_state: GameLoop,
21        agents: Vec<Box<dyn PlayerAgent>>,
22    ) -> Self {
23        Self {
24            game,
25            loop_state,
26            agents,
27        }
28    }
29
30    pub fn run(&mut self, rng: &mut impl rand::Rng, max_turns: u32) -> Option<PlayerId> {
31        self.loop_state
32            .run(&mut self.game, &mut self.agents, rng, max_turns)
33    }
34
35    pub fn run_opening_hand_actions(&mut self) {
36        self.loop_state
37            .run_opening_hand_actions(&mut self.game, &mut self.agents);
38    }
39
40    pub fn run_turn(&mut self, rng: &mut impl rand::Rng) {
41        self.loop_state
42            .run_turn(&mut self.game, &mut self.agents, rng);
43    }
44
45    pub fn game(&self) -> &GameState {
46        &self.game
47    }
48
49    pub fn game_mut(&mut self) -> &mut GameState {
50        &mut self.game
51    }
52
53    pub fn loop_state(&self) -> &GameLoop {
54        &self.loop_state
55    }
56
57    pub fn loop_state_mut(&mut self) -> &mut GameLoop {
58        &mut self.loop_state
59    }
60
61    pub fn agents(&self) -> &[Box<dyn PlayerAgent>] {
62        &self.agents
63    }
64
65    pub fn agents_mut(&mut self) -> &mut [Box<dyn PlayerAgent>] {
66        &mut self.agents
67    }
68
69    pub fn into_parts(self) -> (GameState, GameLoop, Vec<Box<dyn PlayerAgent>>) {
70        (self.game, self.loop_state, self.agents)
71    }
72}