CGGItemSets/src/data_source.rs
nyyu 9577598783
All checks were successful
continuous-integration/drone/push Build is passing
Reformat
2021-03-14 11:21:57 +01:00

86 lines
2.2 KiB
Rust

use crate::ChampInfo;
use indexmap::IndexMap;
use log::{error, info};
use serde_derive::{Deserialize, Serialize};
use serde_json::Value;
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,
}
pub trait DataSource {
fn get_alias(&self) -> &str;
fn get_timeout(&self) -> u64;
fn get_champs_with_positions_and_patch(
&self,
client: &ureq::Agent,
) -> (IndexMap<String, Vec<String>>, String);
fn get_champ_data_with_win_pourcentage(
&self,
champ: &ChampInfo,
position: &str,
client: &ureq::Agent,
) -> Option<(Vec<Value>, f64)>;
fn write_item_set(
&self,
champ: &ChampInfo,
pos: &str,
ver: &str,
path: &PathBuf,
client: &ureq::Agent,
) {
info!("Retrieving data for {} at {}", champ.name, pos);
let data = self.get_champ_data_with_win_pourcentage(champ, pos, client);
match data {
Some(data) => {
let item_set = ItemSet {
title: format!("{} {} {} - {:.2}%", self.get_alias(), pos, ver, data.1),
type_: "custom".to_string(),
map: "any".to_string(),
mode: "any".to_string(),
priority: false,
sortrank: 0,
blocks: data.0,
};
info!("Writing item set for {} at {}", champ.name, pos);
fs::write(
path.join(format!("{}_{}_{}.json", self.get_alias(), champ.id, pos)),
serde_json::to_string_pretty(&item_set).unwrap(),
)
.unwrap();
}
None => {
error!("Can't get data for {} at {}", champ.id, pos);
}
}
}
}