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};
|
|
|
|
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>,
|
|
|
|
}
|
|
|
|
|
2019-09-30 17:56:03 +02:00
|
|
|
#[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
|
|
|
|
|
|
|
pub trait DataSource {
|
2019-09-30 17:56:03 +02:00
|
|
|
fn get_alias(&self) -> &str;
|
|
|
|
|
2018-06-16 10:48:46 +02:00
|
|
|
fn get_champs_with_positions_and_patch(
|
2018-06-16 12:48:14 +02:00
|
|
|
&self,
|
2020-06-28 18:34:31 +02:00
|
|
|
client: &reqwest::blocking::Client,
|
2018-06-16 10:48:46 +02:00
|
|
|
) -> (IndexMap<String, Vec<String>>, String);
|
|
|
|
|
2019-09-30 17:56:03 +02:00
|
|
|
fn get_champ_data_with_win_pourcentage(
|
2018-06-16 18:13:24 +02:00
|
|
|
&self,
|
2019-09-30 17:56:03 +02:00
|
|
|
id: &str,
|
|
|
|
position: &str,
|
2020-06-28 18:34:31 +02:00
|
|
|
client: &reqwest::blocking::Client,
|
2019-09-30 17:56:03 +02:00
|
|
|
) -> Option<(Vec<Value>, f64)>;
|
|
|
|
|
2018-06-16 10:48:46 +02:00
|
|
|
fn write_item_set(
|
2018-06-16 12:48:14 +02:00
|
|
|
&self,
|
2018-06-16 10:48:46 +02:00
|
|
|
id: &str,
|
|
|
|
name: &str,
|
|
|
|
pos: &str,
|
|
|
|
ver: &str,
|
|
|
|
path: &PathBuf,
|
2020-06-28 18:34:31 +02:00
|
|
|
client: &reqwest::blocking::Client,
|
2018-06-16 10:48:46 +02:00
|
|
|
) {
|
|
|
|
info!("Retrieving data for {} at {}", name, pos);
|
2019-09-30 17:56:03 +02:00
|
|
|
let data = self.get_champ_data_with_win_pourcentage(id, pos, client);
|
2018-06-16 10:48:46 +02:00
|
|
|
|
|
|
|
match data {
|
|
|
|
Some(data) => {
|
2019-09-30 17:56:03 +02:00
|
|
|
let item_set = ItemSet {
|
|
|
|
title: format!("{} {} {} - {:.2}%", self.get_alias(), pos, ver, data.1),
|
2018-06-16 10:48:46 +02:00
|
|
|
type_: "custom".to_string(),
|
|
|
|
map: "any".to_string(),
|
|
|
|
mode: "any".to_string(),
|
|
|
|
priority: false,
|
|
|
|
sortrank: 0,
|
2019-09-30 17:56:03 +02:00
|
|
|
blocks: data.0,
|
2018-06-16 10:48:46 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
info!("Writing item set for {} at {}", name, pos);
|
|
|
|
fs::write(
|
2019-09-30 17:56:03 +02:00
|
|
|
path.join(format!("{}_{}_{}.json", self.get_alias(), id, pos)),
|
2018-06-16 10:48:46 +02:00
|
|
|
serde_json::to_string_pretty(&item_set).unwrap(),
|
2019-04-21 22:34:08 +02:00
|
|
|
)
|
|
|
|
.unwrap();
|
2018-06-16 10:48:46 +02:00
|
|
|
}
|
|
|
|
None => {
|
|
|
|
error!("Can't get data for {} at {}", id, pos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|