From 7b286806d07d73d85cad0d27ee3827810651eded Mon Sep 17 00:00:00 2001 From: cha0s Date: Fri, 31 May 2024 08:33:11 -0500 Subject: [PATCH] feat: basic latency simulation --- src/constants.js | 6 ++++++ src/net/client/client.js | 12 +++++++++++- src/net/server/server.js | 16 +++++++++++++--- 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/constants.js b/src/constants.js index d7d729a..9a53d81 100644 --- a/src/constants.js +++ b/src/constants.js @@ -1,8 +1,14 @@ +export const CLIENT_LATENCY = 100; + export const RESOLUTION = [ 800, 450, ]; +export const SERVER_LATENCY = 100; + +export const TPS = 60; + export const ACTION_MAP = { w: 'moveUp', d: 'moveRight', diff --git a/src/net/client/client.js b/src/net/client/client.js index 067ce5e..5a6112c 100644 --- a/src/net/client/client.js +++ b/src/net/client/client.js @@ -1,3 +1,5 @@ +import {CLIENT_LATENCY} from '@/constants.js'; + import Packet from '../packet/packet.js'; export default class Client { @@ -20,6 +22,14 @@ export default class Client { } } send(packet) { - this.transmit(Packet.transmit(packet)); + if (CLIENT_LATENCY > 0) { + setTimeout(() => { + this.transmit(Packet.transmit(packet)); + }, CLIENT_LATENCY); + } + else { + this.transmit(Packet.transmit(packet)); + } + } } diff --git a/src/net/server/server.js b/src/net/server/server.js index 3d388a8..68a63dd 100644 --- a/src/net/server/server.js +++ b/src/net/server/server.js @@ -1,10 +1,13 @@ -import {MOVE_MAP} from '@/constants.js'; +import { + MOVE_MAP, + SERVER_LATENCY, + TPS, +} from '@/constants.js'; import Cell from '@/cell.js'; import {System} from '@/ecs/index.js'; import Packet from "../packet/packet.js"; const SPEED = 100; -const TPS = 60; await import ('@/isomorphinit.js'); @@ -82,7 +85,14 @@ export default class Server { } send(connection, packet) { - this.transmit(connection, Packet.transmit(packet)); + if (SERVER_LATENCY > 0) { + setTimeout(() => { + this.transmit(connection, Packet.transmit(packet)); + }, SERVER_LATENCY); + } + else { + this.transmit(connection, Packet.transmit(packet)); + } } start() {