Skip to content

Commit

Permalink
fix(client): Don’t run playerView locally on multiplayer clients (#819)
Browse files Browse the repository at this point in the history
* fix(client): Fix playerView executed twice do not strip again if is multiplayer

* Address review changes
  • Loading branch information
larry801 authored Oct 7, 2020
1 parent 6b6b175 commit 3c2aadd
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
40 changes: 40 additions & 0 deletions src/client/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,46 @@ describe('multiplayer', () => {
});
});

describe('strip secret only on server', () => {
let client0;
let client1;
let spec;
let initial = { secret: [1, 2, 3, 4], sum: 0 };
beforeAll(() => {
spec = {
game: {
setup: () => initial,
playerView: (G, ctx, playerID) => {
let r = { ...G };
r.sum = r.secret.reduce((prev, curr) => {
return prev + curr;
});
delete r.secret;
return r;
},
moves: { A: (G, ctx) => ({ A: ctx.playerID }) },
},
multiplayer: Local(),
};

client0 = Client({ ...spec, playerID: '0' });
client1 = Client({ ...spec, playerID: '1' });

client0.start();
client1.start();
});

test('secret stripped', () => {
expect(client0.getState().G).toEqual({ sum: 10 });
expect(client1.getState().G).toEqual({ sum: 10 });
});

afterAll(() => {
client0.stop();
client1.stop();
});
});

test('accepts enhancer for store', () => {
let spyDispatcher;
const spyEnhancer = vanillaCreateStore => (...args) => {
Expand Down
6 changes: 5 additions & 1 deletion src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,11 @@ export class _ClientImpl<G extends any = any> {
// Secrets are normally stripped on the server,
// but we also strip them here so that game developers
// can see their effects while prototyping.
const G = this.game.playerView(state.G, state.ctx, this.playerID);
// Do not strip again if this is a multiplayer game
// since the server has already stripped secret info. (issue #818)
const G = this.multiplayer
? state.G
: this.game.playerView(state.G, state.ctx, this.playerID);

// Combine into return value.
return {
Expand Down

0 comments on commit 3c2aadd

Please sign in to comment.