Skip to content

Commit 86e65fe

Browse files
delucisnicolodavis
authored andcommitted
fix: Move player to “next” stage on endStage (#484)
* test: Add test for `endStage` when stage has `next` field * feat: Move player to `next` stage on `endStage`
1 parent 652c46b commit 86e65fe

2 files changed

Lines changed: 42 additions & 3 deletions

File tree

src/core/flow.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -553,14 +553,20 @@ export function Flow({ moves, phases, endIf, turn, events, plugins }) {
553553
let { ctx } = state;
554554
let { activePlayers, _activePlayersMoveLimit } = ctx;
555555

556+
const playerInStage = activePlayers !== null && playerID in activePlayers;
557+
558+
if (!arg && playerInStage) {
559+
const conf = GetPhase(ctx);
560+
const stage = conf.turn.stages[activePlayers[playerID]];
561+
if (stage && stage.next) arg = stage.next;
562+
}
563+
556564
if (next && arg) {
557565
next.push({ fn: UpdateStage, arg, playerID });
558566
}
559567

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

565571
// Remove player from activePlayers.
566572
activePlayers = Object.keys(activePlayers)

src/core/flow.test.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,39 @@ describe('stage events', () => {
569569
state = flow.processEvent(state, gameEvent('endStage'));
570570
expect(state.ctx._activePlayersNumMoves).toMatchObject({ '0': 1 });
571571
});
572+
573+
test('sets to next', () => {
574+
let flow = Flow({
575+
turn: {
576+
activePlayers: { player: 'A1', others: 'B1' },
577+
stages: {
578+
A1: { next: 'A2' },
579+
B1: { next: 'B2' },
580+
},
581+
},
582+
});
583+
let state = { G: {}, ctx: flow.ctx(2) };
584+
state = flow.init(state);
585+
586+
expect(state.ctx.activePlayers).toMatchObject({
587+
'0': 'A1',
588+
'1': 'B1',
589+
});
590+
591+
state = flow.processEvent(state, gameEvent('endStage', null, '0'));
592+
593+
expect(state.ctx.activePlayers).toMatchObject({
594+
'0': 'A2',
595+
'1': 'B1',
596+
});
597+
598+
state = flow.processEvent(state, gameEvent('endStage', null, '1'));
599+
600+
expect(state.ctx.activePlayers).toMatchObject({
601+
'0': 'A2',
602+
'1': 'B2',
603+
});
604+
});
572605
});
573606
});
574607

0 commit comments

Comments
 (0)