diff --git a/src/cgg_data_source.rs b/src/cgg_data_source.rs index 72b9a36..be01a08 100644 --- a/src/cgg_data_source.rs +++ b/src/cgg_data_source.rs @@ -108,7 +108,7 @@ impl DataSource for CGGDataSource { fn get_champs_with_positions_and_patch( &self, client: &ureq::Agent, - ) -> (IndexMap>, String) { + ) -> (IndexMap>, String) { let req = client.get("https://champion.gg").call().unwrap(); let page = &req.into_string().unwrap(); @@ -126,9 +126,9 @@ impl DataSource for CGGDataSource { } let patch = datas.lol.champions_report[0].patch.to_owned(); - let mut champions: IndexMap> = IndexMap::new(); + let mut champions: IndexMap> = IndexMap::new(); for champ in &datas.lol.champions_report { - let id = champ.champion_id.to_string(); + 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()); diff --git a/src/data_source.rs b/src/data_source.rs index 2b62d2b..f637502 100644 --- a/src/data_source.rs +++ b/src/data_source.rs @@ -47,7 +47,7 @@ pub trait DataSource { fn get_champs_with_positions_and_patch( &self, client: &ureq::Agent, - ) -> (IndexMap>, String); + ) -> (IndexMap>, String); fn get_champ_data_with_win_pourcentage( &self, diff --git a/src/kb_data_source.rs b/src/kb_data_source.rs index fdd9175..3f76065 100644 --- a/src/kb_data_source.rs +++ b/src/kb_data_source.rs @@ -91,7 +91,6 @@ struct Summoner { lazy_static! { static ref TOKEN: Mutex> = Mutex::new(None); - static ref INTERNAL_CLASSNAME_MAPPING: Mutex> = Mutex::new(IndexMap::new()); } impl KBDataSource { @@ -120,16 +119,6 @@ impl KBDataSource { } } - fn get_classname_mapping(&self, client: &ureq::Agent) -> IndexMap { - let mut mapping = IndexMap::new(); - if let Some(data) = self.get_champion_response(client) { - for champ in data.champions { - mapping.insert(champ.classname, champ.name); - } - }; - mapping - } - fn get_champion_response(&self, client: &ureq::Agent) -> Option { if let Some(token) = TOKEN.lock().unwrap().as_ref() { return match client @@ -291,10 +280,6 @@ impl DataSource for KBDataSource { if let Some(t) = self.get_auth_token(client) { TOKEN.lock().unwrap().replace(t); } - - for v in self.get_classname_mapping(client) { - INTERNAL_CLASSNAME_MAPPING.lock().unwrap().insert(v.0, v.1); - } } fn get_alias(&self) -> &str { @@ -308,7 +293,7 @@ impl DataSource for KBDataSource { fn get_champs_with_positions_and_patch( &self, client: &ureq::Agent, - ) -> (IndexMap>, String) { + ) -> (IndexMap>, String) { let mut champions = IndexMap::new(); let data: ChampionResponse = match self.get_champion_response(client) { Some(val) => val, @@ -321,7 +306,7 @@ impl DataSource for KBDataSource { None => return (champions, String::new()), }; for champ in data.champions { - champions.insert(champ.classname, KBDataSource::get_positions(champ.builds)); + champions.insert(champ.id, KBDataSource::get_positions(champ.builds)); } (champions, patch) } @@ -334,11 +319,10 @@ impl DataSource for KBDataSource { ) -> Vec<(String, Vec, Stat)> { let mut champ_data = vec![]; if let Some(token) = TOKEN.lock().unwrap().as_ref() { - if let Some(map_id) = INTERNAL_CLASSNAME_MAPPING.lock().unwrap().get(&champ.id) { let data: BuildResponse = match client .get(&format!( "https://api.koreanbuilds.net/builds?chmpname={}&patchid=-2&position=COMPOSITE", - map_id + champ.id )) .set("Accept", "application/json") .set("Authorization", token.as_str()) @@ -369,7 +353,6 @@ impl DataSource for KBDataSource { champ_data.push(self.get_build(&b)); } } - } } champ_data } @@ -396,7 +379,8 @@ mod tests { let client = ureq::AgentBuilder::new() .timeout(Duration::from_secs(10)) .build(); - let datasource = KBDataSource::new(&client); + 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); @@ -407,15 +391,16 @@ mod tests { let client = ureq::AgentBuilder::new() .timeout(Duration::from_secs(10)) .build(); - let datasource = KBDataSource::new(&client); + let datasource = KBDataSource; + datasource.init(&client); let champ = ChampInfo { - id: String::from("Aatrox"), - name: String::from("Aatrox"), + id: String::from("Annie"), + name: String::from("Annie"), key: String::from("1"), }; let result = datasource.get_champ_data_with_win_pourcentage( &champ, - &vec!["TOP".to_string()], + &vec!["MID".to_string()], &client, ); assert!(!result.is_empty()); diff --git a/src/main.rs b/src/main.rs index 82f1e85..915f58a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -118,9 +118,9 @@ fn main() -> Result<(), Box> { Ok(()) } -fn get_champ_from_key(champs: &Champion, key: &str) -> Option { +fn get_champ_from_key(champs: &Champion, key: u32) -> Option { for champ in champs.data.values() { - if key == champ.key { + if key.to_string() == champ.key { return Some(champ.id.to_owned()); } } @@ -152,7 +152,7 @@ fn execute_data_source( champion, lol_champs_dir, &patch, - id, + *id, positions, ); }); @@ -164,7 +164,7 @@ fn execute_data_source( champion, lol_champs_dir, &patch, - id, + *id, positions, ); thread::sleep(Duration::from_millis(data_source.get_timeout())); @@ -178,27 +178,21 @@ fn get_and_write_item_set( champion: &Champion, lol_champs_dir: &PathBuf, patch: &str, - id: &str, + id: u32, positions: &[String], ) { - let mut champ_id: String = id.to_owned(); - - if id.parse::().is_ok() { - if let Some(c_id) = get_champ_from_key(&champion, &champ_id) { - champ_id = c_id; - } - } - - if let Some(champ) = champion.data.get(&champ_id) { - if positions.is_empty() { - error!("{}: {} empty positions", data_source.get_alias(), &champ_id); + if let Some(champ_id) = get_champ_from_key(&champion, id) { + if let Some(champ) = champion.data.get(&champ_id) { + if positions.is_empty() { + error!("{}: {} empty positions", data_source.get_alias(), &champ_id); + } 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); + } } 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); + error!("{} not found in LoL champs", &champ_id); } - } else { - error!("{} not found in LoL champs", &champ_id); } } diff --git a/src/pb_data_source.rs b/src/pb_data_source.rs index f070481..1065cf8 100644 --- a/src/pb_data_source.rs +++ b/src/pb_data_source.rs @@ -19,7 +19,7 @@ impl DataSource for PBDataSource { fn get_champs_with_positions_and_patch( &self, _client: &ureq::Agent, - ) -> (IndexMap>, String) { + ) -> (IndexMap>, String) { (IndexMap::new(), String::new()) }