Skip to content

Commit

Permalink
fix: Move player to “next” stage on endStage (#484)
Browse files Browse the repository at this point in the history
* test: Add test for `endStage` when stage has `next` field

* feat: Move player to `next` stage on `endStage`
  • Loading branch information
delucis authored and nicolodavis committed Sep 30, 2019
1 parent 652c46b commit 86e65fe
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/core/flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -553,14 +553,20 @@ export function Flow({ moves, phases, endIf, turn, events, plugins }) {
let { ctx } = state;
let { activePlayers, _activePlayersMoveLimit } = ctx;

const playerInStage = activePlayers !== null && playerID in activePlayers;

if (!arg && playerInStage) {
const conf = GetPhase(ctx);
const stage = conf.turn.stages[activePlayers[playerID]];
if (stage && stage.next) arg = stage.next;
}

if (next && arg) {
next.push({ fn: UpdateStage, arg, playerID });
}

// If player isn’t in a stage, there is nothing else to do.
if (activePlayers === null || !(playerID in activePlayers)) {
return state;
}
if (!playerInStage) return state;

// Remove player from activePlayers.
activePlayers = Object.keys(activePlayers)
Expand Down
33 changes: 33 additions & 0 deletions src/core/flow.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,39 @@ describe('stage events', () => {
state = flow.processEvent(state, gameEvent('endStage'));
expect(state.ctx._activePlayersNumMoves).toMatchObject({ '0': 1 });
});

test('sets to next', () => {
let flow = Flow({
turn: {
activePlayers: { player: 'A1', others: 'B1' },
stages: {
A1: { next: 'A2' },
B1: { next: 'B2' },
},
},
});
let state = { G: {}, ctx: flow.ctx(2) };
state = flow.init(state);

expect(state.ctx.activePlayers).toMatchObject({
'0': 'A1',
'1': 'B1',
});

state = flow.processEvent(state, gameEvent('endStage', null, '0'));

expect(state.ctx.activePlayers).toMatchObject({
'0': 'A2',
'1': 'B1',
});

state = flow.processEvent(state, gameEvent('endStage', null, '1'));

expect(state.ctx.activePlayers).toMatchObject({
'0': 'A2',
'1': 'B2',
});
});
});
});

Expand Down

0 comments on commit 86e65fe

Please sign in to comment.