From 8c754cfadc051f10005ffcc2e93f0c7ebef0f855 Mon Sep 17 00:00:00 2001 From: nyyu Date: Sun, 14 Mar 2021 10:52:27 +0100 Subject: [PATCH] CGG: Add champ win_rate --- src/cgg_data_source.rs | 48 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/src/cgg_data_source.rs b/src/cgg_data_source.rs index 24f5ffe..cd3ce09 100644 --- a/src/cgg_data_source.rs +++ b/src/cgg_data_source.rs @@ -3,6 +3,8 @@ use lazy_static::lazy_static; use regex::Regex; use serde_derive::Deserialize; use serde_json::{json, Value}; +use std::collections::HashMap; +use std::sync::Mutex; use crate::data_source::DataSource; use crate::ChampInfo; @@ -46,8 +48,28 @@ struct Build { games: u32 } +#[derive(Deserialize, Debug, Clone)] +struct ChampStat { + champion_id: Option, + stats: Option, + matchups: Option>, + #[serde(rename = "winRate")] + win_rate: Option, + games: Option, + kda: Option, +} + +#[derive(Deserialize, Debug, Clone)] +struct Info { + id: String +} + static mut CHAMPIONS_REPORT: Vec = Vec::new(); +lazy_static! { + static ref CHAMPIONS_STATS: Mutex> = Mutex::new(HashMap::new()); +} + pub struct CGGDataSource; impl CGGDataSource { @@ -94,8 +116,21 @@ impl DataSource for CGGDataSource { 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 = - 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 = 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(); @@ -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_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