From 1025d42ea2850ebe58c4cca90a645fa3ea2f16b5 Mon Sep 17 00:00:00 2001 From: Zutatensuppe Date: Thu, 24 Dec 2020 15:29:32 +0100 Subject: [PATCH] reduce data sent to clients --- common/Util.js | 33 +++++++++++++++++++++++++ game/game.js | 6 ++--- game/index.js | 4 +-- server/index.js | 66 +++++++++++++++++++------------------------------ 4 files changed, 63 insertions(+), 46 deletions(-) diff --git a/common/Util.js b/common/Util.js index f6b79b3..95c1172 100644 --- a/common/Util.js +++ b/common/Util.js @@ -144,6 +144,36 @@ function decodePlayer(data) { } } +function encodeGame(data) { + if (Array.isArray(data)) { + return data + } + return [ + data.id, + data.rng.type, + Rng.serialize(data.rng.obj), + data.puzzle, + data.players, + data.evtInfos, + ] +} + +function decodeGame(data) { + if (!Array.isArray(data)) { + return data + } + return { + id: data[0], + rng: { + type: data[1], + obj: Rng.unserialize(data[2]), + }, + puzzle: data[3], + players: data[4], + evtInfos: data[5], + } +} + function coordByTileIdx(info, tileIdx) { const wTiles = info.width / info.tileSize return { @@ -181,5 +211,8 @@ export default { encodePlayer, decodePlayer, + encodeGame, + decodeGame, + coordByTileIdx, } diff --git a/game/game.js b/game/game.js index ffcdd8c..0fbf929 100644 --- a/game/game.js +++ b/game/game.js @@ -368,12 +368,10 @@ async function main() { if (MODE === 'play') { const game = await Communication.connect(gameId, CLIENT_ID) - game.rng.obj = Rng.unserialize(game.rng.obj) - Game.newGame(game) + Game.newGame(Util.decodeGame(game)) } else if (MODE === 'replay') { const {game, log} = await Communication.connectReplay(gameId, CLIENT_ID) - game.rng.obj = Rng.unserialize(game.rng.obj) - Game.newGame(game) + Game.newGame(Util.decodeGame(game)) GAME_LOG = log lastRealTime = Util.timestamp() GAME_START_TS = GAME_LOG[0][GAME_LOG[0].length - 1] diff --git a/game/index.js b/game/index.js index b30bfe1..cd288e0 100644 --- a/game/index.js +++ b/game/index.js @@ -83,7 +83,7 @@ export default { (or select from below) - Start new game + Start new game

Image lib

@@ -131,7 +131,7 @@ export default { mediaImgUploaded(j) { this.image = j.image }, - async newGame() { + async onNewGameClick() { const res = await fetch('/newgame', { method: 'post', headers: { diff --git a/server/index.js b/server/index.js index ad9197f..a39f779 100644 --- a/server/index.js +++ b/server/index.js @@ -10,7 +10,6 @@ import Game from './Game.js' import twing from 'twing' 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' @@ -74,6 +73,7 @@ app.post('/upload', (req, res) => { }) }) }) + app.post('/newgame', bodyParser.json(), async (req, res) => { console.log(req.body.tiles, req.body.image) const gameId = Util.uniqId() @@ -151,19 +151,10 @@ wss.on('message', async ({socket, data}) => { log[0][4] ) notify( - [Protocol.EV_SERVER_INIT_REPLAY, { - id: game.id, - rng: { - type: game.rng.type, - obj: Rng.serialize(game.rng.obj), - }, - puzzle: game.puzzle, - players: game.players, - evtInfos: game.evtInfos, - }, log], + [Protocol.EV_SERVER_INIT_REPLAY, Util.encodeGame(game), log], [socket] ) - } break; + } break case Protocol.EV_CLIENT_INIT: { if (!Game.exists(gameId)) { @@ -174,19 +165,10 @@ wss.on('message', async ({socket, data}) => { GameSockets.addSocket(gameId, socket) const game = Game.get(gameId) notify( - [Protocol.EV_SERVER_INIT, { - id: game.id, - rng: { - type: game.rng.type, - obj: Rng.serialize(game.rng.obj), - }, - puzzle: game.puzzle, - players: game.players, - evtInfos: game.evtInfos, - }], + [Protocol.EV_SERVER_INIT, Util.encodeGame(game)], [socket] ) - } break; + } break case Protocol.EV_CLIENT_EVENT: { if (!Game.exists(gameId)) { @@ -196,25 +178,29 @@ wss.on('message', async ({socket, data}) => { const clientEvtData = msg[2] const ts = Util.timestamp() - Game.addPlayer(gameId, clientId, ts) - GameSockets.addSocket(gameId, socket) + let sendGame = false + if (!Game.playerExists(gameId, clientId)) { + Game.addPlayer(gameId, clientId, ts) + sendGame = true + } + if (!GameSockets.socketExists(gameId, socket)) { + GameSockets.addSocket(gameId, socket) + sendGame = true + } + if (sendGame) { + const game = Game.get(gameId) + notify( + [Protocol.EV_SERVER_INIT, Util.encodeGame(game)], + [socket] + ) + } - const game = Game.get(gameId) - notify( - [Protocol.EV_SERVER_INIT, { - id: game.id, - puzzle: game.puzzle, - players: game.players, - evtInfos: game.evtInfos, - }], - [socket] - ) const changes = Game.handleInput(gameId, clientId, clientEvtData, ts) notify( [Protocol.EV_SERVER_EVENT, clientId, clientSeq, changes], GameSockets.getSockets(gameId) ) - } break; + } break } } catch (e) { console.error(e) @@ -243,7 +229,7 @@ memoryUsageHuman() // persist games in fixed interval const persistInterval = setInterval(() => { - console.log('Persisting games...'); + console.log('Persisting games...') Game.persistChangedGames() memoryUsageHuman() @@ -271,12 +257,12 @@ const gracefulShutdown = (signal) => { // used by nodemon process.once('SIGUSR2', function () { gracefulShutdown('SIGUSR2') -}); +}) process.once('SIGINT', function (code) { gracefulShutdown('SIGINT') -}); +}) process.once('SIGTERM', function (code) { gracefulShutdown('SIGTERM') -}); +})