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

View File

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