diff --git a/src/cgg_data_source.rs b/src/cgg_data_source.rs deleted file mode 100644 index 0aba79f..0000000 --- a/src/cgg_data_source.rs +++ /dev/null @@ -1,188 +0,0 @@ -use indexmap::IndexMap; -use log::error; -use serde_derive::Deserialize; -use serde_json::{json, Value}; -use std::sync::Mutex; - -use crate::data_source::{DataSource, Stat}; -use crate::ChampInfo; -use crate::Champion; - -#[derive(Deserialize)] -struct BuildResponse { - lol: Lol, -} - -#[derive(Deserialize)] -struct Lol { - #[serde(rename = "championsReport")] - champions_report: Vec, -} - -#[derive(Deserialize)] -struct ChampionReport { - champion_id: u32, - role: String, - patch: String, - stats: Stats, -} - -#[derive(Deserialize)] -struct Stats { - starting_items: Build, - core_builds: Build, - big_item_builds: Build, - skills: Build, - most_common_starting_items: Build, - most_common_core_builds: Build, - most_common_big_item_builds: Build, - most_common_skills: Build, - games: u32, - kills: u32, - deaths: u32, - wins: u32, - assists: u32, -} - -#[derive(Deserialize)] -struct Build { - build: Vec, - win_rate: f32, - games: u32, -} - -pub struct CGGDataSource { - champions_report: Mutex>, -} - -impl CGGDataSource { - pub fn new() -> CGGDataSource { - Self { - champions_report: Mutex::new(Vec::new()), - } - } - - fn make_item_set(&self, build: &Build, label: &str) -> Value { - json!({ - "items": build.build.iter().map(|x| json!({"id": x.to_string(), "count": 1})).collect::>(), - "type": format!("{} ({:.2}% - {} games)", label, build.win_rate * 100., build.games) - }) - } - - fn make_item_set_from_list(&self, build: &Build, label: &str, skills: &Build) -> Value { - let key_order = skills - .build - .iter() - .map(|x| x.to_string()) - .collect::>() - .join(""); - - self.make_item_set(build, [label, key_order.as_str()].join(" ").as_str()) - } -} - -fn extract_json(pattern: &str, page: &str) -> String { - let json = page[page.find(pattern).unwrap() + pattern.len()..].to_owned(); - json[..json.find("};").unwrap() + 1].replace("undefined", "null") -} - -impl DataSource for CGGDataSource { - fn get_alias(&self) -> &str { - "CGG" - } - - fn get_timeout(&self) -> u64 { - 0 - } - - fn get_champs_with_positions( - &self, - client: &ureq::Agent, - _champion: &Champion, - ) -> IndexMap> { - let mut champions: IndexMap> = IndexMap::new(); - - match client.get("https://champion.gg").call() { - Ok(req) => { - let page = &req.into_string().unwrap(); - - let datas: BuildResponse = - serde_json::from_str(&extract_json("window.__PRELOADED_STATE__ = ", page)) - .unwrap(); - - for champ in &datas.lol.champions_report { - let id = champ.champion_id; - if champions.contains_key(&id) { - let mut roles = champions.get(&id).unwrap().to_owned(); - roles.push(champ.role.to_owned()); - champions.insert(id, roles); - } else { - champions.insert(id, vec![champ.role.to_owned()]); - } - } - - for report in datas.lol.champions_report { - self.champions_report.lock().unwrap().push(report); - } - } - Err(e) => error!("Error retrieving data: {}", e), - } - champions - } - - fn get_champ_data_with_win_pourcentage( - &self, - champ: &ChampInfo, - positions: &[String], - _client: &ureq::Agent, - ) -> Vec<(String, Vec, Stat)> { - let mut data = vec![]; - for position in positions { - let mut some_champ: Option<&ChampionReport> = None; - let reports = self.champions_report.lock().unwrap(); - for champion in reports.iter() { - if champion.champion_id.to_string() == champ.key && champion.role == *position { - some_champ = Some(champion); - break; - } - } - if let Some(champ) = some_champ { - let blocks = vec![ - self.make_item_set_from_list( - &champ.stats.starting_items, - "Highest % Win Starting Items | Skills: ", - &champ.stats.skills, - ), - self.make_item_set(&champ.stats.core_builds, "Highest % Win Core Build Path:"), - self.make_item_set(&champ.stats.big_item_builds, "Highest % Win Big Items:"), - self.make_item_set_from_list( - &champ.stats.most_common_starting_items, - "Most Frequent Starting Items | Skills: ", - &champ.stats.most_common_skills, - ), - self.make_item_set( - &champ.stats.most_common_core_builds, - "Most Frequent Build Path", - ), - self.make_item_set( - &champ.stats.most_common_big_item_builds, - "Most Frequent Big Items:", - ), - ]; - - data.push(( - position.to_owned(), - blocks, - Stat { - win_rate: (champ.stats.wins as f32 / champ.stats.games as f32) * 100f32, - games: champ.stats.games, - kda: (champ.stats.kills + champ.stats.assists) as f32 - / champ.stats.deaths as f32, - patch: champ.patch.to_owned(), - }, - )); - } - } - data - } -} diff --git a/src/main.rs b/src/main.rs index fbdbdde..40b3754 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,13 +19,11 @@ use time::Duration; #[cfg(target_os = "windows")] use winreg::RegKey; -mod cgg_data_source; mod data_source; mod kb_data_source; mod ms_data_source; mod pb_data_source; -use cgg_data_source::CGGDataSource; use data_source::DataSource; use kb_data_source::KBDataSource; use ms_data_source::MSDataSource; @@ -104,7 +102,6 @@ fn main() -> Result<(), Box> { let data_sources: Vec> = vec![ Box::new(PBDataSource), - Box::new(CGGDataSource::new()), Box::new(KBDataSource::new()), Box::new(MSDataSource), ];