From f14c3dc0548e18b55d22c153a5474d103c2de4e5 Mon Sep 17 00:00:00 2001 From: Isaac Thoman <49598528+IsaacThoman@users.noreply.github.com> Date: Fri, 3 Jan 2025 16:36:02 -0500 Subject: [PATCH 1/2] make player class more concise --- src/server/DataValidator.ts | 32 +++++++++-- src/shared/Player.ts | 112 +++++++++--------------------------- 2 files changed, 52 insertions(+), 92 deletions(-) diff --git a/src/server/DataValidator.ts b/src/server/DataValidator.ts index 5909410..ef6bbee 100644 --- a/src/server/DataValidator.ts +++ b/src/server/DataValidator.ts @@ -43,12 +43,12 @@ export class DataValidator { gameVersion: z.string().refine((val) => val === this.SERVER_VERSION, { message: `Game version must be ${this.SERVER_VERSION}`, }), - position: DataValidator.vector3Schema, - velocity: DataValidator.vector3Schema, - inputVelocity: DataValidator.vector3Schema, + position: this.vector3Schema, + velocity: this.vector3Schema, + inputVelocity: this.vector3Schema, gravity: z.number(), - lookQuaternion: DataValidator.quaternionSchema, - quaternion: DataValidator.quaternionSchema, + lookQuaternion: this.quaternionSchema, + quaternion: this.quaternionSchema, chatActive: z.boolean(), chatMsg: z.string().max(300), latency: z.number(), @@ -62,7 +62,27 @@ export class DataValidator { playerSpectating: z.number(), gameMsgs: z.array(z.string()), gameMsgs2: z.array(z.string()), - }).strict().transform((data) => Player.fromObject(data)); + }).strict().transform((data) => { + const withVectors = { + ...data, + position: new THREE.Vector3(data.position.x, data.position.y, data.position.z), + velocity: new THREE.Vector3(data.velocity.x, data.velocity.y, data.velocity.z), + inputVelocity: new THREE.Vector3(data.inputVelocity.x, data.inputVelocity.y, data.inputVelocity.z), + lookQuaternion: new THREE.Quaternion( + data.lookQuaternion.x, + data.lookQuaternion.y, + data.lookQuaternion.z, + data.lookQuaternion.w, + ), + quaternion: new THREE.Quaternion( + data.quaternion.x, + data.quaternion.y, + data.quaternion.z, + data.quaternion.w, + ), + }; + return Player.fromObject(withVectors as Player); + }); static chatMsgSchema = z.object({ id: z.number(), diff --git a/src/shared/Player.ts b/src/shared/Player.ts index 8ecb840..a3cc79f 100644 --- a/src/shared/Player.ts +++ b/src/shared/Player.ts @@ -4,110 +4,50 @@ import * as THREE from 'three'; export type PlayerData = z.input; -// All public class fields as an object without functions -// deno-lint-ignore ban-types -type PlayerFields = { [K in keyof Player as Player[K] extends Function ? never : K]: Player[K] }; - export class Player { - public position: THREE.Vector3; - public velocity: THREE.Vector3; - public inputVelocity: THREE.Vector3; - public gravity: number; - public lookQuaternion: THREE.Quaternion; - public quaternion: THREE.Quaternion; - public id: number; - public gameVersion: string; - public name: string; - public speed: number; - public acceleration: number; - public chatActive: boolean; - public chatMsg: string; - public latency: number; - public health: number; - public forced: boolean; - public forcedAcknowledged: boolean; - public inventory: number[]; - public idLastDamagedBy?: number; - public playerSpectating: number; - public gameMsgs: string[]; - public gameMsgs2: string[]; + public position = new THREE.Vector3(0, 100, 0); + public velocity = new THREE.Vector3(0, 0, 0); + public inputVelocity = new THREE.Vector3(); + public gravity = 0; + public lookQuaternion = new THREE.Quaternion(); + public quaternion = new THREE.Quaternion(); + public id = Math.floor(Math.random() * 1000000000); + public gameVersion = ''; + public name = ''; + public speed = 5; + public acceleration = 100; + public chatActive = false; + public chatMsg = ''; + public latency = 1000; + public health = 100; + public forced = false; + public forcedAcknowledged = false; + public inventory: number[] = []; + public idLastDamagedBy?: number = -1; + public playerSpectating = -1; + public gameMsgs: string[] = []; + public gameMsgs2: string[] = []; public updateTimestamp?: number; public lastDamageTime?: number; - constructor() { - this.position = new THREE.Vector3(0, 100, 0); - this.velocity = new THREE.Vector3(0, 0, 0); - this.inputVelocity = new THREE.Vector3(); - this.gravity = 0; - this.lookQuaternion = new THREE.Quaternion(); - this.quaternion = new THREE.Quaternion(); - this.id = Math.floor(Math.random() * 1000000000); - this.gameVersion = ''; - this.name = ''; - this.speed = 5; - this.acceleration = 100; - this.chatActive = false; - this.chatMsg = ''; - this.latency = 1000; - this.health = 100; - this.forced = false; - this.forcedAcknowledged = false; - this.inventory = []; - this.idLastDamagedBy = -1; - this.playerSpectating = -1; - this.gameMsgs = []; - this.gameMsgs2 = []; - this.updateTimestamp = undefined; - this.lastDamageTime = undefined; - } - - static fromObject(data: PlayerFields) { + static fromObject(data: Player): Player { const instance = new Player(); Object.assign(instance, data); - return instance; } // Gets called by Socket.IO during JSON.stringify, this will match our zod schema (PlayerData) toJSON(): PlayerData { - const serializableVec3 = ({ x, y, z }: THREE.Vector3) => ({ - x, - y, - z, - }); - - const serializableQuaternion = ({ x, y, z, w }: THREE.Quaternion) => ({ - x, - y, - z, - w, - }); + const serializableVec3 = ({ x, y, z }: THREE.Vector3) => ({ x, y, z }); + const serializableQuaternion = ({ x, y, z, w }: THREE.Quaternion) => ({ x, y, z, w }); return { - id: this.id, - speed: this.speed, - acceleration: this.acceleration, - name: this.name, - gameVersion: this.gameVersion, + ...this, position: serializableVec3(this.position), velocity: serializableVec3(this.velocity), inputVelocity: serializableVec3(this.inputVelocity), - gravity: this.gravity, lookQuaternion: serializableQuaternion(this.lookQuaternion), quaternion: serializableQuaternion(this.quaternion), - chatActive: this.chatActive, - chatMsg: this.chatMsg, - latency: this.latency, - health: this.health, - forced: this.forced, - forcedAcknowledged: this.forcedAcknowledged, - inventory: this.inventory, - idLastDamagedBy: this.idLastDamagedBy, - playerSpectating: this.playerSpectating, - gameMsgs: this.gameMsgs, - gameMsgs2: this.gameMsgs2, - updateTimestamp: this.updateTimestamp, - lastDamageTime: this.lastDamageTime, }; } } From e30adc9b8e76dd3615dd05d83dc8020c0ff51fe6 Mon Sep 17 00:00:00 2001 From: Isaac Thoman <49598528+IsaacThoman@users.noreply.github.com> Date: Fri, 3 Jan 2025 16:46:22 -0500 Subject: [PATCH 2/2] remove redundancy in dataValidator --- src/server/DataValidator.ts | 22 +--------------------- 1 file changed, 1 insertion(+), 21 deletions(-) diff --git a/src/server/DataValidator.ts b/src/server/DataValidator.ts index ef6bbee..64f0c1a 100644 --- a/src/server/DataValidator.ts +++ b/src/server/DataValidator.ts @@ -62,27 +62,7 @@ export class DataValidator { playerSpectating: z.number(), gameMsgs: z.array(z.string()), gameMsgs2: z.array(z.string()), - }).strict().transform((data) => { - const withVectors = { - ...data, - position: new THREE.Vector3(data.position.x, data.position.y, data.position.z), - velocity: new THREE.Vector3(data.velocity.x, data.velocity.y, data.velocity.z), - inputVelocity: new THREE.Vector3(data.inputVelocity.x, data.inputVelocity.y, data.inputVelocity.z), - lookQuaternion: new THREE.Quaternion( - data.lookQuaternion.x, - data.lookQuaternion.y, - data.lookQuaternion.z, - data.lookQuaternion.w, - ), - quaternion: new THREE.Quaternion( - data.quaternion.x, - data.quaternion.y, - data.quaternion.z, - data.quaternion.w, - ), - }; - return Player.fromObject(withVectors as Player); - }); + }).strict().transform((data) => Player.fromObject(data as Player)); static chatMsgSchema = z.object({ id: z.number(),