Skip to content

Commit

Permalink
add { all: true } option to SetActionPlayers
Browse files Browse the repository at this point in the history
  • Loading branch information
darthfiddler committed Jul 31, 2018
1 parent acb9d8c commit b4e3e09
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 40 deletions.
74 changes: 36 additions & 38 deletions src/core/turn-order.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,31 +30,37 @@ export const Pass = (G, ctx) => {
/**
* Event to change the actionPlayers array.
* @param {object} state - The game state.
* @param {object} actionPlayers - An array of playerID's or
* TurnOrder.ALL.
* @param {object} arg - An array of playerID's or <object> of:
* {
* value: [], // array of playerID's (optional if all is set).
* all: true, // set value to all playerID's
* once: true, // players have one move.
* }
*/
export function SetActionPlayers(state, actionPlayers, opts) {
export function SetActionPlayers(state, arg) {
let _actionPlayersOnce = false;
if (opts && opts.once) {
let actionPlayers = [];

if (arg.once) {
_actionPlayersOnce = true;
}

if (actionPlayers == TurnOrder.ALL) {
if (arg.value) {
actionPlayers = arg.value;
}

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

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

return state;
return {
...state,
ctx: { ...state.ctx, actionPlayers, _actionPlayersOnce },
};
}

/**
Expand Down Expand Up @@ -145,30 +151,22 @@ export function UpdateTurnOrderState(G, ctx, turnOrder, nextPlayer) {
return { endPhase, ctx };
}

/**
* Set of different turn orders possible in a phase.
* These are meant to be passed to the `turnOrder` setting
* in the flow objects.
*
* Each object defines the first player when the phase / game
* begins, and also a function `next` to determine who the
* next player is when the turn ends.
*
* first / next can also return an object of type
* { playOrderPos, actionPlayers }
* in which case they can also set actionPlayers simultaneously.
*
* The phase ends if next() returns undefined.
*/
export const TurnOrder = {
/**
* Constant that can be used as an argument to
* setActionPlayers to make it set actionPlayers
* to all the players in the game.
*/
ALL: 'all',

/**
* Set of different turn orders possible in a phase.
* These are meant to be passed to the `turnOrder` setting
* in the flow objects.
*
* Each object defines the first player when the phase / game
* begins, and also a function `next` to determine who the
* next player is when the turn ends.
*
* first / next can also return an object of type
* { playOrderPos, actionPlayers }
* in which case they can also set actionPlayers simultaneously.
*
* The phase ends if next() returns undefined.
*/

/**
* DEFAULT
*
Expand Down
4 changes: 2 additions & 2 deletions src/core/turn-order.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ describe('SetActionPlayers', () => {
test('all', () => {
const newState = flow.processGameEvent(
state,
gameEvent('setActionPlayers', [TurnOrder.ALL])
gameEvent('setActionPlayers', [{ all: true }])
);
expect(newState.ctx.actionPlayers).toMatchObject(['0', '1']);
});
Expand All @@ -217,7 +217,7 @@ describe('SetActionPlayers', () => {

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

0 comments on commit b4e3e09

Please sign in to comment.