Skip to content

Commit 659007a

Browse files
committed
pass game object to plugins
1 parent 6bce313 commit 659007a

4 files changed

Lines changed: 54 additions & 59 deletions

File tree

src/core/flow.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ export function Flow({
190190
* @param {Array} redactedMoves - List of moves to be redacted
191191
* from the log.
192192
*
193-
* @param {Array} plugins - List of plugins.
193+
* @param {object} game - The game object.
194194
*
195195
* @param {...object} optimisticUpdate - (G, ctx, move) => boolean
196196
* Control whether a move should
@@ -268,7 +268,7 @@ export function FlowWithPhases({
268268
allowedMoves,
269269
redactedMoves,
270270
optimisticUpdate,
271-
plugins,
271+
game,
272272
}) {
273273
// Attach defaults.
274274
if (endPhase === undefined && phases) {
@@ -286,8 +286,8 @@ export function FlowWithPhases({
286286
if (optimisticUpdate === undefined) {
287287
optimisticUpdate = () => true;
288288
}
289-
if (plugins === undefined) {
290-
plugins = [];
289+
if (game === undefined) {
290+
game = { plugins: [] };
291291
}
292292
if (!phases) phases = {};
293293
if (!startingPhase) startingPhase = 'default';
@@ -317,11 +317,11 @@ export function FlowWithPhases({
317317
if (conf.onPhaseBegin === undefined) {
318318
conf.onPhaseBegin = G => G;
319319
}
320-
conf.onPhaseBegin = FnWrap(conf.onPhaseBegin, plugins);
320+
conf.onPhaseBegin = FnWrap(conf.onPhaseBegin, game);
321321
if (conf.onPhaseEnd === undefined) {
322322
conf.onPhaseEnd = G => G;
323323
}
324-
conf.onPhaseEnd = FnWrap(conf.onPhaseEnd, plugins);
324+
conf.onPhaseEnd = FnWrap(conf.onPhaseEnd, game);
325325
if (conf.movesPerTurn === undefined) {
326326
conf.movesPerTurn = movesPerTurn;
327327
}
@@ -334,15 +334,15 @@ export function FlowWithPhases({
334334
if (conf.onTurnBegin === undefined) {
335335
conf.onTurnBegin = onTurnBegin;
336336
}
337-
conf.onTurnBegin = FnWrap(conf.onTurnBegin, plugins);
337+
conf.onTurnBegin = FnWrap(conf.onTurnBegin, game);
338338
if (conf.onTurnEnd === undefined) {
339339
conf.onTurnEnd = onTurnEnd;
340340
}
341-
conf.onTurnEnd = FnWrap(conf.onTurnEnd, plugins);
341+
conf.onTurnEnd = FnWrap(conf.onTurnEnd, game);
342342
if (conf.onMove === undefined) {
343343
conf.onMove = onMove;
344344
}
345-
conf.onMove = FnWrap(conf.onMove, plugins);
345+
conf.onMove = FnWrap(conf.onMove, game);
346346
if (conf.turnOrder === undefined) {
347347
conf.turnOrder = turnOrder;
348348
}

src/core/game.js

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -92,30 +92,25 @@ import { FlowWithPhases } from './flow';
9292
* setup: (G, ctx) => G,
9393
* }
9494
*/
95-
function Game({ name, setup, moves, playerView, flow, seed, plugins }) {
96-
if (name === undefined) name = 'default';
97-
if (setup === undefined) setup = () => ({});
98-
if (moves === undefined) moves = {};
99-
if (playerView === undefined) playerView = G => G;
100-
if (plugins === undefined) plugins = [];
95+
function Game(game) {
96+
if (game.name === undefined) game.name = 'default';
97+
if (game.setup === undefined) game.setup = () => ({});
98+
if (game.moves === undefined) game.moves = {};
99+
if (game.playerView === undefined) game.playerView = G => G;
100+
if (game.plugins === undefined) game.plugins = [];
101101

102-
if (!flow || flow.processGameEvent === undefined) {
103-
flow = FlowWithPhases({ plugins, ...flow });
102+
if (!game.flow || game.flow.processGameEvent === undefined) {
103+
game.flow = FlowWithPhases({ game, ...game.flow });
104104
}
105105

106106
return {
107-
name,
108-
setup,
109-
playerView,
110-
flow,
111-
seed,
112-
plugins,
113-
moveNames: Object.getOwnPropertyNames(moves),
107+
...game,
108+
moveNames: Object.getOwnPropertyNames(game.moves),
114109
processMove: (G, action, ctx) => {
115-
if (moves.hasOwnProperty(action.type)) {
110+
if (game.moves.hasOwnProperty(action.type)) {
116111
const ctxWithPlayerID = { ...ctx, playerID: action.playerID };
117112
const args = [G, ctxWithPlayerID].concat(action.args);
118-
const fn = FnWrap(moves[action.type], plugins);
113+
const fn = FnWrap(game.moves[action.type], game);
119114
return fn(...args);
120115
}
121116
return G;

src/core/reducer.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ export function CreateGameReducer({
128128
ctx._random = { seed };
129129

130130
// Pass ctx through all the plugins that want to modify it.
131-
ctx = plugins.SetupCtx(ctx, game.plugins);
131+
ctx = plugins.SetupCtx(ctx, game);
132132

133133
// Augment ctx with the enhancers (TODO: move these into plugins).
134134
const apiCtx = new ContextEnhancer(ctx, game, ctx.currentPlayer);
@@ -137,7 +137,7 @@ export function CreateGameReducer({
137137
let initialG = game.setup(ctxWithAPI, setupData);
138138

139139
// Pass G through all the plugins that want to modify it.
140-
initialG = plugins.SetupG(initialG, ctxWithAPI, game.plugins);
140+
initialG = plugins.SetupG(initialG, ctxWithAPI, game);
141141

142142
const initial = {
143143
// User managed state.

src/plugins/main.js

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ const DEFAULT_PLUGINS = [PluginImmer];
1717
* Applies the provided plugins to ctx during game setup.
1818
*
1919
* @param {object} ctx - The ctx object.
20-
* @param {Array} plugins - Array of plugins.
20+
* @param {object} game - The game object.
2121
*/
22-
export const SetupCtx = (ctx, plugins) => {
23-
[...DEFAULT_PLUGINS, ...plugins]
22+
export const SetupCtx = (ctx, game) => {
23+
[...DEFAULT_PLUGINS, ...game.plugins]
2424
.filter(plugin => plugin.setupCtx !== undefined)
2525
.forEach(plugin => {
26-
ctx = plugin.setupCtx(ctx);
26+
ctx = plugin.setupCtx(ctx, game);
2727
});
2828
return ctx;
2929
};
@@ -33,13 +33,13 @@ export const SetupCtx = (ctx, plugins) => {
3333
*
3434
* @param {object} G - The G object.
3535
* @param {object} ctx - The ctx object.
36-
* @param {Array} plugins - Array of plugins.
36+
* @param {object} game - The game object.
3737
*/
38-
export const SetupG = (G, ctx, plugins) => {
39-
[...DEFAULT_PLUGINS, ...plugins]
38+
export const SetupG = (G, ctx, game) => {
39+
[...DEFAULT_PLUGINS, ...game.plugins]
4040
.filter(plugin => plugin.setupG !== undefined)
4141
.forEach(plugin => {
42-
G = plugin.setupG(G, ctx);
42+
G = plugin.setupG(G, ctx, game);
4343
});
4444
return G;
4545
};
@@ -48,13 +48,13 @@ export const SetupG = (G, ctx, plugins) => {
4848
* Applies the provided plugins to ctx before processing a move / event.
4949
*
5050
* @param {object} ctx - The ctx object.
51-
* @param {Array} plugins - Array of plugins.
51+
* @param {object} game - The game object.
5252
*/
53-
export const AddToCtx = (ctx, plugins) => {
54-
[...DEFAULT_PLUGINS, ...plugins]
53+
export const AddToCtx = (ctx, game) => {
54+
[...DEFAULT_PLUGINS, ...game.plugins]
5555
.filter(plugin => plugin.addToCtx !== undefined)
5656
.forEach(plugin => {
57-
ctx = plugin.addToCtx(ctx);
57+
ctx = plugin.addToCtx(ctx, game);
5858
});
5959
return ctx;
6060
};
@@ -63,13 +63,13 @@ export const AddToCtx = (ctx, plugins) => {
6363
* Removes the provided plugins to ctx after processing a move / event.
6464
*
6565
* @param {object} ctx - The ctx object.
66-
* @param {Array} plugins - Array of plugins.
66+
* @param {object} game - The game object.
6767
*/
68-
export const RemoveFromCtx = (ctx, plugins) => {
69-
[...DEFAULT_PLUGINS, ...plugins]
68+
export const RemoveFromCtx = (ctx, game) => {
69+
[...DEFAULT_PLUGINS, ...game.plugins]
7070
.filter(plugin => plugin.removeFromCtx !== undefined)
7171
.forEach(plugin => {
72-
ctx = plugin.removeFromCtx(ctx);
72+
ctx = plugin.removeFromCtx(ctx, game);
7373
});
7474
return ctx;
7575
};
@@ -78,13 +78,13 @@ export const RemoveFromCtx = (ctx, plugins) => {
7878
* Applies the provided plugins to G before processing a move / event.
7979
*
8080
* @param {object} G - The G object.
81-
* @param {Array} plugins - Array of plugins.
81+
* @param {object} game - The game object.
8282
*/
83-
export const AddToG = (G, plugins) => {
84-
[...DEFAULT_PLUGINS, ...plugins]
83+
export const AddToG = (G, game) => {
84+
[...DEFAULT_PLUGINS, ...game.plugins]
8585
.filter(plugin => plugin.addToG !== undefined)
8686
.forEach(plugin => {
87-
G = plugin.addToG(G);
87+
G = plugin.addToG(G, game);
8888
});
8989
return G;
9090
};
@@ -93,13 +93,13 @@ export const AddToG = (G, plugins) => {
9393
* Removes the provided plugins to G after processing a move / event.
9494
*
9595
* @param {object} G - The G object.
96-
* @param {Array} plugins - Array of plugins.
96+
* @param {object} game - The game object.
9797
*/
98-
export const RemoveFromG = (G, plugins) => {
99-
[...DEFAULT_PLUGINS, ...plugins]
98+
export const RemoveFromG = (G, game) => {
99+
[...DEFAULT_PLUGINS, ...game.plugins]
100100
.filter(plugin => plugin.removeFromG !== undefined)
101101
.forEach(plugin => {
102-
G = plugin.removeFromG(G);
102+
G = plugin.removeFromG(G, game);
103103
});
104104
return G;
105105
};
@@ -108,20 +108,20 @@ export const RemoveFromG = (G, plugins) => {
108108
* Applies the provided plugins to the given move / flow function.
109109
*
110110
* @param {function} fn - The move function or trigger to apply the plugins to.
111-
* @param {Array} plugins - Array of plugins.
111+
* @param {object} game - The game object.
112112
*/
113-
export const FnWrap = (fn, plugins) => {
113+
export const FnWrap = (fn, game) => {
114114
const reducer = (acc, { fnWrap }) => fnWrap(acc);
115-
const g = [...DEFAULT_PLUGINS, ...plugins]
115+
const g = [...DEFAULT_PLUGINS, ...game.plugins]
116116
.filter(plugin => plugin.fnWrap !== undefined)
117117
.reduce(reducer, fn);
118118

119119
return (G, ctx, ...args) => {
120-
G = AddToG(G, plugins);
121-
ctx = AddToCtx(ctx, plugins);
120+
G = AddToG(G, game);
121+
ctx = AddToCtx(ctx, game);
122122
G = g(G, ctx, ...args);
123-
ctx = RemoveFromCtx(ctx, plugins);
124-
ctx = RemoveFromG(G, plugins);
123+
ctx = RemoveFromCtx(ctx, game);
124+
ctx = RemoveFromG(G, game);
125125
return G;
126126
};
127127
};

0 commit comments

Comments
 (0)