Skip to content

Commit b9ce7f1

Browse files
committed
move event disablers inside separate section in config
1 parent 67be0a9 commit b9ce7f1

2 files changed

Lines changed: 68 additions & 59 deletions

File tree

src/client/client.test.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -318,9 +318,11 @@ describe('event dispatchers', () => {
318318

319319
test('all events', () => {
320320
const game = {
321-
endPhase: true,
322-
endGame: true,
323-
setActionPlayers: true,
321+
events: {
322+
endPhase: true,
323+
endGame: true,
324+
setActionPlayers: true,
325+
},
324326
};
325327
const client = Client({ game });
326328
expect(Object.keys(client.events)).toEqual([
@@ -336,8 +338,10 @@ describe('event dispatchers', () => {
336338

337339
test('no events', () => {
338340
const game = {
339-
endPhase: false,
340-
endTurn: false,
341+
events: {
342+
endPhase: false,
343+
endTurn: false,
344+
},
341345
};
342346
const client = Client({ game });
343347
expect(Object.keys(client.events)).toEqual([]);

src/core/flow.js

Lines changed: 59 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ import * as logging from './logger';
2828
* @param {...object} ctx - Function with the signature
2929
* numPlayers => ctx
3030
* that determines the initial value of ctx.
31-
* @param {...object} events - Object containing functions
32-
* named after events that this
33-
* reducer will handle. Each function
34-
* has the following signature:
35-
* ({G, ctx}) => {G, ctx}
31+
* @param {...object} eventHandlers - Object containing functions
32+
* named after events that this
33+
* reducer will handle. Each function
34+
* has the following signature:
35+
* ({G, ctx}) => {G, ctx}
3636
* @param {...object} enabledEvents - Map of eventName -> bool indicating
3737
* which events are callable from the client
3838
* or from within moves.
@@ -41,21 +41,21 @@ import * as logging from './logger';
4141
*/
4242
export function Flow({
4343
ctx,
44-
events,
44+
eventHandlers,
4545
enabledEvents,
4646
init,
4747
processMove,
4848
moveMap,
4949
}) {
5050
if (!ctx) ctx = () => ({});
51-
if (!events) events = {};
51+
if (!eventHandlers) eventHandlers = {};
5252
if (!enabledEvents) enabledEvents = {};
5353
if (!init) init = state => state;
5454
if (!processMove) processMove = state => state;
5555

5656
const dispatch = (state, action) => {
5757
const { payload } = action;
58-
if (events.hasOwnProperty(payload.type)) {
58+
if (eventHandlers.hasOwnProperty(payload.type)) {
5959
const context = { playerID: payload.playerID, dispatch };
6060
const logEntry = {
6161
action,
@@ -66,7 +66,7 @@ export function Flow({
6666
const deltalog = [...(state.deltalog || []), logEntry];
6767
state = { ...state, deltalog };
6868
const args = [state].concat(payload.args);
69-
return events[payload.type].apply(context, args);
69+
return eventHandlers[payload.type].apply(context, args);
7070
}
7171
return state;
7272
};
@@ -76,7 +76,7 @@ export function Flow({
7676
init,
7777
moveMap,
7878

79-
eventNames: Object.getOwnPropertyNames(events),
79+
eventNames: Object.getOwnPropertyNames(eventHandlers),
8080
enabledEventNames: Object.getOwnPropertyNames(enabledEvents),
8181

8282
processMove: (state, action) => {
@@ -111,44 +111,49 @@ export function Flow({
111111
* - A move whitelist that disallows other moves during the phase.
112112
*
113113
* @param {...object} endIf - The game automatically ends if this function
114-
* returns anything (checked after each move).
115-
* The return value is available at ctx.gameover.
116-
* (G, ctx) => {}
114+
* returns anything (checked after each move).
115+
* The return value is available at ctx.gameover.
116+
* (G, ctx) => {}
117117
*
118118
* @param {...object} turn - Customize the turn structure (see turn-order.js).
119119
*
120-
* {
121-
* // The turn order.
122-
* order: TurnOrder.DEFAULT,
120+
* {
121+
* // The turn order.
122+
* order: TurnOrder.DEFAULT,
123+
*
124+
* // Code to run at the beginning of the turn.
125+
* onBegin: (G, ctx) => G,
126+
*
127+
* // Code to run at the end of the turn.
128+
* onEnd: (G, ctx) => G,
123129
*
124-
* // Code to run at the beginning of the turn.
125-
* onBegin: (G, ctx) => G,
130+
* // The turn automatically ends if this returns a truthy
131+
* // value (checked after each move).
132+
* // If the return value is { next: playerID },
133+
* // then that player is the next player
134+
* // instead of following the turn order.
135+
* endIf: (G, ctx) => boolean|object,
126136
*
127-
* // Code to run at the end of the turn.
128-
* onEnd: (G, ctx) => G,
137+
* // End the turn automatically after a certain number
138+
* // of moves.
139+
* moveLimit: 1,
129140
*
130-
* // The turn automatically ends if this returns a truthy
131-
* // value (checked after each move).
132-
* // If the return value is { next: playerID },
133-
* // then that player is the next player
134-
* // instead of following the turn order.
135-
* endIf: (G, ctx) => boolean|object,
141+
* // Code to run at the end of a move.
142+
* onMove: (G, ctx, { type: 'moveName', args: [] }) => G
143+
* }
136144
*
137-
* // End the turn automatically after a certain number
138-
* // of moves.
139-
* moveLimit: 1,
145+
* @param {...object} events - Section that allows enabling / disabling events.
140146
*
141-
* // Code to run at the end of a move.
142-
* onMove: (G, ctx, { type: 'moveName', args: [] }) => G
143-
* }
147+
* {
148+
* endTurn - Set to false to disable the `endTurn` event.
144149
*
145-
* @param {...object} endTurn - Set to false to disable the `endTurn` event.
150+
* endPhase - Set to false to disable the `endPhase` event.
146151
*
147-
* @param {...object} endPhase - Set to false to disable the `endPhase` event.
152+
* endGame - Set to true to enable the `endGame` event.
148153
*
149-
* @param {...object} endGame - Set to true to enable the `endGame` event.
154+
* setActionPlayers - Set to true to enable the `setActionPlayers` event.
155+
* }
150156
*
151-
* @param {...object} setActionPlayers - Set to true to enable the `setActionPlayers` event.
152157
*
153158
* @param {...object} phases - A map of phases in the game.
154159
*
@@ -176,24 +181,24 @@ export function FlowWithPhases({
176181
startingPhase,
177182
endIf,
178183
turn,
179-
endTurn,
180-
endPhase,
181-
endGame,
182-
setActionPlayers,
184+
events,
183185
plugins,
184186
}) {
185187
// Attach defaults.
186-
if (endPhase === undefined && phases) {
187-
endPhase = true;
188+
if (events === undefined) {
189+
events = {};
190+
}
191+
if (events.endPhase === undefined && phases) {
192+
events.endPhase = true;
188193
}
189-
if (endTurn === undefined) {
190-
endTurn = true;
194+
if (events.endTurn === undefined) {
195+
events.endTurn = true;
191196
}
192-
if (endGame === undefined) {
193-
endGame = false;
197+
if (events.endGame === undefined) {
198+
events.endGame = false;
194199
}
195-
if (setActionPlayers === undefined) {
196-
setActionPlayers = false;
200+
if (events.setActionPlayers === undefined) {
201+
events.setActionPlayers = false;
197202
}
198203
if (plugins === undefined) {
199204
plugins = [];
@@ -572,24 +577,24 @@ export function FlowWithPhases({
572577
return state;
573578
}
574579

575-
const events = {
580+
const eventHandlers = {
576581
endTurn: endTurnEvent,
577582
endPhase: endPhaseEvent,
578583
endGame: endGameEvent,
579584
setActionPlayers: SetActionPlayersEvent,
580585
};
581586

582587
let enabledEvents = {};
583-
if (endTurn) {
588+
if (events.endTurn) {
584589
enabledEvents['endTurn'] = true;
585590
}
586-
if (endPhase) {
591+
if (events.endPhase) {
587592
enabledEvents['endPhase'] = true;
588593
}
589-
if (endGame) {
594+
if (events.endGame) {
590595
enabledEvents['endGame'] = true;
591596
}
592-
if (setActionPlayers) {
597+
if (events.setActionPlayers) {
593598
enabledEvents['setActionPlayers'] = true;
594599
}
595600

@@ -611,7 +616,7 @@ export function FlowWithPhases({
611616
init: state => {
612617
return startGame(state);
613618
},
614-
events,
619+
eventHandlers,
615620
enabledEvents,
616621
processMove,
617622
moveMap,

0 commit comments

Comments
 (0)