import _ from 'lodash' ; import log from '../util/Logger' ; import NI from '../util/NetworkInterface' ; import * as queries from '../scripts/phaseGroupQueries' ; import { IPhaseGroup , IPhaseGroupData , IPhaseGroupDataFull , IPhaseGroupEventData , IPhaseGroupSeedData , IPhaseGroupAttendeeData , IPhaseGroupEntrantData , IPhaseGroupSetData } from '../interfaces/IPhaseGroup' ; import { IAttendee , IAttendeeData , IAttendeeOptions } from '../interfaces/IAttendee' ; import { IEntrant , IEntrantData , IEntrantDataFull , IEntrantOptions } from '../interfaces/IEntrant' ; import { IGGSet , IGGSetData , IGGSetOptions } from '../interfaces/IGGSet' ; import { ISeed , ISeedData , ISeedOptions } from '../interfaces/ISeed' ; import { Attendee } from './Attendee' ; import { Entrant } from './Entrant' ; import { GGSet } from './GGSet' ; import { Seed } from './Seed' ; export class PhaseGroup implements IPhaseGroup { public static parse ( data ) { return new PhaseGroup ( data . id , data . phaseId , data . displayIdentifier , data . firstRoundTime , data . state , data . waveId , data . tiebreakOrder ) ; } public static parseFull ( data ) { return PhaseGroup . parse ( data . phaseGroup ) ; } public static parseEventData ( data ) { return data . event . phaseGroups . map ( pg => PhaseGroup . parse ( pg ) ) ; } public static async get ( theId ) { log . info ( 'Getting Phase Group with id %s' , theId ) ; const data = await NI . query ( queries . phaseGroup , { id : theId } ) ; return PhaseGroup . parse ( data . phaseGroup ) ; } public id : number ; public phaseId : number ; public displayIdentifier : string | null ; public firstRoundTime : number | null ; public state : number | null ; public waveId : number | null ; public tiebreakOrder : object | null ; constructor ( id , phaseId , displayIdentifier , firstRoundTime , state , waveId , tiebreakOrder ) { this . id = id ; this . phaseId = phaseId ; this . displayIdentifier = displayIdentifier ; this . firstRoundTime = firstRoundTime ; this . state = state ; this . waveId = waveId ; this . tiebreakOrder = tiebreakOrder ; } public getId ( ) { return this . id ; } public getPhaseId ( ) { return this . phaseId ; } public getDisplayIdentifier ( ) { return this . displayIdentifier ; } public getFirstRoundTime ( ) { return this . firstRoundTime ; } public getState ( ) { return this . state ; } public getWaveId ( ) { return this . waveId ; } public getTiebreakOrder ( ) { return this . tiebreakOrder ; } public async getSeeds ( options = Seed . getDefaultSeedOptions ( ) ) { log . info ( 'Getting Seeds for Phase Group [%s]' , this . id ) ; log . verbose ( 'Query variables: %s' , JSON . stringify ( options ) ) ; const data = await NI . paginatedQuery ( `Phase Group Seeds [${ this . id }]` , queries . phaseGroupSeeds , { id : this . id } , options , { } , 2 ) ; const phaseGroups = _ . flatten ( data . map ( pg => pg . phaseGroup ) ) ; const seedData = _ . flatten ( phaseGroups . map ( pg => pg . paginatedSeeds . nodes ) ) ; const seeds = seedData . map ( seed => Seed . parse ( seed ) ) ; return _ . uniqBy ( seeds , 'id' ) ; } public async getEntrants ( options = Entrant . getDefaultEntrantOptions ( ) ) { log . info ( 'Getting Entrants for Phase Group [%s]' , this . id ) ; log . verbose ( 'Query variables: %s' , JSON . stringify ( options ) ) ; const phaseGroupEntrantData = await NI . paginatedQuery ( `Phase Group Entrants [${ this . id }]` , queries . phaseGroupEntrants , { id : this . id } , options , { } , 2 ) ; const phaseGroups = phaseGroupEntrantData . map ( groupData => groupData . phaseGroup ) ; const entrants = _ . flatten ( phaseGroups . map ( pg => pg . paginatedSeeds . nodes . map ( ( e ) => Entrant . parseFull ( e ) ) . filter ( seed => seed != null ) ) ) ; return _ . uniqBy ( entrants , 'id' ) ; } public async getAttendees ( options = Attendee . getDefaultAttendeeOptions ( ) ) { log . info ( 'Getting Attendees for Phase Group [%s]' , this . id ) ; log . verbose ( 'Query variables: %s' , JSON . stringify ( options ) ) ; const data = await NI . paginatedQuery ( `Phase Group Attendees [${ this . id }]` , queries . phaseGroupAttendees , { id : this . id } , options , { } , 2 ) ; const seeds = _ . flatten ( data . map ( entrant => entrant . phaseGroup . paginatedSeeds . nodes ) ) ; const entrants = seeds . map ( seed => seed . entrant ) . filter ( entrant => entrant != null ) ; const attendeeData = _ . flatten ( entrants . map ( entrant => entrant . participants ) ) ; const attendees = attendeeData . map ( a => Attendee . parse ( a ) ) ; return _ . uniqBy ( attendees , 'id' ) ; } public async getSets ( options = GGSet . getDefaultSetOptions ( ) ) { log . info ( 'Getting Sets for Phase Group [%s]' , this . id ) ; log . verbose ( 'Query variables: %s' , JSON . stringify ( options ) ) ; const data = await NI . paginatedQuery ( `Phase Group Sets [${ this . id }]` , queries . phaseGroupSets , { id : this . id } , options , { } , 2 ) ; const phaseGroups = data . map ( pg => pg . phaseGroup ) ; let sets = _ . flatten ( phaseGroups . map ( pg => pg . paginatedSets . nodes . map ( set => GGSet . parse ( set ) ) . filter ( set => set != null ) ) ) ; if ( options . filterByes ) sets = GGSet . filterOutDQs ( sets ) ; if ( options . filterResets ) sets = GGSet . filterOutResets ( sets ) ; if ( options . filterByes ) sets = GGSet . filterOutByes ( sets ) ; return _ . uniqBy ( sets , 'id' ) ; } public async getCompleteSets ( options = GGSet . getDefaultSetOptions ( ) ) { log . info ( 'Getting Completed sets for Phase Group [%s]' , this . id ) ; const sets = await this . getSets ( options ) ; return GGSet . filterForCompleteSets ( sets ) ; } public async getIncompleteSets ( options = GGSet . getDefaultSetOptions ( ) ) { log . info ( 'Getting Incompleted sets for Phase Group [%s]' , this . id ) ; const sets = await this . getSets ( options ) ; return GGSet . filterForIncompleteSets ( sets ) ; } public async getSetsXMinutesBack ( minutes , options = GGSet . getDefaultSetOptions ( ) ) { log . info ( 'Getting sets completed %s minutes ago for Phase Group [%s]' , minutes , this . id ) ; const sets = await this . getSets ( options ) ; return GGSet . filterForXMinutesBack ( sets , minutes ) ; } }