reduce data sent to clients
This commit is contained in:
parent
cac1f02557
commit
1025d42ea2
4 changed files with 63 additions and 46 deletions
|
|
@ -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) {
|
function coordByTileIdx(info, tileIdx) {
|
||||||
const wTiles = info.width / info.tileSize
|
const wTiles = info.width / info.tileSize
|
||||||
return {
|
return {
|
||||||
|
|
@ -181,5 +211,8 @@ export default {
|
||||||
encodePlayer,
|
encodePlayer,
|
||||||
decodePlayer,
|
decodePlayer,
|
||||||
|
|
||||||
|
encodeGame,
|
||||||
|
decodeGame,
|
||||||
|
|
||||||
coordByTileIdx,
|
coordByTileIdx,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -368,12 +368,10 @@ async function main() {
|
||||||
|
|
||||||
if (MODE === 'play') {
|
if (MODE === 'play') {
|
||||||
const game = await Communication.connect(gameId, CLIENT_ID)
|
const game = await Communication.connect(gameId, CLIENT_ID)
|
||||||
game.rng.obj = Rng.unserialize(game.rng.obj)
|
Game.newGame(Util.decodeGame(game))
|
||||||
Game.newGame(game)
|
|
||||||
} else if (MODE === 'replay') {
|
} else if (MODE === 'replay') {
|
||||||
const {game, log} = await Communication.connectReplay(gameId, CLIENT_ID)
|
const {game, log} = await Communication.connectReplay(gameId, CLIENT_ID)
|
||||||
game.rng.obj = Rng.unserialize(game.rng.obj)
|
Game.newGame(Util.decodeGame(game))
|
||||||
Game.newGame(game)
|
|
||||||
GAME_LOG = log
|
GAME_LOG = log
|
||||||
lastRealTime = Util.timestamp()
|
lastRealTime = Util.timestamp()
|
||||||
GAME_START_TS = GAME_LOG[0][GAME_LOG[0].length - 1]
|
GAME_START_TS = GAME_LOG[0][GAME_LOG[0].length - 1]
|
||||||
|
|
|
||||||
|
|
@ -83,7 +83,7 @@ export default {
|
||||||
(or select from below)
|
(or select from below)
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
<span class="btn" :class="" @click="newGame">Start new game</span>
|
<span class="btn" :class="" @click="onNewGameClick">Start new game</span>
|
||||||
|
|
||||||
<h1>Image lib</h1>
|
<h1>Image lib</h1>
|
||||||
<div>
|
<div>
|
||||||
|
|
@ -131,7 +131,7 @@ export default {
|
||||||
mediaImgUploaded(j) {
|
mediaImgUploaded(j) {
|
||||||
this.image = j.image
|
this.image = j.image
|
||||||
},
|
},
|
||||||
async newGame() {
|
async onNewGameClick() {
|
||||||
const res = await fetch('/newgame', {
|
const res = await fetch('/newgame', {
|
||||||
method: 'post',
|
method: 'post',
|
||||||
headers: {
|
headers: {
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,6 @@ import Game from './Game.js'
|
||||||
import twing from 'twing'
|
import twing from 'twing'
|
||||||
import bodyParser from 'body-parser'
|
import bodyParser from 'body-parser'
|
||||||
import v8 from 'v8'
|
import v8 from 'v8'
|
||||||
import { Rng } from '../common/Rng.js'
|
|
||||||
import GameLog from './GameLog.js'
|
import GameLog from './GameLog.js'
|
||||||
import GameSockets from './GameSockets.js'
|
import GameSockets from './GameSockets.js'
|
||||||
|
|
||||||
|
|
@ -74,6 +73,7 @@ app.post('/upload', (req, res) => {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
app.post('/newgame', bodyParser.json(), async (req, res) => {
|
app.post('/newgame', bodyParser.json(), async (req, res) => {
|
||||||
console.log(req.body.tiles, req.body.image)
|
console.log(req.body.tiles, req.body.image)
|
||||||
const gameId = Util.uniqId()
|
const gameId = Util.uniqId()
|
||||||
|
|
@ -151,19 +151,10 @@ wss.on('message', async ({socket, data}) => {
|
||||||
log[0][4]
|
log[0][4]
|
||||||
)
|
)
|
||||||
notify(
|
notify(
|
||||||
[Protocol.EV_SERVER_INIT_REPLAY, {
|
[Protocol.EV_SERVER_INIT_REPLAY, Util.encodeGame(game), log],
|
||||||
id: game.id,
|
|
||||||
rng: {
|
|
||||||
type: game.rng.type,
|
|
||||||
obj: Rng.serialize(game.rng.obj),
|
|
||||||
},
|
|
||||||
puzzle: game.puzzle,
|
|
||||||
players: game.players,
|
|
||||||
evtInfos: game.evtInfos,
|
|
||||||
}, log],
|
|
||||||
[socket]
|
[socket]
|
||||||
)
|
)
|
||||||
} break;
|
} break
|
||||||
|
|
||||||
case Protocol.EV_CLIENT_INIT: {
|
case Protocol.EV_CLIENT_INIT: {
|
||||||
if (!Game.exists(gameId)) {
|
if (!Game.exists(gameId)) {
|
||||||
|
|
@ -174,19 +165,10 @@ wss.on('message', async ({socket, data}) => {
|
||||||
GameSockets.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, Util.encodeGame(game)],
|
||||||
id: game.id,
|
|
||||||
rng: {
|
|
||||||
type: game.rng.type,
|
|
||||||
obj: Rng.serialize(game.rng.obj),
|
|
||||||
},
|
|
||||||
puzzle: game.puzzle,
|
|
||||||
players: game.players,
|
|
||||||
evtInfos: game.evtInfos,
|
|
||||||
}],
|
|
||||||
[socket]
|
[socket]
|
||||||
)
|
)
|
||||||
} break;
|
} break
|
||||||
|
|
||||||
case Protocol.EV_CLIENT_EVENT: {
|
case Protocol.EV_CLIENT_EVENT: {
|
||||||
if (!Game.exists(gameId)) {
|
if (!Game.exists(gameId)) {
|
||||||
|
|
@ -196,25 +178,29 @@ wss.on('message', async ({socket, data}) => {
|
||||||
const clientEvtData = msg[2]
|
const clientEvtData = msg[2]
|
||||||
const ts = Util.timestamp()
|
const ts = Util.timestamp()
|
||||||
|
|
||||||
|
let sendGame = false
|
||||||
|
if (!Game.playerExists(gameId, clientId)) {
|
||||||
Game.addPlayer(gameId, clientId, ts)
|
Game.addPlayer(gameId, clientId, ts)
|
||||||
|
sendGame = true
|
||||||
|
}
|
||||||
|
if (!GameSockets.socketExists(gameId, socket)) {
|
||||||
GameSockets.addSocket(gameId, socket)
|
GameSockets.addSocket(gameId, socket)
|
||||||
|
sendGame = true
|
||||||
|
}
|
||||||
|
if (sendGame) {
|
||||||
const game = Game.get(gameId)
|
const game = Game.get(gameId)
|
||||||
notify(
|
notify(
|
||||||
[Protocol.EV_SERVER_INIT, {
|
[Protocol.EV_SERVER_INIT, Util.encodeGame(game)],
|
||||||
id: game.id,
|
|
||||||
puzzle: game.puzzle,
|
|
||||||
players: game.players,
|
|
||||||
evtInfos: game.evtInfos,
|
|
||||||
}],
|
|
||||||
[socket]
|
[socket]
|
||||||
)
|
)
|
||||||
|
}
|
||||||
|
|
||||||
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],
|
||||||
GameSockets.getSockets(gameId)
|
GameSockets.getSockets(gameId)
|
||||||
)
|
)
|
||||||
} break;
|
} break
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e)
|
console.error(e)
|
||||||
|
|
@ -243,7 +229,7 @@ memoryUsageHuman()
|
||||||
|
|
||||||
// persist games in fixed interval
|
// persist games in fixed interval
|
||||||
const persistInterval = setInterval(() => {
|
const persistInterval = setInterval(() => {
|
||||||
console.log('Persisting games...');
|
console.log('Persisting games...')
|
||||||
Game.persistChangedGames()
|
Game.persistChangedGames()
|
||||||
|
|
||||||
memoryUsageHuman()
|
memoryUsageHuman()
|
||||||
|
|
@ -271,12 +257,12 @@ const gracefulShutdown = (signal) => {
|
||||||
// used by nodemon
|
// used by nodemon
|
||||||
process.once('SIGUSR2', function () {
|
process.once('SIGUSR2', function () {
|
||||||
gracefulShutdown('SIGUSR2')
|
gracefulShutdown('SIGUSR2')
|
||||||
});
|
})
|
||||||
|
|
||||||
process.once('SIGINT', function (code) {
|
process.once('SIGINT', function (code) {
|
||||||
gracefulShutdown('SIGINT')
|
gracefulShutdown('SIGINT')
|
||||||
});
|
})
|
||||||
|
|
||||||
process.once('SIGTERM', function (code) {
|
process.once('SIGTERM', function (code) {
|
||||||
gracefulShutdown('SIGTERM')
|
gracefulShutdown('SIGTERM')
|
||||||
});
|
})
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue