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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add endStage and setStage events #458

Merged
merged 21 commits into from
Sep 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
e8ac4b3
refactor: Move `activePlayers` reset logic to reuseable function
delucis Sep 15, 2019
ffb5811
feat: Add `startStage` and `endStage` events
delucis Sep 15, 2019
919fa2d
test: Fix failing tests with new events
delucis Sep 15, 2019
251d1ff
test: Write basic tests for `setStage` and `endStage`
delucis Sep 15, 2019
accaa5b
feat: Add hypothetical move limit logic
delucis Sep 15, 2019
8e54cd6
test: Add failing test for move limit with `setStage`
delucis Sep 15, 2019
c26e6c2
feat: Bail out of `UpdateStage` early if `arg` is undefined
delucis Sep 15, 2019
f080ddb
Merge branch 'master' into delucis/stage-events
delucis Sep 16, 2019
b0eb65f
feat: Allow `setStage` event to receive object argument
delucis Sep 16, 2019
358eee5
test: Update `setStage` move limit test with object syntax
delucis Sep 16, 2019
32b40ed
test: Add test for `setStage` object syntax without move limit
delucis Sep 16, 2019
23b7b2e
test: Test how `setStage` & `endStage` mutate `_activePlayersNumMoves`
delucis Sep 16, 2019
205e3f4
test: Add test for `endStage` with more than one currently active player
delucis Sep 16, 2019
92f3cf2
refactor: Use `EndStage` to simplify `ProcessMove`
delucis Sep 16, 2019
1655245
test: Increase `setStage`/`endStage` coverage
delucis Sep 16, 2019
43d4719
refactor: Rename `UpdateActivePlayers` to `UpdateActivePlayersOnceEmpty`
delucis Sep 18, 2019
d64e22d
refactor: Don鈥檛 call no-op `UpdateStage` when `arg` is undefined
delucis Sep 18, 2019
7a69a73
Merge branch 'master' into delucis/stage-events
delucis Sep 18, 2019
46492ee
Merge branch 'master' into delucis/stage-events
delucis Sep 18, 2019
5196ec0
group event defaults
nicolodavis Sep 18, 2019
bcd8005
remove redundant statement
nicolodavis Sep 18, 2019
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
10 changes: 9 additions & 1 deletion src/client/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,12 @@ describe('event dispatchers', () => {
test('default', () => {
const game = {};
const client = Client({ game });
expect(Object.keys(client.events)).toEqual(['endTurn', 'setActivePlayers']);
expect(Object.keys(client.events)).toEqual([
'endTurn',
'setActivePlayers',
'endStage',
'setStage',
]);
expect(client.getState().ctx.turn).toBe(1);
client.events.endTurn();
expect(client.getState().ctx.turn).toBe(2);
Expand All @@ -355,6 +360,8 @@ describe('event dispatchers', () => {
'setPhase',
'endGame',
'setActivePlayers',
'endStage',
'setStage',
]);
expect(client.getState().ctx.turn).toBe(1);
client.events.endTurn();
Expand All @@ -367,6 +374,7 @@ describe('event dispatchers', () => {
endPhase: false,
endTurn: false,
setActivePlayers: false,
endStage: false,
},
};
const client = Client({ game });
Expand Down
177 changes: 123 additions & 54 deletions src/core/flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import {
SetActivePlayersEvent,
SetActivePlayers,
UpdateActivePlayersOnceEmpty,
InitTurnOrderState,
UpdateTurnOrderState,
Stage,
Expand Down Expand Up @@ -208,10 +209,12 @@ export function Flow({ moves, phases, endIf, turn, events, plugins }) {
if (events.setActivePlayers === undefined) {
events.setActivePlayers = true;
}
if (events.endStage === undefined) {
events.endStage = true;
events.setStage = true;
}
if (events.endPhase === undefined && phases) {
events.endPhase = true;
}
if (events.setPhase === undefined && phases) {
events.setPhase = true;
}
if (events.endTurn === undefined) {
Expand Down Expand Up @@ -498,6 +501,43 @@ export function Flow({ moves, phases, endIf, turn, events, plugins }) {
return state;
}

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;
nicolodavis marked this conversation as resolved.
Show resolved Hide resolved
_activePlayersNumMoves[playerID] = 0;
nicolodavis marked this conversation as resolved.
Show resolved Hide resolved

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

ctx = {
...ctx,
activePlayers,
_activePlayersMoveLimit,
_activePlayersNumMoves,
};

return { ...state, ctx };
}

///////////////
// ShouldEnd //
///////////////
Expand Down Expand Up @@ -631,6 +671,63 @@ export function Flow({ moves, phases, endIf, turn, events, plugins }) {
return { ...state, G, ctx, deltalog, _undo: [], _redo: [] };
}

function EndStage(state, { arg, next, automatic, playerID }) {
playerID = playerID || state.ctx.currentPlayer;

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

if (next && arg) {
next.push({ fn: UpdateStage, arg, playerID });
delucis marked this conversation as resolved.
Show resolved Hide resolved
}

// If player isn鈥檛 in a stage, there is nothing else to do.
if (activePlayers === null || !(playerID in activePlayers)) {
return state;
}

// Remove player from activePlayers.
activePlayers = Object.keys(activePlayers)
.filter(id => id !== playerID)
.reduce((obj, key) => {
obj[key] = activePlayers[key];
return obj;
}, {});

if (_activePlayersMoveLimit) {
// Remove player from _activePlayersMoveLimit.
_activePlayersMoveLimit = Object.keys(_activePlayersMoveLimit)
.filter(id => id !== playerID)
.reduce((obj, key) => {
obj[key] = _activePlayersMoveLimit[key];
return obj;
}, {});
}

ctx = UpdateActivePlayersOnceEmpty({
...ctx,
activePlayers,
_activePlayersMoveLimit,
});

// Add log entry.
const action = gameEvent('endStage', arg);
const logEntry = {
action,
_stateID: state._stateID,
turn: state.ctx.turn,
phase: state.ctx.phase,
};

if (automatic) {
logEntry.automatic = true;
}

const deltalog = [...(state.deltalog || []), logEntry];

return { ...state, ctx, deltalog };
}

/**
* Retrieves the relevant move that can be played by playerID.
*
Expand Down Expand Up @@ -683,57 +780,11 @@ export function Flow({ moves, phases, endIf, turn, events, plugins }) {
let conf = GetPhase(state.ctx);

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

const { playerID } = action;

if (activePlayers) _activePlayersNumMoves[playerID]++;

if (
_activePlayersMoveLimit &&
_activePlayersNumMoves[playerID] >= _activePlayersMoveLimit[playerID]
) {
activePlayers = Object.keys(activePlayers)
.filter(id => id !== playerID)
.reduce((obj, key) => {
obj[key] = activePlayers[key];
return obj;
}, {});
_activePlayersMoveLimit = Object.keys(_activePlayersMoveLimit)
.filter(id => id !== playerID)
.reduce((obj, key) => {
obj[key] = _activePlayersMoveLimit[key];
return obj;
}, {});
}

if (activePlayers && Object.keys(activePlayers).length == 0) {
if (ctx._nextActivePlayers) {
ctx = SetActivePlayers(ctx, ctx._nextActivePlayers);
({
activePlayers,
_activePlayersMoveLimit,
_activePlayersNumMoves,
_prevActivePlayers,
} = ctx);
} else if (_prevActivePlayers.length > 0) {
const lastIndex = _prevActivePlayers.length - 1;
({
activePlayers,
_activePlayersMoveLimit,
_activePlayersNumMoves,
} = _prevActivePlayers[lastIndex]);
_prevActivePlayers = _prevActivePlayers.slice(0, lastIndex);
} else {
activePlayers = null;
_activePlayersMoveLimit = null;
}
}
if (ctx.activePlayers) _activePlayersNumMoves[playerID]++;

let numMoves = state.ctx.numMoves;
if (playerID == state.ctx.currentPlayer) {
Expand All @@ -744,14 +795,18 @@ export function Flow({ moves, phases, endIf, turn, events, plugins }) {
...state,
ctx: {
...ctx,
activePlayers,
_activePlayersMoveLimit,
_activePlayersNumMoves,
_prevActivePlayers,
numMoves,
_activePlayersNumMoves,
},
};

if (
ctx._activePlayersMoveLimit &&
_activePlayersNumMoves[playerID] >= ctx._activePlayersMoveLimit[playerID]
) {
state = EndStage(state, { playerID, automatic: true });
}

const G = conf.turn.onMove(state.G, state.ctx, action);
state = { ...state, G };

Expand All @@ -772,6 +827,14 @@ export function Flow({ moves, phases, endIf, turn, events, plugins }) {
return ProcessEvents(state, events);
}

function SetStageEvent(state, arg) {
return ProcessEvents(state, [{ fn: EndStage, arg }]);
}

function EndStageEvent(state) {
return ProcessEvents(state, [{ fn: EndStage }]);
}

function SetPhaseEvent(state, newPhase) {
return ProcessEvents(state, [
{
Expand Down Expand Up @@ -802,6 +865,8 @@ export function Flow({ moves, phases, endIf, turn, events, plugins }) {
}

const eventHandlers = {
endStage: EndStageEvent,
setStage: SetStageEvent,
endTurn: EndTurnEvent,
endPhase: EndPhaseEvent,
setPhase: SetPhaseEvent,
Expand All @@ -823,6 +888,10 @@ export function Flow({ moves, phases, endIf, turn, events, plugins }) {
if (events.setActivePlayers) {
enabledEvents['setActivePlayers'] = true;
}
if (events.endStage) {
enabledEvents['endStage'] = true;
enabledEvents['setStage'] = true;
}

return FlowInternal({
ctx: numPlayers => ({
Expand Down
Loading