From 6f969cd231f1dde37c26127601dabb00d332d85c Mon Sep 17 00:00:00 2001 From: Kiana Sheibani Date: Thu, 2 Nov 2023 14:03:01 -0400 Subject: [PATCH] Fix underflow bug when computing time decay --- src/queries/event_sets.rs | 5 +++-- src/sync.rs | 19 +++++++++++++------ 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/queries/event_sets.rs b/src/queries/event_sets.rs index 3b41f36..8a9e1d3 100644 --- a/src/queries/event_sets.rs +++ b/src/queries/event_sets.rs @@ -24,7 +24,7 @@ pub struct EventSets { #[derive(cynic::QueryFragment, Debug)] #[cynic(variables = "EventSetsVars")] struct Event { - #[arguments(page: $page, perPage: 50)] + #[arguments(page: $page, perPage: 40, sortType: RECENT)] sets: Option, } @@ -43,6 +43,7 @@ struct PageInfo { #[derive(cynic::QueryFragment, Debug)] struct Set { start_at: Option, + started_at: Option, #[arguments(includeByes: true)] #[cynic(flatten)] slots: Vec, @@ -125,7 +126,7 @@ impl QueryUnwrap for EventSets { }) .try_collect()?; Some(SetData { - time: set.start_at?, + time: set.start_at.or(set.started_at)?, teams, winner, }) diff --git a/src/sync.rs b/src/sync.rs index a90aaa6..299a36a 100644 --- a/src/sync.rs +++ b/src/sync.rs @@ -199,6 +199,10 @@ fn update_from_set( let (deviation1, volatility1, last_played1) = get_player_data(connection, dataset, player1)?; let (deviation2, volatility2, last_played2) = get_player_data(connection, dataset, player1)?; + + let time1 = results.time.0.checked_sub(last_played1.0).unwrap_or(0); + let time2 = results.time.0.checked_sub(last_played2.0).unwrap_or(0); + let advantage = match get_advantage(connection, dataset, player1, player2) { Err(e) => Err(e)?, Ok(None) => initialize_edge(connection, dataset, metadata.decay_rate, player1, player2)?, @@ -210,7 +214,7 @@ fn update_from_set( volatility1, deviation2, results.winner == 0, - results.time.0 - last_played1.0, + time1, metadata, ); let (adjust2, dev_new2, vol_new2) = glicko_adjust( @@ -219,7 +223,7 @@ fn update_from_set( volatility2, deviation1, results.winner == 1, - results.time.0 - last_played2.0, + time2, metadata, ); @@ -270,13 +274,16 @@ pub fn sync_dataset( event.0, i, num_events ); - let sets = + let mut sets = get_event_sets(event, auth).unwrap_or_else(|| error("Could not access start.gg", 1)); - println!(" Updating ratings from event..."); + if sets.is_empty() { + println!(" No valid sets"); + } else { + println!(" Updating ratings from event..."); - sets.into_iter() - .try_for_each(|set| update_from_set(connection, dataset, &metadata, set))?; + sets.sort_by_key(|set| set.time); + sets.into_iter() } connection.execute("COMMIT;") }