From 79bf8e95e65abd9f56d518aac6d68ca327c5e196 Mon Sep 17 00:00:00 2001 From: Kiana Sheibani Date: Fri, 3 Nov 2023 18:09:08 -0400 Subject: [PATCH] Store set IDs in database --- src/datasets.rs | 25 +++++++++++++++---------- src/queries/event_sets.rs | 3 +++ src/sync.rs | 8 ++++---- 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/src/datasets.rs b/src/datasets.rs index 60ba3a9..7f41443 100644 --- a/src/datasets.rs +++ b/src/datasets.rs @@ -119,8 +119,8 @@ pub fn new_dataset( prefix TEXT, deviation REAL NOT NULL, volatility REAL NOT NULL, - sets_won INTEGER NOT NULL, - sets_lost INTEGER NOT NULL, + sets_won TEXT NOT NULL, + sets_lost TEXT NOT NULL, last_played INTEGER NOT NULL ); @@ -128,8 +128,8 @@ CREATE TABLE "{0}_network" ( player_A INTEGER NOT NULL, player_B INTEGER NOT NULL, advantage REAL NOT NULL, - sets_A INTEGER NOT NULL DEFAULT 0, - sets_B INTEGER NOT NULL DEFAULT 0, + sets_A TEXT NOT NULL, + sets_B TEXT NOT NULL, UNIQUE (player_A, player_B), CHECK (player_A < player_B), @@ -146,7 +146,7 @@ CREATE INDEX "{0}_network_B" CREATE VIEW "{0}_view" (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, - 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_B ON player_B = players_B.id;"#, dataset @@ -222,7 +222,7 @@ pub fn add_players( ) -> sqlite::Result<()> { let query = format!( r#"INSERT OR IGNORE INTO "{}_players" - VALUES (?, ?, ?, 2.01, 0.06, 0, 0, ?)"#, + VALUES (?, ?, ?, 2.01, 0.06, '', '', ?)"#, dataset ); @@ -266,10 +266,12 @@ pub fn set_player_data( deviation: f64, volatility: f64, won: bool, + set: SetId, ) -> sqlite::Result<()> { let query = format!( 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 ); @@ -279,6 +281,7 @@ pub fn set_player_data( statement.bind((":last", last_played.0 as i64))?; statement.bind((":id", player.0 as i64))?; statement.bind((":won", if won { 1 } else { 0 }))?; + statement.bind((":set", set.0 as i64))?; statement.next()?; Ok(()) } @@ -315,7 +318,7 @@ pub fn insert_advantage( ) -> sqlite::Result<()> { let query = format!( 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 ); @@ -333,12 +336,13 @@ pub fn adjust_advantage( player2: PlayerId, adjust: f64, winner: usize, + set: SetId, ) -> sqlite::Result<()> { let query = format!( r#"UPDATE "{}_network" SET advantage = advantage + iif(:a > :b, -:v, :v), - sets_A = iif(:w = (:a > :b), sets_A + 1, sets_A), - sets_B = iif(:w = (:b > :a), sets_B + 1, sets_B), + sets_A = iif(:w = (:a > :b), sets_A || :set || ',', sets_A), + sets_B = iif(:w = (:b > :a), sets_B || :set || ',', sets_B) WHERE player_A = min(:a, :b) AND player_B = max(:a, :b)"#, dataset ); @@ -348,6 +352,7 @@ pub fn adjust_advantage( statement.bind((":b", player2.0 as i64))?; statement.bind((":v", adjust))?; statement.bind((":w", winner as i64))?; + statement.bind((":set", set.0 as i64))?; statement.into_iter().try_for_each(|x| x.map(|_| ())) } diff --git a/src/queries/event_sets.rs b/src/queries/event_sets.rs index 2b758cd..fe53636 100644 --- a/src/queries/event_sets.rs +++ b/src/queries/event_sets.rs @@ -43,6 +43,7 @@ struct PageInfo { #[derive(cynic::QueryFragment, Debug)] struct Set { + id: Option, start_at: Option, started_at: Option, #[arguments(includeByes: true)] @@ -84,6 +85,7 @@ pub struct EventSetsResponse { #[derive(Debug)] pub struct SetData { + pub id: SetId, pub time: Timestamp, pub teams: Teams, pub winner: usize, @@ -127,6 +129,7 @@ impl QueryUnwrap for EventSets { }) .try_collect()?; Some(SetData { + id: set.id?, time: set.start_at.or(set.started_at)?, teams, winner, diff --git a/src/sync.rs b/src/sync.rs index 788accc..23730e9 100644 --- a/src/sync.rs +++ b/src/sync.rs @@ -97,8 +97,6 @@ fn get_event_sets(event: EventId, auth: &str) -> Option> { } else if pages == 1 { Some(sets_response.sets) } else { - println!(" (Page 1)"); - let mut sets = sets_response.sets; for page in 2..=pages { @@ -147,8 +145,6 @@ fn get_tournament_events(metadata: &DatasetMetadata, auth: &str) -> Option>(), ) } else { - println!(" (Page 1)"); - let mut tournaments = tour_response .tournaments .into_iter() @@ -244,6 +240,7 @@ fn update_from_set( dev_new1, vol_new1, results.winner == 0, + results.id, )?; set_player_data( connection, @@ -253,6 +250,7 @@ fn update_from_set( dev_new2, vol_new2, results.winner == 1, + results.id, )?; let decay_rate = metadata.decay_rate; @@ -265,6 +263,7 @@ fn update_from_set( player2, (1.0 - decay_rate) * (adjust2 - adjust1), results.winner, + results.id, ) } @@ -321,6 +320,7 @@ mod tests { "test", &metadata(), SetData { + id: SetId(0), time: Timestamp(0), teams: players, winner: 0,