Skip to content

Commit 278b369

Browse files
janosimasnicolodavis
authored andcommitted
Fix bug that was ending phase incorrectly (#176)
* test endPhaseIf count on processMove * fix bug that was ending phase incorrectly
1 parent bfafa94 commit 278b369

2 files changed

Lines changed: 45 additions & 9 deletions

File tree

src/core/flow.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

src/core/flow.test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -717,3 +717,35 @@ test('undo / redo restricted by undoableMoves', () => {
717717
state = reducer(state, undo());
718718
expect(state.G).toEqual({});
719719
});
720+
721+
test('endPhaseOnMove', () => {
722+
let endPhaseACount = 0;
723+
let endPhaseBCount = 0;
724+
const onMove = () => ({ A: true });
725+
726+
let flow = FlowWithPhases({
727+
onMove,
728+
phases: [
729+
{
730+
name: 'A',
731+
endTurnIf: () => true,
732+
endPhaseIf: () => true,
733+
onPhaseEnd: () => ++endPhaseACount,
734+
},
735+
{
736+
name: 'B',
737+
endTurnIf: () => false,
738+
endPhaseIf: () => false,
739+
onPhaseEnd: () => ++endPhaseBCount,
740+
},
741+
],
742+
});
743+
let state = { G: {}, ctx: flow.ctx(2) };
744+
745+
expect(state.ctx.phase).toBe('A');
746+
state = flow.processMove(state, { payload: {} });
747+
expect(state.ctx.phase).toBe('B');
748+
749+
expect(endPhaseACount).toEqual(1);
750+
expect(endPhaseBCount).toEqual(0);
751+
});

0 commit comments

Comments
 (0)