Skip to main content

manabrew_server/analytics/
event.rs

1use serde::Serialize;
2use serde_json::Value;
3
4use crate::protocol::{EngineKind, GameFormat, SeatTransportReport};
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// An event is built, serialised to one JSONL line and dropped; nothing holds
50// a collection of them, so the size of the widest variant (EngineStats, a
51// column per timing) costs nothing worth boxing for.
52#[allow(clippy::large_enum_variant)]
53#[derive(Debug, Serialize)]
54#[serde(tag = "event", rename_all = "snake_case")]
55pub enum AnalyticsEvent {
56    ClientConnected {
57        ts: String,
58        username: String,
59        platform: String,
60        /// The client's reported app version. `None` means a build from before
61        /// clients reported one, which is the only handle we have on how much
62        /// of the player base is running a stale install.
63        #[serde(skip_serializing_if = "Option::is_none")]
64        version: Option<String>,
65        reconnected: bool,
66        engine_gate: String,
67        #[serde(skip_serializing_if = "Option::is_none")]
68        acquisition_source: Option<&'static str>,
69    },
70    GameStarted {
71        ts: String,
72        game_id: String,
73        room_id: String,
74        format: GameFormat,
75        engine: EngineKind,
76        hosted: bool,
77        official: bool,
78        #[serde(skip_serializing_if = "Option::is_none")]
79        table_style: Option<String>,
80        starting_life: i32,
81        players: Vec<SeatInfo>,
82    },
83    GameEnded {
84        ts: String,
85        game_id: String,
86        room_id: String,
87        reason: GameEndReason,
88        duration_s: f64,
89        game_over: bool,
90        winner: Option<String>,
91        conceded: Vec<String>,
92        fatal_message: Option<String>,
93        /// Whether the host filed the outcome; false means the reason is the
94        /// room lifecycle's fallback and `game_over` says nothing.
95        reported: bool,
96        #[serde(skip_serializing_if = "Option::is_none")]
97        turns: Option<u32>,
98    },
99    /// Which seats left the relay's data plane, as reported by the host.
100    TransportUsed {
101        ts: String,
102        room_id: String,
103        game_id: String,
104        host: String,
105        seats: Vec<SeatTransportReport>,
106    },
107    /// One end's account of one direct-plane attempt, whether or not it worked.
108    PlaneQuality {
109        ts: String,
110        #[serde(skip_serializing_if = "Option::is_none")]
111        room_id: Option<String>,
112        /// Relay-attested; never client supplied.
113        username: String,
114        /// The other end, as the reporter named it.
115        peer: String,
116        plane: String,
117        outcome: String,
118        /// `settled` or `measured`; a connected peer reports both.
119        phase: String,
120        #[serde(skip_serializing_if = "Option::is_none")]
121        connect_ms: Option<u32>,
122        #[serde(skip_serializing_if = "Option::is_none")]
123        rtt_ms: Option<u32>,
124        #[serde(skip_serializing_if = "Option::is_none")]
125        relay_rtt_ms: Option<u32>,
126        #[serde(skip_serializing_if = "Option::is_none")]
127        candidate_pair: Option<String>,
128    },
129    EngineStats {
130        ts: String,
131        /// The room the seat was in when the report landed, which at game over
132        /// is often none: the report outlives the seat's membership. Nothing
133        /// downstream reads it, so it stays a breadcrumb rather than a key.
134        #[serde(skip_serializing_if = "Option::is_none")]
135        room_id: Option<String>,
136        username: String,
137        report_id: String,
138        #[serde(skip_serializing_if = "Option::is_none")]
139        game_id: Option<String>,
140        engine: String,
141        client_version: String,
142        platform: String,
143        #[serde(skip_serializing_if = "Option::is_none")]
144        format: Option<String>,
145        seats: u32,
146        multiplayer: bool,
147        duration_s: u32,
148        end_reason: String,
149        decisions: u32,
150        turnaround_p50: u32,
151        turnaround_p90: u32,
152        turnaround_max: u32,
153        #[serde(skip_serializing_if = "Option::is_none")]
154        engine_p50: Option<u32>,
155        #[serde(skip_serializing_if = "Option::is_none")]
156        engine_p90: Option<u32>,
157        #[serde(skip_serializing_if = "Option::is_none")]
158        engine_max: Option<u32>,
159        #[serde(skip_serializing_if = "Option::is_none")]
160        engine_same_p50: Option<u32>,
161        #[serde(skip_serializing_if = "Option::is_none")]
162        engine_same_p90: Option<u32>,
163        #[serde(skip_serializing_if = "Option::is_none")]
164        engine_same_max: Option<u32>,
165        #[serde(skip_serializing_if = "Option::is_none")]
166        engine_cross_p50: Option<u32>,
167        #[serde(skip_serializing_if = "Option::is_none")]
168        engine_cross_p90: Option<u32>,
169        #[serde(skip_serializing_if = "Option::is_none")]
170        engine_cross_max: Option<u32>,
171        think_hidden: u32,
172        /// `turnaround` cut at the first reply frame reaching the client:
173        /// server, wire and transfer on one side, parse, apply and render on
174        /// the other. Absent from clients that predate the cut.
175        #[serde(skip_serializing_if = "Option::is_none")]
176        reply_wait_p50: Option<u32>,
177        #[serde(skip_serializing_if = "Option::is_none")]
178        reply_wait_p90: Option<u32>,
179        #[serde(skip_serializing_if = "Option::is_none")]
180        reply_wait_max: Option<u32>,
181        #[serde(skip_serializing_if = "Option::is_none")]
182        client_work_p50: Option<u32>,
183        #[serde(skip_serializing_if = "Option::is_none")]
184        client_work_p90: Option<u32>,
185        #[serde(skip_serializing_if = "Option::is_none")]
186        client_work_max: Option<u32>,
187        /// `engine_*` split by who owned the time: bot prompts on one side,
188        /// the rules engine on the other. Absent from an engine that does not
189        /// tag its windows.
190        #[serde(skip_serializing_if = "Option::is_none")]
191        engine_bot_p50: Option<u32>,
192        #[serde(skip_serializing_if = "Option::is_none")]
193        engine_bot_p90: Option<u32>,
194        #[serde(skip_serializing_if = "Option::is_none")]
195        engine_bot_max: Option<u32>,
196        #[serde(skip_serializing_if = "Option::is_none")]
197        engine_rules_p50: Option<u32>,
198        #[serde(skip_serializing_if = "Option::is_none")]
199        engine_rules_p90: Option<u32>,
200        #[serde(skip_serializing_if = "Option::is_none")]
201        engine_rules_max: Option<u32>,
202        #[serde(skip_serializing_if = "Option::is_none")]
203        checkpoints: Option<manabrew_protocol::telemetry::EngineCheckpointStats>,
204    },
205
206    DeckSelected {
207        ts: String,
208        room_id: String,
209        username: String,
210        is_bot: bool,
211        deck_name: String,
212        commander: Option<String>,
213        cards: Vec<CardEntry>,
214        sideboard_count: usize,
215    },
216    SeatJoined {
217        ts: String,
218        room_id: String,
219        username: String,
220        is_bot: bool,
221        observer: bool,
222    },
223    SeatLeft {
224        ts: String,
225        room_id: String,
226        username: String,
227    },
228}
229
230#[derive(Serialize)]
231pub struct CaptureLine<'a> {
232    pub ts: String,
233    pub from: &'a str,
234    pub envelope: &'a Value,
235    /// Set only on envelopes a player sent, where `from` is that player and the
236    /// figure is theirs. Lets a decision be read against the link it crossed
237    /// rather than against a fleet-wide average.
238    #[serde(rename = "clientRttMs", skip_serializing_if = "Option::is_none")]
239    pub client_rtt_ms: Option<u32>,
240}