Skip to content

Commit

Permalink
TurnOrder.RESET
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolodavis committed Sep 10, 2019
1 parent 4953ab7 commit ec15ad2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/core/turn-order.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,16 @@ export const TurnOrder = {
next: (G, ctx) => (ctx.playOrderPos + 1) % ctx.playOrder.length,
},

/**
* RESET
*
* Similar to DEFAULT, but starts from 0 each time.
*/
RESET: {
first: () => 0,
next: (G, ctx) => (ctx.playOrderPos + 1) % ctx.playOrder.length,
},

/**
* ONCE
*
Expand Down
30 changes: 29 additions & 1 deletion src/core/turn-order.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describe('turn orders', () => {

test('DEFAULT', () => {
const flow = Flow({
phases: { A: { start: true }, B: {} },
phases: { A: { start: true, next: 'B' }, B: {} },
});

let state = { ctx: flow.ctx(2) };
Expand All @@ -62,7 +62,35 @@ describe('turn orders', () => {
expect(state.ctx.currentPlayer).toBe('1');
state = flow.processGameEvent(state, gameEvent('endTurn'));
expect(state.ctx.currentPlayer).toBe('0');
state = flow.processGameEvent(state, gameEvent('endTurn'));
expect(state.ctx.currentPlayer).toBe('1');
expect(state.ctx.phase).toBe('A');
state = flow.processGameEvent(state, gameEvent('endPhase'));
expect(state.ctx.currentPlayer).toBe('1');
expect(state.ctx.phase).toBe('B');
});

test('RESET', () => {
const flow = Flow({
turn: { order: TurnOrder.RESET },
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.processGameEvent(state, gameEvent('endTurn'));
expect(state.ctx.currentPlayer).toBe('1');
state = flow.processGameEvent(state, gameEvent('endTurn'));
expect(state.ctx.currentPlayer).toBe('0');
state = flow.processGameEvent(state, gameEvent('endTurn'));
expect(state.ctx.currentPlayer).toBe('1');
expect(state.ctx.phase).toBe('A');
state = flow.processGameEvent(state, gameEvent('endPhase'));
expect(state.ctx.currentPlayer).toBe('0');
expect(state.ctx.phase).toBe('B');
});

test('ONCE', () => {
Expand Down

0 comments on commit ec15ad2

Please sign in to comment.