From 2d72bab0fe758be294544d20f9da82f43201d3aa Mon Sep 17 00:00:00 2001 From: nyyu Date: Tue, 16 Mar 2021 11:13:54 +0100 Subject: [PATCH] patch is by champ --- src/cgg_data_source.rs | 8 ++--- src/data_source.rs | 8 ++--- src/kb_data_source.rs | 81 ++++++++++++++++++++---------------------- src/main.rs | 8 ++--- src/pb_data_source.rs | 6 ++-- 5 files changed, 51 insertions(+), 60 deletions(-) diff --git a/src/cgg_data_source.rs b/src/cgg_data_source.rs index be01a08..7f43d39 100644 --- a/src/cgg_data_source.rs +++ b/src/cgg_data_source.rs @@ -105,10 +105,10 @@ impl DataSource for CGGDataSource { 0 } - fn get_champs_with_positions_and_patch( + fn get_champs_with_positions( &self, client: &ureq::Agent, - ) -> (IndexMap>, String) { + ) -> IndexMap> { let req = client.get("https://champion.gg").call().unwrap(); let page = &req.into_string().unwrap(); @@ -124,7 +124,6 @@ impl DataSource for CGGDataSource { .unwrap() .insert(entry.0.to_owned(), entry.1.to_owned()); } - let patch = datas.lol.champions_report[0].patch.to_owned(); let mut champions: IndexMap> = IndexMap::new(); for champ in &datas.lol.champions_report { @@ -142,7 +141,7 @@ impl DataSource for CGGDataSource { CHAMPIONS_REPORT.lock().unwrap().push(report); } - (champions, patch) + champions } fn get_champ_data_with_win_pourcentage( @@ -208,6 +207,7 @@ impl DataSource for CGGDataSource { win_rate: stat.win_rate.unwrap(), games: stat.games.unwrap(), kda: stat.kda.unwrap(), + patch: champ.patch.to_owned() }, )); } diff --git a/src/data_source.rs b/src/data_source.rs index f637502..48a8615 100644 --- a/src/data_source.rs +++ b/src/data_source.rs @@ -35,6 +35,7 @@ pub struct Stat { pub win_rate: f64, pub games: u32, pub kda: f64, + pub patch: String } pub trait DataSource { @@ -44,10 +45,10 @@ pub trait DataSource { fn get_timeout(&self) -> u64; - fn get_champs_with_positions_and_patch( + fn get_champs_with_positions( &self, client: &ureq::Agent, - ) -> (IndexMap>, String); + ) -> IndexMap>; fn get_champ_data_with_win_pourcentage( &self, @@ -60,7 +61,6 @@ pub trait DataSource { &self, champ: &ChampInfo, positions: &[String], - ver: &str, path: &PathBuf, client: &ureq::Agent, ) { @@ -100,7 +100,7 @@ pub trait DataSource { "{} {} {} - {:.2}% wins - {} games - {:.2} kda", self.get_alias(), build.0, - ver, + build.2.patch, build.2.win_rate, build.2.games, build.2.kda diff --git a/src/kb_data_source.rs b/src/kb_data_source.rs index 3f76065..4cfe5bc 100644 --- a/src/kb_data_source.rs +++ b/src/kb_data_source.rs @@ -73,8 +73,9 @@ struct KBBuild { #[serde(rename = "skillOrder")] skill_order: String, wins: f64, - games: f64, + games: u32, summoner: Summoner, + patch: Patch, } #[derive(Deserialize, Debug)] @@ -267,9 +268,10 @@ impl KBDataSource { build.position.to_owned().to_uppercase(), blocks, Stat { - win_rate: (build.wins / build.games) * 100., - games: build.games as u32, + win_rate: (build.wins / build.games as f64) * 100., + games: build.games, kda: 0.0, + patch: build.patch.patch_version.to_owned(), }, ) } @@ -290,25 +292,18 @@ impl DataSource for KBDataSource { 300 } - fn get_champs_with_positions_and_patch( - &self, - client: &ureq::Agent, - ) -> (IndexMap>, String) { + fn get_champs_with_positions(&self, client: &ureq::Agent) -> IndexMap> { let mut champions = IndexMap::new(); let data: ChampionResponse = match self.get_champion_response(client) { Some(val) => val, None => { - return (champions, String::new()); + return champions; } }; - let patch = match data.patches.get(0) { - Some(p) => p.patch_version.clone(), - None => return (champions, String::new()), - }; for champ in data.champions { champions.insert(champ.id, KBDataSource::get_positions(champ.builds)); } - (champions, patch) + champions } fn get_champ_data_with_win_pourcentage( @@ -319,40 +314,40 @@ impl DataSource for KBDataSource { ) -> Vec<(String, Vec, Stat)> { let mut champ_data = vec![]; if let Some(token) = TOKEN.lock().unwrap().as_ref() { - let data: BuildResponse = match client - .get(&format!( - "https://api.koreanbuilds.net/builds?chmpname={}&patchid=-2&position=COMPOSITE", - champ.id - )) - .set("Accept", "application/json") - .set("Authorization", token.as_str()) - .call() - { - Ok(resp) => match resp.into_json() { - Ok(val) => val, - Err(_) => { - return vec![]; - } - }, + let data: BuildResponse = match client + .get(&format!( + "https://api.koreanbuilds.net/builds?chmpname={}&patchid=-2&position=COMPOSITE", + champ.id + )) + .set("Accept", "application/json") + .set("Authorization", token.as_str()) + .call() + { + Ok(resp) => match resp.into_json() { + Ok(val) => val, Err(_) => { return vec![]; } - }; + }, + Err(_) => { + return vec![]; + } + }; - for pos in position { - let mut build: Option<&KBBuild> = None; + for pos in position { + let mut build: Option<&KBBuild> = None; - for b in &data.builds2 { - if b.position.to_uppercase() == *pos { - build = Some(b); - break; - } - } - - if let Some(b) = build { - champ_data.push(self.get_build(&b)); + for b in &data.builds2 { + if b.position.to_uppercase() == *pos { + build = Some(b); + break; } } + + if let Some(b) = build { + champ_data.push(self.get_build(&b)); + } + } } champ_data } @@ -381,9 +376,9 @@ mod tests { .build(); let datasource = KBDataSource; datasource.init(&client); - let champs_with_positions_and_patch = - datasource.get_champs_with_positions_and_patch(&client); - assert!(champs_with_positions_and_patch.0.len() > 0); + let champs_with_positions = + datasource.get_champs_with_positions(&client); + assert!(champs_with_positions.len() > 0); } #[test] diff --git a/src/main.rs b/src/main.rs index 915f58a..e5f82a7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -135,9 +135,8 @@ fn execute_data_source( ) { data_source.init(client); - let (champs, patch) = data_source.get_champs_with_positions_and_patch(&client); + let champs = data_source.get_champs_with_positions(&client); - info!("{} version: {}", data_source.get_alias(), patch); info!( "{} numbers of champs: {}", data_source.get_alias(), @@ -151,7 +150,6 @@ fn execute_data_source( client, champion, lol_champs_dir, - &patch, *id, positions, ); @@ -163,7 +161,6 @@ fn execute_data_source( client, champion, lol_champs_dir, - &patch, *id, positions, ); @@ -177,7 +174,6 @@ fn get_and_write_item_set( client: &ureq::Agent, champion: &Champion, lol_champs_dir: &PathBuf, - patch: &str, id: u32, positions: &[String], ) { @@ -188,7 +184,7 @@ fn get_and_write_item_set( } else { let path = lol_champs_dir.join(&champ_id).join("Recommended"); fs::create_dir_all(&path).unwrap(); - data_source.write_item_set(&champ, &positions, &patch, &path, &client); + data_source.write_item_set(&champ, &positions, &path, &client); } } else { error!("{} not found in LoL champs", &champ_id); diff --git a/src/pb_data_source.rs b/src/pb_data_source.rs index 1065cf8..4dfa50b 100644 --- a/src/pb_data_source.rs +++ b/src/pb_data_source.rs @@ -16,11 +16,11 @@ impl DataSource for PBDataSource { 0 } - fn get_champs_with_positions_and_patch( + fn get_champs_with_positions( &self, _client: &ureq::Agent, - ) -> (IndexMap>, String) { - (IndexMap::new(), String::new()) + ) -> IndexMap> { + IndexMap::new() } fn get_champ_data_with_win_pourcentage(