Skip to content

Commit c1b4a03

Browse files
committed
add playerSetup option to PluginPlayer
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).
1 parent 140267b commit c1b4a03

3 files changed

Lines changed: 27 additions & 18 deletions

File tree

docs/plugins.md

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,9 @@ that they are specified (from left to right).
8080
```js
8181
import { PluginPlayer } from 'boardgame.io/plugins';
8282

83-
function PlayerState(playerID) {
84-
return { ... };
85-
}
86-
8783
Game({
88-
plugins: [PluginPlayer(PlayerState)],
84+
playerSetup: (playerID) => ({ ... }),
85+
plugins: [PluginPlayer],
8986
})
9087
```
9188

@@ -104,9 +101,7 @@ G: {
104101
}
105102
```
106103

107-
The initial values of these states are determined by the parameter
108-
`PlayerState`, which is a function that returns the state for a
109-
particular `playerID`.
104+
The initial values of these states are determined by the `playerSetup` function, which creates the state for a particular `playerID`.
110105

111106
Before each move, the plugin makes the state associated with the
112107
current player available at `G.player`. If this is a 2 player game,

src/plugins/plugin-player.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*
1414
* @param {function} initPlayerState - Function of type (playerID) => playerState.
1515
*/
16-
export default initPlayerState => ({
16+
export default {
1717
fnWrap: moveFn => {
1818
return (G, ctx, ...args) => {
1919
const current = ctx.currentPlayer;
@@ -49,16 +49,16 @@ export default initPlayerState => ({
4949
},
5050

5151
G: {
52-
setup: (G, ctx) => {
52+
setup: (G, ctx, game) => {
5353
let players = {};
5454
for (let i = 0; i < ctx.numPlayers; i++) {
55-
const playerState = {};
56-
if (initPlayerState !== undefined) {
57-
initPlayerState(i + '');
55+
let playerState = {};
56+
if (game.playerSetup !== undefined) {
57+
playerState = game.playerSetup(i + '');
5858
}
5959
players[i + ''] = playerState;
6060
}
6161
return { ...G, players };
6262
},
6363
},
64-
});
64+
};

src/plugins/plugin-player.test.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,22 @@ import { makeMove, gameEvent } from '../core/action-creators';
1313

1414
describe('default values', () => {
1515
test('playerState is not passed', () => {
16-
const value = PluginPlayer().G.setup({}, { numPlayers: 2 });
17-
expect(value).toEqual({ players: { '0': {}, '1': {} } });
16+
const game = Game({
17+
plugins: [PluginPlayer],
18+
});
19+
const reducer = CreateGameReducer({ game });
20+
const state = reducer(undefined, { type: 'init' });
21+
expect(state.G).toEqual({ players: { '0': {}, '1': {} } });
22+
});
23+
24+
test('playerState is passed', () => {
25+
const game = Game({
26+
playerSetup: () => ({ A: 1 }),
27+
plugins: [PluginPlayer],
28+
});
29+
const reducer = CreateGameReducer({ game });
30+
const state = reducer(undefined, { type: 'init' });
31+
expect(state.G).toEqual({ players: { '0': { A: 1 }, '1': { A: 1 } } });
1832
});
1933
});
2034

@@ -32,7 +46,7 @@ describe('2 player game', () => {
3246
},
3347
},
3448

35-
plugins: [PluginPlayer(() => ({}))],
49+
plugins: [PluginPlayer],
3650
});
3751

3852
reducer = CreateGameReducer({ game });
@@ -75,7 +89,7 @@ describe('3 player game', () => {
7589
},
7690
},
7791

78-
plugins: [PluginPlayer()],
92+
plugins: [PluginPlayer],
7993
});
8094

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

0 commit comments

Comments
 (0)