Skip to content

Commit

Permalink
call endTurnIf inside endPhase
Browse files Browse the repository at this point in the history
  • Loading branch information
darthfiddler committed Mar 27, 2018
1 parent 9b0324c commit 2105f46
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/core/flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ export function Flow({
* not automatically end after a certain number of moves).
*
* @param {...object} endTurnIf - The turn automatically ends if this
* returns anything other than undefined
* returns a truthy value
* (checked after each move).
* If the return value is a playerID,
* that player is the next player
Expand Down Expand Up @@ -213,8 +213,8 @@ export function Flow({
* // Any cleanup code to run after the phase ends.
* onPhaseEnd: (G, ctx) => G,
*
* // The phase ends if this function returns anything other than
* // undefined. If the return value is the name of another phase,
* // The phase ends if this function returns a truthy value.
* // If the return value is the name of another phase,
* // that will be chosen as the next phase (as opposed
* // to the next one in round-robin order).
* endPhaseIf: (G, ctx) => boolean|string,
Expand Down Expand Up @@ -438,6 +438,12 @@ export function FlowWithPhases({
}
}

// End turn if endTurnIf returns something.
const endTurn = shouldEndTurn(state);
if (endTurn) {
state = endTurnEvent(state, endTurn);
}

return state;
}

Expand Down
29 changes: 29 additions & 0 deletions src/core/flow.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,13 @@ test('endTurn / endPhase args', () => {
expect(t.ctx.playOrderPos).toBe(undefined);
expect(t.ctx.currentPlayer).toBe('any');
}

{
let t = state;
t = flow.processGameEvent(t, gameEvent('endTurn', '0').payload);
expect(t.ctx.playOrderPos).toBe(0);
expect(t.ctx.currentPlayer).toBe('0');
}
});

test('resetGame', () => {
Expand Down Expand Up @@ -763,3 +770,25 @@ test('endPhaseOnMove', () => {
expect(endPhaseACount).toEqual(1);
expect(endPhaseBCount).toEqual(0);
});

test('end turn when final phase is reached', () => {
let flow = FlowWithPhases({
endTurnIf: (G, ctx) => ctx.phase === 'C',
phases: [{ name: 'A' }, { name: 'B' }, { name: 'C' }],
});

let state = { G: {}, ctx: flow.ctx(2) };

expect(state.ctx.phase).toBe('A');
expect(state.ctx.currentPlayer).toBe('0');

state = flow.processGameEvent(state, gameEvent('endPhase').payload);

expect(state.ctx.phase).toBe('B');
expect(state.ctx.currentPlayer).toBe('0');

state = flow.processGameEvent(state, gameEvent('endPhase').payload);

expect(state.ctx.phase).toBe('C');
expect(state.ctx.currentPlayer).toBe('1');
});

0 comments on commit 2105f46

Please sign in to comment.