2023-10-02 20:29:39 -04:00
|
|
|
use crate::error;
|
2023-09-23 02:39:29 -04:00
|
|
|
use crate::queries::*;
|
2023-09-23 02:38:49 -04:00
|
|
|
use sqlite::*;
|
2023-09-26 21:35:14 -04:00
|
|
|
use std::fs::{self, OpenOptions};
|
|
|
|
use std::io;
|
2023-08-27 05:26:11 -04:00
|
|
|
use std::path::{Path, PathBuf};
|
2023-10-02 20:29:39 -04:00
|
|
|
use std::time::SystemTime;
|
|
|
|
|
2023-10-03 01:26:25 -04:00
|
|
|
pub struct DatasetMetadata {
|
2023-10-02 20:29:39 -04:00
|
|
|
pub last_sync: Timestamp,
|
|
|
|
pub game_id: VideogameId,
|
2023-10-03 01:25:35 -04:00
|
|
|
pub game_name: String,
|
2023-10-02 20:29:39 -04:00
|
|
|
pub state: Option<String>,
|
|
|
|
}
|
2023-08-27 05:26:11 -04:00
|
|
|
|
2023-09-30 01:43:33 -04:00
|
|
|
/// Return the path to the datasets file.
|
|
|
|
fn datasets_path(config_dir: &Path) -> io::Result<PathBuf> {
|
2023-08-27 05:26:11 -04:00
|
|
|
let mut path = config_dir.to_owned();
|
2023-09-27 15:19:28 -04:00
|
|
|
path.push("ggelo");
|
2023-09-23 02:38:49 -04:00
|
|
|
|
|
|
|
// Create datasets path if it doesn't exist
|
2023-09-26 21:35:14 -04:00
|
|
|
fs::create_dir_all(&path)?;
|
2023-09-23 02:38:49 -04:00
|
|
|
|
2023-09-30 04:37:10 -04:00
|
|
|
path.push("datasets.sqlite");
|
2023-09-26 21:35:14 -04:00
|
|
|
|
2023-09-27 15:19:28 -04:00
|
|
|
// Create datasets file if it doesn't exist
|
2023-09-26 21:35:14 -04:00
|
|
|
OpenOptions::new().write(true).create(true).open(&path)?;
|
|
|
|
|
|
|
|
Ok(path)
|
2023-08-27 05:26:11 -04:00
|
|
|
}
|
|
|
|
|
2023-09-30 00:22:48 -04:00
|
|
|
pub fn open_datasets(config_dir: &Path) -> sqlite::Result<Connection> {
|
2023-09-30 01:43:33 -04:00
|
|
|
let path = datasets_path(config_dir).unwrap();
|
2023-09-27 15:19:28 -04:00
|
|
|
|
2023-08-27 05:26:11 -04:00
|
|
|
let query = "
|
2023-09-27 15:19:28 -04:00
|
|
|
CREATE TABLE IF NOT EXISTS datasets (
|
2023-09-30 01:43:33 -04:00
|
|
|
name TEXT UNIQUE NOT NULL,
|
2023-10-02 20:29:39 -04:00
|
|
|
last_sync INTEGER DEFAULT 1,
|
|
|
|
game_id INTEGER NOT NULL,
|
2023-10-03 01:25:35 -04:00
|
|
|
game_name TEXT NOT NULL,
|
2023-10-02 20:29:39 -04:00
|
|
|
state TEXT
|
2023-09-30 00:22:48 -04:00
|
|
|
) STRICT;";
|
2023-09-27 15:19:28 -04:00
|
|
|
|
|
|
|
let connection = sqlite::open(path)?;
|
|
|
|
connection.execute(query)?;
|
|
|
|
Ok(connection)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Sanitize dataset names
|
|
|
|
|
2023-10-03 01:26:25 -04:00
|
|
|
pub fn list_dataset_names(connection: &Connection) -> sqlite::Result<Vec<String>> {
|
|
|
|
let query = "SELECT name FROM datasets";
|
|
|
|
|
|
|
|
connection
|
|
|
|
.prepare(query)?
|
|
|
|
.into_iter()
|
|
|
|
.map(|r| r.map(|x| x.read::<&str, _>("name").to_owned()))
|
|
|
|
.try_collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn list_datasets(connection: &Connection) -> sqlite::Result<Vec<(String, DatasetMetadata)>> {
|
2023-09-27 15:19:28 -04:00
|
|
|
let query = "SELECT * FROM datasets";
|
|
|
|
|
|
|
|
connection
|
|
|
|
.prepare(query)?
|
|
|
|
.into_iter()
|
2023-10-03 01:26:25 -04:00
|
|
|
.map(|r| {
|
|
|
|
let r_ = r?;
|
|
|
|
Ok((
|
|
|
|
r_.read::<&str, _>("name").to_owned(),
|
|
|
|
DatasetMetadata {
|
|
|
|
last_sync: Timestamp(r_.read::<i64, _>("last_sync") as u64),
|
|
|
|
game_id: VideogameId(r_.read::<i64, _>("game_id") as u64),
|
|
|
|
game_name: r_.read::<&str, _>("game_name").to_owned(),
|
|
|
|
state: r_.read::<Option<&str>, _>("state").map(String::from),
|
|
|
|
},
|
|
|
|
))
|
|
|
|
})
|
2023-09-27 15:19:28 -04:00
|
|
|
.try_collect()
|
|
|
|
}
|
|
|
|
|
2023-09-30 00:22:48 -04:00
|
|
|
pub fn delete_dataset(connection: &Connection, dataset: &str) -> sqlite::Result<()> {
|
|
|
|
let query = format!(
|
|
|
|
r#"DELETE FROM datasets WHERE name = '{0}';
|
|
|
|
DROP TABLE "dataset_{0}";"#,
|
|
|
|
dataset
|
|
|
|
);
|
|
|
|
|
|
|
|
connection.execute(query)
|
|
|
|
}
|
|
|
|
|
2023-10-02 20:29:39 -04:00
|
|
|
pub fn new_dataset(
|
|
|
|
connection: &Connection,
|
|
|
|
dataset: &str,
|
2023-10-03 01:26:25 -04:00
|
|
|
config: DatasetMetadata,
|
2023-10-02 20:29:39 -04:00
|
|
|
) -> sqlite::Result<()> {
|
2023-10-03 01:25:35 -04:00
|
|
|
let query1 = r#"INSERT INTO datasets (name, game_id, game_name, state)
|
|
|
|
VALUES (?, ?, ?, ?)"#;
|
2023-10-02 20:29:39 -04:00
|
|
|
let query2 = format!(
|
|
|
|
r#" CREATE TABLE "dataset_{0}" (
|
2023-09-23 02:38:49 -04:00
|
|
|
id INTEGER PRIMARY KEY,
|
2023-08-27 05:26:11 -04:00
|
|
|
name TEXT,
|
2023-09-23 02:38:49 -04:00
|
|
|
prefix TEXT,
|
|
|
|
elo REAL NOT NULL
|
2023-09-30 00:22:48 -04:00
|
|
|
) STRICT;"#,
|
2023-09-27 15:19:28 -04:00
|
|
|
dataset
|
|
|
|
);
|
2023-08-27 05:26:11 -04:00
|
|
|
|
2023-10-02 20:29:39 -04:00
|
|
|
connection
|
|
|
|
.prepare(query1)?
|
|
|
|
.into_iter()
|
|
|
|
.bind((1, dataset))?
|
|
|
|
.bind((2, config.game_id.0 as i64))?
|
2023-10-03 01:25:35 -04:00
|
|
|
.bind((3, &config.game_name[..]))?
|
|
|
|
.bind((4, config.state.as_deref()))?
|
2023-10-02 20:29:39 -04:00
|
|
|
.try_for_each(|x| x.map(|_| ()))?;
|
|
|
|
|
|
|
|
connection.execute(query2)
|
2023-08-27 05:26:11 -04:00
|
|
|
}
|
2023-09-23 02:36:28 -04:00
|
|
|
|
2023-10-03 01:26:25 -04:00
|
|
|
pub fn get_metadata(
|
2023-10-02 20:29:39 -04:00
|
|
|
connection: &Connection,
|
|
|
|
dataset: &str,
|
2023-10-03 01:26:25 -04:00
|
|
|
) -> sqlite::Result<Option<DatasetMetadata>> {
|
|
|
|
let query = "SELECT last_sync, game_id, game_name, state FROM datasets WHERE name = ?";
|
2023-09-30 01:43:33 -04:00
|
|
|
|
|
|
|
Ok(connection
|
|
|
|
.prepare(query)?
|
|
|
|
.into_iter()
|
|
|
|
.bind((1, dataset))?
|
|
|
|
.next()
|
2023-10-02 20:29:39 -04:00
|
|
|
.map(|r| {
|
|
|
|
let r_ = r?;
|
2023-10-03 01:26:25 -04:00
|
|
|
Ok(DatasetMetadata {
|
2023-10-02 20:29:39 -04:00
|
|
|
last_sync: Timestamp(r_.read::<i64, _>("last_sync") as u64),
|
|
|
|
game_id: VideogameId(r_.read::<i64, _>("game_id") as u64),
|
2023-10-03 01:25:35 -04:00
|
|
|
game_name: r_.read::<&str, _>("game_name").to_owned(),
|
2023-10-02 20:29:39 -04:00
|
|
|
state: r_.read::<Option<&str>, _>("state").map(String::from),
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.and_then(Result::ok))
|
2023-09-30 01:43:33 -04:00
|
|
|
}
|
|
|
|
|
2023-10-02 20:29:39 -04:00
|
|
|
pub fn update_last_sync(connection: &Connection, dataset: &str) -> sqlite::Result<()> {
|
2023-09-30 01:43:33 -04:00
|
|
|
let query = "UPDATE datasets SET last_sync = :sync WHERE name = :dataset";
|
|
|
|
|
2023-10-02 20:29:39 -04:00
|
|
|
let current_time = SystemTime::now()
|
|
|
|
.duration_since(SystemTime::UNIX_EPOCH)
|
|
|
|
.unwrap_or_else(|_| error("System time is before the Unix epoch (1970)!", 2))
|
|
|
|
.as_secs();
|
|
|
|
|
2023-09-30 01:43:33 -04:00
|
|
|
connection
|
|
|
|
.prepare(query)?
|
|
|
|
.into_iter()
|
2023-10-02 20:29:39 -04:00
|
|
|
.bind((":sync", current_time as i64))?
|
2023-09-30 01:43:33 -04:00
|
|
|
.bind((":dataset", dataset))?
|
|
|
|
.try_for_each(|x| x.map(|_| ()))
|
|
|
|
}
|
|
|
|
|
2023-09-23 02:39:29 -04:00
|
|
|
// Database Updating
|
|
|
|
|
2023-09-27 15:19:28 -04:00
|
|
|
pub fn add_players(
|
|
|
|
connection: &Connection,
|
|
|
|
dataset: &str,
|
|
|
|
teams: &Teams<PlayerData>,
|
|
|
|
) -> sqlite::Result<()> {
|
|
|
|
let query = format!(
|
2023-09-30 00:22:48 -04:00
|
|
|
r#"INSERT OR IGNORE INTO "dataset_{}" VALUES (?, ?, ?, 1500)"#,
|
2023-09-27 15:19:28 -04:00
|
|
|
dataset
|
|
|
|
);
|
2023-09-23 02:39:29 -04:00
|
|
|
|
|
|
|
teams.iter().try_for_each(|team| {
|
|
|
|
team.iter().try_for_each(|PlayerData { id, name, prefix }| {
|
2023-09-27 15:19:28 -04:00
|
|
|
let mut statement = connection.prepare(&query)?;
|
2023-09-23 02:39:29 -04:00
|
|
|
statement.bind((1, id.0 as i64))?;
|
|
|
|
statement.bind((2, name.as_ref().map(|x| &x[..])))?;
|
|
|
|
statement.bind((3, prefix.as_ref().map(|x| &x[..])))?;
|
2023-09-27 15:19:28 -04:00
|
|
|
statement.into_iter().try_for_each(|x| x.map(|_| ()))
|
2023-09-23 02:39:29 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_ratings(
|
|
|
|
connection: &Connection,
|
2023-09-27 15:19:28 -04:00
|
|
|
dataset: &str,
|
2023-09-23 02:39:29 -04:00
|
|
|
teams: &Teams<PlayerData>,
|
|
|
|
) -> sqlite::Result<Teams<(PlayerId, f64)>> {
|
2023-09-30 00:22:48 -04:00
|
|
|
let query = format!(r#"SELECT id, elo FROM "dataset_{}" WHERE id = ?"#, dataset);
|
2023-09-23 02:39:29 -04:00
|
|
|
|
|
|
|
teams
|
|
|
|
.iter()
|
|
|
|
.map(|team| {
|
|
|
|
team.iter()
|
|
|
|
.map(|data| {
|
2023-09-27 15:19:28 -04:00
|
|
|
let mut statement = connection.prepare(&query)?;
|
2023-09-23 02:39:29 -04:00
|
|
|
statement.bind((1, data.id.0 as i64))?;
|
|
|
|
statement.next()?;
|
|
|
|
Ok((data.id, statement.read::<f64, _>("elo")?))
|
|
|
|
})
|
|
|
|
.try_collect()
|
|
|
|
})
|
|
|
|
.try_collect()
|
|
|
|
}
|
|
|
|
|
2023-09-27 15:19:28 -04:00
|
|
|
pub fn update_ratings(
|
|
|
|
connection: &Connection,
|
|
|
|
dataset: &str,
|
|
|
|
elos: Teams<(PlayerId, f64)>,
|
|
|
|
) -> sqlite::Result<()> {
|
|
|
|
let query = format!(
|
2023-09-30 00:22:48 -04:00
|
|
|
r#"UPDATE "dataset_{}" SET elo = :elo WHERE id = :id"#,
|
2023-09-27 15:19:28 -04:00
|
|
|
dataset
|
|
|
|
);
|
2023-09-23 02:39:29 -04:00
|
|
|
elos.into_iter().try_for_each(|team| {
|
|
|
|
team.into_iter().try_for_each(|(id, elo)| {
|
2023-09-27 15:19:28 -04:00
|
|
|
let mut statement = connection.prepare(&query)?;
|
2023-09-23 02:39:29 -04:00
|
|
|
statement.bind((":elo", elo))?;
|
|
|
|
statement.bind((":id", id.0 as i64))?;
|
2023-09-27 15:19:28 -04:00
|
|
|
statement.into_iter().try_for_each(|x| x.map(|_| ()))
|
2023-09-23 02:39:29 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|