use chrono::{DateTime, FixedOffset};
use disciplines::DisciplineId;
use games::Games;
use opponents::Opponents;
use tournaments::TournamentId;
#[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 enum MatchFormat {
#[serde(rename = "none")]
None,
#[serde(rename = "one")]
One,
#[serde(rename = "home_away")]
HomeAway,
#[serde(rename = "bo3")]
BestOf3,
#[serde(rename = "bo5")]
BestOf5,
#[serde(rename = "bo7")]
BestOf7,
#[serde(rename = "bo9")]
BestOf9,
#[serde(rename = "bo11")]
BestOf11,
}
#[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,
#[serde(skip_serializing_if = "Option::is_none")]
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>);
}
impl Match {
pub fn iter_tournament<'a>(&self, client: &'a ::Toornament) -> ::TournamentMatchIter<'a> {
::TournamentMatchIter::new(
client,
self.tournament_id.clone(),
self.id.clone(),
self.games.is_some(),
)
}
pub fn into_iter_tournament(self, client: &::Toornament) -> ::TournamentMatchIter<'_> {
::TournamentMatchIter::new(client, self.tournament_id, self.id, self.games.is_some())
}
pub fn iter_discipline<'a>(&self, client: &'a ::Toornament) -> ::DisciplineMatchesIter<'a> {
::DisciplineMatchesIter::new(client, self.discipline_id.clone())
}
pub fn into_iter_discipline(self, client: &::Toornament) -> ::DisciplineMatchesIter<'_> {
::DisciplineMatchesIter::new(client, self.discipline_id)
}
}
#[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, MatchStatus, MatchType};
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 common::MatchResultSimple;
use matches::{MatchResult, MatchStatus};
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);
}
}