Skip to main content

manabrew_relay_protocol/
lib.rs

1//! Relay wire protocol: the session handshake, lobby management and game
2//! transport envelopes exchanged between clients and `manabrew-server`.
3//! Engine and UI DTOs live in `manabrew-protocol`.
4pub mod identity_token;
5pub mod state_delta;
6
7pub use manabrew_protocol::deck_dto::Deck;
8pub use manabrew_protocol::game::{EngineKind, GameFormat};
9pub use manabrew_protocol::telemetry::EnginePlayStats;
10use serde::{Deserialize, Serialize};
11use ts_rs::TS;
12
13// The wire-compat number IS the crate major: a breaking wire change must ship
14// as a breaking (major) release of this crate, never as a separate constant.
15pub const PROTOCOL_VERSION: u32 = major_of(env!("CARGO_PKG_VERSION_MAJOR"));
16
17const fn major_of(major: &str) -> u32 {
18    let bytes = major.as_bytes();
19    let mut value = 0u32;
20    let mut i = 0;
21    while i < bytes.len() {
22        value = value * 10 + (bytes[i] - b'0') as u32;
23        i += 1;
24    }
25    value
26}
27
28#[derive(Debug, Clone, Serialize, Deserialize, TS)]
29#[ts(export, export_to = "lobby/index.ts")]
30pub struct PlayerDeckInfo {
31    pub username: String,
32    pub deck_name: String,
33    pub deck: Deck,
34    #[serde(default, skip_serializing_if = "Option::is_none")]
35    #[ts(optional)]
36    pub published_deck_id: Option<String>,
37    #[serde(skip_serializing_if = "Option::is_none")]
38    #[ts(optional)]
39    pub commander_name: Option<String>,
40    #[serde(default, skip_serializing_if = "Option::is_none")]
41    #[ts(optional)]
42    pub avatar_url: Option<String>,
43}
44
45#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
46pub struct IdentityProof {
47    #[serde(default, skip_serializing_if = "Option::is_none")]
48    pub token: Option<String>,
49    #[serde(default, skip_serializing_if = "Option::is_none")]
50    pub device: Option<String>,
51}
52
53#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize, TS)]
54#[serde(rename_all = "snake_case")]
55#[ts(export, export_to = "lobby/index.ts")]
56pub enum ClientPlatform {
57    Web,
58    Pwa,
59    Desktop,
60    Mobile,
61    #[default]
62    Unknown,
63}
64
65/// Why the client is or is not running the engine in its own browser. The
66/// relay never acts on it; it exists so the hosted-node population can be told
67/// apart from the browsers that chose not to use it.
68#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize, TS)]
69#[serde(rename_all = "snake_case")]
70#[ts(export, export_to = "lobby/index.ts")]
71pub enum EngineGate {
72    InBrowser,
73    Ios,
74    Isolation,
75    TrialTimeout,
76    TrialFailed,
77    Disabled,
78    #[default]
79    Unknown,
80}
81
82impl EngineGate {
83    pub const fn as_str(self) -> &'static str {
84        match self {
85            Self::InBrowser => "in_browser",
86            Self::Ios => "ios",
87            Self::Isolation => "isolation",
88            Self::TrialTimeout => "trial_timeout",
89            Self::TrialFailed => "trial_failed",
90            Self::Disabled => "disabled",
91            Self::Unknown => "unknown",
92        }
93    }
94}
95
96impl ClientPlatform {
97    pub const fn as_str(self) -> &'static str {
98        match self {
99            Self::Web => "web",
100            Self::Pwa => "pwa",
101            Self::Desktop => "desktop",
102            Self::Mobile => "mobile",
103            Self::Unknown => "unknown",
104        }
105    }
106}
107
108/// A peer's addressing information for the direct data plane.
109#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
110pub struct TransportEndpoint {
111    pub endpoint_id: String,
112    #[serde(default, skip_serializing_if = "Option::is_none")]
113    pub relay_url: Option<String>,
114    #[serde(default, skip_serializing_if = "Vec::is_empty")]
115    pub direct_addrs: Vec<String>,
116    /// Data planes this peer speaks, most preferred first. Seats follow the host's.
117    #[serde(default, skip_serializing_if = "Vec::is_empty")]
118    pub kinds: Vec<String>,
119}
120
121impl TransportEndpoint {
122    /// Whether this peer advertises a plane.
123    pub fn speaks(&self, kind: &str) -> bool {
124        self.kinds.iter().any(|k| k == kind)
125    }
126}
127
128/// An ICE server, shaped like the `RTCIceServer` dictionary.
129#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
130pub struct IceServer {
131    pub urls: Vec<String>,
132    #[serde(default, skip_serializing_if = "Option::is_none")]
133    pub username: Option<String>,
134    #[serde(default, skip_serializing_if = "Option::is_none")]
135    pub credential: Option<String>,
136}
137
138/// How one seat's game traffic travelled, reported by the room's engine host.
139#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
140pub struct SeatTransportReport {
141    pub username: String,
142    /// One of the `TRANSPORT_*` constants.
143    pub transport: String,
144}
145
146/// One end's account of one direct-plane attempt. Every field is a client claim.
147#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
148pub struct PlaneQualityReport {
149    /// The other end's relay-attested username.
150    pub peer: String,
151    /// One of [`PLANE_OUTCOMES`]; anything else is dropped.
152    pub outcome: String,
153    /// The plane attempted, a `TRANSPORT_*` constant.
154    pub plane: String,
155    /// [`PLANE_PHASE_SETTLED`] once per attempt, then [`PLANE_PHASE_MEASURED`].
156    pub phase: String,
157    /// First offer to open channel.
158    #[serde(default, skip_serializing_if = "Option::is_none")]
159    pub connect_ms: Option<u32>,
160    /// Median round trip measured on the channel itself.
161    #[serde(default, skip_serializing_if = "Option::is_none")]
162    pub rtt_ms: Option<u32>,
163    /// The same session's relay round trip, sampled at the same time.
164    #[serde(default, skip_serializing_if = "Option::is_none")]
165    pub relay_rtt_ms: Option<u32>,
166    /// The winning ICE pair as `local/remote`.
167    #[serde(default, skip_serializing_if = "Option::is_none")]
168    pub candidate_pair: Option<String>,
169}
170
171pub const PLANE_PHASE_SETTLED: &str = "settled";
172pub const PLANE_PHASE_MEASURED: &str = "measured";
173
174pub const PLANE_OUTCOME_CONNECTED: &str = "connected";
175pub const PLANE_OUTCOME_FAILED: &str = "failed";
176pub const PLANE_OUTCOME_TIMEOUT: &str = "timeout";
177
178/// Outcomes the relay records; anything else is dropped.
179pub const PLANE_OUTCOMES: &[&str] = &[
180    PLANE_OUTCOME_CONNECTED,
181    PLANE_OUTCOME_FAILED,
182    PLANE_OUTCOME_TIMEOUT,
183];
184
185pub const ACQUISITION_SOURCES: &[&str] = &[
186    "direct", "landing", "reddit", "discord", "search", "ai", "youtube", "social", "github",
187    "other",
188];
189
190/// Longest `candidate_pair` the relay records.
191pub const MAX_CANDIDATE_PAIR_BYTES: usize = 64;
192
193/// Longest duration the relay records; ten minutes.
194pub const MAX_PLANE_MS: u32 = 600_000;
195
196/// A browser pair on an `RTCDataChannel`.
197pub const TRANSPORT_WEBRTC: &str = "webrtc";
198
199/// Names for [`TransportEndpoint::kinds`].
200pub const TRANSPORT_KIND_WEBRTC: &str = "webrtc";
201
202/// One room member's endpoint. `username` is relay-attested, never client supplied.
203#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
204pub struct TransportMember {
205    pub username: String,
206    pub endpoint: TransportEndpoint,
207    #[serde(default)]
208    pub host: bool,
209}
210
211#[derive(Debug, Clone, Serialize, Deserialize)]
212#[serde(tag = "type")]
213#[allow(clippy::large_enum_variant)]
214pub enum ClientMessage {
215    Authenticate {
216        /// Legacy name field, read only when `identity` carries no token.
217        /// The session name is the `handle` claim of `identity.token`
218        /// ([`identity_token`]); remove this field in the next major.
219        #[serde(default)]
220        username: String,
221        password: String,
222        #[serde(default)]
223        service: bool,
224        #[serde(default, skip_serializing_if = "Option::is_none")]
225        identity: Option<IdentityProof>,
226        #[serde(default)]
227        client_platform: ClientPlatform,
228        /// The client's app version (`APP_VERSION`), when it sends one. Absent
229        /// means a build older than the one that started reporting it, which is
230        /// how the relay decides what wire features that seat can handle.
231        #[serde(default, skip_serializing_if = "Option::is_none")]
232        client_version: Option<String>,
233        #[serde(default)]
234        engine_gate: EngineGate,
235        // One of `ACQUISITION_SOURCES`; the relay records anything else as `other`.
236        #[serde(default, skip_serializing_if = "Option::is_none")]
237        acquisition_source: Option<String>,
238    },
239
240    Ping,
241
242    ListRooms,
243
244    ListPlayers,
245
246    /// The session entered or left a game running on its own machine. `None`
247    /// clears it. Such a game opens no room and sends no state, so the relay
248    /// has no other way to know it is happening.
249    SetLocalGame {
250        #[serde(default)]
251        kind: Option<LocalGameKind>,
252    },
253
254    CreateRoom {
255        room_name: String,
256        max_players: u8,
257        format: GameFormat,
258        #[serde(default)]
259        protocol_version: u32,
260        #[serde(default)]
261        hosted: bool,
262        #[serde(default)]
263        engine: EngineKind,
264        #[serde(default)]
265        draft_config: Option<DraftConfig>,
266        #[serde(default)]
267        sealed_config: Option<SealedConfig>,
268        #[serde(default)]
269        official_key: Option<String>,
270        #[serde(default)]
271        password: Option<String>,
272        #[serde(default)]
273        reconnect_timeout_s: Option<u32>,
274        #[serde(default)]
275        table_style: Option<String>,
276    },
277
278    JoinRoom {
279        room_id: String,
280        #[serde(default, skip_serializing_if = "Option::is_none")]
281        seat_index: Option<u8>,
282        #[serde(default)]
283        observe: bool,
284        #[serde(default)]
285        as_bot: bool,
286        #[serde(default)]
287        password: Option<String>,
288    },
289
290    ResumeRoom(ResumeRoomRequest),
291
292    LeaveRoom,
293
294    SetReady {
295        ready: bool,
296    },
297
298    SetDeckSelection {
299        deck_name: String,
300        deck: Deck,
301        #[serde(default)]
302        published_deck_id: Option<String>,
303        commander_name: Option<String>,
304        #[serde(default)]
305        avatar_url: Option<String>,
306    },
307
308    SetFormat {
309        format: GameFormat,
310    },
311
312    SetAllowSpectators {
313        allow: bool,
314    },
315
316    SetMaxPlayers {
317        max_players: u8,
318    },
319
320    StartGame {
321        #[serde(default)]
322        format: Option<GameFormat>,
323    },
324
325    EndGame {
326        game_id: String,
327    },
328
329    FinishGame {
330        game_id: String,
331    },
332
333    SetRematchIntent {
334        game_id: String,
335        intent: RematchIntent,
336    },
337
338    ReturnToLobby {
339        game_id: String,
340    },
341
342    ReportEngineStats {
343        #[serde(default, skip_serializing_if = "Option::is_none")]
344        game_id: Option<String>,
345        stats: EnginePlayStats,
346    },
347
348    /// How the game ended, from the seat that ran the engine. The relay
349    /// stopped reading it out of the state stream, which need not pass
350    /// through it any more.
351    ReportGameOutcome {
352        game_id: String,
353        outcome: GameOutcomeReport,
354    },
355
356    RequestResync,
357
358    BroadcastState {
359        state: serde_json::Value,
360        #[serde(default, skip_serializing_if = "Option::is_none")]
361        /// A null value will broadcast to the whole room
362        target_player: Option<String>,
363    },
364
365    TurnChange {
366        new_active_player: String,
367        turn_number: u32,
368    },
369    /// Which transport each seat's traffic took for this game. Analytics only.
370    ReportTransport {
371        game_id: String,
372        seats: Vec<SeatTransportReport>,
373    },
374
375    /// Publishes, or with `None` withdraws, this session's data-plane endpoint.
376    AnnounceTransport {
377        #[serde(default)]
378        endpoint: Option<TransportEndpoint>,
379    },
380
381    /// An opaque signalling blob for a room member, named by username.
382    SignalPeer {
383        to: String,
384        payload: serde_json::Value,
385    },
386
387    /// How one direct-plane attempt turned out, sent by the seat. Analytics only.
388    ReportPlaneQuality {
389        report: PlaneQualityReport,
390    },
391
392    SendChat {
393        scope: ChatScope,
394        text: String,
395    },
396
397    InviteToRoom {
398        username: String,
399    },
400}
401
402#[derive(Debug, Clone, Serialize, Deserialize)]
403#[serde(tag = "type")]
404pub enum ServerMessage {
405    Pong,
406
407    AuthResult {
408        success: bool,
409        player_id: Option<String>,
410        reconnected: Option<bool>,
411        error: Option<String>,
412        /// Wire features this relay understands, so a newer client can stay
413        /// quiet instead of sending a message an older one would reject with
414        /// a parse error. Absent from relays built before the list existed.
415        #[serde(default, skip_serializing_if = "Vec::is_empty")]
416        features: Vec<String>,
417        /// Where this relay serves card art, when it serves any. A self-hosted
418        /// box holding the images is the reason to run one, and the client
419        /// cannot guess the port. Absent means fall back to the CDN.
420        #[serde(default, skip_serializing_if = "Option::is_none")]
421        art_base_url: Option<String>,
422    },
423
424    SessionTakenOver,
425
426    RoomList {
427        rooms: Vec<RoomInfo>,
428    },
429
430    PlayerList {
431        players: Vec<PlayerInfo>,
432    },
433
434    RoomCreated {
435        room_id: String,
436        room_name: String,
437        room: RoomInfo,
438        #[serde(default)]
439        resume_token: Option<String>,
440    },
441
442    RoomResumed {
443        room: RoomInfo,
444    },
445
446    PlayerJoined {
447        room_id: String,
448        username: String,
449    },
450
451    PlayerLeft {
452        room_id: String,
453        username: String,
454    },
455
456    PlayerConnected {
457        username: String,
458    },
459
460    PlayerDisconnected {
461        username: String,
462    },
463
464    ReadyStateChanged {
465        username: String,
466        ready: bool,
467    },
468
469    RoomUpdate {
470        room: RoomInfo,
471    },
472
473    GameStarted {
474        room_id: String,
475        game_id: String,
476        player_order: Vec<String>,
477        player_decks: Vec<PlayerDeckInfo>,
478        starting_life: i32,
479    },
480
481    StateUpdate {
482        from_player: String,
483        state: serde_json::Value,
484    },
485
486    TurnChanged {
487        from_player: String,
488        new_active_player: String,
489        turn_number: u32,
490    },
491
492    GameAborted {
493        room_id: String,
494    },
495
496    Error {
497        code: String,
498        message: String,
499    },
500
501    ServerShuttingDown {
502        reconnect_in_s: u32,
503    },
504    /// The room's data-plane roster. Sent only to room members.
505    RoomTransport {
506        room_id: String,
507        /// STUN, and TURN where configured, for the WebRTC plane.
508        #[serde(default, skip_serializing_if = "Vec::is_empty")]
509        ice_servers: Vec<IceServer>,
510        #[serde(default, skip_serializing_if = "Option::is_none")]
511        host: Option<TransportMember>,
512        members: Vec<TransportMember>,
513    },
514
515    /// A signalling blob from a room member. `from` is relay-attested.
516    PeerSignal {
517        from: String,
518        payload: serde_json::Value,
519    },
520
521    ChatMessage(ChatMessage),
522
523    ChatHistory {
524        scope: ChatScope,
525        #[serde(default, skip_serializing_if = "Option::is_none")]
526        room_id: Option<String>,
527        messages: Vec<ChatMessage>,
528    },
529
530    RoomInvite {
531        from: String,
532        room: RoomInfo,
533        #[serde(default, skip_serializing_if = "Option::is_none")]
534        password: Option<String>,
535    },
536}
537
538#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
539pub enum ChatScope {
540    Lobby,
541    Room,
542}
543
544#[derive(Debug, Clone, Serialize, Deserialize)]
545pub struct ChatMessage {
546    pub scope: ChatScope,
547    #[serde(default, skip_serializing_if = "Option::is_none")]
548    pub room_id: Option<String>,
549    pub from: String,
550    #[serde(default, skip_serializing_if = "Option::is_none")]
551    pub avatar_url: Option<String>,
552    #[serde(default, skip_serializing_if = "Option::is_none")]
553    pub qualification: Option<String>,
554    pub text: String,
555    pub sent_at_ms: u64,
556    #[serde(default, skip_serializing_if = "Option::is_none")]
557    pub seal: Option<String>,
558}
559
560pub const CHAT_MESSAGE_MAX_CHARS: usize = 500;
561pub const CHAT_HISTORY_MAX_MESSAGES: usize = 100;
562pub const CHAT_HISTORY_MAX_AGE_MS: u64 = 24 * 60 * 60 * 1000;
563pub const CHAT_MIN_INTERVAL_MS: u64 = 400;
564
565#[derive(Debug, Clone, Serialize, Deserialize, TS)]
566#[ts(export, export_to = "lobby/index.ts")]
567pub struct ResumeRoomRequest {
568    pub room_id: String,
569    pub resume_token: String,
570    pub room_name: String,
571    pub max_players: u8,
572    pub format: GameFormat,
573    #[serde(default)]
574    pub hosted: bool,
575    #[serde(default, skip_serializing_if = "Option::is_none")]
576    #[ts(optional)]
577    pub allow_spectators: Option<bool>,
578    #[serde(default)]
579    pub engine: EngineKind,
580    #[serde(default, skip_serializing_if = "Option::is_none")]
581    #[ts(optional)]
582    pub official_key: Option<String>,
583    #[serde(default, skip_serializing_if = "Option::is_none")]
584    #[ts(optional)]
585    pub password: Option<String>,
586    #[serde(default, skip_serializing_if = "Option::is_none")]
587    #[ts(optional)]
588    pub reconnect_timeout_s: Option<u32>,
589    #[serde(default, skip_serializing_if = "Option::is_none")]
590    #[ts(optional)]
591    pub table_style: Option<String>,
592    #[serde(default, skip_serializing_if = "Option::is_none")]
593    #[ts(optional)]
594    pub draft_config: Option<DraftConfig>,
595    #[serde(default, skip_serializing_if = "Option::is_none")]
596    #[ts(optional)]
597    pub sealed_config: Option<SealedConfig>,
598    pub player_order: Vec<String>,
599    #[serde(default, skip_serializing_if = "Option::is_none")]
600    #[ts(optional)]
601    pub seat_indices: Option<Vec<u8>>,
602    pub player_decks: Vec<PlayerDeckInfo>,
603    pub starting_life: i32,
604    #[serde(default)]
605    pub bot_players: Vec<String>,
606    pub game_id: String,
607}
608
609#[derive(Debug, Clone, Serialize, Deserialize)]
610pub struct RoomInfo {
611    pub room_id: String,
612    pub room_name: String,
613    pub host: String,
614    #[serde(default)]
615    pub protocol_version: u32,
616    #[serde(default)]
617    pub hosted: bool,
618    #[serde(default)]
619    pub official: bool,
620    #[serde(default)]
621    pub password_protected: bool,
622    #[serde(default)]
623    pub allow_spectators: bool,
624    #[serde(default)]
625    pub can_spectate: bool,
626    #[serde(default)]
627    pub spectator_count: usize,
628    pub players: Vec<RoomPlayerInfo>,
629    pub max_players: u8,
630    pub format: GameFormat,
631    pub status: RoomStatus,
632    #[serde(default)]
633    pub engine: EngineKind,
634    #[serde(default = "default_reconnect_timeout_s")]
635    pub reconnect_timeout_s: u32,
636    #[serde(default, skip_serializing_if = "Option::is_none")]
637    pub table_style: Option<String>,
638    #[serde(default, skip_serializing_if = "Option::is_none")]
639    pub draft_config: Option<DraftConfig>,
640    #[serde(default, skip_serializing_if = "Option::is_none")]
641    pub sealed_config: Option<SealedConfig>,
642    #[serde(default, skip_serializing_if = "Option::is_none")]
643    pub rematch: Option<RematchInfo>,
644}
645
646pub const DEFAULT_RECONNECT_TIMEOUT_S: u32 = 60;
647
648fn default_reconnect_timeout_s() -> u32 {
649    DEFAULT_RECONNECT_TIMEOUT_S
650}
651
652#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, TS)]
653#[ts(export, export_to = "lobby/index.ts")]
654pub struct SealedConfig {
655    #[serde(default, skip_serializing_if = "Option::is_none")]
656    #[ts(optional)]
657    pub set_code: Option<String>,
658    #[serde(default, skip_serializing_if = "Option::is_none")]
659    #[ts(optional)]
660    pub cube_id: Option<String>,
661    #[serde(default, skip_serializing_if = "Option::is_none")]
662    #[ts(optional)]
663    pub cube_name: Option<String>,
664    #[serde(default)]
665    pub singleton: bool,
666    pub num_boosters: u8,
667    #[serde(default, skip_serializing_if = "Option::is_none")]
668    #[ts(optional, type = "number")]
669    pub base_seed: Option<u64>,
670}
671
672#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, TS)]
673#[ts(export, export_to = "lobby/index.ts")]
674pub struct DraftConfig {
675    #[serde(default, skip_serializing_if = "Option::is_none")]
676    #[ts(optional)]
677    pub set_code: Option<String>,
678    #[serde(default, skip_serializing_if = "Option::is_none")]
679    #[ts(optional)]
680    pub cube_id: Option<String>,
681    #[serde(default, skip_serializing_if = "Option::is_none")]
682    #[ts(optional)]
683    pub cube_name: Option<String>,
684    pub rounds: u8,
685    pub picks_per_pass: u8,
686    #[serde(default, skip_serializing_if = "Option::is_none")]
687    #[ts(optional)]
688    pub pick_seconds: Option<u16>,
689    #[serde(default, skip_serializing_if = "Option::is_none")]
690    #[ts(optional, type = "number")]
691    pub seed: Option<u64>,
692    pub fill_with_bots: bool,
693}
694
695#[derive(Debug, Clone, Serialize, Deserialize)]
696pub struct RoomPlayerInfo {
697    pub username: String,
698    #[serde(default, skip_serializing_if = "Option::is_none")]
699    pub seat_index: Option<u8>,
700    pub ready: bool,
701    pub connected: bool,
702    #[serde(default)]
703    pub is_bot: bool,
704    #[serde(skip_serializing_if = "Option::is_none")]
705    pub selected_deck_name: Option<String>,
706}
707
708#[derive(Debug, Clone, Serialize, Deserialize)]
709pub struct PlayerInfo {
710    pub username: String,
711    pub player_id: String,
712    pub connected: bool,
713    #[serde(default)]
714    pub verified: bool,
715    #[serde(default, skip_serializing_if = "Option::is_none")]
716    pub qualification: Option<String>,
717    #[serde(default, skip_serializing_if = "Option::is_none")]
718    pub avatar_url: Option<String>,
719    #[serde(skip_serializing_if = "Option::is_none")]
720    pub room_id: Option<String>,
721    /// In a game on their own machine rather than in a relay room. Never set
722    /// at the same time as `room_id`.
723    #[serde(default, skip_serializing_if = "Option::is_none")]
724    pub local_game: Option<LocalGameKind>,
725    /// Opaque to clients: the session's IP sealed together with its handle,
726    /// which only the hub can open. Carried verbatim into chat reports.
727    #[serde(default, skip_serializing_if = "Option::is_none")]
728    pub seal: Option<String>,
729}
730
731/// Names [`ClientMessage::SetLocalGame`] in `AuthResult::features`.
732pub const FEATURE_LOCAL_GAME: &str = "local_game";
733/// Names [`ClientMessage::SendChat`] in `AuthResult::features`.
734pub const FEATURE_CHAT: &str = "chat";
735/// Names [`ClientMessage::InviteToRoom`] in `AuthResult::features`.
736pub const FEATURE_ROOM_INVITES: &str = "room_invites";
737/// Names [`ClientMessage::ReportGameOutcome`] in `AuthResult::features`.
738pub const FEATURE_GAME_OUTCOME: &str = "game_outcome";
739pub const FEATURE_LIMITED_SESSIONS: &str = "limited_sessions";
740pub const FEATURE_LIMITED_DRAFT_CLOCKS: &str = "limited_draft_clocks";
741pub const FEATURE_LIMITED_SESSION_RECOVERY: &str = "limited_session_recovery";
742pub const FEATURE_GAME_REMATCH: &str = "game_rematch";
743pub const FEATURE_ROOM_SEATS: &str = "room_seats";
744
745/// Names [`ClientMessage::AnnounceTransport`] in `AuthResult::features`.
746pub const FEATURE_ROOM_TRANSPORT: &str = "room_transport";
747
748/// Names [`ClientMessage::SignalPeer`] in `AuthResult::features`.
749pub const FEATURE_PEER_SIGNAL: &str = "peer_signal";
750
751/// Names [`ClientMessage::ReportPlaneQuality`] in `AuthResult::features`.
752pub const FEATURE_PLANE_QUALITY: &str = "plane_quality";
753
754pub const FEATURE_CARD_COSMETICS: &str = "card_cosmetics";
755pub const FEATURE_CARD_COSMETICS_V2: &str = "card_cosmetics_v2";
756pub const FEATURE_CARD_COSMETICS_V3: &str = "card_cosmetics_v3";
757
758pub const FEATURE_SPECTATORS: &str = "spectators";
759
760pub const FEATURES: &[&str] = &[
761    FEATURE_SPECTATORS,
762    FEATURE_LOCAL_GAME,
763    FEATURE_ROOM_TRANSPORT,
764    FEATURE_PEER_SIGNAL,
765    FEATURE_PLANE_QUALITY,
766    FEATURE_CHAT,
767    FEATURE_ROOM_INVITES,
768    FEATURE_GAME_OUTCOME,
769    FEATURE_GAME_REMATCH,
770    FEATURE_ROOM_SEATS,
771    FEATURE_CARD_COSMETICS,
772    FEATURE_CARD_COSMETICS_V2,
773    FEATURE_CARD_COSMETICS_V3,
774    FEATURE_LIMITED_SESSIONS,
775    FEATURE_LIMITED_DRAFT_CLOCKS,
776    FEATURE_LIMITED_SESSION_RECOVERY,
777];
778
779/// Largest signalling blob the relay forwards.
780pub const MAX_SIGNAL_BYTES: usize = 16 * 1024;
781
782/// Longest `fatal_message` the relay keeps.
783pub const MAX_FATAL_MESSAGE_CHARS: usize = 500;
784
785/// Engine slots (`player-N`), never usernames: the relay holds the seat map
786/// and does not take a name from a client.
787#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
788pub struct GameOutcomeReport {
789    pub game_over: bool,
790    #[serde(default, skip_serializing_if = "Option::is_none")]
791    pub winner_slot: Option<String>,
792    #[serde(default, skip_serializing_if = "Vec::is_empty")]
793    pub conceded_slots: Vec<String>,
794    #[serde(default, skip_serializing_if = "Option::is_none")]
795    pub fatal_message: Option<String>,
796    #[serde(default, skip_serializing_if = "Option::is_none")]
797    pub turns: Option<u32>,
798}
799
800/// A game running on the player's own machine, which the relay never sees.
801#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
802pub enum LocalGameKind {
803    /// Play vs AI.
804    Singleplayer,
805}
806
807impl LocalGameKind {
808    /// So the exporter can publish a zero for an unplayed kind rather than
809    /// leaving its series stale at the last value it had.
810    pub const ALL: &'static [Self] = &[Self::Singleplayer];
811
812    pub fn as_str(self) -> &'static str {
813        match self {
814            Self::Singleplayer => "singleplayer",
815        }
816    }
817}
818
819#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
820pub enum RoomStatus {
821    Lobby,
822    InGame,
823    Finished,
824}
825
826#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, TS)]
827#[ts(export, export_to = "lobby/index.ts")]
828pub enum RematchIntent {
829    Deciding,
830    ChoosingDeck,
831    Ready,
832}
833
834#[derive(Debug, Clone, Serialize, Deserialize, TS)]
835#[ts(export, export_to = "lobby/index.ts")]
836pub struct RematchPlayerInfo {
837    pub username: String,
838    pub intent: RematchIntent,
839}
840
841#[derive(Debug, Clone, Serialize, Deserialize, TS)]
842#[ts(export, export_to = "lobby/index.ts")]
843pub struct RematchInfo {
844    pub game_id: String,
845    pub players: Vec<RematchPlayerInfo>,
846    pub cancelled: bool,
847}