import GameState from "../../../GameState" ; import WildlingsAttackGameState from "./WildlingsAttackGameState" ; import Game from "../../game-data-structure/Game" ; import House from "../../game-data-structure/House" ; import { findOrphanedShipsAndDestroyThem } from "../../port-helper/PortHelper" ; /**
 * A lot of Wildling Cards have the same logic where the lowest bidder
 * must do something, and then all the other houses, in turn order, do a slightly different thing.
 *
 * This class helps implement these kind of Wildling Cards by extracting the
 * "in turn order, [action]" part of the logic, and let child classes implement
 * the "[action]".
 *
 * In practice, the child classes will implement the two abstract methods.
 * These two methods must make sure that, at some point, `onExecuteFinish` is called
 * so that the execution of the card can proceed.
 */ export default abstract class WildlingCardEffectInTurnOrderGameState < C extends GameState < any , any > > extends GameState < WildlingsAttackGameState , C > { get game ( ) : Game { return this . parentGameState . game ; } firstStart ( ) { this . proceedNextHouse ( null ) ; } onExecuteFinish ( house ) { this . proceedNextHouse ( house ) ; } proceedNextHouse ( previousHouse ) { findOrphanedShipsAndDestroyThem ( this . parentGameState . ingame ) ; if ( previousHouse == null ) { this . executeForLowestBidder ( this . parentGameState . lowestBidder ) ; return ; } const nextHouse = this . getNextHouseInEveryoneElse ( previousHouse ) ; if ( nextHouse == null ) { this . parentGameState . onWildlingCardExecuteEnd ( ) ; return ; } if ( this . parentGameState . ingame . isVassalHouse ( nextHouse ) ) { this . proceedNextHouse ( nextHouse ) ; return ; } this . executeForEveryoneElse ( nextHouse ) ; } getNextHouseInEveryoneElse ( previousHouse ) { const turnOrder = this . parentGameState . game . ironThroneTrack . filter ( h => h != this . parentGameState . lowestBidder ) . filter ( h => this . parentGameState . participatingHouses . includes ( h ) ) ; if ( previousHouse == this . parentGameState . lowestBidder ) { return turnOrder [ 0 ] ; } const i = turnOrder . indexOf ( previousHouse ) ; if ( i == - 1 ) { throw new Error ( ) ; } if ( i == turnOrder . length - 1 ) { return null ; } return turnOrder [ i + 1 ] ; } abstract executeForLowestBidder ( house ) ; abstract executeForEveryoneElse ( house ) ; }