CGGItemSets/src/data_source.rs

130 lines
3.2 KiB
Rust
Raw Normal View History

2021-03-14 11:21:57 +01:00
use crate::ChampInfo;
2018-06-16 10:48:46 +02:00
use indexmap::IndexMap;
use log::{error, info};
use serde_derive::{Deserialize, Serialize};
use serde_json::Value;
2018-06-16 10:48:46 +02:00
use std::fs;
use std::path::PathBuf;
#[derive(Serialize, Deserialize)]
struct ItemSet {
title: String,
#[serde(rename = "type")]
type_: String,
map: String,
mode: String,
priority: bool,
sortrank: u32,
blocks: Vec<Value>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Build {
#[serde(rename = "type")]
pub type_: String,
pub items: Vec<Item>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Item {
pub id: String,
pub count: u8,
}
2018-06-16 10:48:46 +02:00
2021-03-14 14:25:37 +01:00
pub struct Stat {
pub win_rate: f64,
pub games: u32,
pub kda: f64,
2021-03-17 09:13:19 +01:00
pub patch: String,
2021-03-14 14:25:37 +01:00
}
2018-06-16 10:48:46 +02:00
pub trait DataSource {
fn get_alias(&self) -> &str;
2021-03-14 09:49:18 +01:00
fn get_timeout(&self) -> u64;
2021-03-17 09:13:19 +01:00
fn get_champs_with_positions(&self, client: &ureq::Agent) -> IndexMap<u32, Vec<String>>;
2018-06-16 10:48:46 +02:00
fn get_champ_data_with_win_pourcentage(
2018-06-16 18:13:24 +02:00
&self,
2021-03-13 23:02:26 +01:00
champ: &ChampInfo,
2021-03-15 23:11:40 +01:00
positions: &[String],
2021-03-10 12:22:00 +01:00
client: &ureq::Agent,
2021-03-15 23:00:20 +01:00
) -> Vec<(String, Vec<Value>, Stat)>;
2018-06-16 10:48:46 +02:00
fn write_item_set(
2018-06-16 12:48:14 +02:00
&self,
2021-03-13 23:02:26 +01:00
champ: &ChampInfo,
2021-03-15 23:11:40 +01:00
positions: &[String],
2018-06-16 10:48:46 +02:00
path: &PathBuf,
2021-03-10 12:22:00 +01:00
client: &ureq::Agent,
2018-06-16 10:48:46 +02:00
) {
2021-03-15 23:00:20 +01:00
info!(
"{}: Retrieving data for {} at {}",
self.get_alias(),
champ.name,
positions.join(", ")
);
let data = self.get_champ_data_with_win_pourcentage(champ, positions, client);
2018-06-16 10:48:46 +02:00
2021-03-15 23:31:02 +01:00
let mut missing_roles = vec![];
2021-03-15 23:00:20 +01:00
for pos in positions {
let mut ok = false;
for build in &data {
2021-03-15 23:11:40 +01:00
if build.0 == *pos {
2021-03-15 23:00:20 +01:00
ok = true;
break;
}
2018-06-16 10:48:46 +02:00
}
2021-03-15 23:00:20 +01:00
if !ok {
2021-03-15 23:31:02 +01:00
missing_roles.push(pos.to_owned());
2018-06-16 10:48:46 +02:00
}
}
2021-03-15 23:31:02 +01:00
if !missing_roles.is_empty() {
error!(
"{}: Can't get data for {} at {}",
self.get_alias(),
champ.id,
missing_roles.join(", ")
);
}
2021-03-15 23:00:20 +01:00
for build in data {
let item_set = ItemSet {
title: format!(
"{} {} {} - {:.2}% wins - {} games - {:.2} kda",
self.get_alias(),
build.0,
2021-03-16 11:13:54 +01:00
build.2.patch,
2021-03-15 23:00:20 +01:00
build.2.win_rate,
build.2.games,
build.2.kda
),
type_: "custom".to_string(),
map: "any".to_string(),
mode: "any".to_string(),
priority: false,
sortrank: 0,
blocks: build.1,
};
info!(
"{}: Writing item set for {} at {}",
self.get_alias(),
champ.name,
build.0
);
fs::write(
path.join(format!(
"{}_{}_{}.json",
self.get_alias(),
champ.id,
build.0
)),
serde_json::to_string_pretty(&item_set).unwrap(),
)
.unwrap();
}
2018-06-16 10:48:46 +02:00
}
}