2021-03-14 11:21:57 +01:00
|
|
|
use crate::ChampInfo;
|
2021-04-08 13:55:10 +02:00
|
|
|
use crate::Champion;
|
2018-06-16 10:48:46 +02:00
|
|
|
use indexmap::IndexMap;
|
2019-09-30 17:56:03 +02:00
|
|
|
use log::{error, info};
|
|
|
|
use serde_derive::{Deserialize, Serialize};
|
2021-10-31 10:12:53 +01:00
|
|
|
use serde_json::{json, Value};
|
2018-06-16 10:48:46 +02:00
|
|
|
use std::fs;
|
2021-05-10 10:40:27 +02:00
|
|
|
use std::path::Path;
|
2018-06-16 10:48:46 +02:00
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
struct ItemSet {
|
|
|
|
title: String,
|
|
|
|
#[serde(rename = "type")]
|
|
|
|
type_: String,
|
|
|
|
map: String,
|
|
|
|
mode: String,
|
|
|
|
priority: bool,
|
|
|
|
sortrank: u32,
|
|
|
|
blocks: Vec<Value>,
|
|
|
|
}
|
|
|
|
|
2021-10-31 10:12:53 +01:00
|
|
|
#[derive(Serialize, Deserialize)]
|
2019-09-30 17:56:03 +02:00
|
|
|
pub struct Build {
|
|
|
|
#[serde(rename = "type")]
|
|
|
|
pub type_: String,
|
|
|
|
pub items: Vec<Item>,
|
|
|
|
}
|
|
|
|
|
2021-10-31 10:12:53 +01:00
|
|
|
#[derive(Serialize, Deserialize)]
|
2019-09-30 17:56:03 +02:00
|
|
|
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 {
|
2019-09-30 17:56:03 +02:00
|
|
|
fn get_alias(&self) -> &str;
|
|
|
|
|
2021-03-14 09:49:18 +01:00
|
|
|
fn get_timeout(&self) -> u64;
|
|
|
|
|
2021-05-13 14:38:31 +02:00
|
|
|
fn get_champs_with_positions(
|
|
|
|
&self,
|
|
|
|
client: &ureq::Agent,
|
|
|
|
champion: &Champion,
|
|
|
|
) -> IndexMap<u32, Vec<String>>;
|
2018-06-16 10:48:46 +02:00
|
|
|
|
2021-10-31 10:12:53 +01:00
|
|
|
fn make_item_set(&self, items: Vec<&str>, label: String) -> Value {
|
|
|
|
json!({
|
|
|
|
"items": items.iter().map(|x| json!({"id": x.to_string(), "count": 1})).collect::<Vec<Value>>(),
|
|
|
|
"type": label
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-09-30 17:56:03 +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)>;
|
2019-09-30 17:56:03 +02:00
|
|
|
|
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],
|
2021-05-10 10:40:27 +02:00
|
|
|
path: &Path,
|
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
|
|
|
}
|
|
|
|
}
|