Skip to content

Commit

Permalink
add playerSetup option to PluginPlayer
Browse files Browse the repository at this point in the history
This is a new field in the game object that is used to
determine the initial player states (as opposed to a
parameter that was passed to PluginPlayer itself). As
a result, PluginPlayer is now an actual plugin object (as opposed
to a function that returns a plugin).
  • Loading branch information
nicolodavis committed Jan 9, 2019
1 parent 140267b commit c1b4a03
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 18 deletions.
11 changes: 3 additions & 8 deletions docs/plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,9 @@ that they are specified (from left to right).
```js
import { PluginPlayer } from 'boardgame.io/plugins';

function PlayerState(playerID) {
return { ... };
}

Game({
plugins: [PluginPlayer(PlayerState)],
playerSetup: (playerID) => ({ ... }),
plugins: [PluginPlayer],
})
```

Expand All @@ -104,9 +101,7 @@ G: {
}
```

The initial values of these states are determined by the parameter
`PlayerState`, which is a function that returns the state for a
particular `playerID`.
The initial values of these states are determined by the `playerSetup` function, which creates the state for a particular `playerID`.

Before each move, the plugin makes the state associated with the
current player available at `G.player`. If this is a 2 player game,
Expand Down
12 changes: 6 additions & 6 deletions src/plugins/plugin-player.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*
* @param {function} initPlayerState - Function of type (playerID) => playerState.
*/
export default initPlayerState => ({
export default {
fnWrap: moveFn => {
return (G, ctx, ...args) => {
const current = ctx.currentPlayer;
Expand Down Expand Up @@ -49,16 +49,16 @@ export default initPlayerState => ({
},

G: {
setup: (G, ctx) => {
setup: (G, ctx, game) => {
let players = {};
for (let i = 0; i < ctx.numPlayers; i++) {
const playerState = {};
if (initPlayerState !== undefined) {
initPlayerState(i + '');
let playerState = {};
if (game.playerSetup !== undefined) {
playerState = game.playerSetup(i + '');
}
players[i + ''] = playerState;
}
return { ...G, players };
},
},
});
};
22 changes: 18 additions & 4 deletions src/plugins/plugin-player.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,22 @@ import { makeMove, gameEvent } from '../core/action-creators';

describe('default values', () => {
test('playerState is not passed', () => {
const value = PluginPlayer().G.setup({}, { numPlayers: 2 });
expect(value).toEqual({ players: { '0': {}, '1': {} } });
const game = Game({
plugins: [PluginPlayer],
});
const reducer = CreateGameReducer({ game });
const state = reducer(undefined, { type: 'init' });
expect(state.G).toEqual({ players: { '0': {}, '1': {} } });
});

test('playerState is passed', () => {
const game = Game({
playerSetup: () => ({ A: 1 }),
plugins: [PluginPlayer],
});
const reducer = CreateGameReducer({ game });
const state = reducer(undefined, { type: 'init' });
expect(state.G).toEqual({ players: { '0': { A: 1 }, '1': { A: 1 } } });
});
});

Expand All @@ -32,7 +46,7 @@ describe('2 player game', () => {
},
},

plugins: [PluginPlayer(() => ({}))],
plugins: [PluginPlayer],
});

reducer = CreateGameReducer({ game });
Expand Down Expand Up @@ -75,7 +89,7 @@ describe('3 player game', () => {
},
},

plugins: [PluginPlayer()],
plugins: [PluginPlayer],
});

reducer = CreateGameReducer({ game, numPlayers: 3 });
Expand Down

0 comments on commit c1b4a03

Please sign in to comment.