From 5207be5823364a0107370ea8834f620a55131134 Mon Sep 17 00:00:00 2001 From: Evandro Abu Kamel Date: Wed, 25 Nov 2020 14:33:48 -0300 Subject: [PATCH] fix(set-stage): setStage event for Stage.NULL makes player active. (closes #848) (#849) * Fixing setStage to make players set on Stage.NULL active. * Update src/core/turn-order.test.ts Co-authored-by: Chris Swithinbank * Update src/core/turn-order.test.ts Co-authored-by: Chris Swithinbank Co-authored-by: Evandro Abu Kamel Co-authored-by: Chris Swithinbank --- src/core/flow.ts | 8 +++--- src/core/turn-order.test.ts | 49 +++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/src/core/flow.ts b/src/core/flow.ts index a645ba2a7..b2e48f7fc 100644 --- a/src/core/flow.ts +++ b/src/core/flow.ts @@ -353,7 +353,7 @@ export function Flow({ } function UpdateStage(state: State, { arg, playerID }): State { - if (typeof arg === 'string') { + if (typeof arg === 'string' || arg === Stage.NULL) { arg = { stage: arg }; } @@ -364,7 +364,8 @@ export function Flow({ _activePlayersNumMoves, } = ctx; - if (arg.stage) { + // Checking if stage is valid, even Stage.NULL + if (arg.stage !== undefined) { if (activePlayers === null) { activePlayers = {}; } @@ -562,7 +563,8 @@ export function Flow({ if (stage && stage.next) arg = stage.next; } - if (next && arg) { + // Checking if arg is a valid stage, even Stage.NULL + if (next && arg !== undefined) { next.push({ fn: UpdateStage, arg, playerID }); } diff --git a/src/core/turn-order.test.ts b/src/core/turn-order.test.ts index 9a6f42f0d..d95ec9aaa 100644 --- a/src/core/turn-order.test.ts +++ b/src/core/turn-order.test.ts @@ -451,6 +451,55 @@ describe('setActivePlayers', () => { expect(state.ctx.activePlayers).toBeNull(); }); + test('set stages to Stage.NULL', () => { + const game = { + moves: { + A: G => G, + B: (G, ctx) => { + ctx.events.setActivePlayers({ + moveLimit: 1, + currentPlayer: 'start', + }); + return G; + }, + }, + turn: { + activePlayers: { + currentPlayer: { + stage: 'start', + }, + others: Stage.NULL, + }, + stages: { + start: { + moves: { + S: (G, ctx) => { + ctx.events.setStage(Stage.NULL); + return G; + }, + }, + }, + }, + }, + }; + const reducer = CreateGameReducer({ game }); + let state = InitializeGame({ game, numPlayers: 3 }); + + expect(state.ctx.currentPlayer).toBe('0'); + expect(Object.keys(state.ctx.activePlayers)).toEqual(['0', '1', '2']); + expect(state.ctx.activePlayers['0']).toEqual('start'); + expect(state.ctx.activePlayers['1']).toEqual(Stage.NULL); + expect(state.ctx.activePlayers['2']).toEqual(Stage.NULL); + + state = reducer(state, makeMove('S', null, '0')); + expect(Object.keys(state.ctx.activePlayers)).toEqual(['0', '1', '2']); + expect(state.ctx.activePlayers['0']).toEqual(Stage.NULL); + + state = reducer(state, makeMove('B', null, '0')); + expect(Object.keys(state.ctx.activePlayers)).toEqual(['0']); + expect(state.ctx.activePlayers['0']).toEqual('start'); + }); + describe('reset behavior', () => { test('start of turn', () => { const game = {