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 {
fn init(&self, _client: &ureq::Agent) {}
fn get_alias(&self) -> &str {
"CGG"
}

View file

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

View file

@ -1,5 +1,4 @@
use indexmap::IndexMap;
use lazy_static::lazy_static;
#[cfg(target_os = "windows")]
use log::debug;
use log::{error, info, LevelFilter};
@ -75,56 +74,47 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
.init()?;
info!("CGG Item Sets");
lazy_static! {
static ref LOL_CHAMPS_DIR: PathBuf = match lol_champ_dir() {
let lol_champs_dir: PathBuf = match lol_champ_dir() {
Ok(x) => x,
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)
.timeout(Duration::from_secs(10))
.build();
static ref REALM: Realm = CLIENT
let realm: Realm = client
.get("https://ddragon.leagueoflegends.com/realms/euw.json")
.call()
.unwrap()
.into_json()
.unwrap();
static ref CHAMPION: Champion = CLIENT
.call()?
.into_json()?;
info!("LoL version: {}", realm.v);
let champion: Champion = client
.get(&format!(
"https://ddragon.leagueoflegends.com/cdn/{}/data/en_US/champion.json",
REALM.v
realm.v
))
.call()
.unwrap()
.into_json()
.unwrap();
static ref DATA_SOURCES: Vec<Box<dyn DataSource + Sync + Send>> = vec![
Box::new(PBDataSource),
Box::new(CGGDataSource),
Box::new(KBDataSource::new(&CLIENT)),
.call()?
.into_json()?;
info!("LoL numbers of champs: {}", champion.data.len());
static DATA_SOURCES: [&'static (dyn DataSource + Sync + Send); 3] = [
&PBDataSource,
&CGGDataSource,
&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();
execute_data_source(&data_source, &CLIENT, &CHAMPION, &LOL_CHAMPS_DIR);
execute_data_source(*data_source, &client, &champion, &lol_champs_dir);
info!(
"{}: done in {} ms",
data_source.get_alias(),
init.elapsed().as_millis()
);
}));
}
for child in threads {
let _ = child.join();
}
});
Ok(())
}
@ -138,11 +128,13 @@ fn get_champ_from_key(champs: &Champion, key: &str) -> Option<String> {
}
fn execute_data_source(
data_source: &Box<dyn DataSource + Sync + Send>,
data_source: &(dyn DataSource + Sync + Send),
client: &ureq::Agent,
champion: &Champion,
lol_champs_dir: &PathBuf,
) {
data_source.init(client);
let (champs, patch) = data_source.get_champs_with_positions_and_patch(&client);
info!("{} version: {}", data_source.get_alias(), patch);
@ -181,7 +173,7 @@ fn execute_data_source(
}
fn get_and_write_item_set(
data_source: &Box<dyn DataSource + Sync + Send>,
data_source: &(dyn DataSource + Sync + Send),
client: &ureq::Agent,
champion: &Champion,
lol_champs_dir: &PathBuf,

View file

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