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    Error(ProtocolError),
15}
16
17#[derive(Debug, Clone, Serialize, Deserialize, TS)]
18#[serde(rename_all = "camelCase", deny_unknown_fields)]
19#[ts(export, export_to = "transport/messages.ts")]
20pub struct StateUpdate {
21    pub game_view: GameViewDto,
22}
23
24#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, TS)]
25#[serde(
26    tag = "type",
27    rename_all = "camelCase",
28    rename_all_fields = "camelCase"
29)]
30#[ts(export, export_to = "transport/messages.ts")]
31pub enum DirectiveInput {
32    Concede,
33    RequestRestore { checkpoint_id: u32 },
34    RestoreVote { vote_id: u32, accept: bool },
35    SetSnapshotRecording { enabled: bool },
36}
37
38#[derive(Debug, Clone, Serialize, Deserialize, TS)]
39#[serde(
40    tag = "kind",
41    rename_all = "camelCase",
42    rename_all_fields = "camelCase"
43)]
44#[ts(export, export_to = "transport/messages.ts")]
45pub enum ClientToServerMessage {
46    Response {
47        prompt_id: u32,
48        action: PromptOutput,
49    },
50    Directive {
51        directive: DirectiveInput,
52    },
53}
54
55#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, TS)]
56#[serde(rename_all = "camelCase")]
57#[ts(export, export_to = "transport/messages.ts")]
58pub enum ProtocolErrorCode {
59    StalePrompt,
60    WrongPlayer,
61    WrongPromptType,
62    UnknownActionId,
63    CancelNotAllowed,
64    InvalidShape,
65}
66
67#[derive(Debug, Clone, Serialize, Deserialize, TS)]
68#[serde(rename_all = "camelCase", deny_unknown_fields)]
69#[ts(export, export_to = "transport/messages.ts")]
70pub struct ProtocolError {
71    pub code: ProtocolErrorCode,
72    pub message: String,
73    #[serde(default, skip_serializing_if = "Option::is_none")]
74    #[ts(optional)]
75    pub prompt_id: Option<u32>,
76}
77
78#[derive(Debug, Clone, Serialize, Deserialize, TS)]
79#[serde(rename_all = "camelCase", deny_unknown_fields)]
80#[ts(export, export_to = "transport/messages.ts")]
81pub struct AgentPrompt {
82    pub prompt_id: u32,
83    #[serde(default, skip_serializing_if = "String::is_empty")]
84    pub deciding_player_id: String,
85    #[serde(default, skip_serializing_if = "Option::is_none")]
86    #[ts(optional)]
87    pub source_card: Option<CardDto>,
88    #[serde(default, skip_serializing_if = "Option::is_none")]
89    #[ts(optional)]
90    pub source_ability_text: Option<String>,
91    pub input: PromptInput,
92}