Solve serialization

This commit is contained in:
schmelczerandras 2020-10-06 21:33:04 +02:00
parent ba8b1a29fd
commit 8e44dd3733
45 changed files with 242 additions and 201 deletions

View file

@ -7,6 +7,7 @@
"start": "concurrently --kill-others \"webpack --mode development -w\" \"nodemon --legacy-watch dist/main.js\"",
"lint": "eslint --fix \"src/**/*.ts\" && prettier --write \"src/**/*.ts\"",
"build": "webpack --mode production",
"try-build": "npm run build && node dist/main.js",
"initialize": "npm install"
},
"keywords": [],
@ -31,4 +32,4 @@
"uws": "^10.148.1",
"webpack-node-externals": "^2.5.2"
}
}
}

View file

@ -6,7 +6,7 @@ import {
applyArrayPlugins,
Random,
TransportEvents,
deserializeCommand,
deserialize,
StepCommand,
} from 'shared';
import './index.html';
@ -23,7 +23,6 @@ applyArrayPlugins();
Random.seed = 42;
const objects = new PhysicalContainer();
createDungeon(objects);
createDungeon(objects);
createDungeon(objects);
@ -59,8 +58,8 @@ app.get('/', function (req, res) {
io.on('connection', (socket: SocketIO.Socket) => {
socket.on(TransportEvents.PlayerJoining, () => {
const player = new Player(objects, socket);
socket.on(TransportEvents.PlayerToServer, (text: string) => {
const command = deserializeCommand(JSON.parse(text));
socket.on(TransportEvents.PlayerToServer, (json: string) => {
const command = deserialize(json);
player.sendCommand(command);
});

View file

@ -6,7 +6,7 @@ import {
settings,
CommandExecutors,
MoveActionCommand,
typeToBaseType,
serializable,
} from 'shared';
import { ImmutableBoundingBox } from '../physics/bounding-boxes/immutable-bounding-box';
@ -15,7 +15,7 @@ import { CirclePhysical } from './circle-physical';
import { Physical } from '../physics/physical';
import { PhysicalContainer } from '../physics/containers/physical-container';
@typeToBaseType
@serializable(CharacterBase)
export class CharacterPhysical extends CharacterBase implements Physical {
public readonly canCollide = true;
public readonly isInverted = false;
@ -166,9 +166,4 @@ export class CharacterPhysical extends CharacterBase implements Physical {
this.container.removeObject(this.leftFoot);
this.container.removeObject(this.rightFoot);
}
public toJSON(): any {
const { type, id, head, leftFoot, rightFoot } = this;
return [type, id, head, leftFoot, rightFoot];
}
}

View file

@ -4,6 +4,7 @@ import {
clamp,
CommandExecutors,
GameObject,
serializable,
settings,
StepCommand,
} from 'shared';
@ -14,6 +15,7 @@ import { BoundingBoxBase } from '../physics/bounding-boxes/bounding-box-base';
import { moveCircle } from '../physics/move-circle';
import { PhysicalContainer } from '../physics/containers/physical-container';
@serializable(Circle)
export class CirclePhysical implements Circle, Physical {
readonly isInverted = false;
readonly canCollide = true;
@ -154,8 +156,8 @@ export class CirclePhysical implements Circle, Physical {
return wasHit;
}
public toJSON(): any {
public toArray(): Array<any> {
const { center, radius } = this;
return { center, radius };
return [center, radius];
}
}

View file

@ -1,11 +1,11 @@
import { vec2, vec3 } from 'gl-matrix';
import { LampBase, settings, id, typeToBaseType } from 'shared';
import { LampBase, settings, id, serializable } from 'shared';
import { ImmutableBoundingBox } from '../physics/bounding-boxes/immutable-bounding-box';
import { Physical } from '../physics/physical';
@typeToBaseType
@serializable(LampBase)
export class LampPhysical extends LampBase implements Physical {
public readonly canCollide = false;
public readonly isInverted = false;
@ -38,9 +38,4 @@ export class LampPhysical extends LampBase implements Physical {
public distance(_: vec2): number {
return 0;
}
public toJSON(): any {
const { type, id, center, color, lightness } = this;
return [type, id, center, color, lightness];
}
}

View file

@ -1,10 +1,10 @@
import { vec2 } from 'gl-matrix';
import { clamp01, mix, TunnelBase, id, typeToBaseType } from 'shared';
import { clamp01, mix, TunnelBase, id, serializable } from 'shared';
import { ImmutableBoundingBox } from '../physics/bounding-boxes/immutable-bounding-box';
import { StaticPhysical } from '../physics/containers/static-physical-object';
@typeToBaseType
@serializable(TunnelBase)
export class TunnelPhysical extends TunnelBase implements StaticPhysical {
public readonly canCollide = true;
public readonly isInverted = true;
@ -45,9 +45,4 @@ export class TunnelPhysical extends TunnelBase implements StaticPhysical {
public get gameObject(): TunnelPhysical {
return this;
}
public toJSON(): any {
const { type, id, from, to, fromRadius, toRadius } = this;
return [type, id, from, to, fromRadius, toRadius];
}
}

View file

@ -7,6 +7,7 @@ import {
CreatePlayerCommand,
DeleteObjectsCommand,
MoveActionCommand,
serialize,
SetViewAreaActionCommand,
TransportEvents,
UpdateObjectsCommand,
@ -16,7 +17,6 @@ import { CharacterPhysical } from '../objects/character-physical';
import { BoundingBox } from '../physics/bounding-boxes/bounding-box';
import { PhysicalContainer } from '../physics/containers/physical-container';
import { Physical } from '../physics/physical';
import { jsonSerialize } from '../serialize';
export class Player extends CommandReceiver {
public isActive = true;
@ -50,7 +50,7 @@ export class Player extends CommandReceiver {
socket.emit(
TransportEvents.ServerToPlayer,
jsonSerialize(new CreatePlayerCommand(jsonSerialize(this.character)))
serialize(new CreatePlayerCommand(this.character))
);
this.sendObjects();
@ -72,13 +72,12 @@ export class Player extends CommandReceiver {
const noLongerIntersecting = this.objectsPreviouslyInViewArea.filter(
(o) => !this.objectsInViewArea.includes(o)
);
this.objectsPreviouslyInViewArea = this.objectsInViewArea;
if (noLongerIntersecting.length > 0) {
this.socket.emit(
TransportEvents.ServerToPlayer,
jsonSerialize(
serialize(
new DeleteObjectsCommand([
...new Set(noLongerIntersecting.map((p) => p.gameObject.id)),
])
@ -89,24 +88,22 @@ export class Player extends CommandReceiver {
if (newlyIntersecting.length > 0) {
this.socket.emit(
TransportEvents.ServerToPlayer,
jsonSerialize(
new CreateObjectsCommand(
jsonSerialize([...new Set(newlyIntersecting.map((p) => p.gameObject))])
)
serialize(
new CreateObjectsCommand([
...new Set(newlyIntersecting.map((p) => p.gameObject)),
])
)
);
}
this.socket.emit(
TransportEvents.ServerToPlayer,
jsonSerialize(
new UpdateObjectsCommand(
jsonSerialize([
...new Set(
this.objectsInViewArea.filter((p) => p.canMove).map((p) => p.gameObject)
),
])
)
serialize(
new UpdateObjectsCommand([
...new Set(
this.objectsInViewArea.filter((p) => p.canMove).map((p) => p.gameObject)
),
])
)
);

View file

@ -1,2 +0,0 @@
export const jsonSerialize = (o: any): string =>
JSON.stringify(o, (key, value) => (value?.toFixed ? Number(value.toFixed(3)) : value));

View file

@ -11,4 +11,4 @@
"module": "commonjs",
"esModuleInterop": true
}
}
}

View file

@ -32,29 +32,14 @@ module.exports = {
new TerserJSPlugin({
sourceMap: true,
cache: true,
test: /\.ts$/i,
terserOptions: {
ecma: 5,
warnings: true,
parse: {},
compress: { defaults: true },
mangle: true,
module: false,
output: null,
toplevel: true,
nameCache: null,
ie8: false,
keep_classnames: false,
keep_fnames: false,
safari10: false,
},
test: /\.ts$/,
}),
],
},
module: {
rules: [
{
test: /\.html$/i,
test: /\.html$/,
use: {
loader: 'file-loader',
query: {