import GameState from "../../../../GameState" ; import ClashOfKingsGameState from "../ClashOfKingsGameState" ; import Player from "../../../Player" ; import House from "../../../game-data-structure/House" ; import { ClientMessage } from "../../../../../messages/ClientMessage" ; import { ServerMessage } from "../../../../../messages/ServerMessage" ; import * as _ from "lodash" ; import EntireGame from "../../../../EntireGame" ; import Game from "../../../game-data-structure/Game" ; import User from "../../../../../server/User" ; export default class ResolveTiesGameState extends GameState < ClashOfKingsGameState > { bidResults : [ number , House [ ] ] [ ] ; get stars ( ) : boolean { return this . parentGameState . currentTrackI == 2 ; } get entireGame ( ) : EntireGame { return this . parentGameState . entireGame ; } get game ( ) : Game { return this . parentGameState . game ; } get decider ( ) : House { return this . parentGameState . game . ironThroneHolder ; } firstStart ( bidResults ) { this . bidResults = bidResults ; } onPlayerMessage ( player , message ) { if ( message . type == "resolve-ties" ) { if ( player . house != this . decider ) { return ; } const resolvedTies = message . resolvedTies . map ( ( hids ) => hids . map ( hid => this . game . houses . get ( hid ) ) ) ; const tiesToResolve = this . getTiesToResolve ( ) ; if ( resolvedTies . length != tiesToResolve . length ) { return ; } for ( let i = 0 ; i < tiesToResolve . length ; i ++ ) { if ( resolvedTies [ i ] . length != tiesToResolve [ i ] . houses . length ) { return ; } if ( _ . difference ( resolvedTies [ i ] , tiesToResolve [ i ] . houses ) . length != 0 ) { return ; } } this . parentGameState . ingame . log ( { type : "ties-decided" , house : this . decider . id } ) ; let tieProgression = - 1 ; const finalOrdering = _ . flatten ( this . bidResults . map ( ( [ _bid , houses ] ) => { if ( houses . length == 1 ) { return houses ; } else { tieProgression ++ ; return resolvedTies [ tieProgression ] ; } } ) ) ; this . parentGameState . onResolveTiesGameState ( this . bidResults , finalOrdering ) ; } } onServerMessage ( _message ) { } resolveTies ( resolvedTies ) { this . entireGame . sendMessageToServer ( { type : "resolve-ties" , resolvedTies : resolvedTies . map ( houses => houses . map ( h => h . id ) ) } ) ; } getWaitedUsers ( ) { return [ this . parentGameState . parentGameState . ingame . getControllerOfHouse ( this . decider ) . user ] ; } getTiesToResolve ( ) { return this . bidResults . map ( ( [ _bid , houses ] , i ) => { return ( { trackerPlace : _ . sum ( this . bidResults . slice ( 0 , i ) . map ( ( [ _bid , houses ] ) => houses . length ) ) , houses : houses } ) ; } ) . filter ( ( { houses } ) => houses . length > 1 ) ; } getBidOfHouse ( house ) { const index = this . bidResults . findIndex ( ( [ _bid , houses ] ) => houses . includes ( house ) ) ; if ( index > - 1 ) { return this . bidResults [ index ] [ 0 ] ; } return - 1 ; } serializeToClient ( _admin , _player ) { return { type : "resolve-ties" , bidResults : this . bidResults . map ( ( [ bid , houses ] ) => ( [ bid , houses . map ( h => h . id ) ] ) ) } ; } static deserializeFromServer ( clashOfKings , data ) { const resolveTies = new ResolveTiesGameState ( clashOfKings ) ; resolveTies . bidResults = data . bidResults . map ( ( [ bid , houseIds ] ) => ( [ bid , houseIds . map ( ( hid => clashOfKings . game . houses . get ( hid ) ) ) ] ) ) ; return resolveTies ; } } export interface SerializedResolveTiesGameState { type : "resolve-ties" ; bidResults : [ number , string [ ] ] [ ] ; }