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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
use bytes::Bytes;
pub use client::ApiClient;
use futures_util::{Stream, StreamExt};
use md5::Context;
use reqwest::StatusCode;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::borrow::Cow;
use std::fmt::{self, Debug, Display, Formatter};
use std::io::Write;
pub use steamid_ng::SteamID;
use thiserror::Error;
use time::OffsetDateTime;
use tinyvec::TinyVec;
use tracing::{debug, error, instrument};

mod client;

#[derive(Debug, Error)]
#[non_exhaustive]
pub enum Error {
    #[error("Invalid base url")]
    InvalidBaseUrl,
    #[error("Request failed: {0}")]
    Request(reqwest::Error),
    #[error("Invalid page requested")]
    InvalidPage,
    #[error("Invalid api key")]
    InvalidApiKey,
    #[error("Hash mismatch")]
    HashMisMatch,
    #[error("Unknown server error")]
    ServerError(u16),
    #[error("Invalid response: {0}")]
    InvalidResponse(String),
    #[error("Demo {0} not found")]
    DemoNotFound(u32),
    #[error("User {0} not found")]
    UserNotFound(u32),
    #[error("Error while writing demo data")]
    Write(#[source] std::io::Error),
    #[error("Operation timed out")]
    TimeOut,
}

impl From<reqwest::Error> for Error {
    fn from(error: reqwest::Error) -> Self {
        if error.is_timeout() {
            Error::TimeOut
        } else {
            match error.status() {
                Some(StatusCode::UNAUTHORIZED) => Error::InvalidApiKey,
                Some(StatusCode::PRECONDITION_FAILED) => Error::HashMisMatch,
                Some(status) if status.is_server_error() => Error::ServerError(status.as_u16()),
                _ => Error::Request(error),
            }
        }
    }
}

#[derive(Clone, Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
/// Data of an uploaded demo
pub struct Demo {
    pub id: u32,
    pub url: String,
    pub name: String,
    pub server: String,
    pub duration: u16,
    pub nick: String,
    pub map: String,
    #[serde(with = "time::serde::timestamp")]
    pub time: OffsetDateTime,
    pub red: String,
    pub blue: String,
    pub red_score: u8,
    pub blue_score: u8,
    pub player_count: u8,
    pub uploader: UserRef,
    #[serde(deserialize_with = "hex_to_digest")]
    pub hash: [u8; 16],
    pub backend: String,
    pub path: String,
    #[serde(default)]
    /// Demos listed using `ApiClient::list` will not have any players set, use `get_players` to automatically
    /// load the players when not set
    pub players: Option<Vec<Player>>,
}

impl Demo {
    /// Return either the stored players info or get the players from the api
    #[instrument]
    pub async fn get_players(&self, client: &ApiClient) -> Result<Cow<'_, [Player]>, Error> {
        match &self.players {
            Some(players) => Ok(Cow::Borrowed(players.as_slice())),
            None => {
                let demo = client.get(self.id).await?;
                Ok(Cow::Owned(demo.players.unwrap_or_default()))
            }
        }
    }

    /// Download a demo, returning a stream of chunks
    #[instrument]
    pub async fn download(
        &self,
        client: &ApiClient,
    ) -> Result<impl Stream<Item = Result<Bytes, Error>>, Error> {
        debug!(id = self.id, url = display(&self.url), "starting download");
        Ok(client
            .download_demo(&self.url, self.duration)
            .await?
            .bytes_stream()
            .map(|chunk| chunk.map_err(Error::from)))
    }

    /// Download a demo and save it to a writer, verifying the md5 hash in the process
    #[instrument(skip(target))]
    pub async fn save<W: Write>(&self, client: &ApiClient, mut target: W) -> Result<(), Error> {
        debug!(id = self.id, url = display(&self.url), "starting download");
        let mut response = client.download_demo(&self.url, self.duration).await?;

        let mut context = Context::new();

        while let Some(chunk) = response.chunk().await? {
            context.consume(&chunk);
            target.write_all(&chunk).map_err(Error::Write)?;
        }

        let calculated = context.compute().0;

        if calculated != self.hash {
            error!(
                calculated = display(hex::encode(calculated)),
                expected = display(hex::encode(self.hash)),
                "hash mismatch"
            );
            return Err(Error::HashMisMatch);
        }
        Ok(())
    }
}

