Skip to main content

manabrew_protocol/transport/
mod.rs

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