move socket handling to extra file (not GameCommon)
This commit is contained in:
parent
f1625b75ff
commit
35364c1058
4 changed files with 52 additions and 46 deletions
|
|
@ -2,19 +2,19 @@ import Geometry from './Geometry.js'
|
||||||
import Protocol from './Protocol.js'
|
import Protocol from './Protocol.js'
|
||||||
import Util from './Util.js'
|
import Util from './Util.js'
|
||||||
|
|
||||||
|
// Map<gameId, GameObject>
|
||||||
const GAMES = {}
|
const GAMES = {}
|
||||||
|
|
||||||
function exists(gameId) {
|
function exists(gameId) {
|
||||||
return (!!GAMES[gameId]) || false
|
return (!!GAMES[gameId]) || false
|
||||||
}
|
}
|
||||||
|
|
||||||
function __createGameObject(id, rng, puzzle, players, sockets, evtInfos) {
|
function __createGameObject(id, rng, puzzle, players, evtInfos) {
|
||||||
return {
|
return {
|
||||||
id: id,
|
id: id,
|
||||||
rng: rng,
|
rng: rng,
|
||||||
puzzle: puzzle,
|
puzzle: puzzle,
|
||||||
players: players,
|
players: players,
|
||||||
sockets: sockets,
|
|
||||||
evtInfos: evtInfos,
|
evtInfos: evtInfos,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -33,8 +33,8 @@ function __createPlayerObject(id, ts) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function newGame({id, rng, puzzle, players, sockets, evtInfos}) {
|
function newGame({id, rng, puzzle, players, evtInfos}) {
|
||||||
const game = __createGameObject(id, rng, puzzle, players, sockets, evtInfos)
|
const game = __createGameObject(id, rng, puzzle, players, evtInfos)
|
||||||
setGame(id, game)
|
setGame(id, game)
|
||||||
return game
|
return game
|
||||||
}
|
}
|
||||||
|
|
@ -116,21 +116,6 @@ function addPlayer(gameId, playerId, ts) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function socketExists(gameId, socket) {
|
|
||||||
return GAMES[gameId].sockets.includes(socket)
|
|
||||||
}
|
|
||||||
|
|
||||||
function addSocket(gameId, socket) {
|
|
||||||
if (!GAMES[gameId].sockets.includes(socket)) {
|
|
||||||
console.log('adding socket: ', gameId, socket.protocol)
|
|
||||||
GAMES[gameId].sockets.push(socket)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function removeSocket(gameId, socket) {
|
|
||||||
GAMES[gameId].sockets = GAMES[gameId].sockets.filter(s => s !== socket)
|
|
||||||
}
|
|
||||||
|
|
||||||
function getAllGames() {
|
function getAllGames() {
|
||||||
return Object.values(GAMES).sort((a, b) => {
|
return Object.values(GAMES).sort((a, b) => {
|
||||||
// when both have same finished state, sort by started
|
// when both have same finished state, sort by started
|
||||||
|
|
@ -179,10 +164,6 @@ function getTilesSortedByZIndex(gameId) {
|
||||||
return tiles.sort((t1, t2) => t1.z - t2.z)
|
return tiles.sort((t1, t2) => t1.z - t2.z)
|
||||||
}
|
}
|
||||||
|
|
||||||
function getSockets(gameId) {
|
|
||||||
return GAMES[gameId].sockets
|
|
||||||
}
|
|
||||||
|
|
||||||
function changePlayer(gameId, playerId, change) {
|
function changePlayer(gameId, playerId, change) {
|
||||||
const player = getPlayer(gameId, playerId)
|
const player = getPlayer(gameId, playerId)
|
||||||
for (let k of Object.keys(change)) {
|
for (let k of Object.keys(change)) {
|
||||||
|
|
@ -655,15 +636,11 @@ export default {
|
||||||
getRelevantPlayers,
|
getRelevantPlayers,
|
||||||
getActivePlayers,
|
getActivePlayers,
|
||||||
addPlayer,
|
addPlayer,
|
||||||
socketExists,
|
|
||||||
addSocket,
|
|
||||||
removeSocket,
|
|
||||||
getFinishedTileCount,
|
getFinishedTileCount,
|
||||||
getTileCount,
|
getTileCount,
|
||||||
getImageUrl,
|
getImageUrl,
|
||||||
get,
|
get,
|
||||||
getAllGames,
|
getAllGames,
|
||||||
getSockets,
|
|
||||||
getPlayerBgColor,
|
getPlayerBgColor,
|
||||||
getPlayerColor,
|
getPlayerColor,
|
||||||
getPlayerName,
|
getPlayerName,
|
||||||
|
|
|
||||||
|
|
@ -40,7 +40,6 @@ function loadAllGames() {
|
||||||
},
|
},
|
||||||
puzzle: game.puzzle,
|
puzzle: game.puzzle,
|
||||||
players: game.players,
|
players: game.players,
|
||||||
sockets: [],
|
|
||||||
evtInfos: {}
|
evtInfos: {}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
@ -58,7 +57,6 @@ async function createGameObject(gameId, targetTiles, image, ts) {
|
||||||
},
|
},
|
||||||
await createPuzzle(rng, targetTiles, image, ts),
|
await createPuzzle(rng, targetTiles, image, ts),
|
||||||
[],
|
[],
|
||||||
[],
|
|
||||||
{}
|
{}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
@ -76,7 +74,6 @@ async function createGame(gameId, targetTiles, image, ts) {
|
||||||
},
|
},
|
||||||
puzzle: await createPuzzle(rng, targetTiles, image, ts),
|
puzzle: await createPuzzle(rng, targetTiles, image, ts),
|
||||||
players: [],
|
players: [],
|
||||||
sockets: [],
|
|
||||||
evtInfos: {},
|
evtInfos: {},
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
@ -97,11 +94,6 @@ function addPlayer(gameId, playerId, ts) {
|
||||||
changedGames[gameId] = true
|
changedGames[gameId] = true
|
||||||
}
|
}
|
||||||
|
|
||||||
function addSocket(gameId, socket) {
|
|
||||||
GameCommon.addSocket(gameId, socket)
|
|
||||||
changedGames[gameId] = true
|
|
||||||
}
|
|
||||||
|
|
||||||
function handleInput(gameId, playerId, input, ts) {
|
function handleInput(gameId, playerId, input, ts) {
|
||||||
const idx = GameCommon.getPlayerIndexById(gameId, playerId)
|
const idx = GameCommon.getPlayerIndexById(gameId, playerId)
|
||||||
const diff = ts - GameCommon.getStartTs(gameId)
|
const diff = ts - GameCommon.getStartTs(gameId)
|
||||||
|
|
@ -136,7 +128,6 @@ export default {
|
||||||
persistChangedGames,
|
persistChangedGames,
|
||||||
createGame,
|
createGame,
|
||||||
addPlayer,
|
addPlayer,
|
||||||
addSocket,
|
|
||||||
handleInput,
|
handleInput,
|
||||||
getAllGames: GameCommon.getAllGames,
|
getAllGames: GameCommon.getAllGames,
|
||||||
getRelevantPlayers: GameCommon.getRelevantPlayers,
|
getRelevantPlayers: GameCommon.getRelevantPlayers,
|
||||||
|
|
@ -146,10 +137,7 @@ export default {
|
||||||
getTileCount: GameCommon.getTileCount,
|
getTileCount: GameCommon.getTileCount,
|
||||||
exists: GameCommon.exists,
|
exists: GameCommon.exists,
|
||||||
playerExists: GameCommon.playerExists,
|
playerExists: GameCommon.playerExists,
|
||||||
socketExists: GameCommon.socketExists,
|
|
||||||
removeSocket: GameCommon.removeSocket,
|
|
||||||
get: GameCommon.get,
|
get: GameCommon.get,
|
||||||
getSockets: GameCommon.getSockets,
|
|
||||||
getStartTs: GameCommon.getStartTs,
|
getStartTs: GameCommon.getStartTs,
|
||||||
getFinishTs: GameCommon.getFinishTs,
|
getFinishTs: GameCommon.getFinishTs,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
43
server/GameSockets.js
Normal file
43
server/GameSockets.js
Normal file
|
|
@ -0,0 +1,43 @@
|
||||||
|
// Map<gameId, Socket[]>
|
||||||
|
const SOCKETS = {}
|
||||||
|
|
||||||
|
function socketExists(gameId, socket) {
|
||||||
|
if (!(gameId in SOCKETS)) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return SOCKETS[gameId].includes(socket)
|
||||||
|
}
|
||||||
|
|
||||||
|
function removeSocket(gameId, socket) {
|
||||||
|
if (!(gameId in SOCKETS)) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
SOCKETS[gameId] = SOCKETS[gameId].filter(s => s !== socket)
|
||||||
|
console.log('removed socket: ', gameId, socket.protocol)
|
||||||
|
console.log('socket count: ', Object.keys(SOCKETS[gameId]).length)
|
||||||
|
}
|
||||||
|
|
||||||
|
function addSocket(gameId, socket) {
|
||||||
|
if (!(gameId in SOCKETS)) {
|
||||||
|
SOCKETS[gameId] = []
|
||||||
|
}
|
||||||
|
if (!SOCKETS[gameId].includes(socket)) {
|
||||||
|
SOCKETS[gameId].push(socket)
|
||||||
|
console.log('added socket: ', gameId, socket.protocol)
|
||||||
|
console.log('socket count: ', Object.keys(SOCKETS[gameId]).length)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function getSockets(gameId) {
|
||||||
|
if (!(gameId in SOCKETS)) {
|
||||||
|
return []
|
||||||
|
}
|
||||||
|
return SOCKETS[gameId]
|
||||||
|
}
|
||||||
|
|
||||||
|
export default {
|
||||||
|
addSocket,
|
||||||
|
removeSocket,
|
||||||
|
socketExists,
|
||||||
|
getSockets,
|
||||||
|
}
|
||||||
|
|
@ -12,6 +12,7 @@ import bodyParser from 'body-parser'
|
||||||
import v8 from 'v8'
|
import v8 from 'v8'
|
||||||
import { Rng } from '../common/Rng.js'
|
import { Rng } from '../common/Rng.js'
|
||||||
import GameLog from './GameLog.js'
|
import GameLog from './GameLog.js'
|
||||||
|
import GameSockets from './GameSockets.js'
|
||||||
|
|
||||||
const allImages = () => [
|
const allImages = () => [
|
||||||
...fs.readdirSync('./../data/uploads/').map(f => ({
|
...fs.readdirSync('./../data/uploads/').map(f => ({
|
||||||
|
|
@ -124,7 +125,7 @@ wss.on('close', async ({socket}) => {
|
||||||
const clientId = proto[0]
|
const clientId = proto[0]
|
||||||
const gameId = proto[1]
|
const gameId = proto[1]
|
||||||
if (Game.exists(gameId)) {
|
if (Game.exists(gameId)) {
|
||||||
Game.removeSocket(gameId, socket)
|
GameSockets.removeSocket(gameId, socket)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
@ -153,7 +154,6 @@ wss.on('message', async ({socket, data}) => {
|
||||||
},
|
},
|
||||||
puzzle: game.puzzle,
|
puzzle: game.puzzle,
|
||||||
players: game.players,
|
players: game.players,
|
||||||
sockets: [],
|
|
||||||
evtInfos: game.evtInfos,
|
evtInfos: game.evtInfos,
|
||||||
}, log],
|
}, log],
|
||||||
[socket]
|
[socket]
|
||||||
|
|
@ -166,7 +166,7 @@ wss.on('message', async ({socket, data}) => {
|
||||||
}
|
}
|
||||||
const ts = Util.timestamp()
|
const ts = Util.timestamp()
|
||||||
Game.addPlayer(gameId, clientId, ts)
|
Game.addPlayer(gameId, clientId, ts)
|
||||||
Game.addSocket(gameId, socket)
|
GameSockets.addSocket(gameId, socket)
|
||||||
const game = Game.get(gameId)
|
const game = Game.get(gameId)
|
||||||
notify(
|
notify(
|
||||||
[Protocol.EV_SERVER_INIT, {
|
[Protocol.EV_SERVER_INIT, {
|
||||||
|
|
@ -177,7 +177,6 @@ wss.on('message', async ({socket, data}) => {
|
||||||
},
|
},
|
||||||
puzzle: game.puzzle,
|
puzzle: game.puzzle,
|
||||||
players: game.players,
|
players: game.players,
|
||||||
sockets: [],
|
|
||||||
evtInfos: game.evtInfos,
|
evtInfos: game.evtInfos,
|
||||||
}],
|
}],
|
||||||
[socket]
|
[socket]
|
||||||
|
|
@ -190,7 +189,7 @@ wss.on('message', async ({socket, data}) => {
|
||||||
const ts = Util.timestamp()
|
const ts = Util.timestamp()
|
||||||
|
|
||||||
Game.addPlayer(gameId, clientId, ts)
|
Game.addPlayer(gameId, clientId, ts)
|
||||||
Game.addSocket(gameId, socket)
|
GameSockets.addSocket(gameId, socket)
|
||||||
|
|
||||||
const game = Game.get(gameId)
|
const game = Game.get(gameId)
|
||||||
notify(
|
notify(
|
||||||
|
|
@ -198,7 +197,6 @@ wss.on('message', async ({socket, data}) => {
|
||||||
id: game.id,
|
id: game.id,
|
||||||
puzzle: game.puzzle,
|
puzzle: game.puzzle,
|
||||||
players: game.players,
|
players: game.players,
|
||||||
sockets: [],
|
|
||||||
evtInfos: game.evtInfos,
|
evtInfos: game.evtInfos,
|
||||||
}],
|
}],
|
||||||
[socket]
|
[socket]
|
||||||
|
|
@ -206,7 +204,7 @@ wss.on('message', async ({socket, data}) => {
|
||||||
const changes = Game.handleInput(gameId, clientId, clientEvtData, ts)
|
const changes = Game.handleInput(gameId, clientId, clientEvtData, ts)
|
||||||
notify(
|
notify(
|
||||||
[Protocol.EV_SERVER_EVENT, clientId, clientSeq, changes],
|
[Protocol.EV_SERVER_EVENT, clientId, clientSeq, changes],
|
||||||
Game.getSockets(gameId)
|
GameSockets.getSockets(gameId)
|
||||||
)
|
)
|
||||||
} break;
|
} break;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue