some fixes for players syncing

This commit is contained in:
Zutatensuppe 2020-12-03 21:11:52 +01:00
parent e75cd7406f
commit 3d1a9ca9c7
5 changed files with 54 additions and 46 deletions

View file

@ -11,6 +11,10 @@ function setGame(gameId, game) {
GAMES[gameId] = game GAMES[gameId] = game
} }
function playerExists(gameId, playerId) {
return !!GAMES[gameId].players[playerId]
}
function addPlayer(gameId, playerId) { function addPlayer(gameId, playerId) {
const ts = Util.timestamp() const ts = Util.timestamp()
if (!GAMES[gameId].players[playerId]) { if (!GAMES[gameId].players[playerId]) {
@ -27,17 +31,22 @@ function addPlayer(gameId, playerId) {
} else { } else {
changePlayer(gameId, playerId, { ts }) changePlayer(gameId, playerId, { ts })
} }
if (!GAMES[gameId].evtInfos[playerId]) {
GAMES[gameId].evtInfos[playerId] = { GAMES[gameId].evtInfos[playerId] = {
_last_mouse: null, _last_mouse: null,
_last_mouse_down: null, _last_mouse_down: null,
} }
} }
}
function socketExists(gameId, socket) {
return GAMES[gameId].sockets.includes(socket)
}
function addSocket(gameId, socket) { function addSocket(gameId, socket) {
const sockets = GAMES[gameId].sockets if (!GAMES[gameId].sockets.includes(socket)) {
console.log('adding socket: ', gameId, socket.protocol)
if (!sockets.includes(socket)) { GAMES[gameId].sockets.push(socket)
sockets.push(socket)
} }
} }
@ -447,7 +456,9 @@ function handleInput(gameId, playerId, input) {
export default { export default {
setGame, setGame,
exists, exists,
playerExists,
addPlayer, addPlayer,
socketExists,
addSocket, addSocket,
removeSocket, removeSocket,
get, get,

View file

@ -46,6 +46,11 @@ export default class WsWrapper {
ws.onmessage = (e) => { ws.onmessage = (e) => {
this.onmessage(e) this.onmessage(e)
} }
ws.onerror = (e) => {
this.handle = null
this.reconnectTimeout = setTimeout(() => { this.connect() }, 1000)
this.onclose(e)
}
ws.onclose = (e) => { ws.onclose = (e) => {
this.handle = null this.handle = null
this.reconnectTimeout = setTimeout(() => { this.connect() }, 1000) this.reconnectTimeout = setTimeout(() => { this.connect() }, 1000)

View file

@ -45,7 +45,9 @@ export default {
createGame, createGame,
exists: GameCommon.exists, exists: GameCommon.exists,
addPlayer: GameCommon.addPlayer, addPlayer: GameCommon.addPlayer,
playerExists: GameCommon.playerExists,
addSocket: GameCommon.addSocket, addSocket: GameCommon.addSocket,
socketExists: GameCommon.socketExists,
removeSocket: GameCommon.removeSocket, removeSocket: GameCommon.removeSocket,
get: GameCommon.get, get: GameCommon.get,
getSockets: GameCommon.getSockets, getSockets: GameCommon.getSockets,

View file

@ -50,44 +50,20 @@ class WebSocketServer {
return return
} }
socket.isAlive = true
socket.on('pong', function () {
this.isAlive = true;
})
socket.on('message', (data) => { socket.on('message', (data) => {
console.log(`ws| `, data) console.log(`ws`, socket.protocol, data)
this.evt.dispatch('message', {socket, data}) this.evt.dispatch('message', {socket, data})
}) })
}) })
this._interval = setInterval(() => {
this._websocketserver.clients.forEach((socket) => {
if (socket.isAlive === false) {
return socket.terminate()
}
socket.isAlive = false
this.evt.dispatch('close', {socket})
socket.ping(() => { })
})
}, 30000)
this._websocketserver.on('close', () => { this._websocketserver.on('close', () => {
clearInterval(this._interval) clearInterval(this._interval)
}) })
} }
notifyOne(data, socket) { notifyOne(data, socket) {
if (socket.isAlive) {
socket.send(JSON.stringify(data)) socket.send(JSON.stringify(data))
} }
} }
notifyAll(data) {
this._websocketserver.clients.forEach((socket) => {
this.notifyOne(data, socket)
})
}
}
export default WebSocketServer export default WebSocketServer

View file

@ -109,6 +109,30 @@ wss.on('close', async ({socket}) => {
} }
}) })
const ensurePlayerGame = (gameId, clientId, socket) => {
Game.addPlayer(gameId, clientId)
Game.addSocket(gameId, socket)
Game.store(gameId)
const game = Game.get(gameId)
notify(
[Protocol.EV_SERVER_INIT, {
puzzle: game.puzzle,
players: game.players,
evtInfos: game.evtInfos,
}],
[socket]
)
}
const handlePlayerInput = (gameId, clientId, clientSeq, clientEvtData) => {
const changes = Game.handleInput(gameId, clientId, clientEvtData)
Game.store(gameId)
notify(
[Protocol.EV_SERVER_EVENT, clientId, clientSeq, changes],
Game.getSockets(gameId)
)
}
wss.on('message', async ({socket, data}) => { wss.on('message', async ({socket, data}) => {
try { try {
const proto = socket.protocol.split('|') const proto = socket.protocol.split('|')
@ -121,24 +145,14 @@ wss.on('message', async ({socket, data}) => {
if (!Game.exists(gameId)) { if (!Game.exists(gameId)) {
throw `[game ${gameId} does not exist... ]` throw `[game ${gameId} does not exist... ]`
} }
Game.addPlayer(gameId, clientId) ensurePlayerGame(gameId, clientId, socket)
Game.addSocket(gameId, socket)
Game.store(gameId)
notify(
[Protocol.EV_SERVER_INIT, Game.get(gameId)],
[socket]
)
} break; } break;
case Protocol.EV_CLIENT_EVENT: { case Protocol.EV_CLIENT_EVENT: {
const clientSeq = msg[1] const clientSeq = msg[1]
const clientEvtData = msg[2] const clientEvtData = msg[2]
const changes = Game.handleInput(gameId, clientId, clientEvtData) ensurePlayerGame(gameId, clientId, socket)
Game.store(gameId) handlePlayerInput(gameId, clientId, clientSeq, clientEvtData)
notify(
[Protocol.EV_SERVER_EVENT, clientId, clientSeq, changes],
Game.getSockets(gameId)
)
} break; } break;
} }
} catch (e) { } catch (e) {