KB: get all positions in one time
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
nyyu 2021-03-15 23:00:20 +01:00
parent 03e0d13ed2
commit 02823b6c3c
5 changed files with 282 additions and 230 deletions

View file

@ -146,63 +146,70 @@ impl DataSource for CGGDataSource {
fn get_champ_data_with_win_pourcentage( fn get_champ_data_with_win_pourcentage(
&self, &self,
champ: &ChampInfo, champ: &ChampInfo,
position: &str, positions: &Vec<String>,
_client: &ureq::Agent, _client: &ureq::Agent,
) -> Option<(Vec<Value>, Stat)> { ) -> Vec<(String, Vec<Value>, Stat)> {
let mut some_champ: Option<&ChampionReport> = None; let mut data = vec![];
let reports = CHAMPIONS_REPORT.lock().unwrap(); for position in positions {
for champion in reports.iter() { let mut some_champ: Option<&ChampionReport> = None;
if champion.champion_id.to_string() == champ.key && champion.role == position { let reports = CHAMPIONS_REPORT.lock().unwrap();
some_champ = Some(champion); for champion in reports.iter() {
break; if champion.champion_id.to_string() == champ.key
} && champion.role == position.to_owned()
} {
if let Some(champ) = some_champ { some_champ = Some(champion);
let mut blocks = vec![]; break;
blocks.push(self.make_item_set_from_list(
&champ.stats.starting_items,
"Highest % Win Starting Items | Skills: ",
&champ.stats.skills,
));
blocks.push(
self.make_item_set(&champ.stats.core_builds, "Highest % Win Core Build Path:"),
);
blocks
.push(self.make_item_set(&champ.stats.big_item_builds, "Highest % Win Big Items:"));
blocks.push(self.make_item_set_from_list(
&champ.stats.most_common_starting_items,
"Most Frequent Starting Items | Skills: ",
&champ.stats.most_common_skills,
));
blocks.push(self.make_item_set(
&champ.stats.most_common_core_builds,
"Most Frequent Build Path",
));
blocks.push(self.make_item_set(
&champ.stats.most_common_big_item_builds,
"Most Frequent Big Items:",
));
let mut key: String = String::new();
let champs_stats = CHAMPIONS_STATS.lock().unwrap();
for val in champs_stats.values() {
if val.champion_id.is_some() && val.champion_id.unwrap() == champ.champion_id {
key = val.stats.as_ref().unwrap().id.to_owned();
} }
} }
if let Some(champ) = some_champ {
let mut blocks = vec![];
let stat = champs_stats.get(&key).unwrap(); blocks.push(self.make_item_set_from_list(
return Some(( &champ.stats.starting_items,
blocks, "Highest % Win Starting Items | Skills: ",
Stat { &champ.stats.skills,
win_rate: stat.win_rate.unwrap(), ));
games: stat.games.unwrap(), blocks.push(
kda: stat.kda.unwrap(), self.make_item_set(&champ.stats.core_builds, "Highest % Win Core Build Path:"),
}, );
)); blocks.push(
self.make_item_set(&champ.stats.big_item_builds, "Highest % Win Big Items:"),
);
blocks.push(self.make_item_set_from_list(
&champ.stats.most_common_starting_items,
"Most Frequent Starting Items | Skills: ",
&champ.stats.most_common_skills,
));
blocks.push(self.make_item_set(
&champ.stats.most_common_core_builds,
"Most Frequent Build Path",
));
blocks.push(self.make_item_set(
&champ.stats.most_common_big_item_builds,
"Most Frequent Big Items:",
));
let mut key: String = String::new();
let champs_stats = CHAMPIONS_STATS.lock().unwrap();
for val in champs_stats.values() {
if val.champion_id.is_some() && val.champion_id.unwrap() == champ.champion_id {
key = val.stats.as_ref().unwrap().id.to_owned();
}
}
let stat = champs_stats.get(&key).unwrap();
data.push((
position.to_owned(),
blocks,
Stat {
win_rate: stat.win_rate.unwrap(),
games: stat.games.unwrap(),
kda: stat.kda.unwrap(),
},
));
}
} }
None data
} }
} }

View file

