Skip to content

Commit 5d3a34d

Browse files
committed
{ once: true } argument to setActionPlayers
1 parent 75a274c commit 5d3a34d

3 files changed

Lines changed: 55 additions & 5 deletions

File tree

src/core/flow.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,9 +517,22 @@ export function FlowWithPhases({
517517
}
518518
const allPlayed = _played.length == state.ctx.numPlayers;
519519

520+
// Update actionPlayers if _actionPlayersOnce is set.
521+
let actionPlayers = state.ctx.actionPlayers;
522+
if (state.ctx._actionPlayersOnce == true) {
523+
const playerID = action.payload.playerID;
524+
actionPlayers = actionPlayers.filter(id => id !== playerID);
525+
}
526+
520527
state = {
521528
...state,
522-
ctx: { ...state.ctx, currentPlayerMoves, _played, allPlayed },
529+
ctx: {
530+
...state.ctx,
531+
actionPlayers,
532+
currentPlayerMoves,
533+
_played,
534+
allPlayed,
535+
},
523536
};
524537

525538
const G = conf.onMove(state.G, state.ctx, action);

src/core/turn-order.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,25 @@ export const Pass = (G, ctx) => {
3333
* @param {object} actionPlayers - An array of playerID's or
3434
* TurnOrder.ALL.
3535
*/
36-
export function SetActionPlayers(state, actionPlayers) {
36+
export function SetActionPlayers(state, actionPlayers, opts) {
37+
let _actionPlayersOnce = false;
38+
if (opts && opts.once) {
39+
_actionPlayersOnce = true;
40+
}
41+
3742
if (actionPlayers == TurnOrder.ALL) {
3843
actionPlayers = [...state.ctx.playOrder];
39-
return { ...state, ctx: { ...state.ctx, actionPlayers } };
44+
return {
45+
...state,
46+
ctx: { ...state.ctx, actionPlayers, _actionPlayersOnce },
47+
};
4048
}
4149

4250
if (actionPlayers && actionPlayers.length) {
43-
return { ...state, ctx: { ...state.ctx, actionPlayers } };
51+
return {
52+
...state,
53+
ctx: { ...state.ctx, actionPlayers, _actionPlayersOnce },
54+
};
4455
}
4556

4657
return state;

src/core/turn-order.test.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ test('playOrder', () => {
189189
expect(state.ctx.currentPlayer).toBe('2');
190190
});
191191

192-
describe('change action players', () => {
192+
describe('SetActionPlayers', () => {
193193
const flow = FlowWithPhases({ setActionPlayers: true });
194194
const state = { ctx: flow.ctx(2) };
195195

@@ -209,6 +209,32 @@ describe('change action players', () => {
209209
expect(newState.ctx.actionPlayers).toMatchObject(['0', '1']);
210210
});
211211

212+
test('once', () => {
213+
const game = Game({
214+
flow: {
215+
setActionPlayers: true,
216+
},
217+
218+
moves: {
219+
B: (G, ctx) => {
220+
ctx.events.setActionPlayers(['0', '1'], { once: true });
221+
return G;
222+
},
223+
A: G => G,
224+
},
225+
});
226+
227+
const reducer = CreateGameReducer({ game, numPlayers: 2 });
228+
229+
let state = reducer(undefined, { type: 'init' });
230+
state = reducer(state, makeMove('B', null, '0'));
231+
expect(state.ctx.actionPlayers).toEqual(['0', '1']);
232+
state = reducer(state, makeMove('A', null, '0'));
233+
expect(state.ctx.actionPlayers).toEqual(['1']);
234+
state = reducer(state, makeMove('A', null, '1'));
235+
expect(state.ctx.actionPlayers).toEqual([]);
236+
});
237+
212238
test('militia', () => {
213239
const game = Game({
214240
flow: { setActionPlayers: true },

0 commit comments

Comments
 (0)