Skip to content

Commit

Permalink
move event disablers inside separate section in config
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolodavis committed Sep 10, 2019
1 parent 67be0a9 commit b9ce7f1
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 59 deletions.
14 changes: 9 additions & 5 deletions src/client/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -318,9 +318,11 @@ describe('event dispatchers', () => {

test('all events', () => {
const game = {
endPhase: true,
endGame: true,
setActionPlayers: true,
events: {
endPhase: true,
endGame: true,
setActionPlayers: true,
},
};
const client = Client({ game });
expect(Object.keys(client.events)).toEqual([
Expand All @@ -336,8 +338,10 @@ describe('event dispatchers', () => {

test('no events', () => {
const game = {
endPhase: false,
endTurn: false,
events: {
endPhase: false,
endTurn: false,
},
};
const client = Client({ game });
expect(Object.keys(client.events)).toEqual([]);
Expand Down
113 changes: 59 additions & 54 deletions src/core/flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ import * as logging from './logger';
* @param {...object} ctx - Function with the signature
* numPlayers => ctx
* that determines the initial value of ctx.
* @param {...object} events - Object containing functions
* named after events that this
* reducer will handle. Each function
* has the following signature:
* ({G, ctx}) => {G, ctx}
* @param {...object} eventHandlers - Object containing functions
* named after events that this
* reducer will handle. Each function
* has the following signature:
* ({G, ctx}) => {G, ctx}
* @param {...object} enabledEvents - Map of eventName -> bool indicating
* which events are callable from the client
* or from within moves.
Expand All @@ -41,21 +41,21 @@ import * as logging from './logger';
*/
export function Flow({
ctx,
events,
eventHandlers,
enabledEvents,
init,
processMove,
moveMap,
}) {
if (!ctx) ctx = () => ({});
if (!events) events = {};
if (!eventHandlers) eventHandlers = {};
if (!enabledEvents) enabledEvents = {};
if (!init) init = state => state;
if (!processMove) processMove = state => state;

const dispatch = (state, action) => {
const { payload } = action;
if (events.hasOwnProperty(payload.type)) {
if (eventHandlers.hasOwnProperty(payload.type)) {
const context = { playerID: payload.playerID, dispatch };
const logEntry = {
action,
Expand All @@ -66,7 +66,7 @@ export function Flow({
const deltalog = [...(state.deltalog || []), logEntry];
state = { ...state, deltalog };
const args = [state].concat(payload.args);
return events[payload.type].apply(context, args);
return eventHandlers[payload.type].apply(context, args);
}
return state;
};
Expand All @@ -76,7 +76,7 @@ export function Flow({
init,
moveMap,

eventNames: Object.getOwnPropertyNames(events),
eventNames: Object.getOwnPropertyNames(eventHandlers),
enabledEventNames: Object.getOwnPropertyNames(enabledEvents),

processMove: (state, action) => {
Expand Down Expand Up @@ -111,44 +111,49 @@ export function Flow({
* - A move whitelist that disallows other moves during the phase.
*
* @param {...object} endIf - The game automatically ends if this function
* returns anything (checked after each move).
* The return value is available at ctx.gameover.
* (G, ctx) => {}
* returns anything (checked after each move).
* The return value is available at ctx.gameover.
* (G, ctx) => {}
*
* @param {...object} turn - Customize the turn structure (see turn-order.js).
*
* {
* // The turn order.
* order: TurnOrder.DEFAULT,
* {
* // The turn order.
* order: TurnOrder.DEFAULT,
*
* // Code to run at the beginning of the turn.
* onBegin: (G, ctx) => G,
*
* // Code to run at the end of the turn.
* onEnd: (G, ctx) => G,
*
* // Code to run at the beginning of the turn.
* onBegin: (G, ctx) => G,
* // The turn automatically ends if this returns a truthy
* // value (checked after each move).
* // If the return value is { next: playerID },
* // then that player is the next player
* // instead of following the turn order.
* endIf: (G, ctx) => boolean|object,
*
* // Code to run at the end of the turn.
* onEnd: (G, ctx) => G,
* // End the turn automatically after a certain number
* // of moves.
* moveLimit: 1,
*
* // The turn automatically ends if this returns a truthy
* // value (checked after each move).
* // If the return value is { next: playerID },
* // then that player is the next player
* // instead of following the turn order.
* endIf: (G, ctx) => boolean|object,
* // Code to run at the end of a move.
* onMove: (G, ctx, { type: 'moveName', args: [] }) => G
* }
*
* // End the turn automatically after a certain number
* // of moves.
* moveLimit: 1,
* @param {...object} events - Section that allows enabling / disabling events.
*
* // Code to run at the end of a move.
* onMove: (G, ctx, { type: 'moveName', args: [] }) => G
* }
* {
* endTurn - Set to false to disable the `endTurn` event.
*
* @param {...object} endTurn - Set to false to disable the `endTurn` event.
* endPhase - Set to false to disable the `endPhase` event.
*
* @param {...object} endPhase - Set to false to disable the `endPhase` event.
* endGame - Set to true to enable the `endGame` event.
*
* @param {...object} endGame - Set to true to enable the `endGame` event.
* setActionPlayers - Set to true to enable the `setActionPlayers` event.
* }
*
* @param {...object} setActionPlayers - Set to true to enable the `setActionPlayers` event.
*
* @param {...object} phases - A map of phases in the game.
*
Expand Down Expand Up @@ -176,24 +181,24 @@ export function FlowWithPhases({
startingPhase,
endIf,
turn,
endTurn,
endPhase,
endGame,
setActionPlayers,
events,
plugins,
}) {
// Attach defaults.
if (endPhase === undefined && phases) {
endPhase = true;
if (events === undefined) {
events = {};
}
if (events.endPhase === undefined && phases) {
events.endPhase = true;
}
if (endTurn === undefined) {
endTurn = true;
if (events.endTurn === undefined) {
events.endTurn = true;
}
if (endGame === undefined) {
endGame = false;
if (events.endGame === undefined) {
events.endGame = false;
}
if (setActionPlayers === undefined) {
setActionPlayers = false;
if (events.setActionPlayers === undefined) {
events.setActionPlayers = false;
}
if (plugins === undefined) {
plugins = [];
Expand Down Expand Up @@ -572,24 +577,24 @@ export function FlowWithPhases({
return state;
}

const events = {
const eventHandlers = {
endTurn: endTurnEvent,
endPhase: endPhaseEvent,
endGame: endGameEvent,
setActionPlayers: SetActionPlayersEvent,
};

let enabledEvents = {};
if (endTurn) {
if (events.endTurn) {
enabledEvents['endTurn'] = true;
}
if (endPhase) {
if (events.endPhase) {
enabledEvents['endPhase'] = true;
}
if (endGame) {
if (events.endGame) {
enabledEvents['endGame'] = true;
}
if (setActionPlayers) {
if (events.setActionPlayers) {
enabledEvents['setActionPlayers'] = true;
}

Expand All @@ -611,7 +616,7 @@ export function FlowWithPhases({
init: state => {
return startGame(state);
},
events,
eventHandlers,
enabledEvents,
processMove,
moveMap,
Expand Down

0 comments on commit b9ce7f1

Please sign in to comment.