Skip to content

Commit

Permalink
Allow result of onPhaseBegin to influence turn order (#341)
Browse files Browse the repository at this point in the history
* #339 - use G value returned from onPhaseBegin in subsequent calls

* Add test

* expand test
  • Loading branch information
djMax authored and nicolodavis committed Jan 21, 2019
1 parent e458be4 commit f544511
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/core/flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ export function FlowWithPhases({
// Helper to perform start-of-phase initialization.
const startPhase = function(state, config) {
let G = config.onPhaseBegin(state.G, state.ctx);
let ctx = InitTurnOrderState(state.G, state.ctx, config.turnOrder);
let ctx = InitTurnOrderState(G, state.ctx, config.turnOrder);

// Allow plugins to modify G and ctx at the beginning of a phase.
G = plugins.G.onPhaseBegin(G, ctx, game);
Expand Down
12 changes: 12 additions & 0 deletions src/core/flow.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,24 +69,36 @@ describe('phases', () => {
next: 'A',
},
},

turnOrder: {
first: G => {
if (G.setupB && !G.cleanupB) return '1';
return '0';
},
},
});

let state = { G: {}, ctx: flow.ctx(2) };
state = flow.init(state);
expect(state.G).toMatchObject({ setupA: true });
expect(state.ctx.currentPlayer).toBe('0');

state = flow.processGameEvent(state, gameEvent('endPhase'));
expect(state.G).toMatchObject({
setupA: true,
cleanupA: true,
setupB: true,
});
expect(state.ctx.currentPlayer).toBe('1');

state = flow.processGameEvent(state, gameEvent('endPhase'));
expect(state.G).toMatchObject({
setupA: true,
cleanupA: true,
setupB: true,
cleanupB: true,
});
expect(state.ctx.currentPlayer).toBe('0');
});

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

0 comments on commit f544511

Please sign in to comment.