Generalize API request to handle teams of any size

This commit is contained in:
Kiana Sheibani 2023-09-02 03:15:38 -04:00
parent 38e7b4a018
commit 1d870362b5
Signed by: toki
GPG key ID: 6CB106C25E86A9F7
2 changed files with 62 additions and 34 deletions

View file

@ -23,6 +23,10 @@ pub struct VideogameId(pub u64);
#[cynic(graphql_type = "ID")] #[cynic(graphql_type = "ID")]
pub struct EntrantId(pub u64); 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)] #[derive(cynic::Scalar, Debug, Clone)]
pub struct Timestamp(pub u64); pub struct Timestamp(pub u64);

View file

@ -1,4 +1,4 @@
use super::{EntrantId, QueryUnwrap, Timestamp, VideogameId}; use super::{EntrantId, PlayerId, QueryUnwrap, Timestamp, VideogameId};
use cynic::GraphQlResponse; use cynic::GraphQlResponse;
use schema::schema; use schema::schema;
@ -10,8 +10,8 @@ pub struct TournamentSetsVars<'a> {
// server-side bug that completely breaks everything when this isn't passed. // server-side bug that completely breaks everything when this isn't passed.
pub last_query: Timestamp, pub last_query: Timestamp,
pub country: Option<&'a str>,
pub game_id: VideogameId, pub game_id: VideogameId,
pub country: Option<&'a str>,
pub state: Option<&'a str>, pub state: Option<&'a str>,
} }
@ -27,57 +27,69 @@ pub struct TournamentSets {
filter: { filter: {
past: true, past: true,
afterDate: $last_query, afterDate: $last_query,
addrState: $state, videogameIds: [$game_id],
countryCode: $country, countryCode: $country,
videogameIds: [$game_id] addrState: $state
}})] }})]
tournaments: Option<TournamentConnection>, pub tournaments: Option<TournamentConnection>,
} }
#[derive(cynic::QueryFragment, Debug)] #[derive(cynic::QueryFragment, Debug)]
#[cynic(variables = "TournamentSetsVars")] #[cynic(variables = "TournamentSetsVars")]
struct TournamentConnection { pub struct TournamentConnection {
#[cynic(flatten)] #[cynic(flatten)]
nodes: Vec<Tournament>, pub nodes: Vec<Tournament>,
} }
#[derive(cynic::QueryFragment, Debug)] #[derive(cynic::QueryFragment, Debug)]
#[cynic(variables = "TournamentSetsVars")] #[cynic(variables = "TournamentSetsVars")]
struct Tournament { pub struct Tournament {
name: Option<String>, pub name: Option<String>,
#[arguments(limit: 1000, filter: { videogameId: [$game_id] })] #[arguments(limit: 99999, filter: { videogameId: [$game_id] })]
#[cynic(flatten)] #[cynic(flatten)]
events: Vec<Event>, pub events: Vec<Event>,
} }
#[derive(cynic::QueryFragment, Debug)] #[derive(cynic::QueryFragment, Debug)]
struct Event { pub struct Event {
#[arguments(page: 1, perPage: 999)] #[arguments(page: 1, perPage: 999)]
sets: Option<SetConnection>, pub sets: Option<SetConnection>,
} }
#[derive(cynic::QueryFragment, Debug)] #[derive(cynic::QueryFragment, Debug)]
struct SetConnection { pub struct SetConnection {
#[cynic(flatten)] #[cynic(flatten)]
nodes: Vec<Set>, pub nodes: Vec<Set>,
} }
#[derive(cynic::QueryFragment, Debug)] #[derive(cynic::QueryFragment, Debug)]
struct Set { pub struct Set {
#[arguments(includeByes: true)] #[arguments(includeByes: true)]
#[cynic(flatten)] #[cynic(flatten)]
slots: Vec<SetSlot>, pub slots: Vec<SetSlot>,
winner_id: Option<i32>, pub winner_id: Option<i32>,
} }
#[derive(cynic::QueryFragment, Debug)] #[derive(cynic::QueryFragment, Debug)]
struct SetSlot { pub struct SetSlot {
entrant: Option<Entrant>, pub entrant: Option<Entrant>,
} }
#[derive(cynic::QueryFragment, Debug)] #[derive(cynic::QueryFragment, Debug)]
struct Entrant { pub struct Entrant {
id: Option<EntrantId>, pub id: Option<EntrantId>,
#[cynic(flatten)]
pub participants: Vec<Participant>,
}
#[derive(cynic::QueryFragment, Debug)]
pub struct Participant {
pub player: Option<Player>,
}
#[derive(cynic::QueryFragment, Debug)]
pub struct Player {
pub id: Option<PlayerId>,
} }
// Unwrap // Unwrap
@ -88,9 +100,8 @@ pub struct TournamentResponse {
} }
pub struct SetResponse { pub struct SetResponse {
pub player1: EntrantId, pub teams: Vec<Vec<PlayerId>>,
pub player2: EntrantId, pub winner: usize,
pub winner: bool,
} }
impl<'a> QueryUnwrap<TournamentSetsVars<'a>> for TournamentSets { impl<'a> QueryUnwrap<TournamentSetsVars<'a>> for TournamentSets {
@ -122,15 +133,28 @@ impl<'a> QueryUnwrap<TournamentSetsVars<'a>> for TournamentSets {
.nodes .nodes
.into_iter() .into_iter()
.filter_map(|set| { .filter_map(|set| {
let slots = set.slots; let winner_id = set.winner_id?;
let player1 = (&slots[0]).entrant.as_ref()?.id?; let winner = set.slots.iter().position(|slot| {
let player2 = (&slots[0]).entrant.as_ref()?.id?; slot.entrant
let winner = set.winner_id? as u64; .as_ref()
Some(SetResponse { .and_then(|x| x.id)
player1, .map(|id| id.0 == winner_id as u64)
player2, .unwrap_or(false)
winner: winner == player2.0, })?;
}) 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::<Vec<_>>(), .collect::<Vec<_>>(),
) )