CGGItemSets/src/cgg_data_source.rs

157 lines
4.5 KiB
Rust
Raw Normal View History

2018-06-16 10:48:46 +02:00
use indexmap::IndexMap;
use lazy_static::lazy_static;
2018-06-16 10:48:46 +02:00
use regex::Regex;
2021-03-13 23:02:26 +01:00
use serde_derive::Deserialize;
use serde_json::{json, Value};
2018-06-16 10:48:46 +02:00
2019-09-26 22:39:25 +02:00
use crate::data_source::DataSource;
2021-03-13 23:02:26 +01:00
use crate::ChampInfo;
2018-06-16 10:48:46 +02:00
2021-03-13 23:02:26 +01:00
#[derive(Deserialize, Debug)]
struct BuildResponse {
lol: Lol
}
#[derive(Deserialize, Debug)]
struct Lol {
#[serde(rename = "championsReport")]
champions_report: Vec<ChampionReport>
}
#[derive(Deserialize, Debug)]
struct ChampionReport {
champion_id: u32,
role: String,
patch: String,
stats: Stats
}
#[derive(Deserialize, Debug)]
struct Stats {
starting_items: Build,
2021-03-14 09:49:18 +01:00
core_builds: Build,
big_item_builds: Build,
skills: Build,
most_common_starting_items: Build,
most_common_core_builds: Build,
most_common_big_item_builds: Build,
most_common_skills: Build
2021-03-13 23:02:26 +01:00
}
#[derive(Deserialize, Debug)]
struct Build {
build: Vec<u32>,
win_rate: f64,
games: u32
}
static mut CHAMPIONS_REPORT: Vec<ChampionReport> = Vec::new();
2018-06-16 10:48:46 +02:00
pub struct CGGDataSource;
impl CGGDataSource {
2021-03-13 23:02:26 +01:00
fn make_item_set(&self, build: &Build, label: &str) -> Value {
json!({
2021-03-14 09:49:18 +01:00
"items": build.build.iter().map(|x| json!({"id": x.to_string(), "count": 1})).collect::<Vec<Value>>(),
2021-03-13 23:02:26 +01:00
"type": format!("{} ({:.2}% - {} games)", label, build.win_rate * 100., build.games)
})
}
fn make_item_set_from_list(
2021-03-14 09:49:18 +01:00
&self, build: &Build, label: &str, skills: &Build,
) -> Value {
2021-03-14 09:49:18 +01:00
let key_order = skills.build
.iter()
2021-03-14 09:49:18 +01:00
.map(|x| { x.to_string() })
.collect::<Vec<String>>()
.join("");
self.make_item_set(build, [label, &key_order.as_str()].join(" ").as_str())
}
}
2018-06-16 10:48:46 +02:00
impl DataSource for CGGDataSource {
fn get_alias(&self) -> &str {
"CGG"
}
2021-03-14 09:49:18 +01:00
fn get_timeout(&self) -> u64 {
0
}
2018-06-16 10:48:46 +02:00
fn get_champs_with_positions_and_patch(
2018-06-16 12:48:14 +02:00
&self,
2021-03-10 12:22:00 +01:00
client: &ureq::Agent,
2018-06-16 10:48:46 +02:00
) -> (IndexMap<String, Vec<String>>, String) {
2021-03-13 23:02:26 +01:00
let req = client
2018-06-16 10:48:46 +02:00
.get("https://champion.gg")
2021-03-10 12:22:00 +01:00
.call()
2018-06-16 10:48:46 +02:00
.unwrap();
2021-03-13 23:02:26 +01:00
lazy_static! {
static ref RE: Regex =
Regex::new(r"(?m)^\s+window.__PRELOADED_STATE__ = (.*);$").unwrap();
}
let datas: BuildResponse =
serde_json::from_str(&RE.captures(&req.into_string().unwrap()).unwrap()[1].replace("undefined", "null")).unwrap();
2021-03-14 09:24:09 +01:00
let patch = datas.lol.champions_report[0].patch.clone();
2018-06-16 10:48:46 +02:00
2021-03-14 09:24:09 +01:00
let mut champions: IndexMap<String, Vec<String>> = IndexMap::new();
2021-03-13 23:02:26 +01:00
for champ in &datas.lol.champions_report {
let id = champ.champion_id.to_string();
2021-03-14 09:24:09 +01:00
let mut roles: Vec<String> = Vec::new();
if champions.contains_key(&id) {
let c = champions.get(&id).unwrap();
let mut new_roles = c.clone();
new_roles.push(champ.role.clone());
} else {
roles.push(champ.role.clone());
}
champions.insert(id, roles);
2018-06-16 10:48:46 +02:00
}
2021-03-13 23:02:26 +01:00
unsafe {
CHAMPIONS_REPORT = datas.lol.champions_report;
}
2018-06-16 10:48:46 +02:00
(champions, patch)
}
fn get_champ_data_with_win_pourcentage(
&self,
2021-03-13 23:02:26 +01:00
champ: &ChampInfo,
position: &str,
2021-03-13 23:02:26 +01:00
_client: &ureq::Agent,
) -> Option<(Vec<Value>, f64)> {
2021-03-13 23:02:26 +01:00
let mut some_champ: Option<&ChampionReport> = None;
unsafe {
for champion in &CHAMPIONS_REPORT {
if champion.champion_id.to_string() == champ.key && champion.role == position {
some_champ = Some(champion);
}
}
2021-03-13 23:02:26 +01:00
}
if let Some(champ) = some_champ {
let mut blocks = vec![];
2021-03-14 09:49:18 +01:00
blocks.push(self.make_item_set_from_list(&champ.stats.starting_items, "Highest % Win Starting Items | Skills: ", &champ.stats.skills));
2021-03-13 23:02:26 +01:00
blocks.push(self.make_item_set(&champ.stats.core_builds, "Highest % Win Core Build Path:"));
2021-03-14 09:49:18 +01:00
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:"));
2021-03-13 23:02:26 +01:00
return Some((blocks, 42.0));
2018-06-16 10:48:46 +02:00
}
2021-03-13 23:02:26 +01:00
None
2018-06-16 10:48:46 +02:00
}
}