Skip to content

Commit ec15ad2

Browse files
committed
TurnOrder.RESET
1 parent 4953ab7 commit ec15ad2

2 files changed

Lines changed: 39 additions & 1 deletion

File tree

src/core/turn-order.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,16 @@ export const TurnOrder = {
191191
next: (G, ctx) => (ctx.playOrderPos + 1) % ctx.playOrder.length,
192192
},
193193

194+
/**
195+
* RESET
196+
*
197+
* Similar to DEFAULT, but starts from 0 each time.
198+
*/
199+
RESET: {
200+
first: () => 0,
201+
next: (G, ctx) => (ctx.playOrderPos + 1) % ctx.playOrder.length,
202+
},
203+
194204
/**
195205
* ONCE
196206
*

src/core/turn-order.test.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ describe('turn orders', () => {
5050

5151
test('DEFAULT', () => {
5252
const flow = Flow({
53-
phases: { A: { start: true }, B: {} },
53+
phases: { A: { start: true, next: 'B' }, B: {} },
5454
});
5555

5656
let state = { ctx: flow.ctx(2) };
@@ -62,7 +62,35 @@ describe('turn orders', () => {
6262
expect(state.ctx.currentPlayer).toBe('1');
6363
state = flow.processGameEvent(state, gameEvent('endTurn'));
6464
expect(state.ctx.currentPlayer).toBe('0');
65+
state = flow.processGameEvent(state, gameEvent('endTurn'));
66+
expect(state.ctx.currentPlayer).toBe('1');
6567
expect(state.ctx.phase).toBe('A');
68+
state = flow.processGameEvent(state, gameEvent('endPhase'));
69+
expect(state.ctx.currentPlayer).toBe('1');
70+
expect(state.ctx.phase).toBe('B');
71+
});
72+
73+
test('RESET', () => {
74+
const flow = Flow({
75+
turn: { order: TurnOrder.RESET },
76+
phases: { A: { start: true, next: 'B' }, B: {} },
77+
});
78+
79+
let state = { ctx: flow.ctx(2) };
80+
state = flow.init(state);
81+
expect(state.ctx.currentPlayer).toBe('0');
82+
expect(state.ctx).not.toHaveUndefinedProperties();
83+
84+
state = flow.processGameEvent(state, gameEvent('endTurn'));
85+
expect(state.ctx.currentPlayer).toBe('1');
86+
state = flow.processGameEvent(state, gameEvent('endTurn'));
87+
expect(state.ctx.currentPlayer).toBe('0');
88+
state = flow.processGameEvent(state, gameEvent('endTurn'));
89+
expect(state.ctx.currentPlayer).toBe('1');
90+
expect(state.ctx.phase).toBe('A');
91+
state = flow.processGameEvent(state, gameEvent('endPhase'));
92+
expect(state.ctx.currentPlayer).toBe('0');
93+
expect(state.ctx.phase).toBe('B');
6694
});
6795

6896
test('ONCE', () => {

0 commit comments

Comments
 (0)