/// Reference to a user, either contains the full user information or only the user id
#[derive(Clone, Debug, Deserialize)]
#[serde(untagged)]
pub enum UserRef {
    User(User),
    Id(u32),
}

impl UserRef {
    /// Id of the user
    #[must_use]
    pub fn id(&self) -> u32 {
        match self {
            UserRef::Id(id) | UserRef::User(User { id, .. }) => *id,
        }
    }

    /// Return the stored user info if available
    #[must_use]
    pub fn user(&self) -> Option<&User> {
        match self {
            UserRef::Id(_) => None,
            UserRef::User(ref user) => Some(user),
        }
    }

    /// Return either the stored user info or get the user information from the api
    #[instrument]
    pub async fn resolve(&self, client: &ApiClient) -> Result<Cow<'_, User>, Error> {
        match self {
            UserRef::User(ref user) => Ok(Cow::Borrowed(user)),
            UserRef::Id(id) => Ok(Cow::Owned(client.get_user(*id).await?)),
        }
    }
}

/// User data
#[derive(Clone, Debug, Deserialize)]
pub struct User {
    pub id: u32,
    #[serde(rename = "steamid")]
    pub steam_id: SteamID,
    pub name: String,
}

/// Data of a player in a demo
#[derive(Clone, Debug, Deserialize)]
pub struct Player {
    #[serde(rename = "id")]
    pub player_id: u32,
    #[serde(flatten)]
    #[serde(deserialize_with = "deserialize_nested_user")]
    pub user: User,
    pub team: Team,
    /// If a player has played multiple classes, the class which the user spawned the most as is taken
    pub class: Class,
    pub kills: u8,
    pub assists: u8,
    pub deaths: u8,
}

#[derive(Clone, Debug, Deserialize)]
struct NestedPlayerUser {
    user_id: u32,
    #[serde(rename = "steamid")]
    steam_id: SteamID,
    name: String,
}

fn deserialize_nested_user<'de, D>(deserializer: D) -> Result<User, D::Error>
where
    D: Deserializer<'de>,
{
    let nested = NestedPlayerUser::deserialize(deserializer)?;
    Ok(User {
        id: nested.user_id,
        steam_id: nested.steam_id,
        name: nested.name,
    })
}

/// Player team, red or blue
#[derive(Clone, Copy, Debug, Deserialize, PartialOrd, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum Team {
    Red,
    Blue,
}

/// Player class
#[derive(Clone, Copy, Debug, Deserialize, PartialOrd, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum Class {
    Scout,
    Soldier,
    Pyro,
    Demoman,
    HeavyWeapons,
    Engineer,
    Medic,
    Sniper,
    Spy,
}

/// Deserializes a lowercase hex string to a `[u8; 16]`.
fn hex_to_digest<'de, D>(deserializer: D) -> Result<[u8; 16], D::Error>
where
    D: Deserializer<'de>,
{
    use hex::FromHex;
    use serde::de::Error;

    let string = <&str>::deserialize(deserializer)?;

    if string.is_empty() {
        return Ok([0; 16]);
    }

    <[u8; 16]>::from_hex(string).map_err(|err| Error::custom(err.to_string()))
}

/// Chat message send in the demo
#[derive(Clone, Debug, Deserialize)]
pub struct ChatMessage {
    pub user: String,
    pub time: u32,
    pub message: String,
}

/// Order for listing demos
#[derive(Debug, Clone, Copy, Serialize)]
#[serde(into = "&str")]
pub enum ListOrder {
    Ascending,
    Descending,
}

/// Game type as recognized by demos.tf, HL, Prolander, 6s or 4v4
#[derive(Debug, Clone, Copy, Serialize)]
pub enum GameType {
    #[serde(rename = "hl")]
    HL,
    #[serde(rename = "prolander")]
    Prolander,
    #[serde(rename = "6v6")]
    Sixes,
    #[serde(rename = "4v4")]
    Fours,
}

impl Default for ListOrder {
    fn default() -> Self {
        ListOrder::Descending
    }
}

impl Display for ListOrder {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        Display::fmt(<&str>::from(*self), f)
    }
}