@ -50,43 +50,79 @@ pub trait DataSource {
fn get_champ_data_with_win_pourcentage( fn get_champ_data_with_win_pourcentage(
&self, &self,
champ: &ChampInfo, champ: &ChampInfo,
position: &str, positions: &Vec<String>,
client: &ureq::Agent, client: &ureq::Agent,
) -> Option<(Vec<Value>, Stat)>; ) -> Vec<(String, Vec<Value>, Stat)>;
fn write_item_set( fn write_item_set(
&self, &self,
champ: &ChampInfo, champ: &ChampInfo,
pos: &str, positions: &Vec<String>,
ver: &str, ver: &str,
path: &PathBuf, path: &PathBuf,
client: &ureq::Agent, client: &ureq::Agent,
) { ) {
info!("{}: Retrieving data for {} at {}", self.get_alias(), champ.name, pos); info!(
let data = self.get_champ_data_with_win_pourcentage(champ, pos, client); "{}: Retrieving data for {} at {}",
self.get_alias(),
champ.name,
positions.join(", ")
);
let data = self.get_champ_data_with_win_pourcentage(champ, positions, client);
match data { for pos in positions {
Some(data) => { let mut ok = false;
let item_set = ItemSet { for build in &data {
title: format!("{} {} {} - {:.2}% wins - {} games - {:.2} kda", self.get_alias(), pos, ver, data.1.win_rate, data.1.games, data.1.kda), if build.0 == pos.to_owned() {
type_: "custom".to_string(), ok = true;
map: "any".to_string(), break;
mode: "any".to_string(), }
priority: false, }
sortrank: 0, if !ok {
blocks: data.0, error!(
}; "{}: Can't get data for {} at {}",
self.get_alias(),
champ.id,
pos
);
}
}
info!("{}: Writing item set for {} at {}", self.get_alias(), champ.name, pos); for build in data {
fs::write( let item_set = ItemSet {
path.join(format!("{}_{}_{}.json", self.get_alias(), champ.id, pos)), title: format!(
serde_json::to_string_pretty(&item_set).unwrap(), "{} {} {} - {:.2}% wins - {} games - {:.2} kda",
) self.get_alias(),
.unwrap(); build.0,
} ver,
None => { build.2.win_rate,
error!("{}: Can't get data for {} at {}", self.get_alias(), champ.id, pos); build.2.games,
} build.2.kda
),
type_: "custom".to_string(),
map: "any".to_string(),
mode: "any".to_string(),
priority: false,
sortrank: 0,
blocks: build.1,
};
info!(
"{}: Writing item set for {} at {}",
self.get_alias(),
champ.name,
build.0
);
fs::write(
path.join(format!(
"{}_{}_{}.json",
self.get_alias(),
champ.id,
build.0
)),
serde_json::to_string_pretty(&item_set).unwrap(),
)
.unwrap();
} }
} }
} }

View file

