From 5b39644df0b33b17831cf74615b21cdfd4f661f6 Mon Sep 17 00:00:00 2001 From: nyyu Date: Mon, 15 Mar 2021 18:59:49 +0100 Subject: [PATCH] some threads --- src/main.rs | 153 +++++++++++++++++++++++++++++----------------------- 1 file changed, 86 insertions(+), 67 deletions(-) diff --git a/src/main.rs b/src/main.rs index 1320710..ddfb5d9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ use indexmap::IndexMap; +use lazy_static::lazy_static; #[cfg(target_os = "windows")] use log::debug; use log::{error, info, LevelFilter}; @@ -71,75 +72,52 @@ fn main() -> Result<(), Box> { .init()?; info!("CGG Item Sets"); - let lol_champs_dir = match lol_champ_dir() { - Ok(x) => x, - Err(_e) => PathBuf::from(DEFAULT_LOL_CHAMPS_DIR), - }; + lazy_static! { + static ref LOL_CHAMPS_DIR: PathBuf = match lol_champ_dir() { + Ok(x) => x, + Err(_e) => PathBuf::from(DEFAULT_LOL_CHAMPS_DIR), + }; + static ref CLIENT: ureq::Agent = ureq::AgentBuilder::new() + .timeout(Duration::from_secs(10)) + .build(); + static ref REALM: Realm = CLIENT + .get("https://ddragon.leagueoflegends.com/realms/euw.json") + .set(USER_AGENT_KEY, USER_AGENT_VALUE) + .call() + .unwrap() + .into_json() + .unwrap(); + static ref CHAMPION: Champion = CLIENT + .get(&format!( + "https://ddragon.leagueoflegends.com/cdn/{}/data/en_US/champion.json", + REALM.v + )) + .set(USER_AGENT_KEY, USER_AGENT_VALUE) + .call() + .unwrap() + .into_json() + .unwrap(); + static ref DATA_SOURCES: [Box; 3] = [ + Box::new(PBDataSource), + Box::new(CGGDataSource), + Box::new(KBDataSource::new(&CLIENT)), + ]; + } + info!( - "LoL Champs Folder: {}", - lol_champs_dir.to_str().unwrap_or(DEFAULT_LOL_CHAMPS_DIR) + "LoL Champs Folder: {}", LOL_CHAMPS_DIR.to_str().unwrap() ); - let client = ureq::AgentBuilder::new() - .timeout(Duration::from_secs(10)) - .build(); + info!("LoL version: {}", REALM.v); + info!("LoL numbers of champs: {}", CHAMPION.data.len()); - let realm: Realm = client - .get("https://ddragon.leagueoflegends.com/realms/euw.json") - .set(USER_AGENT_KEY, USER_AGENT_VALUE) - .call()? - .into_json()?; - info!("LoL version: {}", realm.v); - - let champion: Champion = client - .get(&format!( - "https://ddragon.leagueoflegends.com/cdn/{}/data/en_US/champion.json", - realm.v - )) - .set(USER_AGENT_KEY, USER_AGENT_VALUE) - .call()? - .into_json()?; - info!("LoL numbers of champs: {}", champion.data.len()); - - let data_sources: [Box; 3] = [ - Box::new(PBDataSource), - Box::new(CGGDataSource), - Box::new(KBDataSource::new(&client)), - ]; - - for data_source in data_sources.iter() { - let (champs, patch) = data_source.get_champs_with_positions_and_patch(&client); - - info!("{} version: {}", data_source.get_alias(), patch); - info!( - "{} numbers of champs: {}", - data_source.get_alias(), - champs.len() - ); - - for (id, positions) in &champs { - let mut champ_id = id.to_owned(); - - if !champion.data.contains_key(&champ_id) { - 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!("{} missing positions", &champ_id); - } else { - let path = lol_champs_dir.join(&champ_id).join("Recommended"); - fs::create_dir_all(&path)?; - for pos in positions { - data_source.write_item_set(&champ, &pos, &patch, &path, &client); - thread::sleep(Duration::from_millis(data_source.get_timeout())); - } - } - } else { - error!("{} not found in LoL champs", &champ_id); - } - } + let mut threads = vec![]; + for data_source in DATA_SOURCES.iter() { + threads.push(thread::spawn(move || { + execute_data_source(&data_source, &CLIENT, &CHAMPION, &LOL_CHAMPS_DIR) + })); + } + for child in threads { + let _ = child.join(); } Ok(()) } @@ -153,6 +131,47 @@ fn get_champ_from_key(champs: &Champion, key: &str) -> Option { None } +fn execute_data_source( + data_source: &Box, + client: &ureq::Agent, + champion: &Champion, + lol_champs_dir: &PathBuf, +) { + let (champs, patch) = data_source.get_champs_with_positions_and_patch(&client); + + info!("{} version: {}", data_source.get_alias(), patch); + info!( + "{} numbers of champs: {}", + data_source.get_alias(), + champs.len() + ); + + for (id, positions) in &champs { + let mut champ_id: String = id.to_owned(); + + if !champion.data.contains_key(&champ_id) { + 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!("{} missing positions", &champ_id); + } else { + let path = lol_champs_dir.join(&champ_id).join("Recommended"); + fs::create_dir_all(&path).unwrap(); + for pos in positions { + data_source.write_item_set(&champ, &pos, &patch, &path, &client); + thread::sleep(Duration::from_millis(data_source.get_timeout())); + } + } + } else { + error!("{} not found in LoL champs", &champ_id); + } + } +} + #[cfg(target_os = "windows")] fn lol_champ_dir() -> Result { let hklm = RegKey::predef(winreg::enums::HKEY_LOCAL_MACHINE); @@ -194,7 +213,7 @@ fn lol_champ_dir() -> Result { } else { return Err(Error::from(ErrorKind::NotFound)); }; - Ok(PathBuf::from(path).join("Config").join("Champions")) + Ok(path.join("Config").join("Champions")) } #[cfg(not(target_os = "windows"))]