From 3212938a2a7f711e711134997f223d7cbc1fd776 Mon Sep 17 00:00:00 2001 From: nyyu Date: Sun, 14 Mar 2021 14:25:37 +0100 Subject: [PATCH] Add games and kda to title item set --- src/cgg_data_source.rs | 20 +++++++++++++------- src/data_source.rs | 10 ++++++++-- src/kb_data_source.rs | 13 ++++++++++--- src/pb_data_source.rs | 4 ++-- 4 files changed, 33 insertions(+), 14 deletions(-) diff --git a/src/cgg_data_source.rs b/src/cgg_data_source.rs index 718d2c5..3e78ba1 100644 --- a/src/cgg_data_source.rs +++ b/src/cgg_data_source.rs @@ -6,7 +6,7 @@ use serde_json::{json, Value}; use std::collections::HashMap; use std::sync::Mutex; -use crate::data_source::DataSource; +use crate::data_source::{DataSource, Stat}; use crate::ChampInfo; #[derive(Deserialize, Debug)] @@ -132,14 +132,13 @@ impl DataSource for CGGDataSource { let mut champions: IndexMap> = IndexMap::new(); for champ in &datas.lol.champions_report { let id = champ.champion_id.to_string(); - if champions.contains_key(&id) { let mut roles = champions.get(&id).unwrap().clone(); roles.push(champ.role.clone()); champions.insert(id, roles); } else { - champions.insert(id, [champ.role.clone()].to_vec()); - } + champions.insert(id, vec![champ.role.clone()]); + } } for report in datas.lol.champions_report { @@ -154,7 +153,7 @@ impl DataSource for CGGDataSource { champ: &ChampInfo, position: &str, _client: &ureq::Agent, - ) -> Option<(Vec, f64)> { + ) -> Option<(Vec, Stat)> { let mut some_champ: Option<&ChampionReport> = None; let reports = CHAMPIONS_REPORT.lock().unwrap(); for champion in reports.iter() { @@ -197,9 +196,16 @@ impl DataSource for CGGDataSource { key = val.stats.as_ref().unwrap().id.clone(); } } - let win_rate = champs_stats.get(&key).unwrap().win_rate; - return Some((blocks, win_rate.unwrap())); + let stat = champs_stats.get(&key).unwrap(); + return Some(( + blocks, + Stat { + win_rate: stat.win_rate.unwrap(), + games: stat.games.unwrap(), + kda: stat.kda.unwrap(), + }, + )); } None } diff --git a/src/data_source.rs b/src/data_source.rs index 7edbc48..924c936 100644 --- a/src/data_source.rs +++ b/src/data_source.rs @@ -31,6 +31,12 @@ pub struct Item { pub count: u8, } +pub struct Stat { + pub win_rate: f64, + pub games: u32, + pub kda: f64, +} + pub trait DataSource { fn get_alias(&self) -> &str; @@ -46,7 +52,7 @@ pub trait DataSource { champ: &ChampInfo, position: &str, client: &ureq::Agent, - ) -> Option<(Vec, f64)>; + ) -> Option<(Vec, Stat)>; fn write_item_set( &self, @@ -62,7 +68,7 @@ pub trait DataSource { match data { Some(data) => { let item_set = ItemSet { - title: format!("{} {} {} - {:.2}%", self.get_alias(), pos, ver, data.1), + title: format!("{} {} {} - {:.2}% wins - {} games - {:.2} kda", self.get_alias(), pos, ver, data.1.win_rate, data.1.games, data.1.kda), type_: "custom".to_string(), map: "any".to_string(), mode: "any".to_string(), diff --git a/src/kb_data_source.rs b/src/kb_data_source.rs index 0b33510..dc4fa00 100644 --- a/src/kb_data_source.rs +++ b/src/kb_data_source.rs @@ -1,4 +1,4 @@ -use crate::data_source::{Build, DataSource, Item}; +use crate::data_source::{Build, DataSource, Item, Stat}; #[cfg(test)] use crate::time::Duration; use crate::ChampInfo; @@ -211,7 +211,7 @@ impl DataSource for KBDataSource { champ: &ChampInfo, position: &str, client: &ureq::Agent, - ) -> Option<(Vec, f64)> { + ) -> Option<(Vec, Stat)> { let token = match self.token.clone() { Some(t) => t, None => return None, @@ -338,7 +338,14 @@ impl DataSource for KBDataSource { items: final_items })); } - Some((blocks, winrate)) + Some(( + blocks, + Stat { + win_rate: winrate, + games: data.builds2[0].games as u32, + kda: 0.0, + }, + )) } } diff --git a/src/pb_data_source.rs b/src/pb_data_source.rs index 0de9e56..fd901d5 100644 --- a/src/pb_data_source.rs +++ b/src/pb_data_source.rs @@ -1,7 +1,7 @@ use indexmap::IndexMap; use serde_json::Value; -use crate::data_source::DataSource; +use crate::data_source::{DataSource, Stat}; use crate::ChampInfo; pub struct PBDataSource; @@ -26,7 +26,7 @@ impl DataSource for PBDataSource { _champ: &ChampInfo, _position: &str, _client: &ureq::Agent, - ) -> Option<(Vec, f64)> { + ) -> Option<(Vec, Stat)> { None } }