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    SetMaxPlayers {
313        max_players: u8,
314    },
315
316    StartGame {
317        #[serde(default)]
318        format: Option<GameFormat>,
319    },
320
321    EndGame {
322        game_id: String,
323    },
324
325    FinishGame {
326        game_id: String,
327    },
328
329    SetRematchIntent {
330        game_id: String,
331        intent: RematchIntent,
332    },
333
334    ReturnToLobby {
335        game_id: String,
336    },
337
338    ReportEngineStats {
339        #[serde(default, skip_serializing_if = "Option::is_none")]
340        game_id: Option<String>,
341        stats: EnginePlayStats,
342    },
343
344    /// How the game ended, from the seat that ran the engine. The relay
345    /// stopped reading it out of the state stream, which need not pass
346    /// through it any more.
347    ReportGameOutcome {
348        game_id: String,
349        outcome: GameOutcomeReport,
350    },
351
352    RequestResync,
353
354    BroadcastState {
355        state: serde_json::Value,
356        #[serde(default, skip_serializing_if = "Option::is_none")]
357        /// A null value will broadcast to the whole room
358        target_player: Option<String>,
359    },
360
361    TurnChange {
362        new_active_player: String,
363        turn_number: u32,
364    },
365    /// Which transport each seat's traffic took for this game. Analytics only.
366    ReportTransport {
367        game_id: String,
368        seats: Vec<SeatTransportReport>,
369    },
370
371    /// Publishes, or with `None` withdraws, this session's data-plane endpoint.
372    AnnounceTransport {
373        #[serde(default)]
374        endpoint: Option<TransportEndpoint>,
375    },
376
377    /// An opaque signalling blob for a room member, named by username.
378    SignalPeer {
379        to: String,
380        payload: serde_json::Value,
381    },
382
383    /// How one direct-plane attempt turned out, sent by the seat. Analytics only.
384    ReportPlaneQuality {
385        report: PlaneQualityReport,
386    },
387
388    SendChat {
389        scope: ChatScope,
390        text: String,
391    },
392
393    InviteToRoom {
394        username: String,
395    },
396}
397
398#[derive(Debug, Clone, Serialize, Deserialize)]
399#[serde(tag = "type")]
400pub enum ServerMessage {
401    Pong,
402
403    AuthResult {
404        success: bool,
405        player_id: Option<String>,
406        reconnected: Option<bool>,
407        error: Option<String>,
408        /// Wire features this relay understands, so a newer client can stay
409        /// quiet instead of sending a message an older one would reject with
410        /// a parse error. Absent from relays built before the list existed.
411        #[serde(default, skip_serializing_if = "Vec::is_empty")]
412        features: Vec<String>,
413        /// Where this relay serves card art, when it serves any. A self-hosted
414        /// box holding the images is the reason to run one, and the client
415        /// cannot guess the port. Absent means fall back to the CDN.
416        #[serde(default, skip_serializing_if = "Option::is_none")]
417        art_base_url: Option<String>,
418    },
419
420    SessionTakenOver,
421
422    RoomList {
423        rooms: Vec<RoomInfo>,
424    },
425
426    PlayerList {
427        players: Vec<PlayerInfo>,
428    },
429
430    RoomCreated {
431        room_id: String,
432        room_name: String,
433        room: RoomInfo,
434        #[serde(default)]
435        resume_token: Option<String>,
436    },
437
438    RoomResumed {
439        room: RoomInfo,
440    },
441
442    PlayerJoined {
443        room_id: String,
444        username: String,
445    },
446
447    PlayerLeft {
448        room_id: String,
449        username: String,
450    },
451
452    PlayerConnected {
453        username: String,
454    },
455
456    PlayerDisconnected {
457        username: String,
458    },
459
460    ReadyStateChanged {
461        username: String,
462        ready: bool,
463    },
464
465    RoomUpdate {
466        room: RoomInfo,
467    },
468
469    GameStarted {
470        room_id: String,
471        game_id: String,
472        player_order: Vec<String>,
473        player_decks: Vec<PlayerDeckInfo>,
474        starting_life: i32,
475    },
476
477    StateUpdate {
478        from_player: String,
479        state: serde_json::Value,
480    },
481
482    TurnChanged {
483        from_player: String,
484        new_active_player: String,
485        turn_number: u32,
486    },
487
488    GameAborted {
489        room_id: String,
490    },
491
492    Error {
493        code: String,
494        message: String,
495    },
496
497    ServerShuttingDown {
498        reconnect_in_s: u32,
499    },
500    /// The room's data-plane roster. Sent only to room members.
501    RoomTransport {
502        room_id: String,
503        /// STUN, and TURN where configured, for the WebRTC plane.
504        #[serde(default, skip_serializing_if = "Vec::is_empty")]
505        ice_servers: Vec<IceServer>,
506        #[serde(default, skip_serializing_if = "Option::is_none")]
507        host: Option<TransportMember>,
508        members: Vec<TransportMember>,
509    },
510
511    /// A signalling blob from a room member. `from` is relay-attested.
512    PeerSignal {
513        from: String,
514        payload: serde_json::Value,
515    },
516
517    ChatMessage(ChatMessage),
518
519    ChatHistory {
520        scope: ChatScope,
521        #[serde(default, skip_serializing_if = "Option::is_none")]
522        room_id: Option<String>,
523        messages: Vec<ChatMessage>,
524    },
525
526    RoomInvite {
527        from: String,
528        room: RoomInfo,
529        #[serde(default, skip_serializing_if = "Option::is_none")]
530        password: Option<String>,
531    },
532}
533
534#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
535pub enum ChatScope {
536    Lobby,
537    Room,
538}
539
540#[derive(Debug, Clone, Serialize, Deserialize)]
541pub struct ChatMessage {
542    pub scope: ChatScope,
543    #[serde(default, skip_serializing_if = "Option::is_none")]
544    pub room_id: Option<String>,
545    pub from: String,
546    #[serde(default, skip_serializing_if = "Option::is_none")]
547    pub avatar_url: Option<String>,
548    #[serde(default, skip_serializing_if = "Option::is_none")]
549    pub qualification: Option<String>,
550    pub text: String,
551    pub sent_at_ms: u64,
552    #[serde(default, skip_serializing_if = "Option::is_none")]
553    pub seal: Option<String>,
554}
555
556pub const CHAT_MESSAGE_MAX_CHARS: usize = 500;
557pub const CHAT_HISTORY_MAX_MESSAGES: usize = 100;
558pub const CHAT_HISTORY_MAX_AGE_MS: u64 = 24 * 60 * 60 * 1000;
559pub const CHAT_MIN_INTERVAL_MS: u64 = 400;
560
561#[derive(Debug, Clone, Serialize, Deserialize, TS)]
562#[ts(export, export_to = "lobby/index.ts")]
563pub struct ResumeRoomRequest {
564    pub room_id: String,
565    pub resume_token: String,
566    pub room_name: String,
567    pub max_players: u8,
568    pub format: GameFormat,
569    #[serde(default)]
570    pub hosted: bool,
571    #[serde(default)]
572    pub engine: EngineKind,
573    #[serde(default, skip_serializing_if = "Option::is_none")]
574    #[ts(optional)]
575    pub official_key: Option<String>,
576    #[serde(default, skip_serializing_if = "Option::is_none")]
577    #[ts(optional)]
578    pub password: Option<String>,
579    #[serde(default, skip_serializing_if = "Option::is_none")]
580    #[ts(optional)]
581    pub reconnect_timeout_s: Option<u32>,
582    #[serde(default, skip_serializing_if = "Option::is_none")]
583    #[ts(optional)]
584    pub table_style: Option<String>,
585    #[serde(default, skip_serializing_if = "Option::is_none")]
586    #[ts(optional)]
587    pub draft_config: Option<DraftConfig>,
588    #[serde(default, skip_serializing_if = "Option::is_none")]
589    #[ts(optional)]
590    pub sealed_config: Option<SealedConfig>,
591    pub player_order: Vec<String>,
592    #[serde(default, skip_serializing_if = "Option::is_none")]
593    #[ts(optional)]
594    pub seat_indices: Option<Vec<u8>>,
595    pub player_decks: Vec<PlayerDeckInfo>,
596    pub starting_life: i32,
597    #[serde(default)]
598    pub bot_players: Vec<String>,
599    pub game_id: String,
600}
601
602#[derive(Debug, Clone, Serialize, Deserialize)]
603pub struct RoomInfo {
604    pub room_id: String,
605    pub room_name: String,
606    pub host: String,
607    #[serde(default)]
608    pub protocol_version: u32,
609    #[serde(default)]
610    pub hosted: bool,
611    #[serde(default)]
612    pub official: bool,
613    #[serde(default)]
614    pub password_protected: bool,
615    pub players: Vec<RoomPlayerInfo>,
616    pub max_players: u8,
617    pub format: GameFormat,
618    pub status: RoomStatus,
619    #[serde(default)]
620    pub engine: EngineKind,
621    #[serde(default = "default_reconnect_timeout_s")]
622    pub reconnect_timeout_s: u32,
623    #[serde(default, skip_serializing_if = "Option::is_none")]
624    pub table_style: Option<String>,
625    #[serde(default, skip_serializing_if = "Option::is_none")]
626    pub draft_config: Option<DraftConfig>,
627    #[serde(default, skip_serializing_if = "Option::is_none")]
628    pub sealed_config: Option<SealedConfig>,
629    #[serde(default, skip_serializing_if = "Option::is_none")]
630    pub rematch: Option<RematchInfo>,
631}
632
633pub const DEFAULT_RECONNECT_TIMEOUT_S: u32 = 60;
634
635fn default_reconnect_timeout_s() -> u32 {
636    DEFAULT_RECONNECT_TIMEOUT_S
637}
638
639#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, TS)]
640#[ts(export, export_to = "lobby/index.ts")]
641pub struct SealedConfig {
642    #[serde(default, skip_serializing_if = "Option::is_none")]
643    #[ts(optional)]
644    pub set_code: Option<String>,
645    #[serde(default, skip_serializing_if = "Option::is_none")]
646    #[ts(optional)]
647    pub cube_id: Option<String>,
648    #[serde(default, skip_serializing_if = "Option::is_none")]
649    #[ts(optional)]
650    pub cube_name: Option<String>,
651    #[serde(default)]
652    pub singleton: bool,
653    pub num_boosters: u8,
654    #[serde(default, skip_serializing_if = "Option::is_none")]
655    #[ts(optional, type = "number")]
656    pub base_seed: Option<u64>,
657}
658
659#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, TS)]
660#[ts(export, export_to = "lobby/index.ts")]
661pub struct DraftConfig {
662    #[serde(default, skip_serializing_if = "Option::is_none")]
663    #[ts(optional)]
664    pub set_code: Option<String>,
665    #[serde(default, skip_serializing_if = "Option::is_none")]
666    #[ts(optional)]
667    pub cube_id: Option<String>,
668    #[serde(default, skip_serializing_if = "Option::is_none")]
669    #[ts(optional)]
670    pub cube_name: Option<String>,
671    pub rounds: u8,
672    pub picks_per_pass: u8,
673    #[serde(default, skip_serializing_if = "Option::is_none")]
674    #[ts(optional)]
675    pub pick_seconds: Option<u16>,
676    #[serde(default, skip_serializing_if = "Option::is_none")]
677    #[ts(optional, type = "number")]
678    pub seed: Option<u64>,
679    pub fill_with_bots: bool,
680}
681
682#[derive(Debug, Clone, Serialize, Deserialize)]
683pub struct RoomPlayerInfo {
684    pub username: String,
685    #[serde(default, skip_serializing_if = "Option::is_none")]
686    pub seat_index: Option<u8>,
687    pub ready: bool,
688    pub connected: bool,
689    #[serde(default)]
690    pub is_bot: bool,
691    #[serde(skip_serializing_if = "Option::is_none")]
692    pub selected_deck_name: Option<String>,
693}
694
695#[derive(Debug, Clone, Serialize, Deserialize)]
696pub struct PlayerInfo {
697    pub username: String,
698    pub player_id: String,
699    pub connected: bool,
700    #[serde(default)]
701    pub verified: bool,
702    #[serde(default, skip_serializing_if = "Option::is_none")]
703    pub qualification: Option<String>,
704    #[serde(default, skip_serializing_if = "Option::is_none")]
705    pub avatar_url: Option<String>,
706    #[serde(skip_serializing_if = "Option::is_none")]
707    pub room_id: Option<String>,
708    /// In a game on their own machine rather than in a relay room. Never set
709    /// at the same time as `room_id`.
710    #[serde(default, skip_serializing_if = "Option::is_none")]
711    pub local_game: Option<LocalGameKind>,
712    /// Opaque to clients: the session's IP sealed together with its handle,
713    /// which only the hub can open. Carried verbatim into chat reports.
714    #[serde(default, skip_serializing_if = "Option::is_none")]
715    pub seal: Option<String>,
716}
717
718/// Names [`ClientMessage::SetLocalGame`] in `AuthResult::features`.
719pub const FEATURE_LOCAL_GAME: &str = "local_game";
720/// Names [`ClientMessage::SendChat`] in `AuthResult::features`.
721pub const FEATURE_CHAT: &str = "chat";
722/// Names [`ClientMessage::InviteToRoom`] in `AuthResult::features`.
723pub const FEATURE_ROOM_INVITES: &str = "room_invites";
724/// Names [`ClientMessage::ReportGameOutcome`] in `AuthResult::features`.
725pub const FEATURE_GAME_OUTCOME: &str = "game_outcome";
726pub const FEATURE_LIMITED_SESSIONS: &str = "limited_sessions";
727pub const FEATURE_LIMITED_DRAFT_CLOCKS: &str = "limited_draft_clocks";
728pub const FEATURE_LIMITED_SESSION_RECOVERY: &str = "limited_session_recovery";
729pub const FEATURE_GAME_REMATCH: &str = "game_rematch";
730pub const FEATURE_ROOM_SEATS: &str = "room_seats";
731
732/// Names [`ClientMessage::AnnounceTransport`] in `AuthResult::features`.
733pub const FEATURE_ROOM_TRANSPORT: &str = "room_transport";
734
735/// Names [`ClientMessage::SignalPeer`] in `AuthResult::features`.
736pub const FEATURE_PEER_SIGNAL: &str = "peer_signal";
737
738/// Names [`ClientMessage::ReportPlaneQuality`] in `AuthResult::features`.
739pub const FEATURE_PLANE_QUALITY: &str = "plane_quality";
740
741pub const FEATURE_CARD_COSMETICS: &str = "card_cosmetics";
742pub const FEATURE_CARD_COSMETICS_V2: &str = "card_cosmetics_v2";
743pub const FEATURE_CARD_COSMETICS_V3: &str = "card_cosmetics_v3";
744
745pub const FEATURES: &[&str] = &[
746    FEATURE_LOCAL_GAME,
747    FEATURE_ROOM_TRANSPORT,
748    FEATURE_PEER_SIGNAL,
749    FEATURE_PLANE_QUALITY,
750    FEATURE_CHAT,
751    FEATURE_ROOM_INVITES,
752    FEATURE_GAME_OUTCOME,
753    FEATURE_GAME_REMATCH,
754    FEATURE_ROOM_SEATS,
755    FEATURE_CARD_COSMETICS,
756    FEATURE_CARD_COSMETICS_V2,
757    FEATURE_CARD_COSMETICS_V3,
758    FEATURE_LIMITED_SESSIONS,
759    FEATURE_LIMITED_DRAFT_CLOCKS,
760    FEATURE_LIMITED_SESSION_RECOVERY,
761];
762
763/// Largest signalling blob the relay forwards.
764pub const MAX_SIGNAL_BYTES: usize = 16 * 1024;
765
766/// Longest `fatal_message` the relay keeps.
767pub const MAX_FATAL_MESSAGE_CHARS: usize = 500;
768
769/// Engine slots (`player-N`), never usernames: the relay holds the seat map
770/// and does not take a name from a client.
771#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
772pub struct GameOutcomeReport {
773    pub game_over: bool,
774    #[serde(default, skip_serializing_if = "Option::is_none")]
775    pub winner_slot: Option<String>,
776    #[serde(default, skip_serializing_if = "Vec::is_empty")]
777    pub conceded_slots: Vec<String>,
778    #[serde(default, skip_serializing_if = "Option::is_none")]
779    pub fatal_message: Option<String>,
780    #[serde(default, skip_serializing_if = "Option::is_none")]
781    pub turns: Option<u32>,
782}
783
784/// A game running on the player's own machine, which the relay never sees.
785#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
786pub enum LocalGameKind {
787    /// Play vs AI.
788    Singleplayer,
789}
790
791impl LocalGameKind {
792    /// So the exporter can publish a zero for an unplayed kind rather than
793    /// leaving its series stale at the last value it had.
794    pub const ALL: &'static [Self] = &[Self::Singleplayer];
795
796    pub fn as_str(self) -> &'static str {
797        match self {
798            Self::Singleplayer => "singleplayer",
799        }
800    }
801}
802
803#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
804pub enum RoomStatus {
805    Lobby,
806    InGame,
807    Finished,
808}
809
810#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, TS)]
811#[ts(export, export_to = "lobby/index.ts")]
812pub enum RematchIntent {
813    Deciding,
814    ChoosingDeck,
815    Ready,
816}
817
818#[derive(Debug, Clone, Serialize, Deserialize, TS)]
819#[ts(export, export_to = "lobby/index.ts")]
820pub struct RematchPlayerInfo {
821    pub username: String,
822    pub intent: RematchIntent,
823}
824
825#[derive(Debug, Clone, Serialize, Deserialize, TS)]
826#[ts(export, export_to = "lobby/index.ts")]
827pub struct RematchInfo {
828    pub game_id: String,
829    pub players: Vec<RematchPlayerInfo>,
830    pub cancelled: bool,
831}