Skip to main content

manabrew_protocol/transport/
mod.rs

1use serde::{Deserialize, Serialize};
2use ts_rs::TS;
3
4use crate::display::DisplayEvent;
5use crate::game::{CardDto, GameViewDto};
6use crate::prompts::{PromptInput, PromptOutput};
7
8#[derive(Debug, Clone)]
9#[allow(clippy::large_enum_variant)]
10pub enum AgentMessage {
11    State(StateUpdate),
12    Display(DisplayEvent),
13    Prompt(AgentPrompt),
14    Directive(EngineDirective),
15    Error(ProtocolError),
16}
17
18#[derive(Debug, Clone, Serialize, Deserialize, TS)]
19#[serde(rename_all = "camelCase", deny_unknown_fields)]
20#[ts(export, export_to = "transport/messages.ts")]
21pub struct StateUpdate {
22    pub game_view: GameViewDto,
23}
24
25#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, TS)]
26#[serde(
27    tag = "type",
28    rename_all = "camelCase",
29    rename_all_fields = "camelCase"
30)]
31#[ts(export, export_to = "transport/messages.ts")]
32pub enum DirectiveInput {
33    Concede,
34    RequestRestore { checkpoint_id: u32 },
35    RestoreVote { vote_id: u32, accept: bool },
36    SetSnapshotRecording { enabled: bool },
37    StartRecording,
38    StopRecording,
39    RequestRepeat { iterations: u32 },
40    RepeatVote { vote_id: u32, accept: bool },
41}
42
43#[derive(Debug, Clone, Serialize, Deserialize, TS)]
44#[serde(
45    tag = "type",
46    rename_all = "camelCase",
47    rename_all_fields = "camelCase"
48)]
49#[ts(export, export_to = "transport/messages.ts")]
50pub enum EngineDirective {
51    Recording { status: RecordingStatus },
52    RepeatVote { vote: RepeatVoteDto },
53}
54
55#[derive(Debug, Clone, Serialize, Deserialize, TS)]
56#[serde(
57    tag = "type",
58    rename_all = "camelCase",
59    rename_all_fields = "camelCase"
60)]
61#[ts(export, export_to = "transport/messages.ts")]
62pub enum RecordingStatus {
63    Off,
64    Recording,
65    Ready { actions: Vec<String> },
66    Failed { reason: RecordingFailure },
67}
68
69#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, TS)]
70#[serde(rename_all = "camelCase")]
71#[ts(export, export_to = "transport/messages.ts")]
72pub enum RecordingFailure {
73    NotAtPriority,
74    UnsupportedPrompt,
75    OpponentActed,
76    Empty,
77}
78
79#[derive(Debug, Clone, Serialize, Deserialize, TS)]
80#[serde(rename_all = "camelCase")]
81#[ts(export, export_to = "transport/messages.ts")]
82pub struct RepeatVoteDto {
83    pub vote_id: u32,
84    pub requested_by_player_id: String,
85    pub iterations: u32,
86    pub actions: Vec<String>,
87    pub awaiting_player_ids: Vec<String>,
88    pub status: RepeatVoteStatus,
89}
90
91#[derive(Debug, Clone, Serialize, Deserialize, TS)]
92#[serde(
93    tag = "type",
94    rename_all = "camelCase",
95    rename_all_fields = "camelCase"
96)]
97#[ts(export, export_to = "transport/messages.ts")]
98pub enum RepeatVoteStatus {
99    Pending,
100    Running { completed: u32 },
101    Declined { player_id: String },
102    Cancelled,
103    Applied,
104    Failed { iteration: u32 },
105}
106
107#[derive(Debug, Clone, Serialize, Deserialize, TS)]
108#[serde(
109    tag = "kind",
110    rename_all = "camelCase",
111    rename_all_fields = "camelCase"
112)]
113#[ts(export, export_to = "transport/messages.ts")]
114pub enum ClientToServerMessage {
115    Response {
116        prompt_id: u32,
117        action: PromptOutput,
118    },
119    Directive {
120        directive: DirectiveInput,
121    },
122}
123
124#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, TS)]
125#[serde(rename_all = "camelCase")]
126#[ts(export, export_to = "transport/messages.ts")]
127pub enum ProtocolErrorCode {
128    StalePrompt,
129    WrongPlayer,
130    WrongPromptType,
131    UnknownActionId,
132    CancelNotAllowed,
133    InvalidShape,
134}
135
136#[derive(Debug, Clone, Serialize, Deserialize, TS)]
137#[serde(rename_all = "camelCase", deny_unknown_fields)]
138#[ts(export, export_to = "transport/messages.ts")]
139pub struct ProtocolError {
140    pub code: ProtocolErrorCode,
141    pub message: String,
142    #[serde(default, skip_serializing_if = "Option::is_none")]
143    #[ts(optional)]
144    pub prompt_id: Option<u32>,
145}
146
147#[derive(Debug, Clone, Serialize, Deserialize, TS)]
148#[serde(rename_all = "camelCase", deny_unknown_fields)]
149#[ts(export, export_to = "transport/messages.ts")]
150pub struct AgentPrompt {
151    pub prompt_id: u32,
152    #[serde(default, skip_serializing_if = "String::is_empty")]
153    pub deciding_player_id: String,
154    #[serde(default, skip_serializing_if = "Option::is_none")]
155    #[ts(optional)]
156    pub source_card: Option<CardDto>,
157    #[serde(default, skip_serializing_if = "Option::is_none")]
158    #[ts(optional)]
159    pub source_ability_text: Option<String>,
160    pub input: PromptInput,
161}