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
use chrono::{ DateTime, FixedOffset };

use disciplines::DisciplineId;
use tournaments::TournamentId;
use common::Opponents;
use games::Games;

/// Match unique identificator.
#[derive(Clone, Default, Debug, Eq, Ord, PartialEq, PartialOrd, Serialize, Deserialize)]
pub struct MatchId(pub String);

/// A match type enumeration.
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd, Serialize, Deserialize)]
pub enum MatchType {
    /// Duel match type
    #[serde(rename = "duel")]
    Duel,
    /// FFA match type
    #[serde(rename = "ffa")]
    FreeForAll,
}

/// A match status.
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum MatchStatus {
    /// Implies the match has not started yet
    Pending,
    /// Means it has started but not yet ended
    Running,
    /// Indicates the match is finished
    Completed
}

/// Tournament or discipline match definition.
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd, Serialize, Deserialize)]
pub struct Match {
    /// A hexadecimal unique identifier for this match.
    /// Example: "5617bb3af3df95f2318b4567"
    pub id: MatchId,
    /// Type of match: "duel" means only two opponents are involved; "ffa" means more than two opponents are involved.
    /// Possible values: duel, ffa
    #[serde(rename = "type")]
    pub match_type: MatchType,
    /// The discipline unique identifier of the match.
    /// Example: "my_discipline"
    #[serde(rename = "discipline")]
    pub discipline_id: DisciplineId,
    /// Status of the match: "pending" implies it has not yet started; "running" means it has started but not yet ended; "completed" indicates the match is finished.
    /// Possible values: pending, running, completed
    pub status: MatchStatus,
    /// The tournament's unique identifier of this match.
    /// Example: "5608fd12140ba061298b4569"
    pub tournament_id: TournamentId,
    /// Number of this match.
    /// Example: 1
    pub number: u64,
    /// Stage number of this match.
    /// Example: 1
    pub stage_number: u64,
    /// Group number of this match.
    /// Example: 1
    pub group_number: u64,
    /// Round number of this match.
    /// Example: 1
    pub round_number: u64,
    /// Date of this match, either expected or actual. This value is represented as an ISO 8601 date containing the date, the time and the time zone.
    /// Example: "2015-09-06T00:10:00-0600"
    pub date: DateTime<FixedOffset>,
    /// List of the opponents involved in this match.
    pub opponents: Opponents,
    /// This property is added when the parameter "with_games" is enabled.
    pub games: Option<Games>,
}
impl Match {
    builder!(id, MatchId);
    builder!(match_type, MatchType);
    builder!(discipline_id, DisciplineId);
    builder!(status, MatchStatus);
    builder!(tournament_id, TournamentId);
    builder!(number, u64);
    builder!(stage_number, u64);
    builder!(group_number, u64);
    builder!(round_number, u64);
    builder!(date, DateTime<FixedOffset>);
}

/// A list of `Match` objects.
#[derive(Clone, Debug, Default, Eq, Ord, PartialEq, PartialOrd, Serialize, Deserialize)]
pub struct Matches(pub Vec<Match>);

/// Result of a match
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd, Serialize, Deserialize)]
pub struct MatchResult {
    /// Status of a match
    pub status: MatchStatus,
    /// Opponents in a match
    pub opponents: Opponents,
}


#[cfg(test)]
mod tests {
    use ::serde_json;

    #[test]
    fn test_match_parse() {
        use matches::{ Match, MatchType, MatchStatus };
        let string = r#"
        {
            "id": "5617bb3af3df95f2318b4567",
            "type": "duel",
            "discipline": "my_discipline",
            "status": "pending",
            "tournament_id": "5608fd12140ba061298b4569",
            "number": 1,
            "stage_number": 1,
            "group_number": 2,
            "round_number": 3,
            "date": "2015-09-06T00:10:00-0600",
            "timezone": "America\/Chicago",
            "match_format": "bo3",
            "opponents": [
                {
                    "number": 1,
                    "participant": {
                        "id": "5617c3acf3df959e368b4567",
                        "name": "Evil Geniuses",
                        "country": "US"
                    },
                    "result": 1,
                    "score": null,
                    "forfeit": false
                }
            ]
        }"#;
        let d: Match = serde_json::from_str(string).unwrap();

        assert_eq!(d.id.0, "5617bb3af3df95f2318b4567");
        assert_eq!(d.match_type, MatchType::Duel);
        assert_eq!(d.discipline_id.0, "my_discipline");
        assert_eq!(d.status, MatchStatus::Pending);
        assert_eq!(d.tournament_id.0, "5608fd12140ba061298b4569");
        assert_eq!(d.number, 1u64);
        assert_eq!(d.stage_number, 1u64);
        assert_eq!(d.group_number, 2u64);
        assert_eq!(d.round_number, 3u64);
    }

    #[test]
    fn test_parse_match_results() {
        use matches::{ MatchStatus, MatchResult };
        use common::MatchResultSimple;
        let string = r#"
        {
            "status": "pending",
            "opponents": [
                {
                    "number": 1,
                    "result": 1,
                    "score": null,
                    "forfeit": false
                }
            ]
        }"#;
        let r: MatchResult = serde_json::from_str(string).unwrap();

        assert_eq!(r.status, MatchStatus::Pending);
        let op = r.opponents.0.iter().next().unwrap();
        assert_eq!(op.number, 1);
        assert_eq!(op.result, Some(MatchResultSimple::Win));
        assert_eq!(op.score, None);
        assert_eq!(op.forfeit, false);
    }
}