racetime 0.32.2

racetime.gg category bot library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
use {
    std::collections::BTreeMap,
    chrono::prelude::*,
    lazy_regex::regex_captures,
    serde::{
        Deserialize,
        Serialize,
        de::{
            Deserializer,
            Error as _,
            Unexpected,
        },
    },
    serde_with::{
        Map,
        serde_as,
    },
    url::Url,
    crate::UDuration,
};

#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(tag = "type")]
pub enum Message {
    #[serde(rename = "chat.history")]
    ChatHistory {
        messages: Vec<ChatMessage>,
    },
    #[serde(rename = "chat.message")]
    ChatMessage {
        message: ChatMessage,
    },
    #[serde(rename = "chat.dm")]
    ChatDm {
        message: String,
        from_user: Option<DmUser>,
        from_bot: Option<String>,
        to: DmUser,
    },
    #[serde(rename = "chat.pin")]
    ChatPin {
        message: ChatMessage,
    },
    #[serde(rename = "chat.unpin")]
    ChatUnpin {
        message: ChatMessage,
    },
    #[serde(rename = "chat.delete")]
    ChatDelete {
        delete: ChatDelete,
    },
    #[serde(rename = "chat.purge")]
    ChatPurge {
        purge: ChatPurge,
    },
    #[serde(rename = "error")]
    Error {
        errors: Vec<String>,
    },
    #[serde(rename = "pong")]
    Pong,
    #[serde(rename = "race.data")]
    RaceData {
        race: RaceData,
    },
    #[serde(rename = "race.renders")]
    RaceRenders,
    #[serde(rename = "race.split")]
    RaceSplit,
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
pub struct CategoryData {
    pub name: String,
    pub short_name: String,
    pub slug: String,
    pub url: String,
    pub data_url: String,
    pub image: Option<Url>,
    pub info: Option<String>,
    pub streaming_required: bool,
    pub owners: Vec<UserData>,
    pub moderators: Vec<UserData>,
    pub goals: Vec<String>,
    pub current_races: Vec<RaceSummary>,
    pub emotes: BTreeMap<String, Url>,
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
pub struct CategorySummary {
    pub name: String,
    pub short_name: String,
    pub slug: String,
    pub url: String,
    pub data_url: String,
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
pub struct ChatDelete {
    pub id: String,
    pub user: Option<UserData>,
    pub bot: Option<String>,
    pub is_bot: bool,
    pub deleted_by: UserData,
}

#[serde_as]
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
pub struct ChatMessage {
    pub id: String,
    pub user: Option<UserData>,
    pub bot: Option<String>,
    pub posted_at: DateTime<Utc>,
    pub message: String,
    pub message_plain: String,
    pub highlight: bool,
    pub is_bot: bool,
    pub is_system: Option<bool>,
    pub is_pinned: bool,
    #[serde_as(as = "Map<_, _>")]
    pub actions: Vec<(String, ActionButton)>,
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
pub struct ChatPurge {
    pub user: UserData,
    pub purged_by: UserData,
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
pub struct Goal {
    pub name: String,
    pub custom: bool,
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
pub struct Entrant {
    /// `None` for deleted users.
    pub user: Option<MaybeAnonymizedUserData>,
    pub status: EntrantStatus,
    #[serde(deserialize_with = "deserialize_opt_django_uduration")]
    pub finish_time: Option<UDuration>,
    pub finished_at: Option<DateTime<Utc>>,
    pub place: Option<u32>,
    pub place_ordinal: Option<String>,
    pub score: Option<u32>,
    pub score_change: Option<i32>,
    pub comment: Option<String>,
    pub has_comment: bool,
    pub stream_live: bool,
    pub stream_override: bool,
    pub team: Option<EntrantTeam>,
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
pub struct EntrantStatus {
    pub value: EntrantStatusValue,
    pub verbose_value: String,
    pub help_text: String,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum EntrantStatusValue {
    Requested,
    Invited,
    Declined,
    Ready,
    NotReady,
    InProgress,
    Done,
    Dnf,
    Dq,
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
pub struct EntrantTeam {
    pub name: String,
    pub slug: String,
    pub formal: bool,
    pub url: Option<String>,
    pub data_url: Option<String>,
    pub avatar: Option<Url>,
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
pub struct RaceData {
    pub version: u32,
    pub name: String,
    pub slug: String,
    pub category: CategorySummary,
    pub status: RaceStatus,
    pub url: String,
    pub data_url: String,
    pub websocket_url: String,
    pub websocket_bot_url: String,
    pub websocket_oauth_url: String,
    pub goal: Goal,
    pub info: String,
    pub info_bot: Option<String>,
    pub info_user: Option<String>,
    pub entrants_count: u32,
    pub entrants_count_finished: u32,
    pub entrants_count_inactive: u32,
    pub entrants: Vec<Entrant>,
    pub opened_at: DateTime<Utc>,
    #[serde(deserialize_with = "deserialize_django_uduration")]
    pub start_delay: UDuration,
    pub started_at: Option<DateTime<Utc>>,
    pub ended_at: Option<DateTime<Utc>>,
    pub cancelled_at: Option<DateTime<Utc>>,
    pub ranked: bool,
    pub unlisted: bool,
    #[serde(deserialize_with = "deserialize_django_uduration")]
    pub time_limit: UDuration,
    pub time_limit_auto_complete: bool,
    pub require_even_teams: bool,
    pub streaming_required: bool,
    pub auto_start: bool,
    pub opened_by: Option<UserData>,
    pub monitors: Vec<UserData>,
    pub recordable: bool,
    pub recorded: bool,
    pub recorded_by: Option<UserData>,
    pub disqualify_unready: bool,
    pub allow_comments: bool,
    pub hide_comments: bool,
    pub hide_entrants: bool,
    pub chat_restricted: bool,
    pub allow_midrace_chat: bool,
    pub allow_non_entrant_chat: bool,
    #[serde(deserialize_with = "deserialize_django_uduration")]
    pub chat_message_delay: UDuration,
    pub bot_meta: serde_json::Map<String, serde_json::Value>,
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
pub struct RaceStatus {
    pub value: RaceStatusValue,
    pub verbose_value: String,
    pub help_text: String,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum RaceStatusValue {
    Open,
    Invitational,
    Pending,
    InProgress,
    Finished,
    Cancelled,
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
pub struct RaceSummary {
    pub name: String,
    pub category: Option<CategorySummary>,
    pub status: RaceStatus,
    pub url: String,
    pub data_url: String,
    pub goal: Goal,
    pub info: String,
    pub entrants_count: u32,
    pub entrants_count_finished: u32,
    pub entrants_count_inactive: u32,
    pub opened_at: DateTime<Utc>,
    pub started_at: Option<DateTime<Utc>>,
    #[serde(deserialize_with = "deserialize_django_uduration")]
    pub time_limit: UDuration,
}

/// Can be converted into [`UserData`] using [`TryFrom`]/[`TryInto`].
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
pub struct MaybeAnonymizedUserData {
    /// Will be empty for anonymized entrants
    pub id: String,
    /// Will be a randomly generated name for anonymized entrants
    pub full_name: String,
    /// Will be identical to `full_name` for anonymized entrants
    pub name: String,
    /// Will be `None` for anonymized entrants
    pub discriminator: Option<String>,
    /// Will be `None` for anonymized entrants
    pub url: Option<String>,
    /// Will be `None` for anonymized entrants
    pub avatar: Option<String>,
    /// Will be `None` for anonymized entrants
    pub pronouns: Option<String>,
    /// Will be empty for anonymized entrants
    pub flair: String,
    /// Will be `None` for anonymized entrants
    pub twitch_name: Option<String>,
    /// Will be `None` for anonymized entrants
    pub twitch_display_name: Option<String>,
    /// Will be `None` for anonymized entrants
    pub twitch_channel: Option<Url>,
    pub can_moderate: bool,
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
pub struct UserData {
    pub id: String,
    pub full_name: String,
    pub name: String,
    pub discriminator: Option<String>,
    pub url: String,
    pub avatar: Option<String>,
    pub pronouns: Option<String>,
    pub flair: String,
    pub twitch_name: Option<String>,
    pub twitch_display_name: Option<String>,
    pub twitch_channel: Option<Url>,
    pub can_moderate: bool,
}

/// The error returned when attempting an anonymized [`MaybeAnonymizedUserData`] into [`UserData`] using [`TryFrom`]/[`TryInto`].
#[derive(Debug, Clone, Copy, thiserror::Error)]
#[error("entrant is anonymized")]
pub struct AnonymousError;

impl TryFrom<MaybeAnonymizedUserData> for UserData {
    type Error = AnonymousError;

    fn try_from(value: MaybeAnonymizedUserData) -> Result<Self, Self::Error> {
        Ok(Self {
            id: value.id,
            full_name: value.full_name,
            name: value.name,
            discriminator: value.discriminator,
            url: value.url.ok_or(AnonymousError)?,
            avatar: value.avatar,
            pronouns: value.pronouns,
            flair: value.flair,
            twitch_name: value.twitch_name,
            twitch_display_name: value.twitch_display_name,
            twitch_channel: value.twitch_channel,
            can_moderate: value.can_moderate,
        })
    }
}

/// The variant of user data returned from the `/user/{id}/data` endpoint.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
pub struct UserProfile {
    pub id: String,
    pub full_name: String,
    pub name: String,
    pub discriminator: Option<String>,
    pub url: String,
    pub avatar: Option<String>,
    pub pronouns: Option<String>,
    pub flair: String,
    pub twitch_name: Option<String>,
    pub twitch_display_name: Option<String>,
    pub twitch_channel: Option<Url>,
    pub can_moderate: bool,
    pub teams: Vec<EntrantTeam>,
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
pub struct DmUser {
    pub id: String,
    pub full_name: String,
    pub name: String,
    pub discriminator: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(untagged)]
pub enum ActionButton {
    Message {
        message: String,
        help_text: Option<String>,
        survey: Option<Vec<SurveyQuestion>>,
        submit: Option<String>,
    },
    Url {
        url: Url,
        help_text: Option<String>,
    }
}

#[serde_as]
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
pub struct SurveyQuestion {
    pub name: String,
    pub label: String,
    pub default: Option<serde_json::Value>,
    #[serde(rename = "help")]
    pub help_text: Option<String>,
    #[serde(rename = "type")]
    pub kind: SurveyQuestionKind,
    pub placeholder: Option<String>,
    #[serde_as(as = "Map<_, _>")]
    #[serde(default)]
    pub options: Vec<(String, String)>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum SurveyQuestionKind {
    Input,
    Bool,
    Radio,
    Select,
}

fn deserialize_django_uduration<'de, D: Deserializer<'de>>(deserializer: D) -> Result<UDuration, D::Error> {
    let s = String::deserialize(deserializer)?;
    if let Some((_, days, hours, minutes, seconds, ms)) = regex_captures!("^P([0-9]+)DT([0-9]+)H([0-9]+)M([0-9]+)(?:\\.([0-9]+))?S$", &s) {
        Ok(UDuration::from_secs(days.parse::<u64>().map_err(D::Error::custom)? * 60 * 60 * 24)
        + UDuration::from_secs(hours.parse::<u64>().map_err(D::Error::custom)? * 60 * 60)
        + UDuration::from_secs(minutes.parse::<u64>().map_err(D::Error::custom)? * 60)
        + UDuration::from_secs(seconds.parse().map_err(D::Error::custom)?)
        + UDuration::from_micros(if ms.is_empty() { 0 } else { ms.parse().map_err(D::Error::custom)? }))
    } else {
        Err(D::Error::invalid_value(Unexpected::Str(&s), &"a duration as produced by Django"))
    }
}

fn deserialize_opt_django_uduration<'de, D: Deserializer<'de>>(deserializer: D) -> Result<Option<UDuration>, D::Error> {
    if let Some(s) = Option::<String>::deserialize(deserializer)? {
        if let Some((_, days, hours, minutes, seconds, ms)) = regex_captures!("^P([0-9]+)DT([0-9]+)H([0-9]+)M([0-9]+)(?:\\.([0-9]+))?S$", &s) {
            Ok(Some(UDuration::from_secs(days.parse::<u64>().map_err(D::Error::custom)? * 60 * 60 * 24)
            + UDuration::from_secs(hours.parse::<u64>().map_err(D::Error::custom)? * 60 * 60)
            + UDuration::from_secs(minutes.parse::<u64>().map_err(D::Error::custom)? * 60)
            + UDuration::from_secs(seconds.parse().map_err(D::Error::custom)?)
            + UDuration::from_micros(if ms.is_empty() { 0 } else { ms.parse().map_err(D::Error::custom)? })))
        } else {
            Err(D::Error::invalid_value(Unexpected::Str(&s), &"a duration as produced by Django"))
        }
    } else {
        Ok(None)
    }
}