Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Move player to “next” stage on endStage #484

Merged
merged 2 commits into from
Sep 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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