CGGItemSets/src/data_source.rs

85 lines
2.1 KiB
Rust
Raw Normal View History

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
pub trait DataSource {
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,
2021-03-10 12:22:00 +01:00
client: &ureq::Agent,
2018-06-16 10:48:46 +02:00
) -> (IndexMap<String, Vec<String>>, String);
fn get_champ_data_with_win_pourcentage(
2018-06-16 18:13:24 +02:00
&self,
id: &str,
position: &str,
2021-03-10 12:22:00 +01:00
client: &ureq::Agent,
) -> 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,
2021-03-10 12:22:00 +01:00
client: &ureq::Agent,
2018-06-16 10:48:46 +02:00
) {
info!("Retrieving data for {} at {}", name, pos);
let data = self.get_champ_data_with_win_pourcentage(id, pos, client);
2018-06-16 10:48:46 +02:00
match data {
Some(data) => {
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,
blocks: data.0,
2018-06-16 10:48:46 +02:00
};
info!("Writing item set for {} at {}", name, pos);
fs::write(
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);
}
}
}
}