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
//! A simplified and normalized data model, with shared data referenced by ID.
//!
//! This doesn't include all of the metadata from speedrun.com, and excludes
//! corrupt records and rejected or pending runs.
#![allow(missing_docs)]
use std::convert::From;

use chrono::{DateTime, NaiveDate, Utc};
use getset::Getters;
#[allow(unused)] use log::{debug, error, info, trace, warn};
use serde::{Deserialize, Serialize};
use validator::{Validate, ValidationError, ValidationErrors};
use validator_derive::Validate;

use crate::utils::{base36, src_slugify};

// We currently represent all ids as u64s for efficiency.
// You can use [crate::utils] to convert to and from speedrun.com's
// API IDs. (This isn't the same conversion as speedrun.com uses,
// so the ordering of these IDs doesn't align with insertion time
// or anything like that.)

#[derive(
    Debug,
    Serialize,
    Deserialize,
    Clone,
    PartialEq,
    Hash,
    PartialOrd,
    Ord,
    Eq,
    Getters,
    Validate,
)]
#[serde(deny_unknown_fields)]
#[get = "pub"]
pub struct Category {
    pub game_id: u64,
    #[validate(length(min = 1))]
    pub slug:    String,
    #[validate(length(min = 1))]
    pub name:    String,
    pub id:      u64,
    pub per:     CategoryType,
    pub rules:   String,
}

impl Category {
    /// This item's ID as it would be formatted for SpeedRun.Com.
    pub fn src_id(&self) -> String {
        base36(*self.id())
    }

    /// This item's URL as it would be formatted for SpeedRun.com.
    pub fn src_slug(&self) -> String {
        src_slugify(self.name())
    }
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Hash, PartialOrd, Ord, Eq)]
#[serde(deny_unknown_fields)]
pub enum CategoryType {
    PerGame,
    PerLevel,
}

#[derive(
    Debug,
    Serialize,
    Deserialize,
    Clone,
    PartialEq,
    Hash,
    PartialOrd,
    Ord,
    Eq,
    Getters,
    Validate,
)]
#[serde(deny_unknown_fields)]
#[get = "pub"]
pub struct User {
    pub created: Option<DateTime<Utc>>,
    #[validate(length(min = 1))]
    pub slug:    String,
    #[validate(length(min = 1))]
    pub name:    String,
    pub id:      u64,
}

impl User {
    /// This item's ID as it would be formatted for SpeedRun.Com.
    pub fn src_id(&self) -> String {
        base36(*self.id())
    }

    /// This item's URL as it would be formatted for SpeedRun.com.
    pub fn src_slug(&self) -> String {
        src_slugify(self.name())
    }
}

#[derive(
    Debug,
    Serialize,
    Deserialize,
    Clone,
    PartialEq,
    Hash,
    PartialOrd,
    Ord,
    Eq,
    Getters,
    Validate,
)]
#[serde(deny_unknown_fields)]
#[get = "pub"]
pub struct Game {
    pub id:             u64,
    pub created:        Option<DateTime<Utc>>,
    #[validate(length(min = 1))]
    pub slug:           String,
    #[validate(length(min = 1))]
    pub src_slug:       String,
    #[validate(length(min = 1))]
    pub name:           String,
    pub primary_timing: TimingMethod,
}

impl Game {
    /// This item's ID as it would be formatted for SpeedRun.Com.
    pub fn src_id(&self) -> String {
        base36(*self.id())
    }
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Hash, PartialOrd, Ord, Eq)]
#[serde(deny_unknown_fields)]
#[allow(non_camel_case_types)]
pub enum TimingMethod {
    IGT,
    RTA,
    RTA_NL,
}

#[derive(
    Debug,
    Serialize,
    Deserialize,
    Clone,
    PartialEq,
    Hash,
    PartialOrd,
    Ord,
    Eq,
    Getters,
    Validate,
)]
#[serde(deny_unknown_fields)]
#[get = "pub"]
pub struct Level {
    pub game_id: u64,
    pub id:      u64,
    #[validate(length(min = 1))]
    pub slug:    String,
    #[validate(length(min = 1))]
    pub name:    String,
    pub rules:   String,
}

impl Level {
    /// This item's ID as it would be formatted for SpeedRun.Com.
    pub fn src_id(&self) -> String {
        base36(*self.id())
    }

    /// This item's URL as it would be formatted for SpeedRun.com.
    pub fn src_slug(&self) -> String {
        src_slugify(self.name())
    }
}

#[derive(
    Debug,
    Serialize,
    Deserialize,
    PartialEq,
    Hash,
    Clone,
    PartialOrd,
    Ord,
    Eq,
    Getters,
    Validate,
)]
// disabled for now to allow unused .video_url on supplemental data
// #[serde(deny_unknown_fields)]
#[get = "pub"]
pub struct Run {
    pub game_id:     u64,
    pub category_id: u64,
    pub level_id:    Option<u64>,
    pub id:          u64,
    pub created:     Option<DateTime<Utc>>,
    pub date:        Option<NaiveDate>,
    #[validate]
    pub times_ms:    RunTimesMs,
    #[validate]
    pub players:     Vec<RunPlayer>,
}

impl Run {
    /// This item's ID as it would be formatted for SpeedRun.Com.
    pub fn src_id(&self) -> String {
        base36(*self.id())
    }
}

#[derive(
    Debug, Serialize, Deserialize, PartialEq, Hash, Clone, PartialOrd, Ord, Eq, Getters,
)]
#[serde(deny_unknown_fields)]
#[get = "pub"]
pub struct RunTimesMs {
    pub igt:    Option<u64>,
    pub rta:    Option<u64>,
    pub rta_nl: Option<u64>,
}

impl RunTimesMs {
    pub fn get(&self, timing: &TimingMethod) -> Option<u64> {
        match timing {
            TimingMethod::IGT => *self.igt(),
            TimingMethod::RTA => *self.rta(),
            TimingMethod::RTA_NL => *self.rta_nl(),
        }
    }
}

impl Validate for RunTimesMs {
    fn validate(&self) -> Result<(), ValidationErrors> {
        if self.igt == None && self.rta == None && self.rta_nl == None {
            let mut errors = ValidationErrors::new();
            errors.add("", ValidationError::new("all times were None"));
            return Err(errors)
        }
        Ok(())
    }
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Hash, PartialOrd, Ord, Eq)]
#[serde(deny_unknown_fields)]
pub enum RunPlayer {
    UserId(u64),
    GuestName(String),
}

impl Validate for RunPlayer {
    fn validate(&self) -> Result<(), ValidationErrors> {
        if let RunPlayer::GuestName(name) = self {
            if name.is_empty() {
                let mut errors = ValidationErrors::new();
                errors.add("GuestName.0", ValidationError::new("name is empty"));
                return Err(errors)
            }
        }
        Ok(())
    }
}