Skip to content

Commit

Permalink
Automatically ending turn when game ends, fixing #80 (#88)
Browse files Browse the repository at this point in the history
* Automatically ending turn when game ends, fixing #80

* remove stale comment
  • Loading branch information
vdfdev authored and nicolodavis committed Jan 27, 2018
1 parent 33a61cf commit 2ab3dfc
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 11 deletions.
25 changes: 14 additions & 11 deletions src/core/flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,17 +161,18 @@ export function SimpleFlow({
}
}

// End the game automatically if endGameIf is true.
const gameover = endGameIf(state.G, state.ctx);
if (gameover !== undefined) {
return { ...state, ctx: { ...state.ctx, gameover } };
}

// End the turn automatically if endTurnIf is true.
if (endTurnIfWrap(state.G, state.ctx)) {
// End the turn automatically if endTurnIf is true or if endGameIf returns.
if (endTurnIfWrap(state.G, state.ctx) || gameover !== undefined) {
state = dispatch(state, { type: 'endTurn', playerID: action.playerID });
}

// End the game automatically if endGameIf returns.
if (gameover !== undefined) {
return { ...state, ctx: { ...state.ctx, gameover } };
}

return state;
}

Expand Down Expand Up @@ -465,8 +466,14 @@ export function FlowWithPhases({
}
}

// End the game automatically if endGameIf is true.
const gameover = endGameIfWrap(state.G, state.ctx);

// End the turn automatically if endTurnIf is true or if endGameIf returns.
if (endTurnIfWrap(state.G, state.ctx) || gameover !== undefined) {
state = dispatch(state, { type: 'endTurn', playerID: action.playerID });
}

// End the game automatically if endGameIf returns.
if (gameover !== undefined) {
return { ...state, ctx: { ...state.ctx, gameover } };
}
Expand All @@ -482,10 +489,6 @@ export function FlowWithPhases({
});
}

// End the turn automatically if endTurnIf is true.
if (endTurnIfWrap(state.G, state.ctx)) {
state = dispatch(state, { type: 'endTurn', playerID: action.playerID });
}

return state;
}
Expand Down
44 changes: 44 additions & 0 deletions src/core/flow.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,50 @@ test('endGameIf', () => {
state = flow.processGameEvent(state, { type: 'endTurn' });
expect(state.ctx.gameover).toBe('A');
}

// Test that the turn automatically ended on game over for simple flow.
{
const flow = SimpleFlow({ endGameIf: G => G.win });
const game = Game({
moves: {
A: () => ({ win: 'A' }),
B: G => G,
},
flow,
});
const reducer = createGameReducer({ game, numPlayers: 2 });

let state = reducer(undefined, { type: 'init' });
expect(state.ctx.currentPlayer).toBe('0');
state = reducer(state, makeMove('B'));
expect(state.ctx.gameover).toBe(undefined);
expect(state.ctx.currentPlayer).toBe('0');
state = reducer(state, makeMove('A'));
expect(state.ctx.gameover).toBe('A');
expect(state.log[state.log.length - 1].type).toBe('endTurn');
}

// Test that the turn automatically ended on game over for flow with phases.
{
const flow = FlowWithPhases({ endGameIf: G => G.win });
const game = Game({
moves: {
A: () => ({ win: 'A' }),
B: G => G,
},
flow,
});
const reducer = createGameReducer({ game, numPlayers: 2 });

let state = reducer(undefined, { type: 'init' });
expect(state.ctx.currentPlayer).toBe('0');
state = reducer(state, makeMove('B'));
expect(state.ctx.gameover).toBe(undefined);
expect(state.ctx.currentPlayer).toBe('0');
state = reducer(state, makeMove('A'));
expect(state.ctx.gameover).toBe('A');
expect(state.log[state.log.length - 1].type).toBe('endTurn');
}
});

test('endTurnIf', () => {
Expand Down

0 comments on commit 2ab3dfc

Please sign in to comment.