From 37b516faee33b505531603ebe360972a1c615aed Mon Sep 17 00:00:00 2001 From: biglyderv Date: Mon, 25 Nov 2024 14:14:20 -0500 Subject: [PATCH] significant optimzations --- common/npc.js | 4 ++++ common/player.js | 10 +++++++++- game.js | 32 +++++++++++++++++++++++++++----- index.js | 24 ++++++++++++++++-------- static/js/index.js | 21 +++++++++++++++------ 5 files changed, 71 insertions(+), 20 deletions(-) diff --git a/common/npc.js b/common/npc.js index 6ef3dbb..7799bd8 100644 --- a/common/npc.js +++ b/common/npc.js @@ -13,6 +13,10 @@ class NPC { this.type = 'NPC'; this.immortal = true; + + this.serverProps = [ + 'type', 'pos','vel','you','immortal' + ]; } handleTick(game) { let { entities } = game; diff --git a/common/player.js b/common/player.js index d272465..ff0b38b 100644 --- a/common/player.js +++ b/common/player.js @@ -26,11 +26,19 @@ class Player { this.isMenu = false; this.r = 1; + + this.serverProps = [ + 'type', 'camera','pos','vel','rot','dir','ticks','health','you','isPlayer','headCount','isMenu','r' + ]; + + this.legalProps = [ + 'vel','dir','camera','ticks','isMenu','r' + ]; } bump() { let player = this; - if (player.ticks < 7) { + if (player.ticks < 15) { player.dir *= -1; } diff --git a/game.js b/game.js index fb52290..979367e 100644 --- a/game.js +++ b/game.js @@ -6,19 +6,40 @@ class Game extends GameBasic { super(); this.ws = []; } - sync() { + sync(full = false) { let { entities } = this; let entList = []; for (let entity of entities) { - let { pos, vel, rot, dir, health, headCount, you, camera, ticks, type, isMenu, r, playing } = entity; - entList.push({ pos, vel, rot, dir, health, headCount, you, camera, ticks, type, isMenu, r, playing }); + entity.pos = { x: Math.round(entity.pos.x), y: Math.round(entity.pos.y) }; + entity.vel = { x: Math.round(entity.vel.x), y: Math.round(entity.vel.y) }; + entList.push(entity); } if (entList.length == 0) return; for (let client of this.ws) { if (!client.active) continue; - client.send(JSON.stringify(entList)); + + let wsEnt = client.ent; + if (!wsEnt) continue; + + let filtered; + + if (!full) { + filtered = entList.filter((ent) => + (Math.sqrt(((ent.pos.x - wsEnt.pos.x) ** 2) + ((ent.pos.y - wsEnt.pos.y) ** 2))) < 777 + ); + + } else { + filtered = entList; + } + + filtered= filtered.map(x => { + let props = x.serverProps; + return props.map(prop => x[prop]); + }); + + client.send(JSON.stringify(filtered)); } for (let entity of entities) { @@ -35,7 +56,8 @@ class Game extends GameBasic { that.entities.push(new NPC()) } - setInterval(function () { that.sync() }, 1000 / 5); + setInterval(function () { that.sync(false) }, 1000 / 10); + setInterval(function () { that.sync(true) }, 1000); } } diff --git a/index.js b/index.js index 11df49e..c90a58e 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,7 @@ import express from "express"; import expressW from "express-ws"; import Game from "./game.js"; +import NPC from "./common/npc.js"; import Player from "./common/player.js"; var app = express(); @@ -21,7 +22,8 @@ app.ws('/', function (ws, req) { game.entities[playerI] = player; ws.active = true; - + ws.ent = player; + // This will only work under NGINX. console.log(`A player ${player.you} joined under IP ${req.headers["x-real-ip"]}`) @@ -31,18 +33,24 @@ app.ws('/', function (ws, req) { data = JSON.parse(msg); } catch (err) { console.log(err); - data = {}; + data = []; } - let { vel, dir, you, ticks, isMenu, r, playing } = data; - let data2 = { vel, dir, you, ticks, isMenu, r, playing }; - let you2 = game.entities[playerI].you; + let newEnt = game.entities[playerI]; + + let {you} = newEnt; + + let props = player.legalProps; + + for (let i in props) { + newEnt[props[i]] = data[i]; + } + + let you2 = newEnt.you; if (you != you2) { - console.log(`A player ${you2} now identifies as ${you}`); + console.log(`A player ${you} now identifies as ${you2}`); } - - game.entities[playerI] = Object.assign(game.entities[playerI], data2); }) ws.on('close', function () { diff --git a/static/js/index.js b/static/js/index.js index bec2c99..c5afa5a 100644 --- a/static/js/index.js +++ b/static/js/index.js @@ -137,9 +137,7 @@ class Game extends GameBasic { sync() { let { player } = this; - let { vel, dir, you, ticks, isMenu, r, playing } = player; - - this.ws.send(JSON.stringify({ vel, dir, you, ticks, isMenu, r, playing })); + this.ws.send(JSON.stringify(player.legalProps.map(prop => player[prop]))); } recv({ data }) { let { player } = this; @@ -148,10 +146,19 @@ class Game extends GameBasic { let that = this; let entList = JSON.parse(data); - entList = entList.map(x => { - let type = (Object.keys(legalTypes).indexOf(x.type) == -1) ? Player : legalTypes[x.type]; + entList = entList.map(y => { + let type = (Object.keys(legalTypes).indexOf(y[0]) == -1) ? Player : legalTypes[y[0]]; + + let props = new type().serverProps; + + let x = {}; + + for (let i in props) { + x[props[i]] = y[i]; + } + x.handleTick = type.prototype.handleTick; - if (x.r != 1) { + if (x.r != 1 && type == 'Player') { let a = new Audio(`assets/${emojis[x.r]}.wav`); a.addEventListener('ended', () => { if (x.you == you) { @@ -170,6 +177,8 @@ class Game extends GameBasic { let matchingPlayer = entList.filter(x => x.you == you) + if (matchingPlayer.length == 0) matchingPlayer = entList.filter(x => x.type == 'Player'); + this.player = Object.assign(this.player || new Player(false, false), matchingPlayer[0]); if (this.player.r != 1) {