some threads

This commit is contained in:
nyyu 2021-03-15 18:59:49 +01:00 committed by Gitea
parent 15be38aff9
commit 5b39644df0

View file

@ -1,4 +1,5 @@
use indexmap::IndexMap; use indexmap::IndexMap;
use lazy_static::lazy_static;
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
use log::debug; use log::debug;
use log::{error, info, LevelFilter}; use log::{error, info, LevelFilter};
@ -71,42 +72,71 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
.init()?; .init()?;
info!("CGG Item Sets"); info!("CGG Item Sets");
let lol_champs_dir = match lol_champ_dir() { lazy_static! {
static ref LOL_CHAMPS_DIR: PathBuf = match lol_champ_dir() {
Ok(x) => x, Ok(x) => x,
Err(_e) => PathBuf::from(DEFAULT_LOL_CHAMPS_DIR), Err(_e) => PathBuf::from(DEFAULT_LOL_CHAMPS_DIR),
}; };
info!( static ref CLIENT: ureq::Agent = ureq::AgentBuilder::new()
"LoL Champs Folder: {}",
lol_champs_dir.to_str().unwrap_or(DEFAULT_LOL_CHAMPS_DIR)
);
let client = ureq::AgentBuilder::new()
.timeout(Duration::from_secs(10)) .timeout(Duration::from_secs(10))
.build(); .build();
static ref REALM: Realm = CLIENT
let realm: Realm = client
.get("https://ddragon.leagueoflegends.com/realms/euw.json") .get("https://ddragon.leagueoflegends.com/realms/euw.json")
.set(USER_AGENT_KEY, USER_AGENT_VALUE) .set(USER_AGENT_KEY, USER_AGENT_VALUE)
.call()? .call()
.into_json()?; .unwrap()
info!("LoL version: {}", realm.v); .into_json()
.unwrap();
let champion: Champion = client static ref CHAMPION: Champion = CLIENT
.get(&format!( .get(&format!(
"https://ddragon.leagueoflegends.com/cdn/{}/data/en_US/champion.json", "https://ddragon.leagueoflegends.com/cdn/{}/data/en_US/champion.json",
realm.v REALM.v
)) ))
.set(USER_AGENT_KEY, USER_AGENT_VALUE) .set(USER_AGENT_KEY, USER_AGENT_VALUE)
.call()? .call()
.into_json()?; .unwrap()
info!("LoL numbers of champs: {}", champion.data.len()); .into_json()
.unwrap();
let data_sources: [Box<dyn DataSource>; 3] = [ static ref DATA_SOURCES: [Box<dyn DataSource + Sync + Send>; 3] = [
Box::new(PBDataSource), Box::new(PBDataSource),
Box::new(CGGDataSource), Box::new(CGGDataSource),
Box::new(KBDataSource::new(&client)), Box::new(KBDataSource::new(&CLIENT)),
]; ];
}
for data_source in data_sources.iter() { info!(
"LoL Champs Folder: {}", LOL_CHAMPS_DIR.to_str().unwrap()
);
info!("LoL version: {}", REALM.v);
info!("LoL numbers of champs: {}", CHAMPION.data.len());
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(())
}
fn get_champ_from_key(champs: &Champion, key: &str) -> Option<String> {
for champ in champs.data.values() {
if key == champ.key {
return Some(champ.id.to_owned());
}
}
None
}
fn execute_data_source(
data_source: &Box<dyn DataSource + Sync + Send>,
client: &ureq::Agent,
champion: &Champion,
lol_champs_dir: &PathBuf,
) {
let (champs, patch) = data_source.get_champs_with_positions_and_patch(&client); let (champs, patch) = data_source.get_champs_with_positions_and_patch(&client);
info!("{} version: {}", data_source.get_alias(), patch); info!("{} version: {}", data_source.get_alias(), patch);
@ -117,7 +147,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
); );
for (id, positions) in &champs { for (id, positions) in &champs {
let mut champ_id = id.to_owned(); let mut champ_id: String = id.to_owned();
if !champion.data.contains_key(&champ_id) { if !champion.data.contains_key(&champ_id) {
if let Some(c_id) = get_champ_from_key(&champion, &champ_id) { if let Some(c_id) = get_champ_from_key(&champion, &champ_id) {
@ -130,7 +160,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
error!("{} missing positions", &champ_id); error!("{} missing positions", &champ_id);
} else { } else {
let path = lol_champs_dir.join(&champ_id).join("Recommended"); let path = lol_champs_dir.join(&champ_id).join("Recommended");
fs::create_dir_all(&path)?; fs::create_dir_all(&path).unwrap();
for pos in positions { for pos in positions {
data_source.write_item_set(&champ, &pos, &patch, &path, &client); data_source.write_item_set(&champ, &pos, &patch, &path, &client);
thread::sleep(Duration::from_millis(data_source.get_timeout())); thread::sleep(Duration::from_millis(data_source.get_timeout()));
@ -141,17 +171,6 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
} }
} }
} }
Ok(())
}
fn get_champ_from_key(champs: &Champion, key: &str) -> Option<String> {
for champ in champs.data.values() {
if key == champ.key {
return Some(champ.id.to_owned());
}
}
None
}
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
fn lol_champ_dir() -> Result<PathBuf, Error> { fn lol_champ_dir() -> Result<PathBuf, Error> {
@ -194,7 +213,7 @@ fn lol_champ_dir() -> Result<PathBuf, Error> {
} else { } else {
return Err(Error::from(ErrorKind::NotFound)); 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"))] #[cfg(not(target_os = "windows"))]