Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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(() => {
Expand All @@ -194,7 +195,6 @@ server.on('connection', function (conn) {
})
}, 3000)
}
sendAction('STATE', games.get()[gameId])
})
}
}
Expand Down Expand Up @@ -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) {
Expand Down
5 changes: 3 additions & 2 deletions src/keyValueArrayStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
},

Expand Down