impl From<ListOrder> for &str {
    fn from(order: ListOrder) -> Self {
        match order {
            ListOrder::Ascending => "ASC",
            ListOrder::Descending => "DESC",
        }
    }
}

/// Parameters for demo list command
#[derive(Debug, Default, Serialize)]
pub struct ListParams {
    order: ListOrder,
    backend: Option<String>,
    map: Option<String>,
    players: PlayerList,
    #[serde(rename = "type")]
    ty: Option<GameType>,
    #[serde(serialize_with = "serialize_option_time")]
    after: Option<OffsetDateTime>,
    #[serde(serialize_with = "serialize_option_time")]
    before: Option<OffsetDateTime>,
    before_id: Option<u64>,
    after_id: Option<u64>,
}

fn serialize_option_time<S>(dt: &Option<OffsetDateTime>, serializer: S) -> Result<S::Ok, S::Error>
where
    S: Serializer,
{
    match dt {
        Some(time) => time::serde::timestamp::serialize(time, serializer),
        None => Option::<i64>::serialize(&None, serializer),
    }
}

#[derive(Default, Debug)]
struct PlayerList(TinyVec<[SteamID; 2]>);

impl PlayerList {
    fn new<T: Into<SteamID>, I: IntoIterator<Item = T>>(players: I) -> Self {
        PlayerList(players.into_iter().map(Into::into).collect())
    }
}

impl Display for PlayerList {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        let mut first = true;
        for steam_id in &self.0 {
            if first {
                first = false;
                write!(f, "{}", u64::from(*steam_id))?;
            } else {
                write!(f, ",{}", u64::from(*steam_id))?;
            }
        }

        Ok(())
    }
}

impl Serialize for PlayerList {
    fn serialize<S>(&self, serializer: S) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>
    where
        S: Serializer,
    {
        serializer.collect_str(&self)
    }
}

#[test]
fn test_serialize_player_list() {
    assert_eq!(
        "76561198024494988",
        PlayerList::new([76561198024494988]).to_string()
    );
    assert_eq!(
        "76561198024494988,76561197963701107",
        PlayerList::new([76561198024494988, 76561197963701107]).to_string()
    );
    assert_eq!(
        "76561198024494988,76561197963701107,76561197963701106",
        PlayerList::new([76561198024494988, 76561197963701107, 76561197963701106]).to_string()
    );
}

impl ListParams {
    /// Specify the backend name to filter demos with
    #[must_use]
    pub fn with_backend(self, backend: impl Into<String>) -> Self {
        ListParams {
            backend: Some(backend.into()),
            ..self
        }
    }

    /// Specify the map name to filter demos with
    #[must_use]
    pub fn with_map(self, map: impl Into<String>) -> Self {
        ListParams {
            map: Some(map.into()),
            ..self
        }
    }

    /// Specify the player steam ids to filter demos with
    #[must_use]
    pub fn with_players<T: Into<SteamID>, I: IntoIterator<Item = T>>(self, players: I) -> Self {
        ListParams {
            players: PlayerList::new(players),
            ..self
        }
    }

    /// Specify the game type to filter demos with
    #[must_use]
    pub fn with_type(self, ty: GameType) -> Self {
        ListParams {
            ty: Some(ty),
            ..self
        }
    }

    /// Specify the before date to filter demos with
    #[must_use]
    pub fn with_before(self, before: OffsetDateTime) -> Self {
        ListParams {
            before: Some(before),
            ..self
        }
    }

    /// Specify the after date to filter demos with
    #[must_use]
    pub fn with_after(self, after: OffsetDateTime) -> Self {
        ListParams {
            after: Some(after),
            ..self
        }
    }

    /// Specify the maximum demo id to filter demos with
    #[must_use]
    pub fn with_before_id(self, before: u64) -> Self {
        ListParams {
            before_id: Some(before),
            ..self
        }
    }

    /// Specify the minimum demo id to filter demos with
    #[must_use]
    pub fn with_after_id(self, after: u64) -> Self {
        ListParams {
            after_id: Some(after),
            ..self
        }
    }

    /// Specify the sort
    #[must_use]
    pub fn with_order(self, order: ListOrder) -> Self {
        ListParams { order, ..self }
    }
}