some fixes for players syncing
This commit is contained in:
parent
e75cd7406f
commit
3d1a9ca9c7
5 changed files with 54 additions and 46 deletions
|
|
@ -11,6 +11,10 @@ function setGame(gameId, game) {
|
|||
GAMES[gameId] = game
|
||||
}
|
||||
|
||||
function playerExists(gameId, playerId) {
|
||||
return !!GAMES[gameId].players[playerId]
|
||||
}
|
||||
|
||||
function addPlayer(gameId, playerId) {
|
||||
const ts = Util.timestamp()
|
||||
if (!GAMES[gameId].players[playerId]) {
|
||||
|
|
@ -27,17 +31,22 @@ function addPlayer(gameId, playerId) {
|
|||
} else {
|
||||
changePlayer(gameId, playerId, { ts })
|
||||
}
|
||||
if (!GAMES[gameId].evtInfos[playerId]) {
|
||||
GAMES[gameId].evtInfos[playerId] = {
|
||||
_last_mouse: null,
|
||||
_last_mouse_down: null,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function socketExists(gameId, socket) {
|
||||
return GAMES[gameId].sockets.includes(socket)
|
||||
}
|
||||
|
||||
function addSocket(gameId, socket) {
|
||||
const sockets = GAMES[gameId].sockets
|
||||
|
||||
if (!sockets.includes(socket)) {
|
||||
sockets.push(socket)
|
||||
if (!GAMES[gameId].sockets.includes(socket)) {
|
||||
console.log('adding socket: ', gameId, socket.protocol)
|
||||
GAMES[gameId].sockets.push(socket)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -447,7 +456,9 @@ function handleInput(gameId, playerId, input) {
|
|||
export default {
|
||||
setGame,
|
||||
exists,
|
||||
playerExists,
|
||||
addPlayer,
|
||||
socketExists,
|
||||
addSocket,
|
||||
removeSocket,
|
||||
get,
|
||||
|
|
|
|||
|
|
@ -46,6 +46,11 @@ export default class WsWrapper {
|
|||
ws.onmessage = (e) => {
|
||||
this.onmessage(e)
|
||||
}
|
||||
ws.onerror = (e) => {
|
||||
this.handle = null
|
||||
this.reconnectTimeout = setTimeout(() => { this.connect() }, 1000)
|
||||
this.onclose(e)
|
||||
}
|
||||
ws.onclose = (e) => {
|
||||
this.handle = null
|
||||
this.reconnectTimeout = setTimeout(() => { this.connect() }, 1000)
|
||||
|
|
|
|||
|
|
@ -45,7 +45,9 @@ export default {
|
|||
createGame,
|
||||
exists: GameCommon.exists,
|
||||
addPlayer: GameCommon.addPlayer,
|
||||
playerExists: GameCommon.playerExists,
|
||||
addSocket: GameCommon.addSocket,
|
||||
socketExists: GameCommon.socketExists,
|
||||
removeSocket: GameCommon.removeSocket,
|
||||
get: GameCommon.get,
|
||||
getSockets: GameCommon.getSockets,
|
||||
|
|
|
|||
|
|
@ -50,44 +50,20 @@ class WebSocketServer {
|
|||
return
|
||||
}
|
||||
|
||||
socket.isAlive = true
|
||||
socket.on('pong', function () {
|
||||
this.isAlive = true;
|
||||
})
|
||||
|
||||
socket.on('message', (data) => {
|
||||
console.log(`ws| `, data)
|
||||
console.log(`ws`, socket.protocol, 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', () => {
|
||||
clearInterval(this._interval)
|
||||
})
|
||||
}
|
||||
|
||||
notifyOne(data, socket) {
|
||||
if (socket.isAlive) {
|
||||
socket.send(JSON.stringify(data))
|
||||
}
|
||||
}
|
||||
|
||||
notifyAll(data) {
|
||||
this._websocketserver.clients.forEach((socket) => {
|
||||
this.notifyOne(data, socket)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export default WebSocketServer
|
||||
|
|
|
|||
|
|
@ -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}) => {
|
||||
try {
|
||||
const proto = socket.protocol.split('|')
|
||||
|
|
@ -121,24 +145,14 @@ wss.on('message', async ({socket, data}) => {
|
|||
if (!Game.exists(gameId)) {
|
||||
throw `[game ${gameId} does not exist... ]`
|
||||
}
|
||||
Game.addPlayer(gameId, clientId)
|
||||
Game.addSocket(gameId, socket)
|
||||
Game.store(gameId)
|
||||
notify(
|
||||
[Protocol.EV_SERVER_INIT, Game.get(gameId)],
|
||||
[socket]
|
||||
)
|
||||
ensurePlayerGame(gameId, clientId, socket)
|
||||
} break;
|
||||
|
||||
case Protocol.EV_CLIENT_EVENT: {
|
||||
const clientSeq = msg[1]
|
||||
const clientEvtData = msg[2]
|
||||
const changes = Game.handleInput(gameId, clientId, clientEvtData)
|
||||
Game.store(gameId)
|
||||
notify(
|
||||
[Protocol.EV_SERVER_EVENT, clientId, clientSeq, changes],
|
||||
Game.getSockets(gameId)
|
||||
)
|
||||
ensurePlayerGame(gameId, clientId, socket)
|
||||
handlePlayerInput(gameId, clientId, clientSeq, clientEvtData)
|
||||
} break;
|
||||
}
|
||||
} catch (e) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue