Skip to content

Commit

Permalink
Increment current player at start of phase in TurnOrder.DEFAULT (#521)
Browse files Browse the repository at this point in the history
* feat(core): Increment currentPlayer in TurnOrder.DEFAULT.first

Increments the current player at the start of phases when using TurnOrder.DEFAULT (which currently
maintains the player across phase changes). Includes a check so that the player is NOT incremented
at the start of a game.

BREAKING CHANGE: The current player is now incremented at the start of a new phase. Previously the
current player was unchanged when a phase ended.

* test(core): Fix TurnOrder.DEFAULT test for new behaviour

* test(core): Fix flow tests for new default turn order

* feat(core): Add TurnOrder.CONTINUE to provide legacy behaviour

This implements the previous TurnOrder.DEFAULT for those who need this 
less common pattern.

* test(core): Add test for TurnOrder.CONTINUE

* docs: Document TurnOrder.CONTINUE
  • Loading branch information
delucis authored and nicolodavis committed Nov 24, 2019
1 parent ca64543 commit ad08b8a
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 8 deletions.
10 changes: 8 additions & 2 deletions docs/documentation/turn-order.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,16 @@ specify any turn order.

##### RESET

This is similar to `DEFAULT`, but instead of continuing
from the previous position at the beginning of a phase, it
This is similar to `DEFAULT`, but instead of incrementing
the previous position at the beginning of a phase, it
will always start from `0`.

##### CONTINUE

This is also similar to `DEFAULT`, but instead of incrementing
the previous position at the beginning of a phase, it will
start with the player who ended the previous phase.

##### ONCE

This is another round-robin, but it goes around only once.
Expand Down
11 changes: 6 additions & 5 deletions src/core/flow.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ describe('phases', () => {
expect(state.ctx.phase).toBe(null);
});

test('maintain playOrderPos on phase end', () => {
test('increment playOrderPos on phase end', () => {
const flow = Flow({
phases: { A: { start: true, next: 'B' }, B: { next: 'A' } },
});
Expand All @@ -148,7 +148,7 @@ describe('phases', () => {
state = flow.processEvent(state, gameEvent('endTurn'));
expect(state.ctx.playOrderPos).toBe(1);
state = flow.processEvent(state, gameEvent('endPhase'));
expect(state.ctx.playOrderPos).toBe(1);
expect(state.ctx.playOrderPos).toBe(2);
});

describe('setPhase', () => {
Expand Down Expand Up @@ -282,10 +282,11 @@ describe('turn', () => {

expect(state.ctx.phase).toBe('B');
expect(state.ctx.turn).toBe(3);
expect(state.ctx.currentPlayer).toBe('1');
expect(state.ctx.currentPlayer).toBe('0');

state = flow.processMove(state, makeMove('move', null, '1').payload);
state = flow.processMove(state, makeMove('move', null, '0').payload);
expect(state.ctx.turn).toBe(4);
expect(state.ctx.currentPlayer).toBe('1');
});
});

Expand Down Expand Up @@ -364,7 +365,7 @@ describe('turn', () => {
state = flow.processMove(state, makeMove().payload);

expect(state.ctx.phase).toBe('B');
expect(state.ctx.currentPlayer).toBe('1');
expect(state.ctx.currentPlayer).toBe('0');
expect(state.ctx.turn).toBe(3);
});
});
Expand Down
15 changes: 14 additions & 1 deletion src/core/turn-order.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,10 @@ export const TurnOrder = {
* The default round-robin turn order.
*/
DEFAULT: {
first: (G, ctx) => ctx.playOrderPos,
first: (G, ctx) =>
ctx.turn === 0
? ctx.playOrderPos
: (ctx.playOrderPos + 1) % ctx.playOrder.length,
next: (G, ctx) => (ctx.playOrderPos + 1) % ctx.playOrder.length,
},

Expand All @@ -328,6 +331,16 @@ export const TurnOrder = {
next: (G, ctx) => (ctx.playOrderPos + 1) % ctx.playOrder.length,
},

/**
* CONTINUE
*
* Similar to DEFAULT, but starts with the player who ended the last phase.
*/
CONTINUE: {
first: (G, ctx) => ctx.playOrderPos,
next: (G, ctx) => (ctx.playOrderPos + 1) % ctx.playOrder.length,
},

/**
* ONCE
*
Expand Down
23 changes: 23 additions & 0 deletions src/core/turn-order.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,29 @@ describe('turn orders', () => {
expect(state.ctx.currentPlayer).toBe('0');
expect(state.ctx).not.toHaveUndefinedProperties();

state = flow.processEvent(state, gameEvent('endTurn'));
expect(state.ctx.currentPlayer).toBe('1');
state = flow.processEvent(state, gameEvent('endTurn'));
expect(state.ctx.currentPlayer).toBe('0');
state = flow.processEvent(state, gameEvent('endTurn'));
expect(state.ctx.currentPlayer).toBe('1');
expect(state.ctx.phase).toBe('A');
state = flow.processEvent(state, gameEvent('endPhase'));
expect(state.ctx.currentPlayer).toBe('0');
expect(state.ctx.phase).toBe('B');
});

test('CONTINUE', () => {
const flow = Flow({
turn: { order: TurnOrder.CONTINUE },
phases: { A: { start: true, next: 'B' }, B: {} },
});

let state = { ctx: flow.ctx(2) };
state = flow.init(state);
expect(state.ctx.currentPlayer).toBe('0');
expect(state.ctx).not.toHaveUndefinedProperties();

state = flow.processEvent(state, gameEvent('endTurn'));
expect(state.ctx.currentPlayer).toBe('1');
state = flow.processEvent(state, gameEvent('endTurn'));
Expand Down

0 comments on commit ad08b8a

Please sign in to comment.