CGG: Add champ win_rate

This commit is contained in:
nyyu 2021-03-14 10:52:27 +01:00
parent d238c9a4b9
commit 8c754cfadc

View file

@ -3,6 +3,8 @@ use lazy_static::lazy_static;
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 std::collections::HashMap;
use std::sync::Mutex;
use crate::data_source::DataSource; use crate::data_source::DataSource;
use crate::ChampInfo; use crate::ChampInfo;
@ -46,8 +48,28 @@ struct Build {
games: u32 games: u32
} }
#[derive(Deserialize, Debug, Clone)]
struct ChampStat {
champion_id: Option<u32>,
stats: Option<Info>,
matchups: Option<Vec<Info>>,
#[serde(rename = "winRate")]
win_rate: Option<f64>,
games: Option<u32>,
kda: Option<f64>,
}
#[derive(Deserialize, Debug, Clone)]
struct Info {
id: String
}
static mut CHAMPIONS_REPORT: Vec<ChampionReport> = Vec::new(); static mut CHAMPIONS_REPORT: Vec<ChampionReport> = Vec::new();
lazy_static! {
static ref CHAMPIONS_STATS: Mutex<HashMap<String, ChampStat>> = Mutex::new(HashMap::new());
}
pub struct CGGDataSource; pub struct CGGDataSource;
impl CGGDataSource { impl CGGDataSource {
@ -94,8 +116,21 @@ impl DataSource for CGGDataSource {
Regex::new(r"(?m)^\s+window.__PRELOADED_STATE__ = (.*);$").unwrap(); 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();
}
let page = &req.into_string().unwrap();
let datas: BuildResponse = let datas: BuildResponse =
serde_json::from_str(&RE.captures(&req.into_string().unwrap()).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();
for entry in champs_stats.iter() {
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();
@ -148,7 +183,16 @@ impl DataSource for CGGDataSource {
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_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(&champ.stats.most_common_big_item_builds, "Most Frequent Big Items:"));
return Some((blocks, 42.0)); 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.clone();
}
}
let win_rate = champs_stats.get(&key).unwrap().win_rate;
return Some((blocks, win_rate.unwrap()));
} }
None None