CGGItemSets/src/cgg_data_source.rs

158 lines
4.3 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,
core_builds: Build
}
#[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-13 23:02:26 +01:00
"items": build.build.iter().map(|x| json!({"id": x, "count": 1})).collect::<Vec<Value>>(),
"type": format!("{} ({:.2}% - {} games)", label, build.win_rate * 100., build.games)
})
}
fn make_item_set_from_list(
&self,
2021-03-11 00:00:04 +01:00
list: &[u32],
label: &str,
key: &str,
data: &Value,
) -> Value {
let mut key_order = String::new();
2021-03-11 00:00:04 +01:00
if data["skills"].get("skillInfo").is_some() {
key_order = data["skills"][key]["order"]
.as_array()
.unwrap()
.iter()
.map(|x| {
data["skills"]["skillInfo"].as_array().unwrap()
[x.as_str().unwrap().parse::<usize>().unwrap() - 1]["key"]
.as_str()
.unwrap()
})
.collect::<Vec<&str>>()
.join(".");
}
json!({
"items": list.iter().map(|x| json!({"id": x.to_string(), "count": 1})).collect::<Vec<Value>>(),
"type": format!("{} {}", label, key_order)
})
}
}
2018-06-16 10:48:46 +02:00
impl DataSource for CGGDataSource {
fn get_alias(&self) -> &str {
"CGG"
}
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![];
blocks.push(self.make_item_set(&champ.stats.starting_items, "Highest % Win Starting Items"));
blocks.push(self.make_item_set(&champ.stats.core_builds, "Highest % Win Core Build Path:"));
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
}
}