boardgameio / boardgame.io

State Management and Multiplayer Networking for Turn-Based Games
https://boardgame.io
MIT License
9.99k stars 704 forks source link

New Plugin API #603

Closed delucis closed 4 years ago

delucis commented 4 years ago

Following on from working on #598, I have a question about plugin configuration.

The player plugin expects a playerSetup function in the game config. Do you think there should be a more specific namespace for plugin configuration? Or a way of cleanly associating plugins with their configuration, so that methods don’t clutter up the main game config object? For example, a pluginsConfig object could be keyed by plugin name, and hand the value of that field to the plugin methods:

// game configuration
const game = {
  plugins: [PluginPlayer],
  pluginsConfig: {
    player: {
      setup: (playerID) => ({}),
    },
  },
}

// plugins/plugin-player
setup: ({ ctx, config }) => {
  // use config.setup()
},

This could also make it easier to enforce correct types for Typescript users. For now, I allowed any arbitrary key–value pair in the game config, but that’s not great, because it reduces type-checking for misspelled keys.

An alternative might be to encourage a pattern where plugins that need configuring provide a plugin factory that takes the configuration options and returns a plugin, with no need for custom fields in the game config:

const game = {
  plugins: [
    PluginPlayer(setupFn),
  ],
}
nicolodavis commented 4 years ago

Yes, I think we should stop allowing arbitrary keys in the game config.

The pattern of passing in args to a plugin factory sounds good to me, and would require almost no changes to boardgame.io. We'd just need to migrate PluginPlayer.