Skip to content

Commit

Permalink
{ once: true } argument to setActionPlayers
Browse files Browse the repository at this point in the history
  • Loading branch information
darthfiddler committed Jul 30, 2018
1 parent 75a274c commit 5d3a34d
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 5 deletions.
15 changes: 14 additions & 1 deletion src/core/flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -517,9 +517,22 @@ export function FlowWithPhases({
}
const allPlayed = _played.length == state.ctx.numPlayers;

// Update actionPlayers if _actionPlayersOnce is set.
let actionPlayers = state.ctx.actionPlayers;
if (state.ctx._actionPlayersOnce == true) {
const playerID = action.payload.playerID;
actionPlayers = actionPlayers.filter(id => id !== playerID);
}

state = {
...state,
ctx: { ...state.ctx, currentPlayerMoves, _played, allPlayed },
ctx: {
...state.ctx,
actionPlayers,
currentPlayerMoves,
_played,
allPlayed,
},
};

const G = conf.onMove(state.G, state.ctx, action);
Expand Down
17 changes: 14 additions & 3 deletions src/core/turn-order.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,25 @@ export const Pass = (G, ctx) => {
* @param {object} actionPlayers - An array of playerID's or
* TurnOrder.ALL.
*/
export function SetActionPlayers(state, actionPlayers) {
export function SetActionPlayers(state, actionPlayers, opts) {
let _actionPlayersOnce = false;
if (opts && opts.once) {
_actionPlayersOnce = true;
}

if (actionPlayers == TurnOrder.ALL) {
actionPlayers = [...state.ctx.playOrder];
return { ...state, ctx: { ...state.ctx, actionPlayers } };
return {
...state,
ctx: { ...state.ctx, actionPlayers, _actionPlayersOnce },
};
}

if (actionPlayers && actionPlayers.length) {
return { ...state, ctx: { ...state.ctx, actionPlayers } };
return {
...state,
ctx: { ...state.ctx, actionPlayers, _actionPlayersOnce },
};
}

return state;
Expand Down
28 changes: 27 additions & 1 deletion src/core/turn-order.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ test('playOrder', () => {
expect(state.ctx.currentPlayer).toBe('2');
});

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

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

test('once', () => {
const game = Game({
flow: {
setActionPlayers: true,
},

moves: {
B: (G, ctx) => {
ctx.events.setActionPlayers(['0', '1'], { once: true });
return G;
},
A: G => G,
},
});

const reducer = CreateGameReducer({ game, numPlayers: 2 });

let state = reducer(undefined, { type: 'init' });
state = reducer(state, makeMove('B', null, '0'));
expect(state.ctx.actionPlayers).toEqual(['0', '1']);
state = reducer(state, makeMove('A', null, '0'));
expect(state.ctx.actionPlayers).toEqual(['1']);
state = reducer(state, makeMove('A', null, '1'));
expect(state.ctx.actionPlayers).toEqual([]);
});

test('militia', () => {
const game = Game({
flow: { setActionPlayers: true },
Expand Down

0 comments on commit 5d3a34d

Please sign in to comment.