Skip to content

Commit 3c2aadd

Browse files
authored
fix(client): Don’t run playerView locally on multiplayer clients (#819)
* fix(client): Fix playerView executed twice do not strip again if is multiplayer * Address review changes
1 parent 6b6b175 commit 3c2aadd

2 files changed

Lines changed: 45 additions & 1 deletion

File tree

src/client/client.test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,46 @@ describe('multiplayer', () => {
261261
});
262262
});
263263

264+
describe('strip secret only on server', () => {
265+
let client0;
266+
let client1;
267+
let spec;
268+
let initial = { secret: [1, 2, 3, 4], sum: 0 };
269+
beforeAll(() => {
270+
spec = {
271+
game: {
272+
setup: () => initial,
273+
playerView: (G, ctx, playerID) => {
274+
let r = { ...G };
275+
r.sum = r.secret.reduce((prev, curr) => {
276+
return prev + curr;
277+
});
278+
delete r.secret;
279+
return r;
280+
},
281+
moves: { A: (G, ctx) => ({ A: ctx.playerID }) },
282+
},
283+
multiplayer: Local(),
284+
};
285+
286+
client0 = Client({ ...spec, playerID: '0' });
287+
client1 = Client({ ...spec, playerID: '1' });
288+
289+
client0.start();
290+
client1.start();
291+
});
292+
293+
test('secret stripped', () => {
294+
expect(client0.getState().G).toEqual({ sum: 10 });
295+
expect(client1.getState().G).toEqual({ sum: 10 });
296+
});
297+
298+
afterAll(() => {
299+
client0.stop();
300+
client1.stop();
301+
});
302+
});
303+
264304
test('accepts enhancer for store', () => {
265305
let spyDispatcher;
266306
const spyEnhancer = vanillaCreateStore => (...args) => {

src/client/client.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,11 @@ export class _ClientImpl<G extends any = any> {
435435
// Secrets are normally stripped on the server,
436436
// but we also strip them here so that game developers
437437
// can see their effects while prototyping.
438-
const G = this.game.playerView(state.G, state.ctx, this.playerID);
438+
// Do not strip again if this is a multiplayer game
439+
// since the server has already stripped secret info. (issue #818)
440+
const G = this.multiplayer
441+
? state.G
442+
: this.game.playerView(state.G, state.ctx, this.playerID);
439443

440444
// Combine into return value.
441445
return {

0 commit comments

Comments
 (0)