use crate::client::endpoints::Endpoint;
use crate::parse_date;
use crate::{BrawlClient, BrawlError};
use chrono::{DateTime, Utc};
use serde::Deserialize;
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BattleLog {
pub items: Vec<Battle>,
}
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Battle {
#[serde(deserialize_with = "parse_date")]
pub battle_time: DateTime<Utc>,
pub event: BattleEvent,
pub battle: BattleInfo,
}
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BattleEvent {
pub id: u64,
pub mode: Option<String>,
pub mode_id: u8,
pub map: Option<String>,
}
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BattleInfo {
pub mode: String,
#[serde(rename = "type")]
pub kind: String,
pub result: Option<String>,
pub duration: Option<u16>,
pub rank: Option<u8>,
pub trophy_change: Option<i8>,
#[serde(default)]
pub players: Vec<BattlePlayer>,
pub teams: Option<Vec<Vec<BattlePlayer>>>,
pub star_player: Option<BattlePlayer>,
}
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BattlePlayer {
pub tag: String,
pub name: String,
pub brawler: BattleBrawler,
}
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BattleBrawler {
pub id: u64,
pub name: String,
pub power: u8,
pub trophies: u32,
}
impl BattleLog {
pub async fn get(client: &BrawlClient, tag: &str) -> Result<Self, BrawlError> {
let endpoint = Endpoint::BattleLog(tag.to_string());
client.fetch::<Self>(endpoint).await
}
}