some threads
This commit is contained in:
parent
15be38aff9
commit
5b39644df0
1 changed files with 86 additions and 67 deletions
89
src/main.rs
89
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,42 +72,71 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||
.init()?;
|
||||
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,
|
||||
Err(_e) => PathBuf::from(DEFAULT_LOL_CHAMPS_DIR),
|
||||
};
|
||||
info!(
|
||||
"LoL Champs Folder: {}",
|
||||
lol_champs_dir.to_str().unwrap_or(DEFAULT_LOL_CHAMPS_DIR)
|
||||
);
|
||||
let client = ureq::AgentBuilder::new()
|
||||
static ref CLIENT: ureq::Agent = ureq::AgentBuilder::new()
|
||||
.timeout(Duration::from_secs(10))
|
||||
.build();
|
||||
|
||||
let realm: Realm = client
|
||||
static ref 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
|
||||
.call()
|
||||
.unwrap()
|
||||
.into_json()
|
||||
.unwrap();
|
||||
static ref CHAMPION: Champion = CLIENT
|
||||
.get(&format!(
|
||||
"https://ddragon.leagueoflegends.com/cdn/{}/data/en_US/champion.json",
|
||||
realm.v
|
||||
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] = [
|
||||
.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)),
|
||||
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);
|
||||
|
||||
info!("{} version: {}", data_source.get_alias(), patch);
|
||||
|
@ -117,7 +147,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||
);
|
||||
|
||||
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 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);
|
||||
} else {
|
||||
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 {
|
||||
data_source.write_item_set(&champ, &pos, &patch, &path, &client);
|
||||
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")]
|
||||
fn lol_champ_dir() -> Result<PathBuf, Error> {
|
||||
|
@ -194,7 +213,7 @@ fn lol_champ_dir() -> Result<PathBuf, Error> {
|
|||
} 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"))]
|
||||
|
|
Loading…
Add table
Reference in a new issue