Skip to content

Commit

Permalink
fix bug that was using the wrong playerID when calculating playerView
Browse files Browse the repository at this point in the history
  • Loading branch information
darthfiddler committed Feb 7, 2018
1 parent 6650216 commit 11e215e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function Server({ games, db }) {
// Get clients connected to this current game.
const roomClients = roomInfo.get(gameID);
for (const client of roomClients.values()) {
const playerID = clientInfo.get(client);
const playerID = clientInfo.get(client).playerID;

if (client === socket.id) {
socket.emit('sync', gameID, {
Expand Down
28 changes: 27 additions & 1 deletion src/server/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ test('action', () => {
expect(io.socket.emit).toHaveBeenCalledTimes(2);
});

test('playerView', () => {
test('playerView (sync)', () => {
// Write the player into G.
const game = Game({
playerView: (G, ctx, player) => {
Expand Down Expand Up @@ -196,6 +196,32 @@ test('playerView', () => {
});
});

test('playerView (action)', () => {
const game = Game({
playerView: (G, ctx, player) => {
return { ...G, player };
},
});
const server = Server({ games: [game] });
const io = server.context.io;
const action = ActionCreators.gameEvent('endTurn');

io.socket.id = 'first';
io.socket.receive('sync', 'gameID', '0', 2);
io.socket.id = 'second';
io.socket.receive('sync', 'gameID', '1', 2);
io.socket.emit.mockReset();

io.socket.receive('action', action, 0, 'gameID', '0');
expect(io.socket.emit).toHaveBeenCalledTimes(2);

const G_player0 = io.socket.emit.mock.calls[0][2].G;
const G_player1 = io.socket.emit.mock.calls[1][2].G;

expect(G_player0.player).toBe('0');
expect(G_player1.player).toBe('1');
});

test('custom db implementation', () => {
let getId = null;
class Custom {
Expand Down

0 comments on commit 11e215e

Please sign in to comment.