KB: remove mapping class
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
nyyu 2021-03-16 10:53:16 +01:00
parent c7826d868b
commit 3906d6cc42
5 changed files with 30 additions and 51 deletions

View file

@ -108,7 +108,7 @@ impl DataSource for CGGDataSource {
fn get_champs_with_positions_and_patch(
&self,
client: &ureq::Agent,
) -> (IndexMap<String, Vec<String>>, String) {
) -> (IndexMap<u32, Vec<String>>, String) {
let req = client.get("https://champion.gg").call().unwrap();
let page = &req.into_string().unwrap();
@ -126,9 +126,9 @@ impl DataSource for CGGDataSource {
}
let patch = datas.lol.champions_report[0].patch.to_owned();
let mut champions: IndexMap<String, Vec<String>> = IndexMap::new();
let mut champions: IndexMap<u32, Vec<String>> = IndexMap::new();
for champ in &datas.lol.champions_report {
let id = champ.champion_id.to_string();
let id = champ.champion_id;
if champions.contains_key(&id) {
let mut roles = champions.get(&id).unwrap().to_owned();
roles.push(champ.role.to_owned());

View file

@ -47,7 +47,7 @@ pub trait DataSource {
fn get_champs_with_positions_and_patch(
&self,
client: &ureq::Agent,
) -> (IndexMap<String, Vec<String>>, String);
) -> (IndexMap<u32, Vec<String>>, String);
fn get_champ_data_with_win_pourcentage(
&self,

View file

@ -91,7 +91,6 @@ struct Summoner {
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 {
@ -120,16 +119,6 @@ impl KBDataSource {
}
}
fn get_classname_mapping(&self, client: &ureq::Agent) -> IndexMap<String, String> {
let mut mapping = IndexMap::new();
if let Some(data) = self.get_champion_response(client) {
for champ in data.champions {
mapping.insert(champ.classname, champ.name);
}
};
mapping
}
fn get_champion_response(&self, client: &ureq::Agent) -> Option<ChampionResponse> {
if let Some(token) = TOKEN.lock().unwrap().as_ref() {
return match client
@ -291,10 +280,6 @@ impl DataSource for KBDataSource {
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 {
@ -308,7 +293,7 @@ impl DataSource for KBDataSource {
fn get_champs_with_positions_and_patch(
&self,
client: &ureq::Agent,
) -> (IndexMap<String, Vec<String>>, String) {
) -> (IndexMap<u32, Vec<String>>, String) {
let mut champions = IndexMap::new();
let data: ChampionResponse = match self.get_champion_response(client) {
Some(val) => val,
@ -321,7 +306,7 @@ impl DataSource for KBDataSource {
None => return (champions, String::new()),
};
for champ in data.champions {
champions.insert(champ.classname, KBDataSource::get_positions(champ.builds));
champions.insert(champ.id, KBDataSource::get_positions(champ.builds));
}
(champions, patch)
}
@ -334,11 +319,10 @@ impl DataSource for KBDataSource {
) -> Vec<(String, Vec<Value>, Stat)> {
let mut champ_data = vec![];
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",
map_id
champ.id
))
.set("Accept", "application/json")
.set("Authorization", token.as_str())
@ -370,7 +354,6 @@ impl DataSource for KBDataSource {
}
}
}
}
champ_data
}
}
@ -396,7 +379,8 @@ mod tests {
let client = ureq::AgentBuilder::new()
.timeout(Duration::from_secs(10))
.build();
let datasource = KBDataSource::new(&client);
let datasource = KBDataSource;
datasource.init(&client);
let champs_with_positions_and_patch =
datasource.get_champs_with_positions_and_patch(&client);
assert!(champs_with_positions_and_patch.0.len() > 0);
@ -407,15 +391,16 @@ mod tests {
let client = ureq::AgentBuilder::new()
.timeout(Duration::from_secs(10))
.build();
let datasource = KBDataSource::new(&client);
let datasource = KBDataSource;
datasource.init(&client);
let champ = ChampInfo {
id: String::from("Aatrox"),
name: String::from("Aatrox"),
id: String::from("Annie"),
name: String::from("Annie"),
key: String::from("1"),
};
let result = datasource.get_champ_data_with_win_pourcentage(
&champ,
&vec!["TOP".to_string()],
&vec!["MID".to_string()],
&client,
);
assert!(!result.is_empty());

View file

@ -118,9 +118,9 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
Ok(())
}
fn get_champ_from_key(champs: &Champion, key: &str) -> Option<String> {
fn get_champ_from_key(champs: &Champion, key: u32) -> Option<String> {
for champ in champs.data.values() {
if key == champ.key {
if key.to_string() == champ.key {
return Some(champ.id.to_owned());
}
}
@ -152,7 +152,7 @@ fn execute_data_source(
champion,
lol_champs_dir,
&patch,
id,
*id,
positions,
);
});
@ -164,7 +164,7 @@ fn execute_data_source(
champion,
lol_champs_dir,
&patch,
id,
*id,
positions,
);
thread::sleep(Duration::from_millis(data_source.get_timeout()));
@ -178,17 +178,10 @@ fn get_and_write_item_set(
champion: &Champion,
lol_champs_dir: &PathBuf,
patch: &str,
id: &str,
id: u32,
positions: &[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_id) = get_champ_from_key(&champion, id) {
if let Some(champ) = champion.data.get(&champ_id) {
if positions.is_empty() {
error!("{}: {} empty positions", data_source.get_alias(), &champ_id);
@ -201,6 +194,7 @@ fn get_and_write_item_set(
error!("{} not found in LoL champs", &champ_id);
}
}
}
#[cfg(target_os = "windows")]
fn lol_champ_dir() -> Result<PathBuf, Error> {

View file

@ -19,7 +19,7 @@ impl DataSource for PBDataSource {
fn get_champs_with_positions_and_patch(
&self,
_client: &ureq::Agent,
) -> (IndexMap<String, Vec<String>>, String) {
) -> (IndexMap<u32, Vec<String>>, String) {
(IndexMap::new(), String::new())
}