Skip to content

Commit

Permalink
endGame event
Browse files Browse the repository at this point in the history
  • Loading branch information
darthfiddler committed Mar 18, 2018
1 parent 0f7593d commit 767362f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/client/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ test('event dispatchers', () => {
const game = Game({
flow: {
endPhase: true,
endGame: true,
},
});
const reducer = createGameReducer({ game, numPlayers: 2 });
Expand All @@ -97,6 +98,7 @@ test('event dispatchers', () => {
'redo',
'endTurn',
'endPhase',
'endGame',
]);
expect(store.getState().ctx.turn).toBe(0);
api.endTurn();
Expand Down
15 changes: 15 additions & 0 deletions src/core/flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ export function Flow({
*
* @param {...object} endPhase - Set to false to disable the `endPhase` event.
*
* @param {...object} endGame - Set to true to enable the `endGame` event.
*
* @param {...object} undoableMoves - List of moves that are undoable,
* (default: undefined, i.e. all moves are undoable).
*
Expand Down Expand Up @@ -230,6 +232,7 @@ export function FlowWithPhases({
turnOrder,
endTurn,
endPhase,
endGame,
undoableMoves,
optimisticUpdate,
canMakeMove,
Expand All @@ -241,6 +244,9 @@ export function FlowWithPhases({
if (endTurn === undefined) {
endTurn = true;
}
if (endGame === undefined) {
endGame = false;
}
if (optimisticUpdate === undefined) {
optimisticUpdate = () => true;
}
Expand Down Expand Up @@ -474,6 +480,14 @@ export function FlowWithPhases({
};
}

function endGameEvent(state, arg) {
if (arg === undefined) {
arg = true;
}

return { ...state, ctx: { ...state.ctx, gameover: arg } };
}

function processMove(state, action, dispatch) {
const conf = phaseMap[state.ctx.phase];

Expand Down Expand Up @@ -547,6 +561,7 @@ export function FlowWithPhases({
}
if (endTurn) enabledEvents['endTurn'] = endTurnEvent;
if (endPhase) enabledEvents['endPhase'] = endPhaseEvent;
if (endGame) enabledEvents['endGame'] = endGameEvent;

return Flow({
ctx: numPlayers => ({
Expand Down
15 changes: 15 additions & 0 deletions src/core/flow.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -602,3 +602,18 @@ test('canMakeMove', () => {
expect(flow.canMakeMove({}, { currentPlayer: 0 }, pid)).toBe(false);
expect(flow.canMakeMove({}, {}, 'any')).toBe(false);
});

test('endGame', () => {
const flow = FlowWithPhases({ endGame: true });
const state = { ctx: {} };

{
const t = flow.processGameEvent(state, gameEvent('endGame').payload);
expect(t.ctx.gameover).toBe(true);
}

{
const t = flow.processGameEvent(state, gameEvent('endGame', 42).payload);
expect(t.ctx.gameover).toBe(42);
}
});

1 comment on commit 767362f

@nicolodavis
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.