CGGItemSets/src/main.rs

161 lines
4.4 KiB
Rust
Raw Normal View History

2018-06-09 12:35:47 +02:00
use indexmap::IndexMap;
2020-11-20 08:15:50 +01:00
use log::{error, info, LevelFilter};
use serde_derive::Deserialize;
2020-11-20 08:15:50 +01:00
use simple_logger::SimpleLogger;
2021-03-10 12:22:00 +01:00
use std::io::Error;
#[cfg(target_os = "windows")]
use std::io::ErrorKind;
2018-06-09 09:07:41 +02:00
use std::path::PathBuf;
2018-06-10 15:02:08 +02:00
use std::{fs, thread, time};
2018-06-09 00:06:30 +02:00
use time::Duration;
2020-06-28 18:34:31 +02:00
#[cfg(target_os = "windows")]
2018-06-09 08:31:15 +02:00
use winreg::RegKey;
2019-09-26 22:39:25 +02:00
mod cgg_data_source;
mod data_source;
mod kb_data_source;
2019-09-26 22:39:25 +02:00
mod pb_data_source;
2021-03-13 23:02:26 +01:00
use cgg_data_source::CGGDataSource;
2019-09-26 22:39:25 +02:00
use data_source::DataSource;
use kb_data_source::KBDataSource;
use pb_data_source::PBDataSource;
2018-06-04 22:51:18 +02:00
#[derive(Deserialize)]
struct Realm {
2018-06-09 09:07:41 +02:00
v: String,
2018-06-04 22:51:18 +02:00
}
#[derive(Deserialize)]
struct Champion {
2018-06-09 12:35:47 +02:00
data: IndexMap<String, ChampInfo>,
2018-06-04 22:51:18 +02:00
}
#[derive(Deserialize)]
2021-03-13 23:02:26 +01:00
pub struct ChampInfo {
id: String,
2018-06-09 11:46:02 +02:00
name: String,
2021-03-13 23:02:26 +01:00
key: String
}
2021-03-10 12:22:00 +01:00
const USER_AGENT_KEY: &str = "User-Agent";
const USER_AGENT_VALUE: &str =
2021-03-13 23:02:26 +01:00
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:87.0) Gecko/20100101 Firefox/87.0";
const LOL_CHAMPS_DIR: &str = ".\\champs";
2018-06-08 22:41:01 +02:00
2018-06-04 22:51:18 +02:00
fn main() {
SimpleLogger::new()
.with_level(LevelFilter::Info)
2021-03-10 12:22:00 +01:00
.with_module_level("ureq", LevelFilter::Error)
.init()
.unwrap();
2018-06-04 22:51:18 +02:00
info!("CGG Item Sets");
2018-06-09 08:31:15 +02:00
let lol_champs_dir = match lol_champ_dir() {
Ok(x) => x,
2018-06-09 09:07:41 +02:00
Err(_e) => PathBuf::from(LOL_CHAMPS_DIR),
2018-06-09 08:31:15 +02:00
};
2018-06-10 12:42:02 +02:00
info!("LoL Champs Folder: {}", lol_champs_dir.to_str().unwrap());
2018-06-09 08:31:15 +02:00
2021-03-10 12:22:00 +01:00
let client = ureq::AgentBuilder::new()
2018-06-09 00:06:30 +02:00
.timeout(Duration::from_secs(10))
2021-03-10 12:22:00 +01:00
.build();
2018-06-04 22:51:18 +02:00
2018-06-09 09:07:41 +02:00
let realm: Realm = client
.get("https://ddragon.leagueoflegends.com/realms/euw.json")
2021-03-10 12:22:00 +01:00
.set(USER_AGENT_KEY, USER_AGENT_VALUE)
.call()
2018-06-09 09:07:41 +02:00
.unwrap()
2021-03-10 12:22:00 +01:00
.into_json()
2018-06-09 09:07:41 +02:00
.unwrap();
2018-06-04 22:51:18 +02:00
info!("LoL version: {}", realm.v);
2018-06-09 09:07:41 +02:00
let champion: Champion = client
.get(&format!(
"https://ddragon.leagueoflegends.com/cdn/{}/data/en_US/champion.json",
realm.v
))
2021-03-10 12:22:00 +01:00
.set(USER_AGENT_KEY, USER_AGENT_VALUE)
.call()
2018-06-09 09:07:41 +02:00
.unwrap()
2021-03-10 12:22:00 +01:00
.into_json()
2018-06-09 09:07:41 +02:00
.unwrap();
2018-06-04 22:51:18 +02:00
info!("LoL numbers of champs: {}", champion.data.len());
2021-03-13 23:02:26 +01:00
let data_sources: [Box<dyn DataSource>; 3] = [
Box::new(PBDataSource),
2021-03-13 23:02:26 +01:00
Box::new(CGGDataSource),
Box::new(KBDataSource::new(&client)),
];
2018-06-16 12:48:14 +02:00
for data_source in data_sources.iter() {
let (champs, patch) = data_source.get_champs_with_positions_and_patch(&client);
info!("{} version: {}", data_source.get_alias(), patch);
info!(
"{} numbers of champs: {}",
data_source.get_alias(),
champs.len()
);
2018-06-16 12:48:14 +02:00
for (id, positions) in &champs {
2021-03-13 23:02:26 +01:00
let mut champ_id: String = String::from(id);
if !champion.data.contains_key(&champ_id) {
champ_id = get_champ_from_key(&champion, &champ_id);
}
if champion.data.contains_key(&champ_id) {
let path = lol_champs_dir.join(&champ_id).join("Recommended");
2018-06-16 12:48:14 +02:00
fs::create_dir_all(&path).unwrap();
for pos in positions {
data_source.write_item_set(
2021-03-13 23:02:26 +01:00
&champion.data.get(&champ_id).unwrap(),
2018-06-16 12:48:14 +02:00
&pos,
&patch,
&path,
&client,
);
2021-03-14 09:49:18 +01:00
thread::sleep(Duration::from_millis(data_source.get_timeout()));
2018-06-16 12:48:14 +02:00
}
} else {
error!("{} not found in LoL champs", &id);
}
}
}
}
2021-03-13 23:02:26 +01:00
fn get_champ_from_key(champs: &Champion, key: &String) -> String {
for champ in champs.data.values() {
if key == &champ.key {
return champ.id.clone();
}
}
String::new()
}
2020-06-28 18:34:31 +02:00
#[cfg(target_os = "windows")]
2018-06-10 15:02:08 +02:00
fn lol_champ_dir() -> Result<PathBuf, Error> {
2021-03-14 09:23:54 +01:00
let hklm = RegKey::predef(winreg::enums::HKEY_CURRENT_USER);
let mut node = hklm.open_subkey(r"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\")?;
2018-06-10 15:02:08 +02:00
2021-03-14 09:23:54 +01:00
let key = node
.enum_keys()
.map(|x| x.unwrap())
.find(|x| x == "Riot Game league_of_legends.live");
if key == None {
return Err(Error::new(ErrorKind::NotFound, ""));
2018-06-10 12:42:02 +02:00
}
2021-03-14 09:23:54 +01:00
node = node.open_subkey(key.unwrap())?;
let val: String = node.get_value("InstallLocation")?;
Ok(PathBuf::from(val).join("Config").join("Champions"))
2018-06-09 08:31:15 +02:00
}
2020-06-28 18:34:31 +02:00
#[cfg(not(target_os = "windows"))]
fn lol_champ_dir() -> Result<PathBuf, Error> {
2021-03-14 09:23:54 +01:00
Ok(PathBuf::from(LOL_CHAMPS_DIR))
2020-06-28 18:34:31 +02:00
}