@@ -20,6 +20,7 @@ import { ActionOnHold, C2SActionOnHold, ExpandedClientCard, ProcessedClientCard,
20
20
import { ActionExtractor } from './ActionExtractor' ;
21
21
import { C2SAction , ClientAttackAction , ClientResolvePromptAction , FromClientPassAction , FromClientPlayAction , FromClientPowerAction } from '../../clientProtocol' ;
22
22
import { PROMPT_TYPE_CHOOSE_N_CARDS_FROM_ZONE , PROMPT_TYPE_PAYMENT_SOURCE , ZONE_TYPE_IN_PLAY } from 'moonlands/dist/const' ;
23
+ import { SimulationQueue } from './SimulationQueue' ;
23
24
24
25
const STEP_NAME = {
25
26
ENERGIZE : 0 ,
@@ -201,7 +202,7 @@ export class SimulationStrategy implements Strategy {
201
202
return this . pass ( )
202
203
}
203
204
204
- private simulateAttacksQueue ( simulationQueue : SimulationEntity [ ] , initialScore : number , opponentId : number ) : AnyEffectType {
205
+ private simulateAttacksQueue ( simulationQueue : SimulationQueue , initialScore : number , opponentId : number ) : AnyEffectType {
205
206
const hashes = new Set < string > ( )
206
207
let bestAction : { score : number , action : AnyEffectType [ ] } = {
207
208
score : initialScore ,
@@ -213,10 +214,10 @@ export class SimulationStrategy implements Strategy {
213
214
// Simulation itself
214
215
let failsafe = SimulationStrategy . failsafe
215
216
let counter = 0
216
- while ( simulationQueue . length && failsafe > 0 ) {
217
+ while ( simulationQueue . hasItems ( ) && failsafe > 0 ) {
217
218
failsafe -= 1
218
219
counter += 1
219
- const workEntity = simulationQueue . pop ( )
220
+ const workEntity = simulationQueue . shift ( )
220
221
if ( workEntity ) {
221
222
try {
222
223
workEntity . sim . update ( workEntity . action )
@@ -278,7 +279,7 @@ export class SimulationStrategy implements Strategy {
278
279
return `Unknown action: ${ action . type } `
279
280
}
280
281
281
- private simulateActionsQueue ( simulationQueue : SimulationEntity [ ] , initialScore : number , opponentId : number ) : ActionOnHold [ ] {
282
+ private simulateActionsQueue ( simulationQueue : SimulationQueue , initialScore : number , opponentId : number ) : ActionOnHold [ ] {
282
283
const hashes = new Set < string > ( )
283
284
if ( ! this . playerId ) {
284
285
return [ {
@@ -291,7 +292,7 @@ export class SimulationStrategy implements Strategy {
291
292
292
293
this . leaves . clear ( )
293
294
294
- while ( simulationQueue . length && counter <= SimulationStrategy . failsafe ) {
295
+ while ( simulationQueue . hasItems ( ) && counter <= SimulationStrategy . failsafe ) {
295
296
counter += 1
296
297
const workEntity = simulationQueue . shift ( )
297
298
if ( workEntity && workEntity . action ) {
@@ -425,6 +426,7 @@ export class SimulationStrategy implements Strategy {
425
426
426
427
if ( 'cards' in action && action . cards && action . cards . some ( card => ! promptAvailableCards ?. includes ( card as unknown as string ) ) ) {
427
428
const availableCardPairs : Record < string , string [ ] > = { }
429
+ console . dir ( this . gameState ?. state . promptParams . cards )
428
430
for ( let card of this . gameState ?. state . promptParams . cards ! ) {
429
431
if ( ! ( card . card in availableCardPairs ) ) {
430
432
availableCardPairs [ card . card ] = [ ]
@@ -435,12 +437,12 @@ export class SimulationStrategy implements Strategy {
435
437
const newCards : string [ ] = [ ]
436
438
// @ts -ignore
437
439
for ( const cardName of action . cardNames ) {
438
- if ( ! ( cardName in availableCardPairs ) || availableCardPairs [ cardName ] . length == 0 ) {
439
- console . dir ( action )
440
- console . dir ( cardName )
441
- throw new Error ( `Cannot find ${ cardName } in the prompt zone` )
440
+ if ( ! ( cardName . name in availableCardPairs ) || availableCardPairs [ cardName . name ] . length == 0 ) {
441
+ // console.dir(action)
442
+ // console.dir(cardName)
443
+ throw new Error ( `Cannot find ${ cardName . name } in the prompt zone` )
442
444
}
443
- const newId = availableCardPairs [ cardName ] . pop ( ) !
445
+ const newId = availableCardPairs [ cardName . name ] . pop ( ) !
444
446
newCards . push ( newId )
445
447
}
446
448
@@ -492,8 +494,8 @@ export class SimulationStrategy implements Strategy {
492
494
this . gameState . getPromptType ( ) == PROMPT_TYPE_PAYMENT_SOURCE &&
493
495
! this . gameState . state . promptParams . cards ?. some ( ( { id} ) => id == action . target )
494
496
) {
495
- console . log ( `Target is ${ action . target } ` )
496
- console . dir ( this . gameState . state . promptParams . cards )
497
+ // console.log(`Target is ${action.target}`)
498
+ // console.dir(this.gameState.state.promptParams.cards)
497
499
// const cardIsIn = this.gameState.state.promptParams.cards?.some(card => card.card == action.target)
498
500
return this . fixTargetPromptResolution ( action as ClientResolvePromptAction & { targetName : string } )
499
501
}
@@ -538,8 +540,8 @@ export class SimulationStrategy implements Strategy {
538
540
}
539
541
540
542
if ( this . waitingTarget && this . gameState . waitingForTarget ( this . waitingTarget . source , this . playerId ) ) {
541
- console . log ( `Waiting for target resolve path` )
542
- console . dir ( this . waitingTarget )
543
+ // console.log(`Waiting for target resolve path`)
544
+ // console.dir(this.waitingTarget)
543
545
return this . resolveTargetPrompt ( this . waitingTarget . target , 'waitingTarget' )
544
546
}
545
547
@@ -564,7 +566,7 @@ export class SimulationStrategy implements Strategy {
564
566
) {
565
567
return this . resolveChooseCardsPrompt ( )
566
568
}
567
- console . log ( `Prompt state without previous action: ${ this . gameState . getPromptType ( ) } ` )
569
+ // console.log(`Prompt state without previous action: ${this.gameState.getPromptType()}`)
568
570
}
569
571
const playable = this . gameState . getPlayableCards ( )
570
572
. map ( addCardData )
@@ -596,7 +598,9 @@ export class SimulationStrategy implements Strategy {
596
598
const hash = this . hashBuilder . makeHash ( outerSim )
597
599
const initialScore = getStateScore ( outerSim , this . playerId , TEMPORARY_OPPONENT_ID )
598
600
599
- const simulationQueue : SimulationEntity [ ] = ActionExtractor . extractActions ( outerSim , this . playerId , TEMPORARY_OPPONENT_ID , [ ] , hash , this . hashBuilder )
601
+ const simulationQueue = new SimulationQueue ( ) ;
602
+ simulationQueue . addFromSim ( outerSim , this . playerId , TEMPORARY_OPPONENT_ID , [ ] , hash , this . hashBuilder )
603
+ //const simulationQueue: SimulationEntity[] = ActionExtractor.extractActions(outerSim, this.playerId, TEMPORARY_OPPONENT_ID, [], hash, this.hashBuilder)
600
604
const bestActions = this . simulateActionsQueue ( simulationQueue , initialScore , TEMPORARY_OPPONENT_ID )
601
605
const finalHash = this . hashBuilder . makeHash ( outerSim )
602
606
if ( finalHash !== hash ) {
@@ -673,7 +677,9 @@ export class SimulationStrategy implements Strategy {
673
677
)
674
678
675
679
const hash = this . hashBuilder . makeHash ( outerSim )
676
- const simulationQueue = ActionExtractor . extractActions ( outerSim , this . playerId , TEMPORARY_OPPONENT_ID , [ ] , hash , this . hashBuilder )
680
+ const simulationQueue = new SimulationQueue ( ) ;
681
+ simulationQueue . addFromSim ( outerSim , this . playerId , TEMPORARY_OPPONENT_ID , [ ] , hash , this . hashBuilder )
682
+ // const simulationQueue = ActionExtractor.extractActions(outerSim, this.playerId, TEMPORARY_OPPONENT_ID, [], hash, this.hashBuilder)
677
683
678
684
const initialScore = getStateScore ( outerSim , this . playerId , TEMPORARY_OPPONENT_ID )
679
685
@@ -710,14 +716,14 @@ export class SimulationStrategy implements Strategy {
710
716
}
711
717
const availableCards = this . gameState . state . promptParams . cards || [ ]
712
718
if ( this . gameState . getPromptType ( ) == PROMPT_TYPE_CHOOSE_UP_TO_N_CARDS_FROM_ZONE ) {
713
- console . log ( 'Resolving choose up to N cards prompt' ) ;
714
- console . dir ( this . resolveCardsPrompt (
715
- availableCards . slice ( 0 , this . gameState . state . promptParams . numberOfCards || 0 )
716
- . map ( card => card as unknown as CardInGame ) ,
717
- this . gameState . state . promptType || '' ,
718
- this . gameState . state . promptParams . zone || ZONE_TYPE_IN_PLAY ,
719
- this . gameState . state . promptParams . zoneOwner || 0 ,
720
- ) )
719
+ // console.log('Resolving choose up to N cards prompt');
720
+ // console.dir(this.resolveCardsPrompt(
721
+ // availableCards.slice(0, this.gameState.state.promptParams.numberOfCards || 0)
722
+ // .map(card => card as unknown as CardInGame),
723
+ // this.gameState.state.promptType || '',
724
+ // this.gameState.state.promptParams.zone || ZONE_TYPE_IN_PLAY,
725
+ // this.gameState.state.promptParams.zoneOwner || 0,
726
+ // ))
721
727
}
722
728
// The conversion to CardInGame is OK because resolveCardPrompt only cares about card ids
723
729
return this . resolveCardsPrompt (
0 commit comments