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;
#[derive(Clone, Default, Debug, Eq, Ord, PartialEq, PartialOrd, Serialize, Deserialize)]
pub struct MatchId(pub String);
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd, Serialize, Deserialize)]
pub enum MatchType {
#[serde(rename = "duel")]
Duel,
#[serde(rename = "ffa")]
FreeForAll,
}
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum MatchStatus {
Pending,
Running,
Completed
}
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd, Serialize, Deserialize)]
pub struct Match {
pub id: MatchId,
#[serde(rename = "type")]
pub match_type: MatchType,
#[serde(rename = "discipline")]
pub discipline_id: DisciplineId,
pub status: MatchStatus,
pub tournament_id: TournamentId,
pub number: u64,
pub stage_number: u64,
pub group_number: u64,
pub round_number: u64,
pub date: DateTime<FixedOffset>,
pub opponents: Opponents,
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>);
}
#[derive(Clone, Debug, Default, Eq, Ord, PartialEq, PartialOrd, Serialize, Deserialize)]
pub struct Matches(pub Vec<Match>);
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd, Serialize, Deserialize)]
pub struct MatchResult {
pub status: MatchStatus,
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);
}
}