@ -51,6 +51,7 @@ struct BuildResponse {
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]
struct KBBuild { struct KBBuild {
position: String,
item0: KBItem, item0: KBItem,
item1: KBItem, item1: KBItem,
item2: KBItem, item2: KBItem,
@ -175,6 +176,111 @@ impl KBDataSource {
} }
positions positions
} }
fn get_build(&self, build: &KBBuild) -> (String, Vec<Value>, Stat) {
let mut starting_items: Vec<Item> = vec![];
let mut blocks = vec![];
if build.start_item0.item_id != 0 {
starting_items.push(Item {
id: build.start_item0.item_id.to_string(),
count: 1,
})
}
if build.start_item1.item_id != 0 {
starting_items.push(Item {
id: build.start_item1.item_id.to_string(),
count: 1,
})
}
if build.start_item2.item_id != 0 {
starting_items.push(Item {
id: build.start_item2.item_id.to_string(),
count: 1,
})
}
if build.start_item3.item_id != 0 {
starting_items.push(Item {
id: build.start_item3.item_id.to_string(),
count: 1,
})
}
if build.start_item4.item_id != 0 {
starting_items.push(Item {
id: build.start_item4.item_id.to_string(),
count: 1,
})
}
if build.start_item5.item_id != 0 {
starting_items.push(Item {
id: build.start_item5.item_id.to_string(),
count: 1,
})
}
blocks.push(json!(Build {
type_: format!("Early game items | skillOrder : {}", build.skill_order),
items: starting_items
}));
let mut final_items: Vec<Item> = vec![];
if build.item0.item_id != 0 {
final_items.push(Item {
id: build.item0.item_id.to_string(),
count: 1,
})
}
if build.item1.item_id != 0 {
final_items.push(Item {
id: build.item1.item_id.to_string(),
count: 1,
})
}
if build.item2.item_id != 0 {
final_items.push(Item {
id: build.item2.item_id.to_string(),
count: 1,
})
}
if build.item3.item_id != 0 {
final_items.push(Item {
id: build.item3.item_id.to_string(),
count: 1,
})
}
if build.item4.item_id != 0 {
final_items.push(Item {
id: build.item4.item_id.to_string(),
count: 1,
})
}
if build.item5.item_id != 0 {
final_items.push(Item {
id: build.item5.item_id.to_string(),
count: 1,
})
}
if build.item6.item_id != 0 {
final_items.push(Item {
id: build.item6.item_id.to_string(),
count: 1,
})
}
blocks.push(json!(Build {
type_: format!(
"Item order by time finished | Summoner : {}",
build.summoner.name
),
items: final_items
}));
(
build.position.to_owned().to_uppercase(),
blocks,
Stat {
win_rate: (build.wins / build.games) * 100.,
games: build.games as u32,
kda: 0.0,
},
)
}
} }
impl DataSource for KBDataSource { impl DataSource for KBDataSource {
@ -210,145 +316,51 @@ impl DataSource for KBDataSource {
fn get_champ_data_with_win_pourcentage( fn get_champ_data_with_win_pourcentage(
&self, &self,
champ: &ChampInfo, champ: &ChampInfo,
position: &str, position: &Vec<String>,
client: &ureq::Agent, client: &ureq::Agent,
) -> Option<(Vec<Value>, Stat)> { ) -> Vec<(String, Vec<Value>, Stat)> {
let token = match self.token.clone() { let mut champ_data = vec![];
Some(t) => t, if let Some(token) = self.token.clone() {
None => return None, if let Some(map_id) = self.internal_classname_mapping.get(&champ.id) {
}; let data: BuildResponse = match client
let map_id = match self.internal_classname_mapping.get(&champ.id) { .get(&format!(
Some(m_id) => m_id, "https://api.koreanbuilds.net/builds?chmpname={}&patchid=-2&position=COMPOSITE",
None => return None, map_id
}; ))
let data: BuildResponse = match client .set("Accept", "application/json")
.get(&format!( .set("Authorization", token.as_str())
"https://api.koreanbuilds.net/builds?chmpname={}&patchid=-2&position={}", .call()
map_id, position {
)) Ok(resp) => match resp.into_json() {
.set("Accept", "application/json") Ok(val) => val,
.set("Authorization", token.as_str()) Err(_) => {
.call() return vec![];
{ }
Ok(resp) => match resp.into_json() { },
Ok(val) => val, Err(_) => {
Err(_) => { return vec![];
return None; }
};
for pos in position {
let mut build: Option<&KBBuild> = None;
for b in &data.builds2 {
if b.position.to_uppercase() == pos.to_owned() {
build = Some(b);
break;
}
}
if let Some(b) = build {
champ_data.push(self.get_build(&b));
}
} }
},
Err(_) => {
return None;
} }
}; };
let mut blocks = vec![]; champ_data
if !data.builds2.is_empty() {
let mut starting_items: Vec<Item> = vec![];
if data.builds2[0].start_item0.item_id != 0 {
starting_items.push(Item {
id: data.builds2[0].start_item0.item_id.to_string(),
count: 1,
})
}
if data.builds2[0].start_item1.item_id != 0 {
starting_items.push(Item {
id: data.builds2[0].start_item1.item_id.to_string(),
count: 1,
})
}
if data.builds2[0].start_item2.item_id != 0 {
starting_items.push(Item {
id: data.builds2[0].start_item2.item_id.to_string(),
count: 1,
})
}
if data.builds2[0].start_item3.item_id != 0 {
starting_items.push(Item {
id: data.builds2[0].start_item3.item_id.to_string(),
count: 1,
})
}
if data.builds2[0].start_item4.item_id != 0 {
starting_items.push(Item {
id: data.builds2[0].start_item4.item_id.to_string(),
count: 1,
})
}
if data.builds2[0].start_item5.item_id != 0 {
starting_items.push(Item {
id: data.builds2[0].start_item5.item_id.to_string(),
count: 1,
})
}
blocks.push(json!(Build {
type_: format!(
"Early game items | skillOrder : {}",
data.builds2[0].skill_order
),
items: starting_items
}));
let mut final_items: Vec<Item> = vec![];
if data.builds2[0].item0.item_id != 0 {
final_items.push(Item {
id: data.builds2[0].item0.item_id.to_string(),
count: 1,
})
}
if data.builds2[0].item1.item_id != 0 {
final_items.push(Item {
id: data.builds2[0].item1.item_id.to_string(),
count: 1,
})
}
if data.builds2[0].item2.item_id != 0 {
final_items.push(Item {
id: data.builds2[0].item2.item_id.to_string(),
count: 1,
})
}
if data.builds2[0].item3.item_id != 0 {
final_items.push(Item {
id: data.builds2[0].item3.item_id.to_string(),
count: 1,
})
}
if data.builds2[0].item4.item_id != 0 {
final_items.push(Item {
id: data.builds2[0].item4.item_id.to_string(),
count: 1,
})
}
if data.builds2[0].item5.item_id != 0 {
final_items.push(Item {
id: data.builds2[0].item5.item_id.to_string(),
count: 1,
})
}
if data.builds2[0].item6.item_id != 0 {
final_items.push(Item {
id: data.builds2[0].item6.item_id.to_string(),
count: 1,
})
}
blocks.push(json!(Build {
type_: format!(
"Item order by time finished | Summoner : {}",
data.builds2[0].summoner.name
),
items: final_items
}));
return Some((
blocks,
Stat {
win_rate: (data.builds2[0].wins / data.builds2[0].games) * 100.,
games: data.builds2[0].games as u32,
kda: 0.0,
},
));
}
None
} }
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
@ -391,14 +403,13 @@ mod tests {
name: String::from("Aatrox"), name: String::from("Aatrox"),
key: String::from("1"), key: String::from("1"),
}; };
let result = datasource.get_champ_data_with_win_pourcentage(&champ, "TOP", &client); let result = datasource.get_champ_data_with_win_pourcentage(
assert!(result.is_some()); &champ,
match result { &vec!["TOP".to_string()],
Some(value) => { &client,
assert!(!value.0.is_empty()); );
assert!(value.1.win_rate > 0.); assert!(!result.is_empty());
} assert!(!result[0].1.is_empty());
None => assert!(false), assert!(result[0].2.win_rate > 0.);
}
} }
} }

