some threads
This commit is contained in:
parent
15be38aff9
commit
5b39644df0
1 changed files with 86 additions and 67 deletions
153
src/main.rs
153
src/main.rs
|
@ -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,75 +72,52 @@ 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! {
|
||||||
Ok(x) => x,
|
static ref LOL_CHAMPS_DIR: PathBuf = match lol_champ_dir() {
|
||||||
Err(_e) => PathBuf::from(DEFAULT_LOL_CHAMPS_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<dyn DataSource + Sync + Send>; 3] = [
|
||||||
|
Box::new(PBDataSource),
|
||||||
|
Box::new(CGGDataSource),
|
||||||
|
Box::new(KBDataSource::new(&CLIENT)),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
info!(
|
info!(
|
||||||
"LoL Champs Folder: {}",
|
"LoL Champs Folder: {}", LOL_CHAMPS_DIR.to_str().unwrap()
|
||||||
lol_champs_dir.to_str().unwrap_or(DEFAULT_LOL_CHAMPS_DIR)
|
|
||||||
);
|
);
|
||||||
let client = ureq::AgentBuilder::new()
|
info!("LoL version: {}", REALM.v);
|
||||||
.timeout(Duration::from_secs(10))
|
info!("LoL numbers of champs: {}", CHAMPION.data.len());
|
||||||
.build();
|
|
||||||
|
|
||||||
let realm: Realm = client
|
let mut threads = vec![];
|
||||||
.get("https://ddragon.leagueoflegends.com/realms/euw.json")
|
for data_source in DATA_SOURCES.iter() {
|
||||||
.set(USER_AGENT_KEY, USER_AGENT_VALUE)
|
threads.push(thread::spawn(move || {
|
||||||
.call()?
|
execute_data_source(&data_source, &CLIENT, &CHAMPION, &LOL_CHAMPS_DIR)
|
||||||
.into_json()?;
|
}));
|
||||||
info!("LoL version: {}", realm.v);
|
}
|
||||||
|
for child in threads {
|
||||||
let champion: Champion = client
|
let _ = child.join();
|
||||||
.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<dyn DataSource>; 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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -153,6 +131,47 @@ fn get_champ_from_key(champs: &Champion, key: &str) -> Option<String> {
|
||||||
None
|
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);
|
||||||
|
|
||||||
|
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")]
|
#[cfg(target_os = "windows")]
|
||||||
fn lol_champ_dir() -> Result<PathBuf, Error> {
|
fn lol_champ_dir() -> Result<PathBuf, Error> {
|
||||||
let hklm = RegKey::predef(winreg::enums::HKEY_LOCAL_MACHINE);
|
let hklm = RegKey::predef(winreg::enums::HKEY_LOCAL_MACHINE);
|
||||||
|
@ -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"))]
|
||||||
|
|
Loading…
Add table
Reference in a new issue