use crate::client::Client;
use crate::error::ApiError;
use crate::utils::Team;
const ENDPOINT_URL: &str = "/v1/wvw/match_details";
#[derive(Debug, Deserialize, PartialEq)]
pub struct Match {
#[serde(rename = "match_id")]
id: String,
scores: Vec<u32>,
maps: Vec<Map>,
}
#[derive(Debug, Deserialize, PartialEq)]
pub enum MapType {
RedHome,
GreenHome,
BlueHome,
Center,
}
#[derive(Debug, Deserialize, PartialEq)]
pub struct Objective {
id: u32,
owner: Team,
#[serde(default)]
owner_guild: String,
}
#[derive(Debug, Deserialize, PartialEq)]
pub struct Bonus {
#[serde(rename = "type")]
bonus_type: String,
owner: Team,
}
#[derive(Debug, Deserialize, PartialEq)]
pub struct Map {
#[serde(rename = "type")]
map_type: MapType,
scores: Vec<u32>,
#[serde(default)]
objectives: Vec<Objective>,
#[serde(default)]
bonuses: Vec<Bonus>,
}
impl Match {
pub fn get_by_id(client: &Client, id: String) -> Result<Match, ApiError> {
let url = format!("{}?match_id={}", ENDPOINT_URL, id);
client.request(&url)
}
pub fn id(&self) -> &str {
&self.id
}
pub fn scores(&self) -> &Vec<u32> {
&self.scores
}
pub fn maps(&self) -> &Vec<Map> {
&self.maps
}
}
impl Objective {
pub fn id(&self) -> u32 {
self.id
}
pub fn owner(&self) -> &Team {
&self.owner
}
pub fn owner_guild(&self) -> &str {
&self.owner_guild
}
}
impl Bonus {
pub fn bonus_type(&self) -> &str {
&self.bonus_type
}
pub fn owner(&self) -> &Team {
&self.owner
}
}
impl Map {
pub fn map_type(&self) -> &MapType {
&self.map_type
}
pub fn scores(&self) -> &Vec<u32> {
&self.scores
}
pub fn objectives(&self) -> &Vec<Objective> {
&self.objectives
}
pub fn bonuses(&self) -> &Vec<Bonus> {
&self.bonuses
}
}
#[cfg(test)]
mod tests {
use crate::v1::wvw::match_details::*;
use crate::client::Client;
const JSON_MATCH: &str = r#"
{
"match_id": "1-4",
"scores": [ 155502, 137176, 189824 ],
"maps": [
{
"type": "RedHome",
"scores": [ 80148, 7022, 18582 ],
"objectives": [
{ "id": 32, "owner": "Red", "owner_guild": "277CCE76-6254-4CF2-8A2D-15A30B7110BD" },
{ "id": 35, "owner": "Green" }
],
"bonuses": [
{ "type": "bloodlust", "owner": "Blue" }
]
},
{
"type": "GreenHome",
"scores": [ 13285, 7465, 82817 ],
"objectives": [
],
"bonuses": [
{ "type": "bloodlust", "owner": "Green" }
]
},
{
"type": "BlueHome",
"scores": [ 18592, 54837, 29013 ],
"objectives": [
],
"bonuses": [
]
},
{
"type": "Center",
"scores": [ 43477, 67852, 59412 ],
"objectives": [
],
"bonuses": [
]
}
]
}"#;
#[test]
fn create_match() {
match serde_json::from_str::<Match>(JSON_MATCH) {
Ok(_) => assert!(true),
Err(e) => panic!(e.to_string()),
}
}
#[test]
fn get_match_by_id() {
let client = Client::new();
let id = "1-4";
assert_eq!(Match::get_by_id(&client, id.to_string()).unwrap().id(), id)
}
}