@@ -335,7 +335,12 @@ export function FlowWithPhases({
335335 }
336336 }
337337
338- const endTurnIfWrap = ( G , ctx ) => {
338+ const shouldEndPhase = ( { G, ctx } ) => {
339+ const conf = phaseMap [ ctx . phase ] ;
340+ return conf . endPhaseIf ( G , ctx ) ;
341+ } ;
342+
343+ const shouldEndTurn = ( { G, ctx } ) => {
339344 const conf = phaseMap [ ctx . phase ] ;
340345 if ( conf . movesPerTurn && ctx . currentPlayerMoves >= conf . movesPerTurn ) {
341346 return true ;
@@ -398,7 +403,7 @@ export function FlowWithPhases({
398403 let ctx = state . ctx ;
399404
400405 // Run any cleanup code for the phase that is about to end.
401- let conf = phaseMap [ ctx . phase ] ;
406+ const conf = phaseMap [ ctx . phase ] ;
402407 G = conf . onPhaseEnd ( G , ctx ) ;
403408
404409 const gameover = conf . endGameIf ( G , ctx ) ;
@@ -424,8 +429,7 @@ export function FlowWithPhases({
424429 // a finite number of times.
425430 if ( ! cascadeDepth ) cascadeDepth = 0 ;
426431 if ( cascadeDepth < phases . length - 1 ) {
427- conf = phaseMap [ state . ctx . phase ] ;
428- const end = conf . endPhaseIf ( state . G , state . ctx ) ;
432+ const end = shouldEndPhase ( state ) ;
429433 if ( end ) {
430434 state = endPhaseEvent ( state , end , cascadeDepth + 1 ) ;
431435 }
@@ -476,7 +480,7 @@ export function FlowWithPhases({
476480 } ;
477481
478482 // End phase if condition is met.
479- const end = conf . endPhaseIf ( G , ctx ) ;
483+ const end = shouldEndPhase ( state ) ;
480484 if ( end ) {
481485 return endPhaseEvent ( { ...state , G, ctx } , end ) ;
482486 }
@@ -507,17 +511,17 @@ export function FlowWithPhases({
507511 const gameover = conf . endGameIf ( state . G , state . ctx ) ;
508512
509513 // End the turn automatically if endTurnIf is true or if endGameIf returns.
510- const endTurn = endTurnIfWrap ( state . G , state . ctx ) ;
514+ const endTurn = shouldEndTurn ( state ) ;
511515 if ( endTurn || gameover !== undefined ) {
512516 state = dispatch ( state , { type : 'endTurn' , playerID : action . playerID } ) ;
513517 }
514518
515519 // End the phase automatically if endPhaseIf is true.
516- const end = conf . endPhaseIf ( state . G , state . ctx ) ;
517- if ( end || gameover !== undefined ) {
520+ const endPhase = shouldEndPhase ( state ) ;
521+ if ( endPhase || gameover !== undefined ) {
518522 state = dispatch ( state , {
519523 type : 'endPhase' ,
520- args : [ end ] ,
524+ args : [ endPhase ] ,
521525 playerID : action . playerID ,
522526 } ) ;
523527 }
0 commit comments