Skip to content

Commit

Permalink
make turnOrder a globally configurable option
Browse files Browse the repository at this point in the history
  • Loading branch information
darthfiddler committed Jan 28, 2018
1 parent 1abc042 commit cb09d9a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 10 deletions.
17 changes: 7 additions & 10 deletions src/core/flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ export function Flow({ ctx, events, init, validator, processMove }) {
* @param {...object} onTurnEnd - Any code to run when a turn ends.
* (G, ctx) => G
*
* @param {...object} turnOrder - Customize the turn order (see turn-order.js).
*
* @param {...object} triggers - An array of objects with the format:
* {
* condition: (G, ctx) => boolean,
Expand Down Expand Up @@ -149,15 +151,8 @@ export function Flow({ ctx, events, init, validator, processMove }) {
* // A phase-specific movesPerTurn.
* movesPerTurn: integer,
*
* // Called when `endTurn` is processed, and returns the next player.
* // If not specified, TurnOrder.DEFAULT is used.
* turnOrder: {
* // The first player.
* first: (G, ctx) => playerID,
* // Called whenever `endTurn` is processed to determine
* // the next player.
* next: (G, ctx) => playerID,
* },
* // A phase-specific turnOrder.
* turnOrder: TurnOrder.DEFAULT,
*
* // List of moves that are allowed in this phase.
* allowedMoves: ['moveA', ...],
Expand All @@ -169,6 +164,7 @@ export function FlowWithPhases({
endTurnIf,
endGameIf,
onTurnEnd,
turnOrder,
triggers,
events,
}) {
Expand All @@ -184,6 +180,7 @@ export function FlowWithPhases({
if (!endTurnIf) endTurnIf = () => false;
if (!endGameIf) endGameIf = () => undefined;
if (!onTurnEnd) onTurnEnd = G => G;
if (!turnOrder) turnOrder = TurnOrder.DEFAULT;
if (!triggers) triggers = [];

let phaseKeys = [];
Expand All @@ -194,7 +191,7 @@ export function FlowWithPhases({
phaseMap[conf.name] = conf;

if (!conf.turnOrder) {
conf.turnOrder = TurnOrder.DEFAULT;
conf.turnOrder = turnOrder;
}
if (!conf.endPhaseIf) {
conf.endPhaseIf = () => false;
Expand Down
34 changes: 34 additions & 0 deletions src/core/turn-order.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,37 @@ test('turnOrder', () => {
expect(state.ctx.allPassed).toBe(true);
expect(state.ctx.currentPlayer).toBe(undefined);
});

test('override', () => {
const even = {
first: () => '0',
next: (G, ctx) => (+ctx.currentPlayer + 2) % ctx.numPlayers + '',
};

const odd = {
first: () => '1',
next: (G, ctx) => (+ctx.currentPlayer + 2) % ctx.numPlayers + '',
};

let flow = FlowWithPhases({
turnOrder: even,
phases: [{ name: 'A' }, { name: 'B', turnOrder: odd }],
});

let state = { ctx: flow.ctx(10) };
state = flow.init(state);

expect(state.ctx.currentPlayer).toBe('0');
state = flow.processGameEvent(state, { type: 'endTurn' });
expect(state.ctx.currentPlayer).toBe('2');
state = flow.processGameEvent(state, { type: 'endTurn' });
expect(state.ctx.currentPlayer).toBe('4');

state = flow.processGameEvent(state, { type: 'endPhase' });

expect(state.ctx.currentPlayer).toBe('1');
state = flow.processGameEvent(state, { type: 'endTurn' });
expect(state.ctx.currentPlayer).toBe('3');
state = flow.processGameEvent(state, { type: 'endTurn' });
expect(state.ctx.currentPlayer).toBe('5');
});

0 comments on commit cb09d9a

Please sign in to comment.