Skip to content

Commit cb09d9a

Browse files
committed
make turnOrder a globally configurable option
1 parent 1abc042 commit cb09d9a

2 files changed

Lines changed: 41 additions & 10 deletions

File tree

src/core/flow.js

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ export function Flow({ ctx, events, init, validator, processMove }) {
103103
* @param {...object} onTurnEnd - Any code to run when a turn ends.
104104
* (G, ctx) => G
105105
*
106+
* @param {...object} turnOrder - Customize the turn order (see turn-order.js).
107+
*
106108
* @param {...object} triggers - An array of objects with the format:
107109
* {
108110
* condition: (G, ctx) => boolean,
@@ -149,15 +151,8 @@ export function Flow({ ctx, events, init, validator, processMove }) {
149151
* // A phase-specific movesPerTurn.
150152
* movesPerTurn: integer,
151153
*
152-
* // Called when `endTurn` is processed, and returns the next player.
153-
* // If not specified, TurnOrder.DEFAULT is used.
154-
* turnOrder: {
155-
* // The first player.
156-
* first: (G, ctx) => playerID,
157-
* // Called whenever `endTurn` is processed to determine
158-
* // the next player.
159-
* next: (G, ctx) => playerID,
160-
* },
154+
* // A phase-specific turnOrder.
155+
* turnOrder: TurnOrder.DEFAULT,
161156
*
162157
* // List of moves that are allowed in this phase.
163158
* allowedMoves: ['moveA', ...],
@@ -169,6 +164,7 @@ export function FlowWithPhases({
169164
endTurnIf,
170165
endGameIf,
171166
onTurnEnd,
167+
turnOrder,
172168
triggers,
173169
events,
174170
}) {
@@ -184,6 +180,7 @@ export function FlowWithPhases({
184180
if (!endTurnIf) endTurnIf = () => false;
185181
if (!endGameIf) endGameIf = () => undefined;
186182
if (!onTurnEnd) onTurnEnd = G => G;
183+
if (!turnOrder) turnOrder = TurnOrder.DEFAULT;
187184
if (!triggers) triggers = [];
188185

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

196193
if (!conf.turnOrder) {
197-
conf.turnOrder = TurnOrder.DEFAULT;
194+
conf.turnOrder = turnOrder;
198195
}
199196
if (!conf.endPhaseIf) {
200197
conf.endPhaseIf = () => false;

src/core/turn-order.test.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,37 @@ test('turnOrder', () => {
7070
expect(state.ctx.allPassed).toBe(true);
7171
expect(state.ctx.currentPlayer).toBe(undefined);
7272
});
73+
74+
test('override', () => {
75+
const even = {
76+
first: () => '0',
77+
next: (G, ctx) => (+ctx.currentPlayer + 2) % ctx.numPlayers + '',
78+
};
79+
80+
const odd = {
81+
first: () => '1',
82+
next: (G, ctx) => (+ctx.currentPlayer + 2) % ctx.numPlayers + '',
83+
};
84+
85+
let flow = FlowWithPhases({
86+
turnOrder: even,
87+
phases: [{ name: 'A' }, { name: 'B', turnOrder: odd }],
88+
});
89+
90+
let state = { ctx: flow.ctx(10) };
91+
state = flow.init(state);
92+
93+
expect(state.ctx.currentPlayer).toBe('0');
94+
state = flow.processGameEvent(state, { type: 'endTurn' });
95+
expect(state.ctx.currentPlayer).toBe('2');
96+
state = flow.processGameEvent(state, { type: 'endTurn' });
97+
expect(state.ctx.currentPlayer).toBe('4');
98+
99+
state = flow.processGameEvent(state, { type: 'endPhase' });
100+
101+
expect(state.ctx.currentPlayer).toBe('1');
102+
state = flow.processGameEvent(state, { type: 'endTurn' });
103+
expect(state.ctx.currentPlayer).toBe('3');
104+
state = flow.processGameEvent(state, { type: 'endTurn' });
105+
expect(state.ctx.currentPlayer).toBe('5');
106+
});

0 commit comments

Comments
 (0)