remove box
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
nyyu 2021-03-16 09:12:30 +01:00
parent d175481566
commit c7826d868b
5 changed files with 84 additions and 87 deletions

View file

@ -95,6 +95,8 @@ fn extract_json(pattern: &str, page: &str) -> String {
} }
impl DataSource for CGGDataSource { impl DataSource for CGGDataSource {
fn init(&self, _client: &ureq::Agent) {}
fn get_alias(&self) -> &str { fn get_alias(&self) -> &str {
"CGG" "CGG"
} }

View file

@ -38,6 +38,8 @@ pub struct Stat {
} }
pub trait DataSource { pub trait DataSource {
fn init(&self, client: &ureq::Agent);
fn get_alias(&self) -> &str; fn get_alias(&self) -> &str;
fn get_timeout(&self) -> u64; fn get_timeout(&self) -> u64;

View file

@ -3,13 +3,12 @@ use crate::data_source::{Build, DataSource, Item, Stat};
use crate::time::Duration; use crate::time::Duration;
use crate::ChampInfo; use crate::ChampInfo;
use indexmap::IndexMap; use indexmap::IndexMap;
use lazy_static::lazy_static;
use serde_derive::Deserialize; use serde_derive::Deserialize;
use serde_json::{json, Value}; use serde_json::{json, Value};
use std::sync::Mutex;
pub struct KBDataSource { pub struct KBDataSource;
token: Option<String>,
internal_classname_mapping: IndexMap<String, String>,
}
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]
struct ChampionResponse { struct ChampionResponse {
@ -90,17 +89,12 @@ struct Summoner {
name: String, name: String,
} }
impl KBDataSource { lazy_static! {
pub fn new(client: &ureq::Agent) -> KBDataSource { static ref TOKEN: Mutex<Option<String>> = Mutex::new(None);
let mut datasource = KBDataSource { static ref INTERNAL_CLASSNAME_MAPPING: Mutex<IndexMap<String, String>> = Mutex::new(IndexMap::new());
token: None, }
internal_classname_mapping: IndexMap::new(),
};
datasource.token = datasource.get_auth_token(client);
datasource.internal_classname_mapping = datasource.get_classname_mapping(client);
datasource
}
impl KBDataSource {
// It will be better to use Result... // It will be better to use Result...
fn get_auth_token(&self, client: &ureq::Agent) -> Option<String> { fn get_auth_token(&self, client: &ureq::Agent) -> Option<String> {
let mut bundle = match client.get("https://koreanbuilds.net/bundle.js").call() { let mut bundle = match client.get("https://koreanbuilds.net/bundle.js").call() {
@ -137,11 +131,8 @@ impl KBDataSource {
} }
fn get_champion_response(&self, client: &ureq::Agent) -> Option<ChampionResponse> { fn get_champion_response(&self, client: &ureq::Agent) -> Option<ChampionResponse> {
let token = match self.token.clone() { if let Some(token) = TOKEN.lock().unwrap().as_ref() {
Some(t) => t, return match client
None => String::new(),
};
match client
.get("https://api.koreanbuilds.net/champions?patchid=-1") .get("https://api.koreanbuilds.net/champions?patchid=-1")
.set("Accept", "application/json") .set("Accept", "application/json")
.set("Authorization", token.as_str()) .set("Authorization", token.as_str())
@ -152,7 +143,9 @@ impl KBDataSource {
Err(_) => None, Err(_) => None,
}, },
Err(_) => None, Err(_) => None,
};
} }
None
} }
fn get_positions(position: Option<Position>) -> Vec<String> { fn get_positions(position: Option<Position>) -> Vec<String> {
@ -294,6 +287,16 @@ impl KBDataSource {
} }
impl DataSource for KBDataSource { impl DataSource for KBDataSource {
fn init(&self, client: &ureq::Agent) {
if let Some(t) = self.get_auth_token(client) {
TOKEN.lock().unwrap().replace(t);
}
for v in self.get_classname_mapping(client) {
INTERNAL_CLASSNAME_MAPPING.lock().unwrap().insert(v.0, v.1);
}
}
fn get_alias(&self) -> &str { fn get_alias(&self) -> &str {
"KB" "KB"
} }
@ -330,8 +333,8 @@ impl DataSource for KBDataSource {
client: &ureq::Agent, client: &ureq::Agent,
) -> Vec<(String, Vec<Value>, Stat)> { ) -> Vec<(String, Vec<Value>, Stat)> {
let mut champ_data = vec![]; let mut champ_data = vec![];
if let Some(token) = self.token.clone() { if let Some(token) = TOKEN.lock().unwrap().as_ref() {
if let Some(map_id) = self.internal_classname_mapping.get(&champ.id) { if let Some(map_id) = INTERNAL_CLASSNAME_MAPPING.lock().unwrap().get(&champ.id) {
let data: BuildResponse = match client let data: BuildResponse = match client
.get(&format!( .get(&format!(
"https://api.koreanbuilds.net/builds?chmpname={}&patchid=-2&position=COMPOSITE", "https://api.koreanbuilds.net/builds?chmpname={}&patchid=-2&position=COMPOSITE",
@ -362,13 +365,12 @@ impl DataSource for KBDataSource {
} }
} }
if let Some(b) = build { if let Some(b) = build {
champ_data.push(self.get_build(&b)); champ_data.push(self.get_build(&b));
} }
} }
} }
}; }
champ_data champ_data
} }
} }
@ -379,10 +381,7 @@ mod tests {
#[test] #[test]
fn test_get_auth_token() { fn test_get_auth_token() {
let datasource = KBDataSource { let datasource = KBDataSource;
token: None,
internal_classname_mapping: IndexMap::new(),
};
let client = ureq::AgentBuilder::new() let client = ureq::AgentBuilder::new()
.timeout(Duration::from_secs(10)) .timeout(Duration::from_secs(10))
.build(); .build();

View file

@ -1,5 +1,4 @@
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};
@ -75,56 +74,47 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
.init()?; .init()?;
info!("CGG Item Sets"); info!("CGG Item Sets");
lazy_static! { let lol_champs_dir: PathBuf = match lol_champ_dir() {
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),
}; };
static ref CLIENT: ureq::Agent = ureq::AgentBuilder::new() info!("LoL Champs Folder: {}", lol_champs_dir.to_str().unwrap());
let client: ureq::Agent = ureq::AgentBuilder::new()
.user_agent(USER_AGENT_VALUE) .user_agent(USER_AGENT_VALUE)
.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")
.call() .call()?
.unwrap() .into_json()?;
.into_json() info!("LoL version: {}", realm.v);
.unwrap();
static ref CHAMPION: Champion = CLIENT let 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
)) ))
.call() .call()?
.unwrap() .into_json()?;
.into_json() info!("LoL numbers of champs: {}", champion.data.len());
.unwrap();
static ref DATA_SOURCES: Vec<Box<dyn DataSource + Sync + Send>> = vec![ static DATA_SOURCES: [&'static (dyn DataSource + Sync + Send); 3] = [
Box::new(PBDataSource), &PBDataSource,
Box::new(CGGDataSource), &CGGDataSource,
Box::new(KBDataSource::new(&CLIENT)), &KBDataSource,
]; ];
} DATA_SOURCES.par_iter().for_each(|data_source| {
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 || {
let init = Instant::now(); let init = Instant::now();
execute_data_source(&data_source, &CLIENT, &CHAMPION, &LOL_CHAMPS_DIR); execute_data_source(*data_source, &client, &champion, &lol_champs_dir);
info!( info!(
"{}: done in {} ms", "{}: done in {} ms",
data_source.get_alias(), data_source.get_alias(),
init.elapsed().as_millis() init.elapsed().as_millis()
); );
})); });
}
for child in threads {
let _ = child.join();
}
Ok(()) Ok(())
} }
@ -138,11 +128,13 @@ fn get_champ_from_key(champs: &Champion, key: &str) -> Option<String> {
} }
fn execute_data_source( fn execute_data_source(
data_source: &Box<dyn DataSource + Sync + Send>, data_source: &(dyn DataSource + Sync + Send),
client: &ureq::Agent, client: &ureq::Agent,
champion: &Champion, champion: &Champion,
lol_champs_dir: &PathBuf, lol_champs_dir: &PathBuf,
) { ) {
data_source.init(client);
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);
@ -181,7 +173,7 @@ fn execute_data_source(
} }
fn get_and_write_item_set( fn get_and_write_item_set(
data_source: &Box<dyn DataSource + Sync + Send>, data_source: &(dyn DataSource + Sync + Send),
client: &ureq::Agent, client: &ureq::Agent,
champion: &Champion, champion: &Champion,
lol_champs_dir: &PathBuf, lol_champs_dir: &PathBuf,

View file

@ -6,6 +6,8 @@ use crate::ChampInfo;
pub struct PBDataSource; pub struct PBDataSource;
impl DataSource for PBDataSource { impl DataSource for PBDataSource {
fn init(&self, _client: &ureq::Agent) {}
fn get_alias(&self) -> &str { fn get_alias(&self) -> &str {
"PB" "PB"
} }