From 1aa85bd989b6e5cb49e9b8b13249cd5d4dcd4b4e Mon Sep 17 00:00:00 2001 From: Kiana Sheibani Date: Sat, 18 Nov 2023 20:53:31 -0500 Subject: [PATCH] Fall back to event time if set time isn't recorded --- src/queries/event_sets.rs | 4 ++-- src/queries/tournament_events.rs | 19 ++++++++++++++----- src/sync.rs | 27 ++++++++++++++++----------- 3 files changed, 32 insertions(+), 18 deletions(-) diff --git a/src/queries/event_sets.rs b/src/queries/event_sets.rs index fe53636..7f4f223 100644 --- a/src/queries/event_sets.rs +++ b/src/queries/event_sets.rs @@ -86,7 +86,7 @@ pub struct EventSetsResponse { #[derive(Debug)] pub struct SetData { pub id: SetId, - pub time: Timestamp, + pub time: Option, pub teams: Teams, pub winner: usize, } @@ -130,7 +130,7 @@ impl QueryUnwrap for EventSets { .try_collect()?; Some(SetData { id: set.id?, - time: set.start_at.or(set.started_at)?, + time: set.start_at.or(set.started_at), teams, winner, }) diff --git a/src/queries/tournament_events.rs b/src/queries/tournament_events.rs index ea07730..04bd6aa 100644 --- a/src/queries/tournament_events.rs +++ b/src/queries/tournament_events.rs @@ -53,7 +53,6 @@ struct PageInfo { #[derive(cynic::QueryFragment, Debug)] #[cynic(variables = "TournamentEventsVars")] struct Tournament { - name: Option, #[arguments(limit: 99999, filter: { videogameId: [$game_id] })] #[cynic(flatten)] events: Vec, @@ -63,6 +62,7 @@ struct Tournament { #[cynic(variables = "TournamentEventsVars")] struct Event { id: Option, + start_at: Option, } // Unwrap @@ -75,8 +75,13 @@ pub struct TournamentEventResponse { #[derive(Debug, Clone)] pub struct TournamentData { - pub name: String, - pub events: Vec, + pub events: Vec, +} + +#[derive(Debug, Clone)] +pub struct EventData { + pub id: EventId, + pub time: Timestamp, } impl<'a> QueryUnwrap> for TournamentEvents { @@ -92,11 +97,15 @@ impl<'a> QueryUnwrap> for TournamentEvents { .into_iter() .filter_map(|tour| { Some(TournamentData { - name: tour.name?, events: tour .events .into_iter() - .filter_map(|event| event.id) + .filter_map(|event| { + Some(EventData { + id: event.id?, + time: event.start_at?, + }) + }) .collect(), }) }) diff --git a/src/sync.rs b/src/sync.rs index beb803a..4fe1e1f 100644 --- a/src/sync.rs +++ b/src/sync.rs @@ -118,7 +118,7 @@ fn get_event_sets(event: EventId, auth: &str) -> Option> { } } -fn get_tournament_events(metadata: &DatasetMetadata, auth: &str) -> Option> { +fn get_tournament_events(metadata: &DatasetMetadata, auth: &str) -> Option> { println!("Accessing tournaments..."); let tour_response = run_query::( @@ -182,10 +182,13 @@ fn update_from_set( connection: &Connection, dataset: &str, metadata: &DatasetMetadata, + event_time: Timestamp, results: SetData, ) -> sqlite::Result<()> { let players_data = results.teams; - add_players(connection, dataset, &players_data, results.time)?; + // Fall back to event time if set time is not recorded + let time = results.time.unwrap_or(event_time); + add_players(connection, dataset, &players_data, time)?; // Non-singles matches are currently not supported if players_data.len() != 2 || players_data[0].len() != 1 || players_data[1].len() != 1 { @@ -198,10 +201,10 @@ fn update_from_set( drop(it); let (deviation1, volatility1, last_played1) = get_player_data(connection, dataset, player1)?; - let time1 = results.time.0.checked_sub(last_played1.0).unwrap_or(0); + let time1 = time.0.checked_sub(last_played1.0).unwrap_or(0); let (deviation2, volatility2, last_played2) = get_player_data(connection, dataset, player1)?; - let time2 = results.time.0.checked_sub(last_played2.0).unwrap_or(0); + let time2 = time.0.checked_sub(last_played2.0).unwrap_or(0); let advantage = match get_advantage(connection, dataset, player1, player2) { Err(e) => Err(e)?, @@ -243,7 +246,7 @@ fn update_from_set( connection, dataset, player1, - results.time, + time, dev_new1, vol_new1, results.winner == 0, @@ -253,7 +256,7 @@ fn update_from_set( connection, dataset, player2, - results.time, + time, dev_new2, vol_new2, results.winner == 1, @@ -290,13 +293,13 @@ pub fn sync_dataset( for (i, event) in events.into_iter().enumerate() { println!( "Accessing sets from event ID {}... ({}/{})", - event.0, + event.id.0, i + 1, num_events ); let mut sets = - get_event_sets(event, auth).unwrap_or_else(|| error("Could not access start.gg", 1)); + get_event_sets(event.id, auth).unwrap_or_else(|| error("Could not access start.gg", 1)); if sets.is_empty() { println!(" No valid sets"); @@ -304,8 +307,9 @@ pub fn sync_dataset( println!(" Updating ratings from event..."); sets.sort_by_key(|set| set.time); - sets.into_iter() - .try_for_each(|set| update_from_set(connection, dataset, &metadata, set))?; + sets.into_iter().try_for_each(|set| { + update_from_set(connection, dataset, &metadata, event.time, set) + })?; } } connection.execute("COMMIT;") @@ -327,9 +331,10 @@ mod tests { &connection, "test", &metadata(), + Timestamp(0), SetData { id: SetId(StringOrInt::Int(0)), - time: Timestamp(0), + time: None, teams: players, winner: 0, },