This commit is contained in:
nyyu 2021-03-14 11:21:49 +01:00
parent bc09fe3b4e
commit 80bbf9c3df
2 changed files with 53 additions and 39 deletions

View file

@ -9,16 +9,15 @@ use std::sync::Mutex;
use crate::data_source::DataSource; use crate::data_source::DataSource;
use crate::ChampInfo; use crate::ChampInfo;
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]
struct BuildResponse { struct BuildResponse {
lol: Lol lol: Lol,
} }
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]
struct Lol { struct Lol {
#[serde(rename = "championsReport")] #[serde(rename = "championsReport")]
champions_report: Vec<ChampionReport> champions_report: Vec<ChampionReport>,
} }
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]
@ -26,7 +25,7 @@ struct ChampionReport {
champion_id: u32, champion_id: u32,
role: String, role: String,
patch: String, patch: String,
stats: Stats stats: Stats,
} }
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]
@ -38,14 +37,14 @@ struct Stats {
most_common_starting_items: Build, most_common_starting_items: Build,
most_common_core_builds: Build, most_common_core_builds: Build,
most_common_big_item_builds: Build, most_common_big_item_builds: Build,
most_common_skills: Build most_common_skills: Build,
} }
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]
struct Build { struct Build {
build: Vec<u32>, build: Vec<u32>,
win_rate: f64, win_rate: f64,
games: u32 games: u32,
} }
#[derive(Deserialize, Debug, Clone)] #[derive(Deserialize, Debug, Clone)]
@ -61,7 +60,7 @@ struct ChampStat {
#[derive(Deserialize, Debug, Clone)] #[derive(Deserialize, Debug, Clone)]
struct Info { struct Info {
id: String id: String,
} }
lazy_static! { lazy_static! {
@ -79,12 +78,11 @@ impl CGGDataSource {
}) })
} }
fn make_item_set_from_list( fn make_item_set_from_list(&self, build: &Build, label: &str, skills: &Build) -> Value {
&self, build: &Build, label: &str, skills: &Build, let key_order = skills
) -> Value { .build
let key_order = skills.build
.iter() .iter()
.map(|x| { x.to_string() }) .map(|x| x.to_string())
.collect::<Vec<String>>() .collect::<Vec<String>>()
.join(""); .join("");
@ -105,17 +103,11 @@ impl DataSource for CGGDataSource {
&self, &self,
client: &ureq::Agent, client: &ureq::Agent,
) -> (IndexMap<String, Vec<String>>, String) { ) -> (IndexMap<String, Vec<String>>, String) {
let req = client let req = client.get("https://champion.gg").call().unwrap();
.get("https://champion.gg")
.call()
.unwrap();
lazy_static! { lazy_static! {
static ref RE: Regex = static ref RE: Regex =
Regex::new(r"(?m)^\s+window.__PRELOADED_STATE__ = (.*);$").unwrap(); Regex::new(r"(?m)^\s+window.__PRELOADED_STATE__ = (.*);$").unwrap();
}
lazy_static! {
static ref RE2: Regex = static ref RE2: Regex =
Regex::new(r"(?m)^\s+window.__FLASH_CMS_APOLLO_STATE__ = (.*);$").unwrap(); Regex::new(r"(?m)^\s+window.__FLASH_CMS_APOLLO_STATE__ = (.*);$").unwrap();
} }
@ -123,14 +115,18 @@ impl DataSource for CGGDataSource {
let page = &req.into_string().unwrap(); let page = &req.into_string().unwrap();
let datas: BuildResponse = let datas: BuildResponse =
serde_json::from_str(&RE.captures(&page).unwrap()[1].replace("undefined", "null")).unwrap(); serde_json::from_str(&RE.captures(&page).unwrap()[1].replace("undefined", "null"))
.unwrap();
let champs_stats: HashMap<String, ChampStat> =
let champs_stats: HashMap<String, ChampStat> = serde_json::from_str(&RE2.captures(&page).unwrap()[1].replace("undefined", "null")).unwrap(); serde_json::from_str(&RE2.captures(&page).unwrap()[1].replace("undefined", "null"))
.unwrap();
for entry in champs_stats.iter() { for entry in champs_stats.iter() {
CHAMPIONS_STATS.lock().unwrap().insert(entry.0.clone(), entry.1.clone()); CHAMPIONS_STATS
.lock()
.unwrap()
.insert(entry.0.clone(), entry.1.clone());
} }
let patch = datas.lol.champions_report[0].patch.clone(); let patch = datas.lol.champions_report[0].patch.clone();
let mut champions: IndexMap<String, Vec<String>> = IndexMap::new(); let mut champions: IndexMap<String, Vec<String>> = IndexMap::new();
@ -162,7 +158,6 @@ impl DataSource for CGGDataSource {
position: &str, position: &str,
_client: &ureq::Agent, _client: &ureq::Agent,
) -> Option<(Vec<Value>, f64)> { ) -> Option<(Vec<Value>, f64)> {
let mut some_champ: Option<&ChampionReport> = None; let mut some_champ: Option<&ChampionReport> = None;
let reports = CHAMPIONS_REPORT.lock().unwrap(); let reports = CHAMPIONS_REPORT.lock().unwrap();
for champion in reports.iter() { for champion in reports.iter() {
@ -173,13 +168,30 @@ impl DataSource for CGGDataSource {
if let Some(champ) = some_champ { if let Some(champ) = some_champ {
let mut blocks = vec![]; let mut blocks = vec![];
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_from_list(
blocks.push(self.make_item_set(&champ.stats.core_builds, "Highest % Win Core Build Path:")); &champ.stats.starting_items,
blocks.push(self.make_item_set(&champ.stats.big_item_builds, "Highest % Win Big 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_from_list(
blocks.push(self.make_item_set(&champ.stats.most_common_core_builds, "Most Frequent Build Path")); &champ.stats.most_common_starting_items,
blocks.push(self.make_item_set(&champ.stats.most_common_big_item_builds, "Most Frequent Big 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 mut key: String = String::new();
let champs_stats = CHAMPIONS_STATS.lock().unwrap(); let champs_stats = CHAMPIONS_STATS.lock().unwrap();
@ -192,7 +204,6 @@ impl DataSource for CGGDataSource {
return Some((blocks, win_rate.unwrap())); return Some((blocks, win_rate.unwrap()));
} }
None None
} }
} }

View file

@ -1,11 +1,11 @@
use crate::data_source::{Build, DataSource, Item}; use crate::data_source::{Build, DataSource, Item};
#[cfg(test)] #[cfg(test)]
use crate::time::Duration; use crate::time::Duration;
use crate::ChampInfo;
use indexmap::IndexMap; use indexmap::IndexMap;
use regex::Regex; use regex::Regex;
use serde_derive::Deserialize; use serde_derive::Deserialize;
use serde_json::{json, Value}; use serde_json::{json, Value};
use crate::ChampInfo;
pub struct KBDataSource { pub struct KBDataSource {
token: Option<String>, token: Option<String>,
@ -337,7 +337,6 @@ impl DataSource for KBDataSource {
), ),
items: final_items items: final_items
})); }));
} }
Some((blocks, winrate)) Some((blocks, winrate))
} }
@ -380,7 +379,11 @@ mod tests {
.timeout(Duration::from_secs(10)) .timeout(Duration::from_secs(10))
.build(); .build();
let datasource = KBDataSource::new(&client); let datasource = KBDataSource::new(&client);
let champ = ChampInfo{id: String::from("Aatrox"), name: String::from("Aatrox"), key: String::from("1")}; let champ = ChampInfo {
id: String::from("Aatrox"),
name: String::from("Aatrox"),
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(&champ, "TOP", &client);
assert!(result.is_some()); assert!(result.is_some());
match result { match result {