Skip to main content

cardinal_kernel/engine/
events.rs

1use crate::state::gamestate::GameState;
2use crate::model::command::Command;
3use crate::model::event::Event;
4
5/// Apply a batch of commands to the `GameState` and return emitted events.
6/// Each command mutates the state and produces one or more events.
7pub fn commit_commands(state: &mut GameState, commands: &[Command]) -> Vec<Event> {
8    let mut events = Vec::new();
9
10    for cmd in commands {
11        match cmd {
12            Command::MoveCard { card, from, to } => {
13                // Remove card from source zone
14                if let Some(zone) = state.zones.iter_mut().find(|z| z.id == from.clone()) {
15                    zone.cards.retain(|c| c != card);
16                }
17                // Add card to destination zone
18                if let Some(zone) = state.zones.iter_mut().find(|z| z.id == to.clone()) {
19                    zone.cards.push(*card);
20                }
21                events.push(Event::CardMoved { card: *card, from: from.clone(), to: to.clone() });
22            }
23            Command::ChangeLife { player, delta } => {
24                if let Some(p) = state.players.iter_mut().find(|pl| pl.id == *player) {
25                    p.life += delta;
26                }
27                events.push(Event::LifeChanged { player: *player, delta: *delta });
28            }
29            Command::PushStack { item } => {
30                let stack_id = item.id;
31                state.stack.push(item.clone());
32                events.push(Event::StackPushed { item_id: stack_id });
33            }
34            Command::RequestChoice { player, choice } => {
35                state.pending_choice = Some(choice.clone());
36                events.push(Event::ChoiceRequested { choice_id: choice.id, player: *player });
37            }
38        }
39    }
40
41    events
42}
43
44// Event handling logic