2023-09-23 03:01:36 -04:00
|
|
|
#![feature(iterator_try_collect)]
|
|
|
|
|
2023-09-26 22:36:03 -04:00
|
|
|
use clap::{Parser, Subcommand};
|
2023-09-23 03:01:36 -04:00
|
|
|
use std::io::{self, Write};
|
2023-09-30 01:43:33 -04:00
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::time::SystemTime;
|
2023-09-23 03:01:36 -04:00
|
|
|
|
|
|
|
mod queries;
|
|
|
|
use queries::*;
|
|
|
|
mod datasets;
|
|
|
|
use datasets::*;
|
|
|
|
|
2023-09-26 22:36:03 -04:00
|
|
|
/// ## CLI Structs
|
|
|
|
|
|
|
|
#[derive(Parser)]
|
|
|
|
#[command(name = "StartGGElo")]
|
|
|
|
#[command(author = "Kiana Sheibani <kiana.a.sheibani@gmail.com>")]
|
|
|
|
#[command(version = "0.1.0")]
|
2023-09-30 00:22:48 -04:00
|
|
|
#[command(about = "StartGGElo - Elo rating calculator for start.gg tournaments", long_about = None)]
|
2023-09-26 22:36:03 -04:00
|
|
|
struct Cli {
|
|
|
|
#[command(subcommand)]
|
|
|
|
subcommand: Subcommands,
|
2023-09-30 00:22:48 -04:00
|
|
|
|
|
|
|
#[arg(short = 'A', long = "auth", value_name = "TOKEN", global = true)]
|
|
|
|
auth_token: Option<String>,
|
|
|
|
|
|
|
|
#[arg(short, long = "config", value_name = "DIR", global = true)]
|
|
|
|
config_dir: Option<PathBuf>,
|
2023-09-26 22:36:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Subcommand)]
|
|
|
|
enum Subcommands {
|
|
|
|
Dataset {
|
|
|
|
#[command(subcommand)]
|
|
|
|
subcommand: DatasetSC,
|
|
|
|
},
|
2023-09-30 01:43:33 -04:00
|
|
|
Sync {
|
|
|
|
#[arg(group = "datasets")]
|
|
|
|
names: Vec<String>,
|
|
|
|
#[arg(short, long, group = "datasets")]
|
|
|
|
all: bool,
|
|
|
|
},
|
2023-09-26 22:36:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Subcommand)]
|
|
|
|
enum DatasetSC {
|
|
|
|
List,
|
2023-09-27 15:19:28 -04:00
|
|
|
New { name: Option<String> },
|
2023-09-30 00:22:48 -04:00
|
|
|
Delete { name: Option<String> },
|
2023-09-26 22:36:03 -04:00
|
|
|
}
|
|
|
|
|
2023-09-23 03:01:36 -04:00
|
|
|
fn main() {
|
2023-09-26 22:36:03 -04:00
|
|
|
let cli = Cli::parse();
|
|
|
|
|
|
|
|
match cli.subcommand {
|
|
|
|
Subcommands::Dataset {
|
|
|
|
subcommand: DatasetSC::List,
|
2023-09-27 15:19:28 -04:00
|
|
|
} => dataset_list(),
|
|
|
|
Subcommands::Dataset {
|
|
|
|
subcommand: DatasetSC::New { name },
|
|
|
|
} => dataset_new(name),
|
2023-09-30 00:22:48 -04:00
|
|
|
Subcommands::Dataset {
|
|
|
|
subcommand: DatasetSC::Delete { name },
|
|
|
|
} => dataset_delete(name),
|
2023-09-30 01:43:33 -04:00
|
|
|
|
|
|
|
Subcommands::Sync { names, all } => sync(names, all, cli.auth_token),
|
2023-09-26 22:36:03 -04:00
|
|
|
}
|
2023-09-27 15:19:28 -04:00
|
|
|
}
|
2023-09-26 22:36:03 -04:00
|
|
|
|
2023-09-27 15:19:28 -04:00
|
|
|
fn dataset_list() {
|
|
|
|
let config_dir = dirs::config_dir().unwrap();
|
2023-09-23 03:01:36 -04:00
|
|
|
|
2023-09-30 00:22:48 -04:00
|
|
|
let connection = open_datasets(&config_dir).unwrap();
|
2023-09-27 15:19:28 -04:00
|
|
|
let datasets = list_datasets(&connection).unwrap();
|
2023-09-26 22:36:03 -04:00
|
|
|
|
2023-09-27 15:19:28 -04:00
|
|
|
println!("{:?}", datasets);
|
2023-09-23 03:01:36 -04:00
|
|
|
}
|
2023-09-26 22:36:03 -04:00
|
|
|
|
2023-09-27 15:19:28 -04:00
|
|
|
fn dataset_new(name: Option<String>) {
|
|
|
|
let config_dir = dirs::config_dir().unwrap();
|
|
|
|
|
|
|
|
let name = name.unwrap_or_else(|| {
|
|
|
|
let mut line = String::new();
|
|
|
|
print!("Name of new dataset: ");
|
|
|
|
io::stdout().flush().expect("Could not access stdout");
|
|
|
|
io::stdin()
|
|
|
|
.read_line(&mut line)
|
|
|
|
.expect("Could not read from stdin");
|
|
|
|
line.trim().to_owned()
|
|
|
|
});
|
|
|
|
|
2023-09-30 00:22:48 -04:00
|
|
|
let connection = open_datasets(&config_dir).unwrap();
|
2023-09-27 15:19:28 -04:00
|
|
|
new_dataset(&connection, &name).unwrap();
|
|
|
|
}
|
2023-09-30 00:22:48 -04:00
|
|
|
|
|
|
|
fn dataset_delete(name: Option<String>) {
|
|
|
|
let config_dir = dirs::config_dir().unwrap();
|
|
|
|
|
|
|
|
let name = name.unwrap_or_else(|| {
|
|
|
|
let mut line = String::new();
|
|
|
|
print!("Dataset to delete: ");
|
|
|
|
io::stdout().flush().expect("Could not access stdout");
|
|
|
|
io::stdin()
|
|
|
|
.read_line(&mut line)
|
|
|
|
.expect("Could not read from stdin");
|
|
|
|
line.trim().to_owned()
|
|
|
|
});
|
|
|
|
|
|
|
|
let connection = open_datasets(&config_dir).unwrap();
|
|
|
|
delete_dataset(&connection, &name).unwrap();
|
|
|
|
}
|
2023-09-30 01:43:33 -04:00
|
|
|
|
|
|
|
fn sync(names: Vec<String>, all: bool, auth_token: Option<String>) {
|
|
|
|
let config_dir = dirs::config_dir().unwrap();
|
|
|
|
|
|
|
|
let auth = auth_token.or_else(|| get_auth_token(&config_dir)).unwrap();
|
|
|
|
|
|
|
|
let connection = open_datasets(&config_dir).unwrap();
|
|
|
|
|
|
|
|
let names = if all {
|
|
|
|
list_datasets(&connection).unwrap()
|
|
|
|
} else {
|
|
|
|
names
|
|
|
|
};
|
|
|
|
|
|
|
|
for name in names {
|
|
|
|
let last_sync = get_last_sync(&connection, &name).unwrap().unwrap();
|
|
|
|
|
|
|
|
let results = run_query::<TournamentSets, _>(
|
|
|
|
TournamentSetsVars {
|
|
|
|
last_query: Timestamp(last_sync),
|
|
|
|
game_id: VideogameId(1),
|
|
|
|
country: None,
|
|
|
|
state: None,
|
|
|
|
},
|
|
|
|
&auth,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
update_from_tournaments(&connection, &name, results).unwrap();
|
|
|
|
|
|
|
|
let current_time = SystemTime::now()
|
|
|
|
.duration_since(SystemTime::UNIX_EPOCH)
|
|
|
|
.unwrap()
|
|
|
|
.as_secs();
|
|
|
|
|
|
|
|
update_last_sync(&connection, &name, current_time).unwrap();
|
|
|
|
}
|
|
|
|
}
|