Skip to content

Commit

Permalink
fix(set-stage): setStage event for Stage.NULL makes player active. (c…
Browse files Browse the repository at this point in the history
…loses #848) (#849)

* Fixing setStage to make players set on Stage.NULL active.

* Update src/core/turn-order.test.ts

Co-authored-by: Chris Swithinbank <swithinbank@gmail.com>

* Update src/core/turn-order.test.ts

Co-authored-by: Chris Swithinbank <swithinbank@gmail.com>

Co-authored-by: Evandro Abu Kamel <evandro.costa@maxmilhas.com.br>
Co-authored-by: Chris Swithinbank <swithinbank@gmail.com>
  • Loading branch information
3 people authored Nov 25, 2020
1 parent 5d34610 commit 5207be5
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/core/flow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
}

Expand All @@ -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 = {};
}
Expand Down Expand Up @@ -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 });
}

Expand Down
49 changes: 49 additions & 0 deletions src/core/turn-order.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down

0 comments on commit 5207be5

Please sign in to comment.