From 07284bef06097fc14ebbe56fecf31f909eed2b66 Mon Sep 17 00:00:00 2001 From: Kiana Sheibani Date: Sun, 27 Aug 2023 16:18:22 -0400 Subject: [PATCH] Add tournament query --- cli/src/queries.rs | 4 + cli/src/queries/tournament_sets.rs | 163 +++++++++++++++++++++++++++++ 2 files changed, 167 insertions(+) create mode 100644 cli/src/queries/tournament_sets.rs diff --git a/cli/src/queries.rs b/cli/src/queries.rs index 87e2315..ac03838 100644 --- a/cli/src/queries.rs +++ b/cli/src/queries.rs @@ -2,6 +2,7 @@ use cynic::{GraphQlResponse, QueryBuilder}; use serde::{Deserialize, Serialize}; pub mod search_games; +pub mod tournament_sets; use schema::schema; @@ -19,6 +20,9 @@ pub struct VideogameId(pub u64); #[derive(Debug, Copy, Clone)] pub struct EntrantId(pub u64); +#[derive(cynic::Scalar, Debug, Clone)] +pub struct Timestamp(pub u64); + // Query machinery pub trait QueryUnwrap: QueryBuilder { diff --git a/cli/src/queries/tournament_sets.rs b/cli/src/queries/tournament_sets.rs new file mode 100644 index 0000000..614920d --- /dev/null +++ b/cli/src/queries/tournament_sets.rs @@ -0,0 +1,163 @@ +use super::{EntrantId, QueryUnwrap, Timestamp, VideogameId, ID}; +use cynic::GraphQlResponse; +use schema::schema; + +// Variables + +#[derive(cynic::QueryVariables, Debug)] +pub struct TournamentSetsVarsRaw<'a> { + country: Option<&'a str>, + game_id: ID, + last_query: Option, + state: Option<&'a str>, +} + +// Query + +#[derive(cynic::QueryFragment, Debug)] +#[cynic(graphql_type = "Query", variables = "TournamentSetsVarsRaw")] +pub struct TournamentSets { + #[arguments(query: { + page: 1, + perPage: 1, + sortBy: "startAt desc", + filter: { + past: true, + afterDate: $last_query, + addrState: $state, + countryCode: $country, + videogameIds: [$game_id] + }})] + tournaments: Option, +} + +#[derive(cynic::QueryFragment, Debug)] +#[cynic(variables = "TournamentSetsVarsRaw")] +struct TournamentConnection { + nodes: Option>>, +} + +#[derive(cynic::QueryFragment, Debug)] +#[cynic(variables = "TournamentSetsVarsRaw")] +struct Tournament { + name: Option, + #[arguments(limit: 1000, filter: { videogameId: [$game_id] })] + events: Option>>, +} + +#[derive(cynic::QueryFragment, Debug)] +struct Event { + #[arguments(page: 1, perPage: 999)] + sets: Option, +} + +#[derive(cynic::QueryFragment, Debug)] +struct SetConnection { + nodes: Option>>, +} + +#[derive(cynic::QueryFragment, Debug)] +struct Set { + #[arguments(includeByes: true)] + slots: Option>>, + winner_id: Option, +} + +#[derive(cynic::QueryFragment, Debug)] +struct SetSlot { + entrant: Option, +} + +#[derive(cynic::QueryFragment, Debug)] +struct Entrant { + id: Option, +} + +// Unwrap + +pub struct TournamentSetsVars<'a> { + pub country: Option<&'a str>, + pub game_id: VideogameId, + pub last_query: Option, + pub state: Option<&'a str>, +} + +pub struct TournamentResponse { + pub name: String, + pub sets: Vec, +} + +pub struct SetResponse { + pub player1: EntrantId, + pub player2: EntrantId, + pub winner: bool, +} + +impl<'a> QueryUnwrap> for TournamentSets { + type VarsUnwrapped = TournamentSetsVars<'a>; + type Unwrapped = Vec; + + fn wrap_vars( + TournamentSetsVars { + country, + game_id: VideogameId(game_id), + last_query, + state, + }: TournamentSetsVars, + ) -> TournamentSetsVarsRaw { + TournamentSetsVarsRaw { + country, + game_id: ID(game_id), + last_query, + state, + } + } + + // This might be the most spaghetti code I've ever written + fn unwrap_response( + response: GraphQlResponse, + ) -> Option> { + Some( + response + .data? + .tournaments? + .nodes? + .into_iter() + .filter_map(|tour| { + let tour_ = tour?; + let sets = tour_ + .events? + .into_iter() + .filter_map(|event| { + let event_ = event?; + Some( + event_ + .sets? + .nodes? + .into_iter() + .filter_map(|set| { + let set_ = set?; + let slots = set_.slots?; + let player1 = (&slots[0]).as_ref()?.entrant.as_ref()?.id?.0; + let player2 = (&slots[0]).as_ref()?.entrant.as_ref()?.id?.0; + let winner = set_.winner_id? as u64; + Some(SetResponse { + player1: EntrantId(player1), + player2: EntrantId(player2), + winner: winner == player2, + }) + }) + .collect::>(), + ) + }) + .flatten() + .collect(); + Some(TournamentResponse { + name: tour_.name?, + sets, + }) + }) + .collect(), + ) + } +}