CGGItemSets/src/cgg_data_source.rs
Slany ace57d30fc
All checks were successful
ci/woodpecker/push/linux Pipeline was successful
ci/woodpecker/push/mingw Pipeline was successful
feat: enhancement
2023-01-03 20:48:54 +01:00

188 lines
5.7 KiB
Rust

use indexmap::IndexMap;
use log::error;
use serde_derive::Deserialize;
use serde_json::{json, Value};
use std::sync::Mutex;
use crate::data_source::{DataSource, Stat};
use crate::ChampInfo;
use crate::Champion;
#[derive(Deserialize)]
struct BuildResponse {
lol: Lol,
}
#[derive(Deserialize)]
struct Lol {
#[serde(rename = "championsReport")]
champions_report: Vec<ChampionReport>,
}
#[derive(Deserialize)]
struct ChampionReport {
champion_id: u32,
role: String,
patch: String,
stats: Stats,
}
#[derive(Deserialize)]
struct Stats {
starting_items: Build,
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,
games: u32,
kills: u32,
deaths: u32,
wins: u32,
assists: u32,
}
#[derive(Deserialize)]
struct Build {
build: Vec<u32>,
win_rate: f32,
games: u32,
}
pub struct CGGDataSource {
champions_report: Mutex<Vec<ChampionReport>>,
}
impl CGGDataSource {
pub fn new() -> CGGDataSource {
Self {
champions_report: Mutex::new(Vec::new()),
}
}
fn make_item_set(&self, build: &Build, label: &str) -> Value {
json!({
"items": build.build.iter().map(|x| json!({"id": x.to_string(), "count": 1})).collect::<Vec<Value>>(),
"type": format!("{} ({:.2}% - {} games)", label, build.win_rate * 100., build.games)
})
}
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())
}
}
fn extract_json(pattern: &str, page: &str) -> String {
let json = page[page.find(pattern).unwrap() + pattern.len()..].to_owned();
json[..json.find("};").unwrap() + 1].replace("undefined", "null")
}
impl DataSource for CGGDataSource {
fn get_alias(&self) -> &str {
"CGG"
}
fn get_timeout(&self) -> u64 {
0
}
fn get_champs_with_positions(
&self,
client: &ureq::Agent,
_champion: &Champion,
) -> IndexMap<u32, Vec<String>> {
let mut champions: IndexMap<u32, Vec<String>> = IndexMap::new();
match client.get("https://champion.gg").call() {
Ok(req) => {
let page = &req.into_string().unwrap();
let datas: BuildResponse =
serde_json::from_str(&extract_json("window.__PRELOADED_STATE__ = ", page))
.unwrap();
for champ in &datas.lol.champions_report {
let id = champ.champion_id;
if champions.contains_key(&id) {
let mut roles = champions.get(&id).unwrap().to_owned();
roles.push(champ.role.to_owned());
champions.insert(id, roles);
} else {
champions.insert(id, vec![champ.role.to_owned()]);
}
}
for report in datas.lol.champions_report {
self.champions_report.lock().unwrap().push(report);
}
}
Err(e) => error!("Error retrieving data: {}", e),
}
champions
}
fn get_champ_data_with_win_pourcentage(
&self,
champ: &ChampInfo,
positions: &[String],
_client: &ureq::Agent,
) -> Vec<(String, Vec<Value>, Stat)> {
let mut data = vec![];
for position in positions {
let mut some_champ: Option<&ChampionReport> = None;
let reports = self.champions_report.lock().unwrap();
for champion in reports.iter() {
if champion.champion_id.to_string() == champ.key && champion.role == *position {
some_champ = Some(champion);
break;
}
}
if let Some(champ) = some_champ {
let blocks = vec![
self.make_item_set_from_list(
&champ.stats.starting_items,
"Highest % Win Starting Items | Skills: ",
&champ.stats.skills,
),
self.make_item_set(&champ.stats.core_builds, "Highest % Win Core Build Path:"),
self.make_item_set(&champ.stats.big_item_builds, "Highest % Win Big Items:"),
self.make_item_set_from_list(
&champ.stats.most_common_starting_items,
"Most Frequent Starting Items | Skills: ",
&champ.stats.most_common_skills,
),
self.make_item_set(
&champ.stats.most_common_core_builds,
"Most Frequent Build Path",
),
self.make_item_set(
&champ.stats.most_common_big_item_builds,
"Most Frequent Big Items:",
),
];
data.push((
position.to_owned(),
blocks,
Stat {
win_rate: (champ.stats.wins as f32 / champ.stats.games as f32) * 100f32,
games: champ.stats.games,
kda: (champ.stats.kills + champ.stats.assists) as f32
/ champ.stats.deaths as f32,
patch: champ.patch.to_owned(),
},
));
}
}
data
}
}