Store set IDs in database

This commit is contained in:
Kiana Sheibani 2023-11-03 18:09:08 -04:00
parent 73b5e4fc43
commit 79bf8e95e6
Signed by: toki
GPG key ID: 6CB106C25E86A9F7
3 changed files with 22 additions and 14 deletions

View file

@ -119,8 +119,8 @@ pub fn new_dataset(
prefix TEXT, prefix TEXT,
deviation REAL NOT NULL, deviation REAL NOT NULL,
volatility REAL NOT NULL, volatility REAL NOT NULL,
sets_won INTEGER NOT NULL, sets_won TEXT NOT NULL,
sets_lost INTEGER NOT NULL, sets_lost TEXT NOT NULL,
last_played INTEGER NOT NULL last_played INTEGER NOT NULL
); );
@ -128,8 +128,8 @@ CREATE TABLE "{0}_network" (
player_A INTEGER NOT NULL, player_A INTEGER NOT NULL,
player_B INTEGER NOT NULL, player_B INTEGER NOT NULL,
advantage REAL NOT NULL, advantage REAL NOT NULL,
sets_A INTEGER NOT NULL DEFAULT 0, sets_A TEXT NOT NULL,
sets_B INTEGER NOT NULL DEFAULT 0, sets_B TEXT NOT NULL,
UNIQUE (player_A, player_B), UNIQUE (player_A, player_B),
CHECK (player_A < player_B), CHECK (player_A < player_B),
@ -146,7 +146,7 @@ CREATE INDEX "{0}_network_B"
CREATE VIEW "{0}_view" CREATE VIEW "{0}_view"
(player_A_id, player_B_id, player_A_name, player_B_name, advantage, sets_A, sets_B, sets) AS (player_A_id, player_B_id, player_A_name, player_B_name, advantage, sets_A, sets_B, sets) AS
SELECT players_A.id, players_B.id, players_A.name, players_B.name, advantage, SELECT players_A.id, players_B.id, players_A.name, players_B.name, advantage,
sets_A, sets_B, sets_A + sets_B FROM "{0}_network" sets_A, sets_B, sets_A || sets_B FROM "{0}_network"
INNER JOIN "{0}_players" players_A ON player_A = players_A.id INNER JOIN "{0}_players" players_A ON player_A = players_A.id
INNER JOIN "{0}_players" players_B ON player_B = players_B.id;"#, INNER JOIN "{0}_players" players_B ON player_B = players_B.id;"#,
dataset dataset
@ -222,7 +222,7 @@ pub fn add_players(
) -> sqlite::Result<()> { ) -> sqlite::Result<()> {
let query = format!( let query = format!(
r#"INSERT OR IGNORE INTO "{}_players" r#"INSERT OR IGNORE INTO "{}_players"
VALUES (?, ?, ?, 2.01, 0.06, 0, 0, ?)"#, VALUES (?, ?, ?, 2.01, 0.06, '', '', ?)"#,
dataset dataset
); );
@ -266,10 +266,12 @@ pub fn set_player_data(
deviation: f64, deviation: f64,
volatility: f64, volatility: f64,
won: bool, won: bool,
set: SetId,
) -> sqlite::Result<()> { ) -> sqlite::Result<()> {
let query = format!( let query = format!(
r#"UPDATE "{}_players" SET deviation = :dev, volatility = :vol, last_played = :last, r#"UPDATE "{}_players" SET deviation = :dev, volatility = :vol, last_played = :last,
sets_won = iif(:won, sets_won + 1, sets_won), sets_lost = iif(:won, sets_lost, sets_lost + 1) WHERE id = :id"#, sets_won = iif(:won, sets_won || :set || ',', sets_won),
sets_lost = iif(:won, sets_lost, sets_lost || :set || ',') WHERE id = :id"#,
dataset dataset
); );
@ -279,6 +281,7 @@ pub fn set_player_data(
statement.bind((":last", last_played.0 as i64))?; statement.bind((":last", last_played.0 as i64))?;
statement.bind((":id", player.0 as i64))?; statement.bind((":id", player.0 as i64))?;
statement.bind((":won", if won { 1 } else { 0 }))?; statement.bind((":won", if won { 1 } else { 0 }))?;
statement.bind((":set", set.0 as i64))?;
statement.next()?; statement.next()?;
Ok(()) Ok(())
} }
@ -315,7 +318,7 @@ pub fn insert_advantage(
) -> sqlite::Result<()> { ) -> sqlite::Result<()> {
let query = format!( let query = format!(
r#"INSERT INTO "{}_network" r#"INSERT INTO "{}_network"
VALUES (min(:a, :b), max(:a, :b), iif(:a > :b, -:v, :v), 0, 0)"#, VALUES (min(:a, :b), max(:a, :b), iif(:a > :b, -:v, :v), '', '')"#,
dataset dataset
); );
@ -333,12 +336,13 @@ pub fn adjust_advantage(
player2: PlayerId, player2: PlayerId,
adjust: f64, adjust: f64,
winner: usize, winner: usize,
set: SetId,
) -> sqlite::Result<()> { ) -> sqlite::Result<()> {
let query = format!( let query = format!(
r#"UPDATE "{}_network" r#"UPDATE "{}_network"
SET advantage = advantage + iif(:a > :b, -:v, :v), SET advantage = advantage + iif(:a > :b, -:v, :v),
sets_A = iif(:w = (:a > :b), sets_A + 1, sets_A), sets_A = iif(:w = (:a > :b), sets_A || :set || ',', sets_A),
sets_B = iif(:w = (:b > :a), sets_B + 1, sets_B), sets_B = iif(:w = (:b > :a), sets_B || :set || ',', sets_B)
WHERE player_A = min(:a, :b) AND player_B = max(:a, :b)"#, WHERE player_A = min(:a, :b) AND player_B = max(:a, :b)"#,
dataset dataset
); );
@ -348,6 +352,7 @@ pub fn adjust_advantage(
statement.bind((":b", player2.0 as i64))?; statement.bind((":b", player2.0 as i64))?;
statement.bind((":v", adjust))?; statement.bind((":v", adjust))?;
statement.bind((":w", winner as i64))?; statement.bind((":w", winner as i64))?;
statement.bind((":set", set.0 as i64))?;
statement.into_iter().try_for_each(|x| x.map(|_| ())) statement.into_iter().try_for_each(|x| x.map(|_| ()))
} }

View file

@ -43,6 +43,7 @@ struct PageInfo {
#[derive(cynic::QueryFragment, Debug)] #[derive(cynic::QueryFragment, Debug)]
struct Set { struct Set {
id: Option<SetId>,
start_at: Option<Timestamp>, start_at: Option<Timestamp>,
started_at: Option<Timestamp>, started_at: Option<Timestamp>,
#[arguments(includeByes: true)] #[arguments(includeByes: true)]
@ -84,6 +85,7 @@ pub struct EventSetsResponse {
#[derive(Debug)] #[derive(Debug)]
pub struct SetData { pub struct SetData {
pub id: SetId,
pub time: Timestamp, pub time: Timestamp,
pub teams: Teams<PlayerData>, pub teams: Teams<PlayerData>,
pub winner: usize, pub winner: usize,
@ -127,6 +129,7 @@ impl QueryUnwrap<EventSetsVars> for EventSets {
}) })
.try_collect()?; .try_collect()?;
Some(SetData { Some(SetData {
id: set.id?,
time: set.start_at.or(set.started_at)?, time: set.start_at.or(set.started_at)?,
teams, teams,
winner, winner,

View file

@ -97,8 +97,6 @@ fn get_event_sets(event: EventId, auth: &str) -> Option<Vec<SetData>> {
} else if pages == 1 { } else if pages == 1 {
Some(sets_response.sets) Some(sets_response.sets)
} else { } else {
println!(" (Page 1)");
let mut sets = sets_response.sets; let mut sets = sets_response.sets;
for page in 2..=pages { for page in 2..=pages {
@ -147,8 +145,6 @@ fn get_tournament_events(metadata: &DatasetMetadata, auth: &str) -> Option<Vec<E
.collect::<Vec<_>>(), .collect::<Vec<_>>(),
) )
} else { } else {
println!(" (Page 1)");
let mut tournaments = tour_response let mut tournaments = tour_response
.tournaments .tournaments
.into_iter() .into_iter()
@ -244,6 +240,7 @@ fn update_from_set(
dev_new1, dev_new1,
vol_new1, vol_new1,
results.winner == 0, results.winner == 0,
results.id,
)?; )?;
set_player_data( set_player_data(
connection, connection,
@ -253,6 +250,7 @@ fn update_from_set(
dev_new2, dev_new2,
vol_new2, vol_new2,
results.winner == 1, results.winner == 1,
results.id,
)?; )?;
let decay_rate = metadata.decay_rate; let decay_rate = metadata.decay_rate;
@ -265,6 +263,7 @@ fn update_from_set(
player2, player2,
(1.0 - decay_rate) * (adjust2 - adjust1), (1.0 - decay_rate) * (adjust2 - adjust1),
results.winner, results.winner,
results.id,
) )
} }
@ -321,6 +320,7 @@ mod tests {
"test", "test",
&metadata(), &metadata(),
SetData { SetData {
id: SetId(0),
time: Timestamp(0), time: Timestamp(0),
teams: players, teams: players,
winner: 0, winner: 0,