manabrew_server/analytics/
event.rs1use 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 #[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 DeckSelected {
86 ts: String,
87 room_id: String,
88 username: String,
89 is_bot: bool,
90 deck_name: String,
91 commander: Option<String>,
92 cards: Vec<CardEntry>,
93 sideboard_count: usize,
94 },
95 SeatJoined {
96 ts: String,
97 room_id: String,
98 username: String,
99 is_bot: bool,
100 observer: bool,
101 },
102 SeatLeft {
103 ts: String,
104 room_id: String,
105 username: String,
106 },
107}
108
109#[derive(Serialize)]
110pub struct CaptureLine<'a> {
111 pub ts: String,
112 pub from: &'a str,
113 pub envelope: &'a Value,
114 #[serde(rename = "clientRttMs", skip_serializing_if = "Option::is_none")]
118 pub client_rtt_ms: Option<u32>,
119}