use crate::client::Client;
use crate::error::ApiError;
use crate::utils::{ids_to_string, Profession, Team};
use std::collections::HashMap;
const ENDPOINT_URL: &str = "/v2/pvp/games";
#[derive(Debug, Deserialize, PartialEq)]
pub struct Game {
id: String,
map_id: u32,
#[serde(rename = "started")]
start_time: String,
#[serde(rename = "ended")]
end_time: String,
result: String,
team: Team,
profession: Profession,
scores: HashMap<Team, u32>,
rating_type: RatingType,
rating_change: Option<i32>,
season: Option<String>,
}
#[derive(Debug, Deserialize, PartialEq)]
pub enum RatingType {
Ranked,
Unranked,
#[serde(rename(deserialize = "None"))]
Custom,
}
impl Game {
pub fn get_id(client: &Client, id: String) -> Result<Game, ApiError> {
let url = format!("{}?id={}", ENDPOINT_URL, id);
client.authenticated_request(&url)?
}
pub fn get_all_ids(client: &Client) -> Result<Vec<String>, ApiError> {
client.authenticated_request(ENDPOINT_URL)
}
pub fn get_all_games(client: &Client) -> Result<Vec<Game>, ApiError> {
let url = format!("{}?ids=all", ENDPOINT_URL);
client.authenticated_request(&url)
}
pub fn get_games_by_ids(client: &Client, ids: Vec<String>) -> Result<Vec<Game>, ApiError> {
let url = format!("{}?ids={}", ENDPOINT_URL, ids_to_string(ids));
client.authenticated_request(&url)
}
pub fn id(&self) -> &str {
&self.id
}
pub fn map_id(&self) -> u32 {
self.map_id
}
pub fn start_time(&self) -> &str {
&self.start_time
}
pub fn end_time(&self) -> &str {
&self.end_time
}
pub fn result(&self) -> &str {
&self.result
}
pub fn team(&self) -> &Team {
&self.team
}
pub fn profession(&self) -> &Profession {
&self.profession
}
pub fn scores(&self) -> &HashMap<Team, u32> {
&self.scores
}
pub fn rating_type(&self) -> &RatingType {
&self.rating_type
}
pub fn rating_change(&self) -> &Option<i32> {
&self.rating_change
}
pub fn season(&self) -> &Option<String> {
&self.season
}
}
#[cfg(test)]
mod tests {
use crate::v2::pvp::games::{Game, Team, Profession, RatingType};
use crate::client::Client;
use crate::error::ApiError;
use std::env;
use std::collections::HashMap;
const JSON_GAME: &str = r#"{
"id": "ABCDE02B-8888-FEBA-1234-DE98765C7DEF",
"map_id": 894,
"started": "2015-07-08T21:29:50.000Z",
"ended": "2015-07-08T21:37:02.000Z",
"result": "Defeat",
"team": "Red",
"profession": "Guardian",
"scores": {
"red": 165,
"blue":507
},
"rating_type" : "Ranked",
"rating_change" : -26,
"season" : "49CCE661-9DCC-473B-B106-666FE9942721"
}"#;
#[test]
fn create_game() {
match serde_json::from_str::<Game>(JSON_GAME) {
Ok(_) => assert!(true),
Err(e) => panic!(e.to_string())
}
}
#[test]
fn accessors() {
let game = serde_json::from_str::<Game>(JSON_GAME).unwrap();
assert_eq!("ABCDE02B-8888-FEBA-1234-DE98765C7DEF", game.id());
assert_eq!(894, game.map_id());
assert_eq!("2015-07-08T21:29:50.000Z", game.start_time());
assert_eq!("2015-07-08T21:37:02.000Z", game.end_time());
assert_eq!("Defeat", game.result());
assert_eq!(&Team::Red, game.team());
assert_eq!(&Profession::Guardian, game.profession());
let mut scores = HashMap::new();
scores.insert(Team::Red, 165);
scores.insert(Team::Blue, 507);
assert_eq!(&scores, game.scores());
assert_eq!(&RatingType::Ranked, game.rating_type());
assert_eq!(&Some(-26), game.rating_change());
assert_eq!(&Some("49CCE661-9DCC-473B-B106-666FE9942721".to_string()), game.season());
}
#[test]
fn get_all_games() {
let api_key = env::var("GW2_TEST_KEY").expect("GW2_TEST_KEY environment variable is not set.");
let client = Client::new().set_api_key(api_key);
match Game::get_all_games(&client) {
Ok(_) => assert!(true),
Err(e) => panic!(e.description().to_string()),
};
}
#[test]
fn get_invalid_id() {
let api_key = env::var("GW2_TEST_KEY").expect("GW2_TEST_KEY environment variable is not set.");
let client = Client::new().set_api_key(api_key);
let id = "1".to_string();
assert_eq!(Err(ApiError::new("{\"text\":\"no such id\"}".to_string())), Game::get_id(&client, id.clone()));
}
#[test]
fn get_invalid_games_by_ids() {
let api_key = env::var("GW2_TEST_KEY").expect("GW2_TEST_KEY environment variable is not set.");
let ids = vec!("1".to_string(), "2".to_string());
let client = Client::new().set_api_key(api_key);
assert_eq!(Err(ApiError::new("{\"text\":\"all ids provided are invalid\"}".to_string())), Game::get_games_by_ids(&client, ids));
}
}