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 Util from './Util.js'
|
||||
|
||||
// Map<gameId, GameObject>
|
||||
const GAMES = {}
|
||||
|
||||
function exists(gameId) {
|
||||
return (!!GAMES[gameId]) || false
|
||||
}
|
||||
|
||||
function __createGameObject(id, rng, puzzle, players, sockets, evtInfos) {
|
||||
function __createGameObject(id, rng, puzzle, players, evtInfos) {
|
||||
return {
|
||||
id: id,
|
||||
rng: rng,
|
||||
puzzle: puzzle,
|
||||
players: players,
|
||||
sockets: sockets,
|
||||
evtInfos: evtInfos,
|
||||
}
|
||||
}
|
||||
|
|
@ -33,8 +33,8 @@ function __createPlayerObject(id, ts) {
|
|||
}
|
||||
}
|
||||
|
||||
function newGame({id, rng, puzzle, players, sockets, evtInfos}) {
|
||||
const game = __createGameObject(id, rng, puzzle, players, sockets, evtInfos)
|
||||
function newGame({id, rng, puzzle, players, evtInfos}) {
|
||||
const game = __createGameObject(id, rng, puzzle, players, evtInfos)
|
||||
setGame(id, 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() {
|
||||
return Object.values(GAMES).sort((a, b) => {
|
||||
// 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)
|
||||
}
|
||||
|
||||
function getSockets(gameId) {
|
||||
return GAMES[gameId].sockets
|
||||
}
|
||||
|
||||
function changePlayer(gameId, playerId, change) {
|
||||
const player = getPlayer(gameId, playerId)
|
||||
for (let k of Object.keys(change)) {
|
||||
|
|
@ -655,15 +636,11 @@ export default {
|
|||
getRelevantPlayers,
|
||||
getActivePlayers,
|
||||
addPlayer,
|
||||
socketExists,
|
||||
addSocket,
|
||||
removeSocket,
|
||||
getFinishedTileCount,
|
||||
getTileCount,
|
||||
getImageUrl,
|
||||
get,
|
||||
getAllGames,
|
||||
getSockets,
|
||||
getPlayerBgColor,
|
||||
getPlayerColor,
|
||||
getPlayerName,
|
||||
|
|
|
|||
|
|
@ -40,7 +40,6 @@ function loadAllGames() {
|
|||
},
|
||||
puzzle: game.puzzle,
|
||||
players: game.players,
|
||||
sockets: [],
|
||||
evtInfos: {}
|
||||
})
|
||||
}
|
||||
|
|
@ -58,7 +57,6 @@ async function createGameObject(gameId, 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),
|
||||
players: [],
|
||||
sockets: [],
|
||||
evtInfos: {},
|
||||
})
|
||||
|
||||
|
|
@ -97,11 +94,6 @@ function addPlayer(gameId, playerId, ts) {
|
|||
changedGames[gameId] = true
|
||||
}
|
||||
|
||||
function addSocket(gameId, socket) {
|
||||
GameCommon.addSocket(gameId, socket)
|
||||
changedGames[gameId] = true
|
||||
}
|
||||
|
||||
function handleInput(gameId, playerId, input, ts) {
|
||||
const idx = GameCommon.getPlayerIndexById(gameId, playerId)
|
||||
const diff = ts - GameCommon.getStartTs(gameId)
|
||||
|
|
@ -136,7 +128,6 @@ export default {
|
|||
persistChangedGames,
|
||||
createGame,
|
||||
addPlayer,
|
||||
addSocket,
|
||||
handleInput,
|
||||
getAllGames: GameCommon.getAllGames,
|
||||
getRelevantPlayers: GameCommon.getRelevantPlayers,
|
||||
|
|
@ -146,10 +137,7 @@ export default {
|
|||
getTileCount: GameCommon.getTileCount,
|
||||
exists: GameCommon.exists,
|
||||
playerExists: GameCommon.playerExists,
|
||||
socketExists: GameCommon.socketExists,
|
||||
removeSocket: GameCommon.removeSocket,
|
||||
get: GameCommon.get,
|
||||
getSockets: GameCommon.getSockets,
|
||||
getStartTs: GameCommon.getStartTs,
|
||||
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 { Rng } from '../common/Rng.js'
|
||||
import GameLog from './GameLog.js'
|
||||
import GameSockets from './GameSockets.js'
|
||||
|
||||
const allImages = () => [
|
||||
...fs.readdirSync('./../data/uploads/').map(f => ({
|
||||
|
|
@ -124,7 +125,7 @@ wss.on('close', async ({socket}) => {
|
|||
const clientId = proto[0]
|
||||
const gameId = proto[1]
|
||||
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,
|
||||
players: game.players,
|
||||
sockets: [],
|
||||
evtInfos: game.evtInfos,
|
||||
}, log],
|
||||
[socket]
|
||||
|
|
@ -166,7 +166,7 @@ wss.on('message', async ({socket, data}) => {
|
|||
}
|
||||
const ts = Util.timestamp()
|
||||
Game.addPlayer(gameId, clientId, ts)
|
||||
Game.addSocket(gameId, socket)
|
||||
GameSockets.addSocket(gameId, socket)
|
||||
const game = Game.get(gameId)
|
||||
notify(
|
||||
[Protocol.EV_SERVER_INIT, {
|
||||
|
|
@ -177,7 +177,6 @@ wss.on('message', async ({socket, data}) => {
|
|||
},
|
||||
puzzle: game.puzzle,
|
||||
players: game.players,
|
||||
sockets: [],
|
||||
evtInfos: game.evtInfos,
|
||||
}],
|
||||
[socket]
|
||||
|
|
@ -190,7 +189,7 @@ wss.on('message', async ({socket, data}) => {
|
|||
const ts = Util.timestamp()
|
||||
|
||||
Game.addPlayer(gameId, clientId, ts)
|
||||
Game.addSocket(gameId, socket)
|
||||
GameSockets.addSocket(gameId, socket)
|
||||
|
||||
const game = Game.get(gameId)
|
||||
notify(
|
||||
|
|
@ -198,7 +197,6 @@ wss.on('message', async ({socket, data}) => {
|
|||
id: game.id,
|
||||
puzzle: game.puzzle,
|
||||
players: game.players,
|
||||
sockets: [],
|
||||
evtInfos: game.evtInfos,
|
||||
}],
|
||||
[socket]
|
||||
|
|
@ -206,7 +204,7 @@ wss.on('message', async ({socket, data}) => {
|
|||
const changes = Game.handleInput(gameId, clientId, clientEvtData, ts)
|
||||
notify(
|
||||
[Protocol.EV_SERVER_EVENT, clientId, clientSeq, changes],
|
||||
Game.getSockets(gameId)
|
||||
GameSockets.getSockets(gameId)
|
||||
)
|
||||
} break;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue