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
use serde_json;
use super::Note;

/// This enum represents an event that happens during a game of derby, and is
/// the main container for storing game data. Points, penalties, lineups,
/// and just about anything else that is associated with a jam goes into
/// a Jam Event of some sort. Each event is tagged with an event type,
/// which determined what information it contains.
#[derive(Serialize, Deserialize)]
#[serde(tag = "event")]
pub enum JamEvent {
    /// Information on one skater that has skated in a given jam.
    /// A typical jam will have 10 of these objects in the jam's events.
    #[serde(rename = "line up")]
    Lineup {
        skater: String,
        start_in_box: bool,
        position: Position,
    },
    #[serde(rename = "pack lap")]
    PackLap {
        timestamp: Option<Timestamp>,
        count: Option<u8>,
    },
    #[serde(rename = "penalty")]
    Penalty {
        timestamp: Option<Timestamp>,
        skater: String,
        penalty: String,
        severity: Option<PenaltySeverity>,
        rescinded: Option<bool>,
        involved: Option<Vec<Involved>>,
        cue: Option<String>,
    },
    #[serde(rename = "pass")]
    Pass {
        timestamp: Option<Timestamp>,
        completed: Option<bool>,
        number: u8,
        points: Option<u8>,
        skater: Option<String>,
        ghost_points: Option<Vec<GhostPoint>>
    },
    #[serde(rename = "star pass")]
    StarPass {
        timestamp: Option<Timestamp>,
        skater: Option<String>,
        team: Option<String>,
        completed: Option<bool>,
        failure: Option<String>,
    },
    #[serde(rename = "lead")]
    Lead {
        timestamp: Option<Timestamp>,
        skater: String,
    },
    #[serde(rename = "lost lead")]
    LostLead {
        timestamp: Option<Timestamp>,
        skater: String,
    },
    #[serde(rename = "call")]
    Call {
        timestamp: Option<Timestamp>,
        skater: Option<String>,
        team: Option<String>,
        official: Option<String>,
    },
    #[serde(rename = "enter box")]
    EnterBox {
        timestamp: Option<Timestamp>,
        skater: String,
        duration: Option<serde_json::Number>,
        substitute: Option<Substitute>,
        notes: Option<Vec<Note>>
    },
    #[serde(rename = "exit box", rename_all = "kebab-case")]
    ExitBox {
        timestamp: Option<Timestamp>,
        skater: String,
        duration: Option<serde_json::Number>,
        premature: Option<PrematureExitReason>,
        no_skater: Option<bool>
    },
    /// This object and its contents are not actually specified in the
    /// DerbyJSON spec
    #[serde(rename = "box time")]
    BoxTime { },
    
    #[serde(rename = "injury")]
    Injury {
        timestamp: Option<Timestamp>,
        skater: String,
    },
    #[serde(rename = "note")]
    Note {
        note: String,
        author: Option<String>,
        date: Option<String>,
        notes: Note,
    },

    #[serde(rename = "leave track", rename_all = "kebab-case")]
    LeaveTrack {
        timestamp: Option<Timestamp>,
        skater: String,
        reason: Option<LeaveTrackReason>,
        opposing_pass: u8,
    },

    #[serde(rename = "return track", rename_all = "kebab-case")]
    ReturnTrack {
        timestamp: Option<Timestamp>,
        skater: String,
        opposing_pass: u8,
    },
    // Action, Error,
}

/// A skater's position in a jam.
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum Position { Jammer, Pivot, Blocker }

/// Penalty severity.
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum PenaltySeverity { No, Minor, Major, Expulsion }

/// Reason why a skater left the penalty box early: due to officiating error,
/// skater leaving the box early, a rescinded penalty, or a skater who
/// mistakenly reported to the box.
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum PrematureExitReason { Official, Skater, Rescinded, Mistake }

#[derive(Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum LeaveTrackReason { Penalty, Injury, Malfuction, Other }
    
/// Represents a "ghost point" scored by means other than passing an
/// opponent's hips
#[derive(Serialize, Deserialize)]
pub struct GhostPoint {
    pub skater: Option<String>,
    pub ghost_point: GhostPointType,
}

/// Type of ghost point. Lap of jammer, Jammer in box, Blocker in box,
/// Pivot in box, Not on the track, Out of play, Ghost point of unknown causes
#[derive(Serialize, Deserialize)]
pub enum GhostPointType { L, J, B, P, N, O, G }

#[derive(Serialize, Deserialize)]
pub struct Involved {
    pub skater: String,
    #[serde(default)]
    pub notes: Vec<Note>,
}

#[derive(Serialize, Deserialize)]
pub struct Substitute {
    pub skater: String,
    pub reason: String,
}

/// Periods are broken into Jams, which are the basic unit of play for roller
/// derby.
#[derive(Serialize, Deserialize)]
pub struct Jam {
    pub number: u16,
    pub timestamp: Option<Timestamp>,
    pub duration: Option<u16>,
    pub events: Vec<JamEvent>,
    pub notes: Vec<Note>,
}

/// A stoppage of the game clock, whether for a team timeout, official review,
/// or official timeout.
#[derive(Serialize, Deserialize)]
pub struct Timeout {
    pub timeout: TeamType,
    #[serde(default)]
    pub notes: Vec<Note>,
    pub injury: Option<String>, // Skater
    pub duration: u32, // in seconds, including lineup time
    pub timestamp: Option<Timestamp>,
    pub review: Option<String>,
    pub resolution: Option<String>,
    pub retained: Option<bool>,
}

/// A thing that happens during a period, either a jam or a timeout, or possibly
/// just a note.
#[derive(Serialize, Deserialize)]
#[serde(untagged)]
pub enum ClockEvent {
    Note(Note),
    Jam(Jam),
    Timeout(Timeout),
}

#[derive(Serialize, Deserialize)]
pub enum TeamType { Home, Away, Officials }

#[derive(Serialize, Deserialize, Default)]
pub struct Period {
    pub timestamp: Option<Timestamp>,
    pub end: Option<Timestamp>,
    pub jams: Vec<ClockEvent>
}

#[derive(Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum Timestamp {
    Wall(String),
    Epoch(serde_json::Number),
    Period(String),
    Seconds(serde_json::Number),
    Jam(serde_json::Number),
    // TODO: Range, approximate
}