import GameState from "../../../../GameState" ; import ActionGameState from "../../ActionGameState" ; import { ClientMessage } from "../../../../../messages/ClientMessage" ; import Player from "../../../Player" ; import { ServerMessage } from "../../../../../messages/ServerMessage" ; import Game from "../../../game-data-structure/Game" ; import House from "../../../game-data-structure/House" ; import EntireGame from "../../../../EntireGame" ; import { land , port , sea } from "../../../game-data-structure/regionTypes" ; import Region from "../../../game-data-structure/Region" ; import IngameGameState from "../../../IngameGameState" ; import IronBankOrderType from "../../../game-data-structure/order-types/IronBankOrderType" ; import ResolveConsolidatePowerGameState from "../ResolveConsolidatePowerGameState" ; import ConsolidatePowerOrderType from "../../../game-data-structure/order-types/ConsolidatePowerOrderType" ; import User from "../../../../../server/User" ; import DefenseMusterOrderType from "../../../game-data-structure/order-types/DefenseMusterOrderType" ; import PlayerMusteringGameState , { PlayerMusteringType } from "../../../westeros-game-state/mustering-game-state/player-mustering-game-state/PlayerMusteringGameState" ; import IronBank from "../../../game-data-structure/IronBank" ; import { observable } from "mobx" ; export default class ResolveSingleConsolidatePowerGameState extends GameState < ResolveConsolidatePowerGameState > { @ observable house : House ; get game ( ) : Game { return this . actionGameState . game ; } get entireGame ( ) : EntireGame { return this . actionGameState . entireGame ; } get actionGameState ( ) : ActionGameState { return this . parentGameState . parentGameState ; } get ingame ( ) : IngameGameState { return this . actionGameState . ingameGameState ; } get ironBank ( ) : IronBank | null { return this . game . ironBank ; } firstStart ( house ) { this . house = house ; const availableOrders = this . parentGameState . getAvailableOrdersOfHouse ( house ) . entries ; if ( availableOrders . length == 0 ) { this . onResolveSingleConsolidatePowerFinish ( ) ; return ; } const defenseMusterOrders = availableOrders . filter ( ( [ _r , ot ] ) => ot instanceof DefenseMusterOrderType ) ; const consolidatePowerOrders = availableOrders . filter ( ( [ _r , ot ] ) => ot instanceof ConsolidatePowerOrderType ) ; const ironBankOrders = availableOrders . filter ( ( [ _r , ot ] ) => ot instanceof IronBankOrderType ) ; if ( defenseMusterOrders . length > 1 || ( defenseMusterOrders . length == 1 && ( consolidatePowerOrders . length > 0 || ironBankOrders . length > 0 ) ) ) { throw new Error ( "Too much Defense / Muster orders placed or Defense / Muster together with player CP/IB orders placed!" ) ; } if ( defenseMusterOrders . length > 0 ) { return ; } if ( ironBankOrders . length == 0 && consolidatePowerOrders . every ( ( [ r , ot ] ) => ! ot . starred || ( ot . starred && ! r . hasStructure ) ) ) { const region = consolidatePowerOrders [ 0 ] [ 0 ] ; this . parentGameState . resolveConsolidatePowerOrderForPt ( region , house , true ) ; this . actionGameState . removeOrderFromRegion ( region ) ; this . onResolveSingleConsolidatePowerFinish ( ) ; return ; } if ( ( ironBankOrders . length == 1 && consolidatePowerOrders . length == 0 && this . ironBank && this . ironBank . getPurchasableLoans ( this . house ) . length == 0 ) || ( ironBankOrders . length == 1 && this . ironBank && this . ironBank . loanSlots . every ( lc => lc == null ) ) ) { const region = ironBankOrders [ 0 ] [ 0 ] ; this . actionGameState . removeOrderFromRegion ( region , true , this . house , true ) ; this . onResolveSingleConsolidatePowerFinish ( ) ; } } getPotentialGainedPowerTokens ( region , house ) { const order = this . actionGameState . ordersOnBoard . tryGet ( region , null ) ; if ( order && ! ( order . type instanceof ConsolidatePowerOrderType ) ) { return 0 ; } if ( region . type == sea ) { } else if ( region . type == port ) { const adjacentSea = this . game . world . getAdjacentSeaOfPort ( region ) ; const adjacentSeaController = adjacentSea . getController ( ) ; if ( adjacentSeaController == null || adjacentSeaController == house ) { return 1 ; } } else if ( region . type == land ) { return 1 + region . crownIcons ; } return 0 ; } onPlayerMessage ( player , message ) { if ( message . type == "resolve-consolidate-power-choice" ) { if ( this . ingame . getControllerOfHouse ( this . house ) . user != player . user ) { return ; } const regionOfOrder = this . ingame . world . regions . tryGet ( message . region , null ) ; if ( ! regionOfOrder ) { return ; } const order = this . actionGameState . ordersOnBoard . tryGet ( regionOfOrder , null ) ; if ( ! order || ! ( order . type instanceof ConsolidatePowerOrderType || order . type instanceof IronBankOrderType || order . type instanceof DefenseMusterOrderType ) ) { return ; } if ( order . type instanceof ConsolidatePowerOrderType ) { if ( message . gainPowerTokens ) { this . parentGameState . resolveConsolidatePowerOrderForPt ( regionOfOrder , this . house ) ; this . actionGameState . removeOrderFromRegion ( regionOfOrder ) ; this . onResolveSingleConsolidatePowerFinish ( ) ; return ; } else if ( order . type . starred && message . musterUnits ) { this . parentGameState . setChildGameState ( new PlayerMusteringGameState ( this . parentGameState ) ) . firstStart ( this . house , PlayerMusteringType . STARRED_CONSOLIDATE_POWER ) ; return ; } } else if ( order . type instanceof DefenseMusterOrderType && message . musterUnits ) { this . parentGameState . setChildGameState ( new PlayerMusteringGameState ( this . parentGameState ) ) . firstStart ( this . house , PlayerMusteringType . DEFENSE_MUSTER_ORDER ) ; return ; } else if ( order . type instanceof IronBankOrderType && this . ironBank && message . purchaseLoan !== undefined ) { const loan = this . ironBank . purchaseLoan ( this . house , message . purchaseLoan , regionOfOrder . id ) ; if ( ! loan ) { return ; } this . actionGameState . removeOrderFromRegion ( regionOfOrder ) ; loan . execute ( this , this . house ) ; } else if ( message . ignoreAndRemoveOrder ) { this . ingame . log ( { type : "order-removed" , house : this . house . id , order : order . type . id , region : regionOfOrder . id } ) ; this . actionGameState . removeOrderFromRegion ( regionOfOrder ) ; this . onResolveSingleConsolidatePowerFinish ( ) ; } } } onResolveSingleConsolidatePowerFinish ( ) { this . parentGameState . proceedNextResolve ( this . house ) ; } onServerMessage ( _message ) { } getWaitedUsers ( ) { return [ this . ingame . getControllerOfHouse ( this . house ) . user ] ; } choosePurchaseLoan ( i , region ) { this . entireGame . sendMessageToServer ( { type : "resolve-consolidate-power-choice" , purchaseLoan : i , region : region . id } ) ; } chooseGainPowerTokens ( region ) { this . entireGame . sendMessageToServer ( { type : "resolve-consolidate-power-choice" , gainPowerTokens : true , region : region . id } ) ; } chooseMustering ( region ) { this . entireGame . sendMessageToServer ( { type : "resolve-consolidate-power-choice" , musterUnits : true , region : region . id } ) ; } chooseRemoveOrder ( region ) { this . entireGame . sendMessageToServer ( { type : "resolve-consolidate-power-choice" , region : region . id , ignoreAndRemoveOrder : true } ) ; } serializeToClient ( _admin , _player ) { return { type : "resolve-single-consolidate-power" , house : this . house . id } ; } static deserializeFromServer ( resolveConsolidatePower , data ) { const resolveSingleConsolidatePower = new ResolveSingleConsolidatePowerGameState ( resolveConsolidatePower ) ; resolveSingleConsolidatePower . house = resolveConsolidatePower . game . houses . get ( data . house ) ; return resolveSingleConsolidatePower ; } } export interface SerializedResolveSingleConsolidatePowerGameState { type : "resolve-single-consolidate-power" ; house : string ; }