View file

@ -162,7 +162,7 @@ fn execute_data_source(
&patch, &patch,
id, id,
positions, positions,
) );
}); });
} else { } else {
champs.iter().for_each(|(id, positions)| { champs.iter().for_each(|(id, positions)| {
@ -174,7 +174,8 @@ fn execute_data_source(
&patch, &patch,
id, id,
positions, positions,
) );
thread::sleep(Duration::from_millis(data_source.get_timeout()));
}); });
}; };
} }
@ -202,10 +203,7 @@ fn get_and_write_item_set(
} else { } else {
let path = lol_champs_dir.join(&champ_id).join("Recommended"); let path = lol_champs_dir.join(&champ_id).join("Recommended");
fs::create_dir_all(&path).unwrap(); fs::create_dir_all(&path).unwrap();
positions.iter().for_each(|pos| { data_source.write_item_set(&champ, &positions, &patch, &path, &client);
data_source.write_item_set(&champ, &pos, &patch, &path, &client);
thread::sleep(Duration::from_millis(data_source.get_timeout()));
});
} }
} else { } else {
error!("{} not found in LoL champs", &champ_id); error!("{} not found in LoL champs", &champ_id);

View file

@ -24,9 +24,9 @@ impl DataSource for PBDataSource {
fn get_champ_data_with_win_pourcentage( fn get_champ_data_with_win_pourcentage(
&self, &self,
_champ: &ChampInfo, _champ: &ChampInfo,
_position: &str, _positions: &Vec<String>,
_client: &ureq::Agent, _client: &ureq::Agent,
) -> Option<(Vec<Value>, Stat)> { ) -> Vec<(String, Vec<Value>, Stat)> {
None vec![]
} }
} }