Skip to main content

manabrew_protocol/game/
mod.rs

1use std::collections::{BTreeMap, HashMap};
2
3use serde::{Deserialize, Serialize};
4use ts_rs::TS;
5
6use crate::{prompts::common::TargetRef, TokenScript};
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, TS)]
9#[ts(export, export_to = "game/index.ts")]
10pub enum ManaColor {
11    #[serde(rename = "W")]
12    White,
13    #[serde(rename = "U")]
14    Blue,
15    #[serde(rename = "B")]
16    Black,
17    #[serde(rename = "R")]
18    Red,
19    #[serde(rename = "G")]
20    Green,
21    #[serde(rename = "C")]
22    Colorless,
23}
24
25#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
26#[serde(
27    tag = "kind",
28    rename_all = "camelCase",
29    rename_all_fields = "camelCase"
30)]
31#[ts(export, export_to = "game/index.ts")]
32pub enum CardChoiceDto {
33    Color { colors: Vec<ManaColor> },
34    Type { values: Vec<String> },
35    NamedCard { names: Vec<String> },
36    ChosenCard { count: usize },
37    Number { value: i32 },
38    Mode { value: String },
39    Player { player_id: String, name: String },
40}
41
42#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, TS)]
43#[serde(rename_all = "camelCase")]
44#[ts(export, export_to = "game/index.ts")]
45pub struct Mana {
46    pub color: ManaColor,
47    pub amount: i32,
48}
49
50#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, TS)]
51#[serde(rename_all = "camelCase")]
52#[ts(export, export_to = "game/index.ts")]
53pub enum PlayerCounterKind {
54    Poison,
55    Energy,
56    Experience,
57    Radiation,
58    Ticket,
59}
60
61#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, TS)]
62#[serde(rename_all = "camelCase")]
63#[ts(export, export_to = "game/index.ts")]
64pub enum ZoneKind {
65    Battlefield,
66    Hand,
67    Library,
68    Graveyard,
69    Exile,
70    Command,
71    Attractions,
72    Junkyard,
73    Sideboard,
74}
75
76#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize, TS)]
77#[serde(rename_all = "camelCase")]
78#[ts(export, export_to = "game/index.ts")]
79pub enum StepKind {
80    #[default]
81    Untap,
82    Upkeep,
83    Draw,
84    Main1,
85    CombatBegin,
86    CombatDeclareAttackers,
87    CombatDeclareBlockers,
88    CombatFirstStrikeDamage,
89    CombatDamage,
90    CombatEnd,
91    Main2,
92    EndOfTurn,
93    Cleanup,
94}
95
96#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize, TS)]
97#[serde(rename_all = "camelCase")]
98#[ts(export, export_to = "game/index.ts")]
99pub enum DayTime {
100    #[default]
101    Neither,
102    Day,
103    Night,
104}
105
106#[allow(clippy::large_enum_variant)]
107#[derive(Debug, Clone, Serialize, Deserialize, TS)]
108#[serde(
109    tag = "visibility",
110    rename_all = "camelCase",
111    rename_all_fields = "camelCase"
112)]
113#[ts(export, export_to = "game/index.ts")]
114pub enum CardView {
115    Visible(CardDto),
116    Hidden { id: String },
117}
118
119// One entry per (zone, owner) pair; battlefield cards are bucketed by controller.
120#[derive(Debug, Clone, Serialize, Deserialize, TS)]
121#[serde(rename_all = "camelCase")]
122#[ts(export, export_to = "game/index.ts")]
123pub struct ZoneDto {
124    pub zone: ZoneKind,
125    pub owner_id: String,
126    // Ordered top-first where order is public knowledge
127    // count can be > cards.len if hidden cards are present (library)
128    pub cards: Vec<CardView>,
129    pub count: usize,
130}
131
132#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
133#[serde(rename_all = "camelCase")]
134#[ts(export, export_to = "game/index.ts")]
135pub struct GameViewDto {
136    pub game_id: String,
137    pub turn: u32,
138    pub step: StepKind,
139    pub combat_assignments: Vec<CombatAssignmentDto>,
140    pub active_player_id: String,
141    pub priority_player_id: String,
142    pub players: Vec<PlayerDto>,
143    pub zones: Vec<ZoneDto>,
144    pub stack: Vec<StackObjectDto>,
145    pub game_over: bool,
146    pub winner_id: Option<String>,
147    pub monarch_id: Option<String>,
148    pub initiative_holder_id: Option<String>,
149    pub day_time: DayTime,
150    #[ts(optional)]
151    pub active_plane_names: Option<Vec<String>>,
152    #[serde(default)]
153    pub checkpoints: Vec<CheckpointDto>,
154    #[serde(default)]
155    pub restore_vote: Option<RestoreVoteDto>,
156    #[serde(default)]
157    pub snapshot_recording: bool,
158    #[serde(default, skip_serializing_if = "Option::is_none")]
159    #[ts(optional)]
160    pub decision_history: Option<DecisionHistoryDto>,
161    #[serde(default, skip_serializing_if = "Option::is_none")]
162    #[ts(optional)]
163    pub deciding_player_id: Option<String>,
164}
165
166#[derive(Debug, Clone, Serialize, Deserialize, TS)]
167#[serde(rename_all = "camelCase")]
168#[ts(export, export_to = "game/index.ts")]
169pub struct DecisionHistoryDto {
170    pub rewinding: bool,
171    pub unavailable_reason: Option<String>,
172}
173
174#[derive(Debug, Clone, Serialize, Deserialize, TS)]
175#[serde(rename_all = "camelCase")]
176#[ts(export, export_to = "game/index.ts")]
177pub struct RestoreVoteDto {
178    pub vote_id: u32,
179    pub checkpoint: CheckpointDto,
180    pub requested_by_player_id: String,
181    pub awaiting_player_ids: Vec<String>,
182    pub status: RestoreVoteStatus,
183}
184
185#[derive(Debug, Clone, Serialize, Deserialize, TS)]
186#[serde(
187    tag = "type",
188    rename_all = "camelCase",
189    rename_all_fields = "camelCase"
190)]
191#[ts(export, export_to = "game/index.ts")]
192pub enum RestoreVoteStatus {
193    Pending,
194    Approved,
195    Declined { player_id: String },
196    Unavailable,
197}
198
199#[derive(Debug, Clone, Serialize, Deserialize, TS)]
200#[serde(rename_all = "camelCase")]
201#[ts(export, export_to = "game/index.ts")]
202pub struct CheckpointDto {
203    pub checkpoint_id: u32,
204    pub turn: u32,
205    pub step: StepKind,
206    pub active_player_id: String,
207    #[serde(default, skip_serializing_if = "Option::is_none")]
208    #[ts(optional)]
209    pub decision: Option<String>,
210    #[serde(default, skip_serializing_if = "Option::is_none")]
211    #[ts(optional)]
212    pub deciding_player_id: Option<String>,
213    #[serde(default, skip_serializing_if = "Vec::is_empty")]
214    pub log: Vec<String>,
215}
216
217#[derive(Debug, Clone, Serialize, Deserialize, TS)]
218#[serde(rename_all = "camelCase")]
219#[ts(export, export_to = "game/index.ts")]
220pub struct CombatAssignmentDto {
221    pub blocker_id: String,
222    pub attacker_id: String,
223}
224
225#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize, TS)]
226#[serde(rename_all = "camelCase")]
227#[ts(export, export_to = "game/index.ts")]
228pub enum PlayerStatus {
229    #[default]
230    Playing,
231    Lost,
232    Conceded,
233}
234
235#[derive(Debug, Clone, Serialize, Deserialize, TS)]
236#[serde(rename_all = "camelCase")]
237#[ts(export, export_to = "game/index.ts")]
238pub struct DungeonStateDto {
239    pub name: String,
240    pub room: String,
241}
242
243#[derive(Debug, Clone, Serialize, Deserialize, TS)]
244#[serde(rename_all = "camelCase")]
245#[ts(export, export_to = "game/index.ts")]
246pub struct PlayerDto {
247    pub id: String,
248    pub name: String,
249    pub status: PlayerStatus,
250    pub is_human: bool,
251    pub life: i32,
252    #[serde(default)]
253    pub max_hand_size: i32,
254    #[serde(default)]
255    pub unlimited_hand_size: bool,
256    #[serde(default)]
257    pub lands_played_this_turn: i32,
258    #[serde(default)]
259    pub max_land_plays_per_turn: i32,
260    #[serde(default)]
261    pub unlimited_land_plays: bool,
262    #[serde(default)]
263    pub cards_drawn_this_turn: i32,
264    #[serde(default)]
265    pub damage_prevention: i32,
266    #[serde(default)]
267    pub is_extra_turn: bool,
268    #[serde(default)]
269    pub extra_turn_count: u32,
270    #[ts(optional)]
271    pub controlled_by: Option<String>,
272    #[serde(default)]
273    pub player_keywords: Vec<String>,
274    #[serde(default)]
275    #[ts(type = "Record<string, number>")]
276    pub commander_casts: HashMap<String, u32>,
277    #[ts(optional)]
278    pub dungeon_state: Option<DungeonStateDto>,
279    #[ts(optional)]
280    pub active_scheme_names: Option<Vec<String>>,
281    #[ts(optional)]
282    pub team_number: Option<i32>,
283    pub counters: BTreeMap<PlayerCounterKind, u32>,
284    pub mana_pool: BTreeMap<ManaColor, u32>,
285    #[ts(type = "Record<string, number>")]
286    pub commander_damage: HashMap<String, i32>,
287    pub has_city_blessing: bool,
288    pub has_enduring_story: bool,
289    pub ring_level: i32,
290    pub speed: i32,
291}
292
293#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
294#[serde(rename_all = "camelCase", default)]
295#[ts(export, export_to = "game/index.ts")]
296pub struct CardIdentity {
297    pub name: String,
298    pub set_code: String,
299    pub card_number: String,
300    pub is_token: bool,
301    #[serde(default, skip_serializing_if = "Option::is_none")]
302    #[ts(optional)]
303    pub token_script: Option<TokenScript>,
304}
305
306#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, TS)]
307#[serde(rename_all = "camelCase")]
308#[ts(export, export_to = "game/index.ts")]
309pub enum RoomDoor {
310    Left,
311    Right,
312}
313
314#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
315#[serde(rename_all = "camelCase", default)]
316#[ts(export, export_to = "game/index.ts")]
317pub struct ClassLevelDto {
318    pub level: i32,
319    pub oracle: String,
320    #[serde(default, skip_serializing_if = "Option::is_none")]
321    #[ts(optional)]
322    pub cost: Option<String>,
323}
324
325#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
326#[serde(rename_all = "camelCase", default)]
327#[ts(export, export_to = "game/index.ts")]
328pub struct SagaChapterDto {
329    pub chapters: Vec<i32>,
330    pub oracle: String,
331}
332
333#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
334#[serde(rename_all = "camelCase", default)]
335#[ts(export, export_to = "game/index.ts")]
336pub struct CardDto {
337    pub id: String,
338    pub identity: CardIdentity,
339    #[serde(
340        default,
341        deserialize_with = "crate::deck_dto::CardCosmetics::deserialize_optional",
342        skip_serializing_if = "Option::is_none"
343    )]
344    #[ts(optional)]
345    pub cosmetics: Option<crate::deck_dto::CardCosmetics>,
346    pub color: String,
347    pub mana_cost: String,
348    pub cmc: i32,
349    pub types: Vec<String>,
350    pub subtypes: Vec<String>,
351    pub supertypes: Vec<String>,
352    pub power: Option<String>,
353    pub toughness: Option<String>,
354    #[serde(skip_serializing_if = "Option::is_none")]
355    #[ts(optional)]
356    pub base_power: Option<i32>,
357    #[serde(skip_serializing_if = "Option::is_none")]
358    #[ts(optional)]
359    pub base_toughness: Option<i32>,
360    #[serde(default, skip_serializing_if = "Option::is_none")]
361    #[ts(optional)]
362    pub final_chapter: Option<i32>,
363    #[serde(default, skip_serializing_if = "Option::is_none")]
364    #[ts(optional)]
365    pub class_level: Option<i32>,
366    pub class_levels: Vec<ClassLevelDto>,
367    pub saga_chapters: Vec<SagaChapterDto>,
368    #[serde(default, skip_serializing_if = "Option::is_none")]
369    #[ts(optional)]
370    pub unlocked_doors: Option<Vec<RoomDoor>>,
371    #[serde(default, skip_serializing_if = "Option::is_none")]
372    #[ts(optional)]
373    pub is_solved: Option<bool>,
374    #[serde(default, skip_serializing_if = "Option::is_none")]
375    #[ts(optional)]
376    pub is_suspended: Option<bool>,
377    #[serde(default, skip_serializing_if = "Option::is_none")]
378    #[ts(optional)]
379    pub is_on_adventure: Option<bool>,
380    #[serde(default, skip_serializing_if = "Option::is_none")]
381    #[ts(optional)]
382    pub is_prototyped: Option<bool>,
383    pub text: String,
384    #[serde(default)]
385    pub choices: Vec<CardChoiceDto>,
386    pub controller_id: String,
387    pub owner_id: String,
388    pub tapped: bool,
389    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
390    pub is_crewed: bool,
391    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
392    pub is_attacking: bool,
393    #[serde(default, skip_serializing_if = "Option::is_none")]
394    #[ts(optional)]
395    pub attacking_player_id: Option<String>,
396    #[serde(default, skip_serializing_if = "Option::is_none")]
397    #[ts(optional)]
398    pub attack_target_id: Option<String>,
399    pub keywords: Vec<String>,
400    #[serde(default, skip_serializing_if = "Option::is_none")]
401    #[ts(optional)]
402    pub keyword_labels: Option<BTreeMap<String, String>>,
403    #[serde(default, skip_serializing_if = "Vec::is_empty")]
404    pub granted_keywords: Vec<String>,
405    // Keyed by the engine's canonical `CounterType` display form ("P1P1",
406    // "Loyalty", one-off counter names uppercase); both producers must match it.
407    #[ts(type = "Record<string, number>")]
408    pub counters: BTreeMap<String, u32>,
409    pub damage: i32,
410    pub summoning_sick: bool,
411    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
412    pub is_copy: bool,
413    pub is_double_faced: bool,
414    #[serde(default, skip_serializing_if = "Option::is_none")]
415    #[ts(optional)]
416    pub face_index: Option<u8>,
417    pub is_transformed: bool,
418    pub is_face_down: bool,
419    pub is_bestowed: bool,
420    pub phased_out: bool,
421    pub exerted: bool,
422    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
423    pub is_ring_bearer: bool,
424    #[serde(skip_serializing_if = "Option::is_none")]
425    #[ts(optional)]
426    pub attached_to: Option<String>,
427    #[serde(default, skip_serializing_if = "Vec::is_empty")]
428    pub attachment_ids: Vec<String>,
429    // Mutate pile: the card ids merged under this top card.
430    #[serde(default, skip_serializing_if = "Vec::is_empty")]
431    pub merged_card_ids: Vec<String>,
432    #[serde(skip_serializing_if = "Option::is_none")]
433    #[ts(optional)]
434    pub flashback_cost: Option<String>,
435    #[serde(skip_serializing_if = "Option::is_none")]
436    #[ts(optional)]
437    pub kicker_cost: Option<String>,
438    #[serde(skip_serializing_if = "Option::is_none")]
439    #[ts(optional)]
440    pub effective_mana_cost: Option<String>,
441    #[serde(default, skip_serializing_if = "Option::is_none")]
442    #[ts(optional)]
443    pub commander_tax: Option<i32>,
444    #[serde(skip_serializing_if = "Option::is_none")]
445    #[ts(optional)]
446    pub madness_cost: Option<String>,
447    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
448    pub is_madness_exiled: bool,
449    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
450    pub is_plotted: bool,
451    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
452    pub is_warp_exiled: bool,
453    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
454    pub foil: bool,
455    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
456    pub would_die_in_combat: bool,
457    #[serde(default, skip_serializing_if = "Option::is_none")]
458    #[ts(optional)]
459    pub zone: Option<ZoneKind>,
460}
461
462#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
463#[serde(rename_all = "camelCase", default)]
464#[ts(export, export_to = "game/index.ts")]
465pub struct StackObjectDto {
466    pub id: String,
467    pub source_id: String,
468    pub controller_id: String,
469    pub owner_id: String,
470    #[serde(skip_serializing_if = "Option::is_none")]
471    #[ts(optional)]
472    pub identity: Option<CardIdentity>,
473    pub text: String,
474    #[serde(skip_serializing_if = "Option::is_none")]
475    #[ts(optional)]
476    pub source_ability_text: Option<String>,
477    pub is_permanent_spell: bool,
478    pub is_casting: bool,
479    #[serde(default, skip_serializing_if = "Option::is_none")]
480    #[ts(optional)]
481    pub source_card: Option<CardDto>,
482    pub targets: Vec<TargetRef>,
483}
484
485#[derive(
486    Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize, TS, strum_macros::Display,
487)]
488#[serde(rename_all = "camelCase")]
489#[ts(export, export_to = "game/index.ts")]
490pub enum TargetingIntent {
491    #[default]
492    Damage,
493    Destroy,
494    Sacrifice,
495    Exile,
496    Bounce,
497    Mill,
498    Discard,
499    Counter,
500    Tap,
501    Untap,
502    Copy,
503    Buff,
504    Debuff,
505    Heal,
506    LoseLife,
507    Reveal,
508    Draw,
509    Fetch,
510    GainControl,
511    Fight,
512    Attach,
513    Attack,
514    Block,
515    Hostile,
516    Friendly,
517}
518
519#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
520#[serde(rename_all = "camelCase")]
521#[ts(export, export_to = "game/index.ts")]
522pub struct PlaymatSettings {
523    #[serde(default, skip_serializing_if = "Option::is_none")]
524    #[ts(optional)]
525    pub opacity: Option<f32>,
526    #[serde(default, skip_serializing_if = "Option::is_none")]
527    #[ts(optional)]
528    pub texture: Option<f32>,
529    #[serde(default, skip_serializing_if = "Option::is_none")]
530    #[ts(optional)]
531    pub border_width: Option<f32>,
532    #[serde(default, skip_serializing_if = "Option::is_none")]
533    #[ts(optional)]
534    pub border_color: Option<String>,
535    #[serde(default, skip_serializing_if = "Option::is_none")]
536    #[ts(optional)]
537    pub fit: Option<String>,
538    #[serde(default, skip_serializing_if = "Option::is_none")]
539    #[ts(optional)]
540    pub offset_x: Option<f32>,
541    #[serde(default, skip_serializing_if = "Option::is_none")]
542    #[ts(optional)]
543    pub offset_y: Option<f32>,
544    #[serde(default, skip_serializing_if = "Option::is_none")]
545    #[ts(optional)]
546    pub zoom: Option<f32>,
547    #[serde(default, skip_serializing_if = "Option::is_none")]
548    #[ts(optional)]
549    pub blur: Option<f32>,
550    #[serde(default, skip_serializing_if = "Option::is_none")]
551    #[ts(optional)]
552    pub brightness: Option<f32>,
553    #[serde(default, skip_serializing_if = "Option::is_none")]
554    #[ts(optional)]
555    pub color: Option<String>,
556}
557
558#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, TS)]
559#[ts(export, export_to = "game/index.ts")]
560pub enum GameFormat {
561    Any,
562    Standard,
563    Pioneer,
564    Modern,
565    Legacy,
566    Vintage,
567    Pauper,
568    Premodern,
569    Commander,
570    DuelCommander,
571    PauperCommander,
572    Brawl,
573    Oathbreaker,
574    Draft,
575    Sealed,
576}
577
578#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Default, TS)]
579#[ts(export, export_to = "game/index.ts")]
580pub enum EngineKind {
581    #[default]
582    Manabrew,
583    Forge,
584    Ironsmith,
585}