Skip to content

Commit 7a80f66

Browse files
committed
make changeActionPlayers an opt-in event
1 parent 5946a87 commit 7a80f66

4 files changed

Lines changed: 35 additions & 18 deletions

File tree

examples/react/modules/turnorder/game.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ const TurnExample = Game({
4242
return nextG;
4343
},
4444
},
45+
4546
flow: {
47+
changeActionPlayers: true,
48+
4649
onTurnBegin: (G, ctx) => {
4750
const currentPlayer = ctx.currentPlayer;
4851
const playersNext = [...G.players];

src/client/client.test.js

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,7 @@ test('event dispatchers', () => {
7373
const reducer = createGameReducer({ game, numPlayers: 2 });
7474
const store = createStore(reducer);
7575
const api = createEventDispatchers(game.flow.eventNames, store);
76-
expect(Object.getOwnPropertyNames(api)).toEqual([
77-
'endTurn',
78-
'changeActionPlayers',
79-
]);
76+
expect(Object.getOwnPropertyNames(api)).toEqual(['endTurn']);
8077
expect(store.getState().ctx.turn).toBe(0);
8178
api.endTurn();
8279
expect(store.getState().ctx.turn).toBe(1);
@@ -87,6 +84,7 @@ test('event dispatchers', () => {
8784
flow: {
8885
endPhase: true,
8986
endGame: true,
87+
changeActionPlayers: true,
9088
},
9189
});
9290
const reducer = createGameReducer({ game, numPlayers: 2 });
@@ -115,7 +113,7 @@ test('event dispatchers', () => {
115113
const reducer = createGameReducer({ game, numPlayers: 2 });
116114
const store = createStore(reducer);
117115
const api = createEventDispatchers(game.flow.eventNames, store);
118-
expect(Object.getOwnPropertyNames(api)).toEqual(['changeActionPlayers']);
116+
expect(Object.getOwnPropertyNames(api)).toEqual([]);
119117
}
120118

121119
{
@@ -128,11 +126,7 @@ test('event dispatchers', () => {
128126
const reducer = createGameReducer({ game, numPlayers: 2 });
129127
const store = createStore(reducer);
130128
const api = createEventDispatchers(game.flow.eventNames, store);
131-
expect(Object.getOwnPropertyNames(api)).toEqual([
132-
'endTurn',
133-
'endPhase',
134-
'changeActionPlayers',
135-
]);
129+
expect(Object.getOwnPropertyNames(api)).toEqual(['endTurn', 'endPhase']);
136130
expect(store.getState().ctx.turn).toBe(0);
137131
api.endTurn();
138132
expect(store.getState().ctx.turn).toBe(1);

src/core/flow.js

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ export function Flow({
158158
*
159159
* @param {...object} endGame - Set to true to enable the `endGame` event.
160160
*
161+
* @param {...object} changeActionPlayers - Set to true to enable the `changeActionPlayers` event.
162+
*
161163
* @param {...object} allowedMoves - List of moves that are allowed.
162164
* This can be either a list of
163165
* move names or a function with the
@@ -241,6 +243,7 @@ export function FlowWithPhases({
241243
endTurn,
242244
endPhase,
243245
endGame,
246+
changeActionPlayers,
244247
undoableMoves,
245248
allowedMoves,
246249
optimisticUpdate,
@@ -255,6 +258,9 @@ export function FlowWithPhases({
255258
if (endGame === undefined) {
256259
endGame = false;
257260
}
261+
if (changeActionPlayers === undefined) {
262+
changeActionPlayers = false;
263+
}
258264
if (optimisticUpdate === undefined) {
259265
optimisticUpdate = () => true;
260266
}
@@ -514,6 +520,13 @@ export function FlowWithPhases({
514520
return { ...state, ctx: { ...state.ctx, gameover: arg } };
515521
}
516522

523+
function changeActionPlayersEvent(state, actionPlayers) {
524+
if (actionPlayers && actionPlayers.length) {
525+
return { ...state, ctx: { ...state.ctx, actionPlayers } };
526+
}
527+
return state;
528+
}
529+
517530
function processMove(state, action, dispatch) {
518531
const conf = phaseMap[state.ctx.phase];
519532

@@ -592,13 +605,18 @@ export function FlowWithPhases({
592605
};
593606

594607
let enabledEvents = {};
595-
if (endTurn) enabledEvents['endTurn'] = endTurnEvent;
596-
if (endPhase) enabledEvents['endPhase'] = endPhaseEvent;
597-
if (endGame) enabledEvents['endGame'] = endGameEvent;
598-
599-
enabledEvents['changeActionPlayers'] = (state, actionPlayers) => {
600-
return { ...state, ctx: { ...state.ctx, actionPlayers } };
601-
};
608+
if (endTurn) {
609+
enabledEvents['endTurn'] = endTurnEvent;
610+
}
611+
if (endPhase) {
612+
enabledEvents['endPhase'] = endPhaseEvent;
613+
}
614+
if (endGame) {
615+
enabledEvents['endGame'] = endGameEvent;
616+
}
617+
if (changeActionPlayers) {
618+
enabledEvents['changeActionPlayers'] = changeActionPlayersEvent;
619+
}
602620

603621
return Flow({
604622
ctx: numPlayers => ({

src/core/flow.test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ test('resetGame', () => {
599599
});
600600

601601
test('change action players', () => {
602-
const flow = FlowWithPhases({});
602+
const flow = FlowWithPhases({ changeActionPlayers: true });
603603
const state = { ctx: {} };
604604
const newState = flow.processGameEvent(state, {
605605
type: 'changeActionPlayers',
@@ -610,6 +610,8 @@ test('change action players', () => {
610610

611611
test('change action players - reducer', () => {
612612
let game = Game({
613+
flow: { changeActionPlayers: true },
614+
613615
moves: {
614616
playMilitia: (G, ctx) => {
615617
// change which players need to act

0 commit comments

Comments
 (0)