From 462feacf3ed6f56c475374c9165c9bdd2938d064 Mon Sep 17 00:00:00 2001 From: Albert Arvidsson Date: Tue, 11 Oct 2016 12:25:29 +0200 Subject: [PATCH 1/2] Support player styles for player 4+ as well By just using modulo to repeatedly give style for 1,2,3,1,2,3,1,2,3 --- src/index.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/index.js b/src/index.js index a1840fb..8091172 100644 --- a/src/index.js +++ b/src/index.js @@ -134,7 +134,7 @@ const createOrJoinGame = (() => { } return createOrGetMaze(gameId).then(({ maze, entrance, exit }) => { const { x, y } = entrance - const style = stylesAvailable[getPlayersNumber(gameId)] + const style = stylesAvailable[getPlayersNumber(gameId) % stylesAvailable.length] const direction = getInitialDirection(entrance) console.log('gameId', gameId, 'updateBy') games.update(gameId, 'players', nickname, 'x', x) @@ -182,6 +182,7 @@ server.on('connection', function (conn) { createOrJoinGame(nickname).then(gameId => { clients.updateBy({ nickname }, { gameId }) success({ token, nickname, gameId }) + sendAction('STATE', games.get()[gameId]) if (isGameFull(gameId)) { games.update(gameId, 'details', 'predicates', 'isStarting', true) setTimeout(() => { @@ -194,7 +195,6 @@ server.on('connection', function (conn) { }) }, 3000) } - sendAction('STATE', games.get()[gameId]) }) } } @@ -347,7 +347,13 @@ server.on('connection', function (conn) { }) conn.on('close', function (code, reason) { - console.log('Connection closed') + const client = clients.findBy({ uuid: conn.uuid }) + if (client && client.nickname) { + console.log(`PLAYER LEFT ${client.nickname} ${conn.uuid}`) + } else if (client) { + console.log(`SPECTATOR LEFT ${conn.uuid}`) + clients.removeBy({ uuid: conn.uuid }) // Spectators never rejoin + } }) conn.on('error', function (err) { From d1b1ca0da16f9a9b72da8824f62cdf955fff8c3c Mon Sep 17 00:00:00 2001 From: Albert Arvidsson Date: Tue, 11 Oct 2016 17:36:29 +0200 Subject: [PATCH 2/2] Fix bug in keyValueArrayStore updateBy Make sure to only add when the item is new and otherwise update. Before this change we got duplicates. --- src/keyValueArrayStore.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/keyValueArrayStore.js b/src/keyValueArrayStore.js index 198f399..2f5cc57 100644 --- a/src/keyValueArrayStore.js +++ b/src/keyValueArrayStore.js @@ -22,10 +22,11 @@ export default function keyValueArrayStore () { updateBy (keyValues, newValues) { const match = all.find(matches(keyValues)) - if (!match) { + if (match) { + Object.assign(match, newValues) + } else { return all.push(newValues) } - Object.assign(match, newValues) return match },