Skip to content

Commit

Permalink
pass game object to plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolodavis committed Jan 8, 2019
1 parent 6bce313 commit 659007a
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 59 deletions.
18 changes: 9 additions & 9 deletions src/core/flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -268,7 +268,7 @@ export function FlowWithPhases({
allowedMoves,
redactedMoves,
optimisticUpdate,
plugins,
game,
}) {
// Attach defaults.
if (endPhase === undefined && phases) {
Expand All @@ -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';
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down
29 changes: 12 additions & 17 deletions src/core/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/core/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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.
Expand Down
62 changes: 31 additions & 31 deletions src/plugins/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand All @@ -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;
};
Expand All @@ -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;
};
Expand All @@ -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;
};
Expand All @@ -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;
};
Expand All @@ -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;
};
Expand All @@ -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;
};
};

0 comments on commit 659007a

Please sign in to comment.