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
|
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 })
|
||||||
}
|
}
|
||||||
GAMES[gameId].evtInfos[playerId] = {
|
if (!GAMES[gameId].evtInfos[playerId]) {
|
||||||
_last_mouse: null,
|
GAMES[gameId].evtInfos[playerId] = {
|
||||||
_last_mouse_down: null,
|
_last_mouse: null,
|
||||||
|
_last_mouse_down: null,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function addSocket(gameId, socket) {
|
function socketExists(gameId, socket) {
|
||||||
const sockets = GAMES[gameId].sockets
|
return GAMES[gameId].sockets.includes(socket)
|
||||||
|
}
|
||||||
|
|
||||||
if (!sockets.includes(socket)) {
|
function addSocket(gameId, 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 {
|
export default {
|
||||||
setGame,
|
setGame,
|
||||||
exists,
|
exists,
|
||||||
|
playerExists,
|
||||||
addPlayer,
|
addPlayer,
|
||||||
|
socketExists,
|
||||||
addSocket,
|
addSocket,
|
||||||
removeSocket,
|
removeSocket,
|
||||||
get,
|
get,
|
||||||
|
|
|
||||||
|
|
@ -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)
|
||||||
|
|
|
||||||
|
|
@ -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,
|
||||||
|
|
|
||||||
|
|
@ -50,43 +50,19 @@ 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)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue