StartRNR/src/main.rs

201 lines
5.8 KiB
Rust
Raw Normal View History

#![feature(iterator_try_collect)]
2023-09-26 22:36:03 -04:00
use clap::{Parser, Subcommand};
use std::io::{self, Write};
2023-09-30 01:43:33 -04:00
use std::path::PathBuf;
2023-10-01 14:55:15 -04:00
use std::process::exit;
2023-09-30 01:43:33 -04:00
use std::time::SystemTime;
mod queries;
use queries::*;
mod datasets;
use datasets::*;
mod sync;
use sync::*;
2023-10-01 14:55:15 -04:00
pub fn error(msg: &str, code: i32) -> ! {
println!("\nERROR: {}", msg);
exit(code)
}
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
2023-09-30 05:13:52 -04:00
#[arg(
short = 'A',
long = "auth",
value_name = "TOKEN",
global = true,
help = "Authentication token",
long_help = "The authentication token for accessing start.gg.
A token can be specified using this argument, in the environment variable
AUTH_TOKEN, or in a text file '<CONFIG_DIR>/auth.txt'."
)]
2023-09-30 00:22:48 -04:00
auth_token: Option<String>,
2023-09-30 05:13:52 -04:00
#[arg(
short,
long = "config",
value_name = "DIR",
global = true,
help = "Config directory",
long_help = "This option overrides the default config directory.
If this directory does not exist, it will be created and a database file will
be initialized within it."
)]
2023-09-30 00:22:48 -04:00
config_dir: Option<PathBuf>,
2023-09-26 22:36:03 -04:00
}
#[derive(Subcommand)]
enum Subcommands {
2023-09-30 05:13:52 -04:00
#[command(about = "Manipulate stored datasets")]
2023-09-26 22:36:03 -04:00
Dataset {
#[command(subcommand)]
subcommand: DatasetSC,
},
2023-09-30 05:13:52 -04:00
#[command(
about = "Sync player ratings",
long_about = "Pull recent tournament data off of start.gg and use it to update each player's
stored ratings. This command will automatically keep track of the last time each
dataset was synced."
)]
2023-09-30 01:43:33 -04:00
Sync {
2023-09-30 05:13:52 -04:00
#[arg(
group = "datasets",
help = "The datasets to sync",
long_help = "A list of datasets to sync.
If no datasets are given, then the dataset 'default' is synced. This dataset is
created if it does not already exist."
)]
datasets: Vec<String>,
#[arg(short, long, group = "datasets", help = "Sync all stored databases")]
2023-09-30 01:43:33 -04:00
all: bool,
},
2023-09-26 22:36:03 -04:00
}
#[derive(Subcommand)]
enum DatasetSC {
2023-09-30 05:13:52 -04:00
#[command(about = "List datasets")]
2023-09-26 22:36:03 -04:00
List,
2023-09-30 05:13:52 -04:00
#[command(about = "Create a new dataset")]
New { name: Option<String> },
2023-09-30 05:13:52 -04:00
#[command(about = "Delete a dataset")]
2023-09-30 00:22:48 -04:00
Delete { name: Option<String> },
2023-09-26 22:36:03 -04:00
}
fn main() {
2023-09-26 22:36:03 -04:00
let cli = Cli::parse();
match cli.subcommand {
Subcommands::Dataset {
subcommand: DatasetSC::List,
} => 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
2023-09-30 05:13:52 -04:00
Subcommands::Sync { datasets, all } => sync(datasets, all, cli.auth_token),
2023-09-26 22:36:03 -04:00
}
}
2023-09-26 22:36:03 -04:00
fn dataset_list() {
2023-10-01 14:55:15 -04:00
let config_dir = dirs::config_dir().expect("Could not determine config directory");
2023-10-01 14:55:15 -04:00
let connection =
open_datasets(&config_dir).unwrap_or_else(|_| error("Could not open datasets file", 1));
let datasets = list_datasets(&connection).expect("Error communicating with SQLite");
2023-09-26 22:36:03 -04:00
println!("{:?}", datasets);
}
2023-09-26 22:36:03 -04:00
2023-10-01 14:55:15 -04:00
fn read_string() -> String {
let mut line = String::new();
io::stdout().flush().expect("Could not access stdout");
io::stdin()
.read_line(&mut line)
.expect("Could not read from stdin");
line.trim().to_owned()
}
fn dataset_new(name: Option<String>) {
2023-10-01 14:55:15 -04:00
let config_dir = dirs::config_dir().expect("Could not determine config directory");
let name = name.unwrap_or_else(|| {
print!("Name of new dataset: ");
2023-10-01 14:55:15 -04:00
read_string()
});
2023-10-01 14:55:15 -04:00
let connection =
open_datasets(&config_dir).unwrap_or_else(|_| error("Could not open datasets file", 1));
new_dataset(&connection, &name).expect("Error communicating with SQLite");
}
2023-09-30 00:22:48 -04:00
fn dataset_delete(name: Option<String>) {
2023-10-01 14:55:15 -04:00
let config_dir = dirs::config_dir().expect("Could not determine config directory");
2023-09-30 00:22:48 -04:00
let name = name.unwrap_or_else(|| {
print!("Dataset to delete: ");
2023-10-01 14:55:15 -04:00
read_string()
2023-09-30 00:22:48 -04:00
});
2023-10-01 14:55:15 -04:00
let connection =
open_datasets(&config_dir).unwrap_or_else(|_| error("Could not open datasets file", 1));
delete_dataset(&connection, &name).expect("Error communicating with SQLite");
2023-09-30 00:22:48 -04:00
}
2023-09-30 01:43:33 -04:00
2023-09-30 05:13:52 -04:00
fn sync(datasets: Vec<String>, all: bool, auth_token: Option<String>) {
2023-09-30 01:43:33 -04:00
let config_dir = dirs::config_dir().unwrap();
2023-10-01 14:55:15 -04:00
let auth = auth_token
.or_else(|| get_auth_token(&config_dir))
.unwrap_or_else(|| error("Access token not provided", 1));
2023-09-30 01:43:33 -04:00
2023-10-01 14:55:15 -04:00
let connection =
open_datasets(&config_dir).unwrap_or_else(|_| error("Could not open datasets file", 1));
2023-09-30 01:43:33 -04:00
2023-10-01 14:55:15 -04:00
#[allow(unused_must_use)]
2023-09-30 05:13:52 -04:00
let datasets = if all {
2023-09-30 01:43:33 -04:00
list_datasets(&connection).unwrap()
2023-09-30 05:13:52 -04:00
} else if datasets.len() == 0 {
2023-10-01 14:55:15 -04:00
new_dataset(&connection, "default");
2023-09-30 04:37:10 -04:00
vec![String::from("default")]
2023-09-30 01:43:33 -04:00
} else {
2023-09-30 05:13:52 -04:00
datasets
2023-09-30 01:43:33 -04:00
};
2023-09-30 05:13:52 -04:00
for dataset in datasets {
2023-10-01 14:55:15 -04:00
let last_sync = get_last_sync(&connection, &dataset)
.expect("Error communicating with SQLite")
.unwrap_or_else(|| error(&format!("Dataset {} does not exist!", dataset), 1));
sync_dataset(
&connection,
&dataset,
last_sync,
VideogameId(1386),
Some("GA"),
&auth,
)
.expect("Error communicating with SQLite");
let current_time = SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap_or_else(|_| error("System time is before the Unix epoch!", 2))
.as_secs();
update_last_sync(&connection, &dataset, current_time)
.expect("Error communicating with SQLite");
2023-09-30 01:43:33 -04:00
}
}