Skip to content

Commit 767362f

Browse files
committed
endGame event
1 parent 0f7593d commit 767362f

3 files changed

Lines changed: 32 additions & 0 deletions

File tree

src/client/client.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ test('event dispatchers', () => {
8787
const game = Game({
8888
flow: {
8989
endPhase: true,
90+
endGame: true,
9091
},
9192
});
9293
const reducer = createGameReducer({ game, numPlayers: 2 });
@@ -97,6 +98,7 @@ test('event dispatchers', () => {
9798
'redo',
9899
'endTurn',
99100
'endPhase',
101+
'endGame',
100102
]);
101103
expect(store.getState().ctx.turn).toBe(0);
102104
api.endTurn();

src/core/flow.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ export function Flow({
163163
*
164164
* @param {...object} endPhase - Set to false to disable the `endPhase` event.
165165
*
166+
* @param {...object} endGame - Set to true to enable the `endGame` event.
167+
*
166168
* @param {...object} undoableMoves - List of moves that are undoable,
167169
* (default: undefined, i.e. all moves are undoable).
168170
*
@@ -230,6 +232,7 @@ export function FlowWithPhases({
230232
turnOrder,
231233
endTurn,
232234
endPhase,
235+
endGame,
233236
undoableMoves,
234237
optimisticUpdate,
235238
canMakeMove,
@@ -241,6 +244,9 @@ export function FlowWithPhases({
241244
if (endTurn === undefined) {
242245
endTurn = true;
243246
}
247+
if (endGame === undefined) {
248+
endGame = false;
249+
}
244250
if (optimisticUpdate === undefined) {
245251
optimisticUpdate = () => true;
246252
}
@@ -474,6 +480,14 @@ export function FlowWithPhases({
474480
};
475481
}
476482

483+
function endGameEvent(state, arg) {
484+
if (arg === undefined) {
485+
arg = true;
486+
}
487+
488+
return { ...state, ctx: { ...state.ctx, gameover: arg } };
489+
}
490+
477491
function processMove(state, action, dispatch) {
478492
const conf = phaseMap[state.ctx.phase];
479493

@@ -547,6 +561,7 @@ export function FlowWithPhases({
547561
}
548562
if (endTurn) enabledEvents['endTurn'] = endTurnEvent;
549563
if (endPhase) enabledEvents['endPhase'] = endPhaseEvent;
564+
if (endGame) enabledEvents['endGame'] = endGameEvent;
550565

551566
return Flow({
552567
ctx: numPlayers => ({

src/core/flow.test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,3 +602,18 @@ test('canMakeMove', () => {
602602
expect(flow.canMakeMove({}, { currentPlayer: 0 }, pid)).toBe(false);
603603
expect(flow.canMakeMove({}, {}, 'any')).toBe(false);
604604
});
605+
606+
test('endGame', () => {
607+
const flow = FlowWithPhases({ endGame: true });
608+
const state = { ctx: {} };
609+
610+
{
611+
const t = flow.processGameEvent(state, gameEvent('endGame').payload);
612+
expect(t.ctx.gameover).toBe(true);
613+
}
614+
615+
{
616+
const t = flow.processGameEvent(state, gameEvent('endGame', 42).payload);
617+
expect(t.ctx.gameover).toBe(42);
618+
}
619+
});

0 commit comments

Comments
 (0)