From 659007a4341c0accf85b0c18df027af44ed43820 Mon Sep 17 00:00:00 2001 From: Nicolo Davis Date: Tue, 8 Jan 2019 22:44:08 +0800 Subject: [PATCH] pass game object to plugins --- src/core/flow.js | 18 ++++++------- src/core/game.js | 29 +++++++++------------ src/core/reducer.js | 4 +-- src/plugins/main.js | 62 ++++++++++++++++++++++----------------------- 4 files changed, 54 insertions(+), 59 deletions(-) diff --git a/src/core/flow.js b/src/core/flow.js index a6684261e..a81a0ba9f 100644 --- a/src/core/flow.js +++ b/src/core/flow.js @@ -190,7 +190,7 @@ export function Flow({ * @param {Array} redactedMoves - List of moves to be redacted * from the log. * - * @param {Array} plugins - List of plugins. + * @param {object} game - The game object. * * @param {...object} optimisticUpdate - (G, ctx, move) => boolean * Control whether a move should @@ -268,7 +268,7 @@ export function FlowWithPhases({ allowedMoves, redactedMoves, optimisticUpdate, - plugins, + game, }) { // Attach defaults. if (endPhase === undefined && phases) { @@ -286,8 +286,8 @@ export function FlowWithPhases({ if (optimisticUpdate === undefined) { optimisticUpdate = () => true; } - if (plugins === undefined) { - plugins = []; + if (game === undefined) { + game = { plugins: [] }; } if (!phases) phases = {}; if (!startingPhase) startingPhase = 'default'; @@ -317,11 +317,11 @@ export function FlowWithPhases({ if (conf.onPhaseBegin === undefined) { conf.onPhaseBegin = G => G; } - conf.onPhaseBegin = FnWrap(conf.onPhaseBegin, plugins); + conf.onPhaseBegin = FnWrap(conf.onPhaseBegin, game); if (conf.onPhaseEnd === undefined) { conf.onPhaseEnd = G => G; } - conf.onPhaseEnd = FnWrap(conf.onPhaseEnd, plugins); + conf.onPhaseEnd = FnWrap(conf.onPhaseEnd, game); if (conf.movesPerTurn === undefined) { conf.movesPerTurn = movesPerTurn; } @@ -334,15 +334,15 @@ export function FlowWithPhases({ if (conf.onTurnBegin === undefined) { conf.onTurnBegin = onTurnBegin; } - conf.onTurnBegin = FnWrap(conf.onTurnBegin, plugins); + conf.onTurnBegin = FnWrap(conf.onTurnBegin, game); if (conf.onTurnEnd === undefined) { conf.onTurnEnd = onTurnEnd; } - conf.onTurnEnd = FnWrap(conf.onTurnEnd, plugins); + conf.onTurnEnd = FnWrap(conf.onTurnEnd, game); if (conf.onMove === undefined) { conf.onMove = onMove; } - conf.onMove = FnWrap(conf.onMove, plugins); + conf.onMove = FnWrap(conf.onMove, game); if (conf.turnOrder === undefined) { conf.turnOrder = turnOrder; } diff --git a/src/core/game.js b/src/core/game.js index 64f718343..9f4862f29 100644 --- a/src/core/game.js +++ b/src/core/game.js @@ -92,30 +92,25 @@ import { FlowWithPhases } from './flow'; * setup: (G, ctx) => G, * } */ -function Game({ name, setup, moves, playerView, flow, seed, plugins }) { - if (name === undefined) name = 'default'; - if (setup === undefined) setup = () => ({}); - if (moves === undefined) moves = {}; - if (playerView === undefined) playerView = G => G; - if (plugins === undefined) plugins = []; +function Game(game) { + if (game.name === undefined) game.name = 'default'; + if (game.setup === undefined) game.setup = () => ({}); + if (game.moves === undefined) game.moves = {}; + if (game.playerView === undefined) game.playerView = G => G; + if (game.plugins === undefined) game.plugins = []; - if (!flow || flow.processGameEvent === undefined) { - flow = FlowWithPhases({ plugins, ...flow }); + if (!game.flow || game.flow.processGameEvent === undefined) { + game.flow = FlowWithPhases({ game, ...game.flow }); } return { - name, - setup, - playerView, - flow, - seed, - plugins, - moveNames: Object.getOwnPropertyNames(moves), + ...game, + moveNames: Object.getOwnPropertyNames(game.moves), processMove: (G, action, ctx) => { - if (moves.hasOwnProperty(action.type)) { + if (game.moves.hasOwnProperty(action.type)) { const ctxWithPlayerID = { ...ctx, playerID: action.playerID }; const args = [G, ctxWithPlayerID].concat(action.args); - const fn = FnWrap(moves[action.type], plugins); + const fn = FnWrap(game.moves[action.type], game); return fn(...args); } return G; diff --git a/src/core/reducer.js b/src/core/reducer.js index 77653e40f..437787116 100644 --- a/src/core/reducer.js +++ b/src/core/reducer.js @@ -128,7 +128,7 @@ export function CreateGameReducer({ ctx._random = { seed }; // Pass ctx through all the plugins that want to modify it. - ctx = plugins.SetupCtx(ctx, game.plugins); + ctx = plugins.SetupCtx(ctx, game); // Augment ctx with the enhancers (TODO: move these into plugins). const apiCtx = new ContextEnhancer(ctx, game, ctx.currentPlayer); @@ -137,7 +137,7 @@ export function CreateGameReducer({ let initialG = game.setup(ctxWithAPI, setupData); // Pass G through all the plugins that want to modify it. - initialG = plugins.SetupG(initialG, ctxWithAPI, game.plugins); + initialG = plugins.SetupG(initialG, ctxWithAPI, game); const initial = { // User managed state. diff --git a/src/plugins/main.js b/src/plugins/main.js index 80fd106da..fe2f452c0 100644 --- a/src/plugins/main.js +++ b/src/plugins/main.js @@ -17,13 +17,13 @@ const DEFAULT_PLUGINS = [PluginImmer]; * Applies the provided plugins to ctx during game setup. * * @param {object} ctx - The ctx object. - * @param {Array} plugins - Array of plugins. + * @param {object} game - The game object. */ -export const SetupCtx = (ctx, plugins) => { - [...DEFAULT_PLUGINS, ...plugins] +export const SetupCtx = (ctx, game) => { + [...DEFAULT_PLUGINS, ...game.plugins] .filter(plugin => plugin.setupCtx !== undefined) .forEach(plugin => { - ctx = plugin.setupCtx(ctx); + ctx = plugin.setupCtx(ctx, game); }); return ctx; }; @@ -33,13 +33,13 @@ export const SetupCtx = (ctx, plugins) => { * * @param {object} G - The G object. * @param {object} ctx - The ctx object. - * @param {Array} plugins - Array of plugins. + * @param {object} game - The game object. */ -export const SetupG = (G, ctx, plugins) => { - [...DEFAULT_PLUGINS, ...plugins] +export const SetupG = (G, ctx, game) => { + [...DEFAULT_PLUGINS, ...game.plugins] .filter(plugin => plugin.setupG !== undefined) .forEach(plugin => { - G = plugin.setupG(G, ctx); + G = plugin.setupG(G, ctx, game); }); return G; }; @@ -48,13 +48,13 @@ export const SetupG = (G, ctx, plugins) => { * Applies the provided plugins to ctx before processing a move / event. * * @param {object} ctx - The ctx object. - * @param {Array} plugins - Array of plugins. + * @param {object} game - The game object. */ -export const AddToCtx = (ctx, plugins) => { - [...DEFAULT_PLUGINS, ...plugins] +export const AddToCtx = (ctx, game) => { + [...DEFAULT_PLUGINS, ...game.plugins] .filter(plugin => plugin.addToCtx !== undefined) .forEach(plugin => { - ctx = plugin.addToCtx(ctx); + ctx = plugin.addToCtx(ctx, game); }); return ctx; }; @@ -63,13 +63,13 @@ export const AddToCtx = (ctx, plugins) => { * Removes the provided plugins to ctx after processing a move / event. * * @param {object} ctx - The ctx object. - * @param {Array} plugins - Array of plugins. + * @param {object} game - The game object. */ -export const RemoveFromCtx = (ctx, plugins) => { - [...DEFAULT_PLUGINS, ...plugins] +export const RemoveFromCtx = (ctx, game) => { + [...DEFAULT_PLUGINS, ...game.plugins] .filter(plugin => plugin.removeFromCtx !== undefined) .forEach(plugin => { - ctx = plugin.removeFromCtx(ctx); + ctx = plugin.removeFromCtx(ctx, game); }); return ctx; }; @@ -78,13 +78,13 @@ export const RemoveFromCtx = (ctx, plugins) => { * Applies the provided plugins to G before processing a move / event. * * @param {object} G - The G object. - * @param {Array} plugins - Array of plugins. + * @param {object} game - The game object. */ -export const AddToG = (G, plugins) => { - [...DEFAULT_PLUGINS, ...plugins] +export const AddToG = (G, game) => { + [...DEFAULT_PLUGINS, ...game.plugins] .filter(plugin => plugin.addToG !== undefined) .forEach(plugin => { - G = plugin.addToG(G); + G = plugin.addToG(G, game); }); return G; }; @@ -93,13 +93,13 @@ export const AddToG = (G, plugins) => { * Removes the provided plugins to G after processing a move / event. * * @param {object} G - The G object. - * @param {Array} plugins - Array of plugins. + * @param {object} game - The game object. */ -export const RemoveFromG = (G, plugins) => { - [...DEFAULT_PLUGINS, ...plugins] +export const RemoveFromG = (G, game) => { + [...DEFAULT_PLUGINS, ...game.plugins] .filter(plugin => plugin.removeFromG !== undefined) .forEach(plugin => { - G = plugin.removeFromG(G); + G = plugin.removeFromG(G, game); }); return G; }; @@ -108,20 +108,20 @@ export const RemoveFromG = (G, plugins) => { * Applies the provided plugins to the given move / flow function. * * @param {function} fn - The move function or trigger to apply the plugins to. - * @param {Array} plugins - Array of plugins. + * @param {object} game - The game object. */ -export const FnWrap = (fn, plugins) => { +export const FnWrap = (fn, game) => { const reducer = (acc, { fnWrap }) => fnWrap(acc); - const g = [...DEFAULT_PLUGINS, ...plugins] + const g = [...DEFAULT_PLUGINS, ...game.plugins] .filter(plugin => plugin.fnWrap !== undefined) .reduce(reducer, fn); return (G, ctx, ...args) => { - G = AddToG(G, plugins); - ctx = AddToCtx(ctx, plugins); + G = AddToG(G, game); + ctx = AddToCtx(ctx, game); G = g(G, ctx, ...args); - ctx = RemoveFromCtx(ctx, plugins); - ctx = RemoveFromG(G, plugins); + ctx = RemoveFromCtx(ctx, game); + ctx = RemoveFromG(G, game); return G; }; };