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#[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    /// Which seats left the relay's data plane, as reported by the host.
86    TransportUsed {
87        ts: String,
88        room_id: String,
89        game_id: String,
90        host: String,
91        seats: Vec<SeatTransportReport>,
92    },
93    /// One end's account of one direct-plane attempt, whether or not it worked.
94    PlaneQuality {
95        ts: String,
96        #[serde(skip_serializing_if = "Option::is_none")]
97        room_id: Option<String>,
98        /// Relay-attested; never client supplied.
99        username: String,
100        /// The other end, as the reporter named it.
101        peer: String,
102        plane: String,
103        outcome: String,
104        /// `settled` or `measured`; a connected peer reports both.
105        phase: String,
106        #[serde(skip_serializing_if = "Option::is_none")]
107        connect_ms: Option<u32>,
108        #[serde(skip_serializing_if = "Option::is_none")]
109        rtt_ms: Option<u32>,
110        #[serde(skip_serializing_if = "Option::is_none")]
111        relay_rtt_ms: Option<u32>,
112        #[serde(skip_serializing_if = "Option::is_none")]
113        candidate_pair: Option<String>,
114    },
115    EngineStats {
116        ts: String,
117        /// The room the seat was in when the report landed, which at game over
118        /// is often none: the report outlives the seat's membership. Nothing
119        /// downstream reads it, so it stays a breadcrumb rather than a key.
120        #[serde(skip_serializing_if = "Option::is_none")]
121        room_id: Option<String>,
122        username: String,
123        report_id: String,
124        #[serde(skip_serializing_if = "Option::is_none")]
125        game_id: Option<String>,
126        engine: String,
127        client_version: String,
128        platform: String,
129        #[serde(skip_serializing_if = "Option::is_none")]
130        format: Option<String>,
131        seats: u32,
132        multiplayer: bool,
133        duration_s: u32,
134        end_reason: String,
135        decisions: u32,
136        turnaround_p50: u32,
137        turnaround_p90: u32,
138        turnaround_max: u32,
139        #[serde(skip_serializing_if = "Option::is_none")]
140        engine_p50: Option<u32>,
141        #[serde(skip_serializing_if = "Option::is_none")]
142        engine_p90: Option<u32>,
143        #[serde(skip_serializing_if = "Option::is_none")]
144        engine_max: Option<u32>,
145        #[serde(skip_serializing_if = "Option::is_none")]
146        engine_same_p50: Option<u32>,
147        #[serde(skip_serializing_if = "Option::is_none")]
148        engine_same_p90: Option<u32>,
149        #[serde(skip_serializing_if = "Option::is_none")]
150        engine_same_max: Option<u32>,
151        #[serde(skip_serializing_if = "Option::is_none")]
152        engine_cross_p50: Option<u32>,
153        #[serde(skip_serializing_if = "Option::is_none")]
154        engine_cross_p90: Option<u32>,
155        #[serde(skip_serializing_if = "Option::is_none")]
156        engine_cross_max: Option<u32>,
157        think_hidden: u32,
158    },
159
160    DeckSelected {
161        ts: String,
162        room_id: String,
163        username: String,
164        is_bot: bool,
165        deck_name: String,
166        commander: Option<String>,
167        cards: Vec<CardEntry>,
168        sideboard_count: usize,
169    },
170    SeatJoined {
171        ts: String,
172        room_id: String,
173        username: String,
174        is_bot: bool,
175        observer: bool,
176    },
177    SeatLeft {
178        ts: String,
179        room_id: String,
180        username: String,
181    },
182}
183
184#[derive(Serialize)]
185pub struct CaptureLine<'a> {
186    pub ts: String,
187    pub from: &'a str,
188    pub envelope: &'a Value,
189    /// Set only on envelopes a player sent, where `from` is that player and the
190    /// figure is theirs. Lets a decision be read against the link it crossed
191    /// rather than against a fleet-wide average.
192    #[serde(rename = "clientRttMs", skip_serializing_if = "Option::is_none")]
193    pub client_rtt_ms: Option<u32>,
194}