Skip to content

Commit ca5da32

Browse files
flungloafnicolodavis
authored andcommitted
Passing arbitrary data to game setup (#315)
* Add getting game setup data from request body in game creation api * rename gameSetupData to setupData
1 parent e0413de commit ca5da32

3 files changed

Lines changed: 42 additions & 4 deletions

File tree

src/core/reducer.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,12 @@ export class ContextEnhancer {
108108
* @param {...object} numPlayers - The number of players.
109109
* @param {...object} multiplayer - Set to true if we are in a multiplayer client.
110110
*/
111-
export function CreateGameReducer({ game, numPlayers, multiplayer }) {
111+
export function CreateGameReducer({
112+
game,
113+
numPlayers,
114+
multiplayer,
115+
setupData,
116+
}) {
112117
if (!numPlayers) {
113118
numPlayers = 2;
114119
}
@@ -124,7 +129,7 @@ export function CreateGameReducer({ game, numPlayers, multiplayer }) {
124129
const apiCtx = new ContextEnhancer(ctx, game, ctx.currentPlayer);
125130
let ctxWithAPI = apiCtx.attachToContext(ctx);
126131

127-
let initialG = game.setup(ctxWithAPI);
132+
let initialG = game.setup(ctxWithAPI, setupData);
128133
game.plugins.filter(plugin => plugin.setup !== undefined).forEach(plugin => {
129134
initialG = plugin.setup(initialG, ctxWithAPI);
130135
});

src/server/api.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ export const createApiServer = ({ db, games }) => {
8383
const reducer = CreateGameReducer({
8484
game,
8585
numPlayers,
86+
setupData: ctx.request.body.setupData,
8687
});
8788
const store = Redux.createStore(reducer);
8889
const state = store.getState();

src/server/api.test.js

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,17 @@ describe('.createApiServer', () => {
170170
db = {
171171
set: async (id, state) => setSpy(id, state),
172172
};
173-
games = [Game({ name: 'foo' })];
173+
games = [
174+
Game({
175+
name: 'foo',
176+
setup: (ctx, setupData) =>
177+
setupData
178+
? {
179+
colors: setupData.colors,
180+
}
181+
: {},
182+
}),
183+
];
174184
});
175185

176186
describe('for an unprotected lobby server', () => {
@@ -181,7 +191,15 @@ describe('.createApiServer', () => {
181191

182192
response = await request(app.callback())
183193
.post('/games/foo/create')
184-
.send('numPlayers=3');
194+
.send({
195+
numPlayers: 3,
196+
setupData: {
197+
colors: {
198+
'0': 'green',
199+
'1': 'red',
200+
},
201+
},
202+
});
185203
});
186204

187205
test('is successful', () => {
@@ -199,6 +217,20 @@ describe('.createApiServer', () => {
199217
);
200218
});
201219

220+
test('passes arbitrary data to game setup', () => {
221+
expect(setSpy).toHaveBeenCalledWith(
222+
expect.stringMatching('foo:'),
223+
expect.objectContaining({
224+
G: expect.objectContaining({
225+
colors: {
226+
'0': 'green',
227+
'1': 'red',
228+
},
229+
}),
230+
})
231+
);
232+
});
233+
202234
test('creates game metadata', () => {
203235
expect(setSpy).toHaveBeenCalledWith(
204236
expect.stringMatching(':metadata'),

0 commit comments

Comments
 (0)