more threads

This commit is contained in:
nyyu 2021-03-15 22:09:30 +01:00 committed by Gitea
parent 5117bbe659
commit 6fcf6d4105
3 changed files with 159 additions and 20 deletions

View file

@ -3,6 +3,7 @@ use lazy_static::lazy_static;
#[cfg(target_os = "windows")]
use log::debug;
use log::{error, info, LevelFilter};
use rayon::prelude::*;
use serde_derive::Deserialize;
use simple_logger::SimpleLogger;
use std::env;
@ -98,7 +99,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
.unwrap()
.into_json()
.unwrap();
static ref DATA_SOURCES: [Box<dyn DataSource + Sync + Send>; 3] = [
static ref DATA_SOURCES: Vec<Box<dyn DataSource + Sync + Send>> = vec![
Box::new(PBDataSource),
Box::new(CGGDataSource),
Box::new(KBDataSource::new(&CLIENT)),
@ -151,29 +152,63 @@ fn execute_data_source(
champs.len()
);
for (id, positions) in &champs {
let mut champ_id: String = id.to_owned();
if data_source.get_timeout() == 0 {
champs.par_iter().for_each(|(id, positions)| {
get_and_write_item_set(
data_source,
client,
champion,
lol_champs_dir,
&patch,
id,
positions,
)
});
} else {
champs.iter().for_each(|(id, positions)| {
get_and_write_item_set(
data_source,
client,
champion,
lol_champs_dir,
&patch,
id,
positions,
)
});
};
}
if !champion.data.contains_key(&champ_id) {
if let Some(c_id) = get_champ_from_key(&champion, &champ_id) {
champ_id = c_id;
}
fn get_and_write_item_set(
data_source: &Box<dyn DataSource + Sync + Send>,
client: &ureq::Agent,
champion: &Champion,
lol_champs_dir: &PathBuf,
patch: &String,
id: &String,
positions: &Vec<String>,
) {
let mut champ_id: String = id.to_owned();
if id.parse::<u32>().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);
} 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()));
}
}
if let Some(champ) = champion.data.get(&champ_id) {
if positions.is_empty() {
error!("{}: {} empty positions", data_source.get_alias(), &champ_id);
} else {
error!("{} not found in LoL champs", &champ_id);
let path = lol_champs_dir.join(&champ_id).join("Recommended");
fs::create_dir_all(&path).unwrap();
positions.iter().for_each(|pos| {
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);
}
}