Move CLI crate to root of workspace

Somehow I missed that this was a thing you could do? It's a much
cleaner organization, and it makes it so that you don't have to
explicitly specify the crate to build.
This commit is contained in:
Kiana Sheibani 2023-09-23 03:01:36 -04:00
parent 2c6587aca1
commit dd4f0838ab
Signed by: toki
GPG key ID: 6CB106C25E86A9F7
14 changed files with 75 additions and 83 deletions

View file

@ -0,0 +1,48 @@
use super::{PlayerId, QueryUnwrap};
use cynic::GraphQlResponse;
use schema::schema;
// Variables
#[derive(cynic::QueryVariables, Debug)]
pub struct PlayerInfoVars {
pub id: PlayerId,
}
// Query
#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "Query", variables = "PlayerInfoVars")]
pub struct PlayerInfo {
#[arguments(id: $id)]
player: Option<Player>,
}
#[derive(cynic::QueryFragment, Debug)]
struct Player {
id: Option<PlayerId>,
gamer_tag: Option<String>,
prefix: Option<String>,
}
// Unwrapping
#[derive(Debug, Clone)]
pub struct PlayerData {
pub id: PlayerId,
pub name: Option<String>,
pub prefix: Option<String>,
}
impl QueryUnwrap<PlayerInfoVars> for PlayerInfo {
type Unwrapped = PlayerData;
fn unwrap_response(response: GraphQlResponse<PlayerInfo>) -> Option<PlayerData> {
let player = response.data?.player?;
Some(PlayerData {
id: player.id?,
name: player.gamer_tag,
prefix: player.prefix.filter(|pr| !pr.is_empty()),
})
}
}

View file

@ -0,0 +1,60 @@
use super::{QueryUnwrap, VideogameId};
use cynic::GraphQlResponse;
use schema::schema;
// Variables
#[derive(cynic::QueryVariables)]
pub struct VideogameSearchVars<'a> {
pub name: &'a str,
}
// Query
#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "Query", variables = "VideogameSearchVars")]
pub struct VideogameSearch {
#[arguments(query: { filter: { name: $name }, page: 1, perPage: 10 })]
videogames: Option<VideogameConnection>,
}
#[derive(cynic::QueryFragment, Debug)]
struct VideogameConnection {
#[cynic(flatten)]
nodes: Vec<Videogame>,
}
#[derive(cynic::QueryFragment, Debug)]
struct Videogame {
id: Option<VideogameId>,
name: Option<String>,
}
// Unwrapping
#[derive(Debug, Clone)]
pub struct VideogameData {
pub id: VideogameId,
pub name: String,
}
impl<'a> QueryUnwrap<VideogameSearchVars<'a>> for VideogameSearch {
type Unwrapped = Vec<VideogameData>;
fn unwrap_response(response: GraphQlResponse<VideogameSearch>) -> Option<Vec<VideogameData>> {
Some(
response
.data?
.videogames?
.nodes
.into_iter()
.filter_map(|game| {
Some(VideogameData {
id: game.id?,
name: game.name?,
})
})
.collect(),
)
}
}

View file

@ -0,0 +1,177 @@
use super::{EntrantId, PlayerData, PlayerId, QueryUnwrap, Timestamp, VideogameId};
use cynic::GraphQlResponse;
use schema::schema;
pub type Teams<T> = Vec<Vec<T>>;
// Variables
#[derive(cynic::QueryVariables, Debug)]
pub struct TournamentSetsVars<'a> {
// HACK: This should really be an optional variable, but there seems to be a
// server-side bug that completely breaks everything when this isn't passed.
// We can use a dummy value of 1 when we don't want to filter by time.
pub last_query: Timestamp,
pub game_id: VideogameId,
pub country: Option<&'a str>,
pub state: Option<&'a str>,
}
// Query
#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "Query", variables = "TournamentSetsVars")]
pub struct TournamentSets {
#[arguments(query: {
page: 1,
perPage: 1,
sortBy: "endAt desc",
filter: {
past: true,
afterDate: $last_query,
videogameIds: [$game_id],
countryCode: $country,
addrState: $state
}})]
tournaments: Option<TournamentConnection>,
}
#[derive(cynic::QueryFragment, Debug)]
#[cynic(variables = "TournamentSetsVars")]
struct TournamentConnection {
#[cynic(flatten)]
nodes: Vec<Tournament>,
}
#[derive(cynic::QueryFragment, Debug)]
#[cynic(variables = "TournamentSetsVars")]
struct Tournament {
name: Option<String>,
#[arguments(limit: 99999, filter: { videogameId: [$game_id] })]
#[cynic(flatten)]
events: Vec<Event>,
}
#[derive(cynic::QueryFragment, Debug)]
struct Event {
#[arguments(page: 1, perPage: 999)]
sets: Option<SetConnection>,
}
#[derive(cynic::QueryFragment, Debug)]
struct SetConnection {
#[cynic(flatten)]
nodes: Vec<Set>,
}
#[derive(cynic::QueryFragment, Debug)]
struct Set {
#[arguments(includeByes: true)]
#[cynic(flatten)]
slots: Vec<SetSlot>,
winner_id: Option<i32>,
}
#[derive(cynic::QueryFragment, Debug)]
struct SetSlot {
entrant: Option<Entrant>,
}
#[derive(cynic::QueryFragment, Debug)]
struct Entrant {
id: Option<EntrantId>,
#[cynic(flatten)]
participants: Vec<Participant>,
}
#[derive(cynic::QueryFragment, Debug)]
struct Participant {
player: Option<Player>,
}
#[derive(cynic::QueryFragment, Debug)]
struct Player {
id: Option<PlayerId>,
gamer_tag: Option<String>,
prefix: Option<String>,
}
// Unwrap
#[derive(Debug, Clone)]
pub struct TournamentData {
pub name: String,
pub sets: Vec<SetData>,
}
#[derive(Debug, Clone)]
pub struct SetData {
pub teams: Teams<PlayerData>,
pub winner: usize,
}
impl<'a> QueryUnwrap<TournamentSetsVars<'a>> for TournamentSets {
type Unwrapped = Vec<TournamentData>;
// This might be the most spaghetti code I've ever written
fn unwrap_response(response: GraphQlResponse<TournamentSets>) -> Option<Vec<TournamentData>> {
Some(
response
.data?
.tournaments?
.nodes
.into_iter()
.filter_map(|tour| {
let sets = tour
.events
.into_iter()
.filter_map(|event| {
Some(
event
.sets?
.nodes
.into_iter()
.filter_map(|set| {
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()
.map(|slot| {
slot.entrant?
.participants
.into_iter()
.map(|p| {
let p_ = p.player?;
Some(PlayerData {
id: p_.id?,
name: p_.gamer_tag,
prefix: p_.prefix,
})
})
.try_collect()
})
.try_collect()?;
Some(SetData { teams, winner })
})
.collect::<Vec<_>>(),
)
})
.flatten()
.collect();
Some(TournamentData {
name: tour.name?,
sets,
})
})
.collect(),
)
}
}