From 1d870362b528303708a87c3a8e9c948159bb8ff3 Mon Sep 17 00:00:00 2001 From: Kiana Sheibani Date: Sat, 2 Sep 2023 03:15:38 -0400 Subject: [PATCH] Generalize API request to handle teams of any size --- cli/src/queries.rs | 4 ++ cli/src/queries/tournament_sets.rs | 92 +++++++++++++++++++----------- 2 files changed, 62 insertions(+), 34 deletions(-) diff --git a/cli/src/queries.rs b/cli/src/queries.rs index 1dd9eae..20e4c7f 100644 --- a/cli/src/queries.rs +++ b/cli/src/queries.rs @@ -23,6 +23,10 @@ pub struct VideogameId(pub u64); #[cynic(graphql_type = "ID")] pub struct EntrantId(pub u64); +#[derive(cynic::Scalar, Debug, Copy, Clone)] +#[cynic(graphql_type = "ID")] +pub struct PlayerId(pub u64); + #[derive(cynic::Scalar, Debug, Clone)] pub struct Timestamp(pub u64); diff --git a/cli/src/queries/tournament_sets.rs b/cli/src/queries/tournament_sets.rs index e8e63df..99b7c95 100644 --- a/cli/src/queries/tournament_sets.rs +++ b/cli/src/queries/tournament_sets.rs @@ -1,4 +1,4 @@ -use super::{EntrantId, QueryUnwrap, Timestamp, VideogameId}; +use super::{EntrantId, PlayerId, QueryUnwrap, Timestamp, VideogameId}; use cynic::GraphQlResponse; use schema::schema; @@ -10,8 +10,8 @@ pub struct TournamentSetsVars<'a> { // server-side bug that completely breaks everything when this isn't passed. pub last_query: Timestamp, - pub country: Option<&'a str>, pub game_id: VideogameId, + pub country: Option<&'a str>, pub state: Option<&'a str>, } @@ -27,57 +27,69 @@ pub struct TournamentSets { filter: { past: true, afterDate: $last_query, - addrState: $state, + videogameIds: [$game_id], countryCode: $country, - videogameIds: [$game_id] + addrState: $state }})] - tournaments: Option, + pub tournaments: Option, } #[derive(cynic::QueryFragment, Debug)] #[cynic(variables = "TournamentSetsVars")] -struct TournamentConnection { +pub struct TournamentConnection { #[cynic(flatten)] - nodes: Vec, + pub nodes: Vec, } #[derive(cynic::QueryFragment, Debug)] #[cynic(variables = "TournamentSetsVars")] -struct Tournament { - name: Option, - #[arguments(limit: 1000, filter: { videogameId: [$game_id] })] +pub struct Tournament { + pub name: Option, + #[arguments(limit: 99999, filter: { videogameId: [$game_id] })] #[cynic(flatten)] - events: Vec, + pub events: Vec, } #[derive(cynic::QueryFragment, Debug)] -struct Event { +pub struct Event { #[arguments(page: 1, perPage: 999)] - sets: Option, + pub sets: Option, } #[derive(cynic::QueryFragment, Debug)] -struct SetConnection { +pub struct SetConnection { #[cynic(flatten)] - nodes: Vec, + pub nodes: Vec, } #[derive(cynic::QueryFragment, Debug)] -struct Set { +pub struct Set { #[arguments(includeByes: true)] #[cynic(flatten)] - slots: Vec, - winner_id: Option, + pub slots: Vec, + pub winner_id: Option, } #[derive(cynic::QueryFragment, Debug)] -struct SetSlot { - entrant: Option, +pub struct SetSlot { + pub entrant: Option, } #[derive(cynic::QueryFragment, Debug)] -struct Entrant { - id: Option, +pub struct Entrant { + pub id: Option, + #[cynic(flatten)] + pub participants: Vec, +} + +#[derive(cynic::QueryFragment, Debug)] +pub struct Participant { + pub player: Option, +} + +#[derive(cynic::QueryFragment, Debug)] +pub struct Player { + pub id: Option, } // Unwrap @@ -88,9 +100,8 @@ pub struct TournamentResponse { } pub struct SetResponse { - pub player1: EntrantId, - pub player2: EntrantId, - pub winner: bool, + pub teams: Vec>, + pub winner: usize, } impl<'a> QueryUnwrap> for TournamentSets { @@ -122,15 +133,28 @@ impl<'a> QueryUnwrap> for TournamentSets { .nodes .into_iter() .filter_map(|set| { - let slots = set.slots; - let player1 = (&slots[0]).entrant.as_ref()?.id?; - let player2 = (&slots[0]).entrant.as_ref()?.id?; - let winner = set.winner_id? as u64; - Some(SetResponse { - player1, - player2, - winner: winner == player2.0, - }) + let winner_id = set.winner_id?; + let winner = set.slots.iter().position(|slot| { + slot.entrant + .as_ref() + .and_then(|x| x.id) + .map(|id| id.0 == winner_id as u64) + .unwrap_or(false) + })?; + let teams = set + .slots + .into_iter() + .filter_map(|slot| { + Some( + slot.entrant? + .participants + .into_iter() + .filter_map(|p| Some(p.player?.id?)) + .collect(), + ) + }) + .collect(); + Some(SetResponse { teams, winner }) }) .collect::>(), )