Skip to main content

manabrew_server/analytics/
event.rs

1use serde::Serialize;
2use serde_json::Value;
3
4use crate::protocol::{EngineKind, GameFormat};
5
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
7#[serde(rename_all = "snake_case")]
8pub enum GameEndReason {
9    GameOver,
10    EngineFatal,
11    HostEnded,
12    ReconnectTimeout,
13    HostLost,
14    StaleExpired,
15    Abandoned,
16}
17
18impl GameEndReason {
19    pub fn as_str(self) -> &'static str {
20        match self {
21            Self::GameOver => "game_over",
22            Self::EngineFatal => "engine_fatal",
23            Self::HostEnded => "host_ended",
24            Self::ReconnectTimeout => "reconnect_timeout",
25            Self::HostLost => "host_lost",
26            Self::StaleExpired => "stale_expired",
27            Self::Abandoned => "abandoned",
28        }
29    }
30}
31
32#[derive(Debug, Serialize)]
33pub struct SeatInfo {
34    pub username: String,
35    pub is_bot: bool,
36    pub deck_name: Option<String>,
37    pub commander: Option<String>,
38    pub published_deck_id: Option<String>,
39    pub deck_fingerprint: Option<String>,
40}
41
42#[derive(Debug, Serialize)]
43pub struct CardEntry {
44    pub name: String,
45    pub set_code: String,
46    pub count: u32,
47}
48
49#[derive(Debug, Serialize)]
50#[serde(tag = "event", rename_all = "snake_case")]
51pub enum AnalyticsEvent {
52    ClientConnected {
53        ts: String,
54        username: String,
55        platform: String,
56        /// The client's reported app version. `None` means a build from before
57        /// clients reported one, which is the only handle we have on how much
58        /// of the player base is running a stale install.
59        #[serde(skip_serializing_if = "Option::is_none")]
60        version: Option<String>,
61        reconnected: bool,
62    },
63    GameStarted {
64        ts: String,
65        game_id: String,
66        room_id: String,
67        format: GameFormat,
68        engine: EngineKind,
69        hosted: bool,
70        official: bool,
71        starting_life: i32,
72        players: Vec<SeatInfo>,
73    },
74    GameEnded {
75        ts: String,
76        game_id: String,
77        room_id: String,
78        reason: GameEndReason,
79        duration_s: f64,
80        game_over: bool,
81        winner: Option<String>,
82        conceded: Vec<String>,
83        fatal_message: Option<String>,
84    },
85    EngineStats {
86        ts: String,
87        /// The room the seat was in when the report landed, which at game over
88        /// is often none: the report outlives the seat's membership. Nothing
89        /// downstream reads it, so it stays a breadcrumb rather than a key.
90        #[serde(skip_serializing_if = "Option::is_none")]
91        room_id: Option<String>,
92        username: String,
93        report_id: String,
94        #[serde(skip_serializing_if = "Option::is_none")]
95        game_id: Option<String>,
96        engine: String,
97        client_version: String,
98        platform: String,
99        #[serde(skip_serializing_if = "Option::is_none")]
100        format: Option<String>,
101        seats: u32,
102        multiplayer: bool,
103        duration_s: u32,
104        end_reason: String,
105        decisions: u32,
106        turnaround_p50: u32,
107        turnaround_p90: u32,
108        turnaround_max: u32,
109        #[serde(skip_serializing_if = "Option::is_none")]
110        engine_p50: Option<u32>,
111        #[serde(skip_serializing_if = "Option::is_none")]
112        engine_p90: Option<u32>,
113        #[serde(skip_serializing_if = "Option::is_none")]
114        engine_max: Option<u32>,
115        #[serde(skip_serializing_if = "Option::is_none")]
116        engine_same_p50: Option<u32>,
117        #[serde(skip_serializing_if = "Option::is_none")]
118        engine_same_p90: Option<u32>,
119        #[serde(skip_serializing_if = "Option::is_none")]
120        engine_same_max: Option<u32>,
121        #[serde(skip_serializing_if = "Option::is_none")]
122        engine_cross_p50: Option<u32>,
123        #[serde(skip_serializing_if = "Option::is_none")]
124        engine_cross_p90: Option<u32>,
125        #[serde(skip_serializing_if = "Option::is_none")]
126        engine_cross_max: Option<u32>,
127        think_hidden: u32,
128    },
129
130    DeckSelected {
131        ts: String,
132        room_id: String,
133        username: String,
134        is_bot: bool,
135        deck_name: String,
136        commander: Option<String>,
137        cards: Vec<CardEntry>,
138        sideboard_count: usize,
139    },
140    SeatJoined {
141        ts: String,
142        room_id: String,
143        username: String,
144        is_bot: bool,
145        observer: bool,
146    },
147    SeatLeft {
148        ts: String,
149        room_id: String,
150        username: String,
151    },
152}
153
154#[derive(Serialize)]
155pub struct CaptureLine<'a> {
156    pub ts: String,
157    pub from: &'a str,
158    pub envelope: &'a Value,
159    /// Set only on envelopes a player sent, where `from` is that player and the
160    /// figure is theirs. Lets a decision be read against the link it crossed
161    /// rather than against a fleet-wide average.
162    #[serde(rename = "clientRttMs", skip_serializing_if = "Option::is_none")]
163    pub client_rtt_ms: Option<u32>,
164}