Skip to content

Commit

Permalink
merge setActivePlayers into setStage
Browse files Browse the repository at this point in the history
setStage('A')
currentPlayer is sent to Stage A

setStage({ stage: 'A', moveLimit: 1 })
same as the above, but also applies a moveLimit

setStage({ ...anything else... })
delegates the call to setActivePlayers
  • Loading branch information
nicolodavis committed Sep 21, 2019
1 parent 5ac0c4c commit f302271
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 51 deletions.
2 changes: 0 additions & 2 deletions src/client/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,6 @@ describe('event dispatchers', () => {
const client = Client({ game });
expect(Object.keys(client.events)).toEqual([
'endTurn',
'setActivePlayers',
'endStage',
'setStage',
]);
Expand All @@ -359,7 +358,6 @@ describe('event dispatchers', () => {
'endPhase',
'setPhase',
'endGame',
'setActivePlayers',
'endStage',
'setStage',
]);
Expand Down
41 changes: 16 additions & 25 deletions src/core/flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,6 @@ export function Flow({ moves, phases, endIf, turn, events, plugins }) {
if (events === undefined) {
events = {};
}
if (events.setActivePlayers === undefined) {
events.setActivePlayers = true;
}
if (events.endStage === undefined) {
events.endStage = true;
events.setStage = true;
Expand Down Expand Up @@ -502,30 +499,24 @@ export function Flow({ moves, phases, endIf, turn, events, plugins }) {
}

function UpdateStage(state, { arg, playerID }) {
if (typeof arg === 'string') {
arg = { stage: arg };
}

let { ctx } = state;
let {
activePlayers,
_activePlayersMoveLimit,
_activePlayersNumMoves,
} = ctx;

if (arg.stage) {
if (activePlayers === null) {
activePlayers = {};
}
activePlayers[playerID] = arg.stage;
_activePlayersNumMoves[playerID] = 0;
if (activePlayers === null) {
activePlayers = {};
}
activePlayers[playerID] = arg.stage;
_activePlayersNumMoves[playerID] = 0;

if (arg.moveLimit) {
if (_activePlayersMoveLimit === null) {
_activePlayersMoveLimit = {};
}
_activePlayersMoveLimit[playerID] = arg.moveLimit;
if (arg.moveLimit) {
if (_activePlayersMoveLimit === null) {
_activePlayersMoveLimit = {};
}
_activePlayersMoveLimit[playerID] = arg.moveLimit;
}

ctx = {
Expand Down Expand Up @@ -828,7 +819,11 @@ export function Flow({ moves, phases, endIf, turn, events, plugins }) {
}

function SetStageEvent(state, arg) {
return ProcessEvents(state, [{ fn: EndStage, arg }]);
if (typeof arg === 'string' || arg.stage) {
if (!arg.stage) arg = { stage: arg };
return ProcessEvents(state, [{ fn: EndStage, arg }]);
}
return ProcessEvents(state, [{ fn: SetActivePlayersEvent, arg }]);
}

function EndStageEvent(state) {
Expand Down Expand Up @@ -865,13 +860,12 @@ export function Flow({ moves, phases, endIf, turn, events, plugins }) {
}

const eventHandlers = {
endPhase: EndPhaseEvent,
setPhase: SetPhaseEvent,
endStage: EndStageEvent,
setStage: SetStageEvent,
endTurn: EndTurnEvent,
endPhase: EndPhaseEvent,
setPhase: SetPhaseEvent,
endGame: EndGameEvent,
setActivePlayers: SetActivePlayersEvent,
};

let enabledEvents = {};
Expand All @@ -885,9 +879,6 @@ export function Flow({ moves, phases, endIf, turn, events, plugins }) {
if (events.endGame) {
enabledEvents['endGame'] = true;
}
if (events.setActivePlayers) {
enabledEvents['setActivePlayers'] = true;
}
if (events.endStage) {
enabledEvents['endStage'] = true;
enabledEvents['setStage'] = true;
Expand Down
40 changes: 25 additions & 15 deletions src/core/flow.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ describe('stages', () => {

describe('stage B', () => {
beforeAll(() => {
client.events.setActivePlayers({ currentPlayer: 'B' });
client.events.setStage('B');
});

test('A is not allowed', () => {
Expand All @@ -435,7 +435,7 @@ describe('stages', () => {

describe('stage C', () => {
beforeAll(() => {
client.events.setActivePlayers({ currentPlayer: 'C' });
client.events.setStage('C');
});

test('A is allowed', () => {
Expand Down Expand Up @@ -475,6 +475,19 @@ describe('stage events', () => {
expect(state.ctx.activePlayers).toEqual({ '0': 'A' });
});

test('with move limit', () => {
let flow = Flow({});
let state = { G: {}, ctx: flow.ctx(2) };
state = flow.init(state);

expect(state.ctx._activePlayersMoveLimit).toBeNull();
state = flow.processGameEvent(
state,
gameEvent('setStage', { stage: 'A', moveLimit: 2 })
);
expect(state.ctx._activePlayersMoveLimit).toEqual({ '0': 2 });
});

test('with multiple active players', () => {
let flow = Flow({
turn: {
Expand All @@ -485,11 +498,21 @@ describe('stage events', () => {
state = flow.init(state);

expect(state.ctx.activePlayers).toEqual({ '0': 'A', '1': 'A', '2': 'A' });
expect(state.ctx._activePlayersMoveLimit).toEqual({
'0': 5,
'1': 5,
'2': 5,
});
state = flow.processGameEvent(
state,
gameEvent('setStage', { stage: 'B', moveLimit: 1 })
);
expect(state.ctx.activePlayers).toEqual({ '0': 'B', '1': 'A', '2': 'A' });
expect(state.ctx._activePlayersMoveLimit).toEqual({
'0': 1,
'1': 5,
'2': 5,
});
});

test('resets move count', () => {
Expand All @@ -509,19 +532,6 @@ describe('stage events', () => {
expect(state.ctx._activePlayersNumMoves).toMatchObject({ '0': 0 });
});

test('with move limit', () => {
let flow = Flow({});
let state = { G: {}, ctx: flow.ctx(2) };
state = flow.init(state);

expect(state.ctx._activePlayersMoveLimit).toBeNull();
state = flow.processGameEvent(
state,
gameEvent('setStage', { stage: 'A', moveLimit: 2 })
);
expect(state.ctx._activePlayersMoveLimit).toEqual({ '0': 2 });
});

test('empty argument ends stage', () => {
let flow = Flow({ turn: { activePlayers: { currentPlayer: 'A' } } });
let state = { G: {}, ctx: flow.ctx(2) };
Expand Down
2 changes: 1 addition & 1 deletion src/core/turn-order.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const Pass = (G, ctx) => {
* @param {*} state
* @param {*} arg
*/
export function SetActivePlayersEvent(state, arg) {
export function SetActivePlayersEvent(state, { arg }) {
return { ...state, ctx: SetActivePlayers(state.ctx, arg) };
}

Expand Down
16 changes: 8 additions & 8 deletions src/core/turn-order.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -393,22 +393,22 @@ test('playOrder', () => {
expect(state.ctx.currentPlayer).toBe('2');
});

describe('setActivePlayers', () => {
describe('setStage - advanced', () => {
const flow = Flow({});
const state = { ctx: flow.ctx(2) };

test('basic', () => {
const newState = flow.processGameEvent(
state,
gameEvent('setActivePlayers', [{ value: { '1': Stage.NULL } }])
gameEvent('setStage', [{ value: { '1': Stage.NULL } }])
);
expect(newState.ctx.activePlayers).toMatchObject({ '1': Stage.NULL });
});

test('all', () => {
const newState = flow.processGameEvent(
state,
gameEvent('setActivePlayers', [{ all: Stage.NULL }])
gameEvent('setStage', [{ all: Stage.NULL }])
);
expect(newState.ctx.activePlayers).toMatchObject({
'0': Stage.NULL,
Expand All @@ -420,7 +420,7 @@ describe('setActivePlayers', () => {
const game = {
moves: {
B: (G, ctx) => {
ctx.events.setActivePlayers({
ctx.events.setStage({
value: { '0': Stage.NULL, '1': Stage.NULL },
moveLimit: 1,
});
Expand All @@ -445,7 +445,7 @@ describe('setActivePlayers', () => {
const game = {
moves: {
B: (G, ctx) => {
ctx.events.setActivePlayers({
ctx.events.setStage({
moveLimit: 1,
others: Stage.NULL,
});
Expand Down Expand Up @@ -505,7 +505,7 @@ describe('setActivePlayers', () => {
const game = {
moves: {
A: (G, ctx) => {
ctx.events.setActivePlayers({
ctx.events.setStage({
currentPlayer: 'stage2',
moveLimit: 1,
revert: true,
Expand Down Expand Up @@ -552,7 +552,7 @@ describe('setActivePlayers', () => {
const game = {
moves: {
A: (G, ctx) => {
ctx.events.setActivePlayers({
ctx.events.setStage({
currentPlayer: 'stage2',
moveLimit: 1,
revert: true,
Expand Down Expand Up @@ -854,7 +854,7 @@ describe('setActivePlayers', () => {
const game = {
moves: {
militia: (G, ctx) => {
ctx.events.setActivePlayers({
ctx.events.setStage({
others: 'discard',
moveLimit: 1,
revert: true,
Expand Down

0 comments on commit f302271

Please sign in to comment.