Skip to main content

manabrew_engine/agent/
game_log.rs

1use crate::ids::{CardId, PlayerId};
2
3use super::notification::GameNotification;
4use super::PlayerAgent;
5
6#[derive(Debug, Clone)]
7pub struct GameLogEvent {
8    pub kind: GameLogKind,
9    pub message: String,
10    pub player: Option<PlayerId>,
11    pub card: Option<CardId>,
12    pub source_card: Option<CardId>,
13    pub target_card: Option<CardId>,
14}
15
16#[derive(Debug, Clone, Copy, PartialEq, Eq)]
17pub enum GameLogKind {
18    Info,
19    Action,
20    Stack,
21    Priority,
22    Rule,
23    Warning,
24}
25
26pub fn notify_all_agents(agents: &mut [Box<dyn PlayerAgent>], event: GameLogEvent) {
27    for agent in agents.iter_mut() {
28        agent.notify(GameNotification::Event(event.clone()));
29    }
30}
31
32/// Broadcast a `GameNotification` (other than `Event`) to every agent.
33pub fn broadcast_notification(agents: &mut [Box<dyn PlayerAgent>], event: GameNotification) {
34    for agent in agents.iter_mut() {
35        agent.notify(event.clone());
36    }
37}
38
39impl GameLogEvent {
40    pub fn new(kind: GameLogKind, message: impl Into<String>) -> Self {
41        Self {
42            kind,
43            message: message.into(),
44            player: None,
45            card: None,
46            source_card: None,
47            target_card: None,
48        }
49    }
50
51    pub fn info(message: impl Into<String>) -> Self {
52        Self::new(GameLogKind::Info, message)
53    }
54
55    pub fn action(message: impl Into<String>) -> Self {
56        Self::new(GameLogKind::Action, message)
57    }
58
59    pub fn stack(message: impl Into<String>) -> Self {
60        Self::new(GameLogKind::Stack, message)
61    }
62
63    pub fn priority(message: impl Into<String>) -> Self {
64        Self::new(GameLogKind::Priority, message)
65    }
66
67    pub fn rule(message: impl Into<String>) -> Self {
68        Self::new(GameLogKind::Rule, message)
69    }
70
71    pub fn warning(message: impl Into<String>) -> Self {
72        Self::new(GameLogKind::Warning, message)
73    }
74
75    pub fn with_player(mut self, player: PlayerId) -> Self {
76        self.player = Some(player);
77        self
78    }
79
80    pub fn with_card(mut self, card: CardId) -> Self {
81        self.card = Some(card);
82        if self.source_card.is_none() {
83            self.source_card = Some(card);
84        }
85        self
86    }
87
88    pub fn with_source_card(mut self, card: CardId) -> Self {
89        self.source_card = Some(card);
90        if self.card.is_none() {
91            self.card = Some(card);
92        }
93        self
94    }
95
96    pub fn with_target_card(mut self, card: CardId) -> Self {
97        self.target_card = Some(card);
98        if self.card.is_none() {
99            self.card = Some(card);
100        }
101        self
102    }
103}