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

@ -1,4 +1,4 @@
import { Command, CommandReceiver, TransportEvents } from 'shared';
import { Command, CommandReceiver, serialize, TransportEvents } from 'shared';
export class CommandReceiverSocket extends CommandReceiver {
constructor(private readonly socket: SocketIOClient.Socket) {
@ -6,6 +6,6 @@ export class CommandReceiverSocket extends CommandReceiver {
}
protected defaultCommandExecutor(command: Command) {
this.socket.emit(TransportEvents.PlayerToServer, JSON.stringify(command));
this.socket.emit(TransportEvents.PlayerToServer, serialize(command));
}
}

View file

@ -12,7 +12,9 @@ import {
} from 'sdf-2d';
import {
broadcastCommands,
deserialize,
prettyPrint,
serialize,
settings,
SetViewAreaActionCommand,
StepCommand,
@ -27,9 +29,13 @@ import { RenderCommand } from './commands/types/render';
import { Configuration } from './config/configuration';
import { DeltaTimeCalculator } from './helper/delta-time-calculator';
import { rgb } from './helper/rgb';
import { CharacterView } from './objects/character-view';
import { TunnelView } from './objects/tunnel-view';
import { LampView } from './objects/lamp-view';
import { GameObjectContainer } from './objects/game-object-container';
import { BlobShape } from './shapes/blob-shape';
import { deserialize } from './transport/deserialize';
const a = [CharacterView, TunnelView, LampView];
export class Game {
public readonly gameObjects = new GameObjectContainer();
@ -149,7 +155,7 @@ export class Game {
// todo: Should only send aspect ratio
this.socket.emit(
TransportEvents.PlayerToServer,
JSON.stringify(new SetViewAreaActionCommand(this.gameObjects.camera.viewArea))
serialize(new SetViewAreaActionCommand(this.gameObjects.camera.viewArea))
);
}

View file

@ -1,8 +1,9 @@
import { vec2 } from 'gl-matrix';
import { CharacterBase, CommandExecutors } from 'shared';
import { CharacterBase, CommandExecutors, deserializable } from 'shared';
import { RenderCommand } from '../commands/types/render';
import { BlobShape } from '../shapes/blob-shape';
@deserializable(CharacterBase)
export class CharacterView extends CharacterBase {
private shape = new BlobShape();

View file

@ -10,7 +10,6 @@ import {
StepCommand,
UpdateObjectsCommand,
} from 'shared';
import { deserialize, deserializeJsonArray } from '../transport/deserialize';
import { Camera } from './camera';
import { CharacterView } from './character-view';
@ -21,7 +20,7 @@ export class GameObjectContainer extends CommandReceiver {
protected commandExecutors: CommandExecutors = {
[CreatePlayerCommand.type]: (c: CreatePlayerCommand) => {
this.player = deserialize(c.serializedPlayer) as CharacterView;
this.player = c.character as CharacterView;
this.camera = new Camera();
this.addObject(this.player);
this.addObject(this.camera);
@ -34,13 +33,13 @@ export class GameObjectContainer extends CommandReceiver {
},
[CreateObjectsCommand.type]: (c: CreateObjectsCommand) =>
deserializeJsonArray(c.serializedObjects).forEach((o) => this.addObject(o)),
c.objects.forEach((o) => this.addObject(o)),
[DeleteObjectsCommand.type]: (c: DeleteObjectsCommand) =>
c.ids.forEach((id: Id) => this.objects.delete(id)),
[UpdateObjectsCommand.type]: (c: UpdateObjectsCommand) =>
deserializeJsonArray(c.serializedObjects).forEach((o) => {
c.objects.forEach((o) => {
this.objects.delete(o.id);
this.addObject(o);
if (o.id === this.player.id) {

View file

@ -1,8 +1,9 @@
import { vec2, vec3 } from 'gl-matrix';
import { CircleLight } from 'sdf-2d';
import { CommandExecutors, Id, LampBase } from 'shared';
import { CommandExecutors, deserializable, Id, LampBase } from 'shared';
import { RenderCommand } from '../commands/types/render';
@deserializable(LampBase)
export class LampView extends LampBase {
private light: CircleLight;

View file

@ -1,8 +1,9 @@
import { vec2 } from 'gl-matrix';
import { InvertedTunnel } from 'sdf-2d';
import { CommandExecutors, Id, TunnelBase } from 'shared';
import { CommandExecutors, deserializable, Id, TunnelBase } from 'shared';
import { RenderCommand } from '../commands/types/render';
@deserializable(TunnelBase)
export class TunnelView extends TunnelBase {
private shape: InvertedTunnel;

View file

@ -1,28 +0,0 @@
import {
CharacterBase,
commandConstructors,
GameObject,
LampBase,
TunnelBase,
} from 'shared';
import { CharacterView } from '../objects/character-view';
import { LampView } from '../objects/lamp-view';
import { TunnelView } from '../objects/tunnel-view';
const constructors: { [type: string]: new (...values: Array<any>) => GameObject } = {
[TunnelBase.type]: TunnelView,
[LampBase.type]: LampView,
[CharacterBase.type]: CharacterView,
...commandConstructors,
};
export const deserialize = (json: string): GameObject => {
const [type, ...values] = JSON.parse(json);
return new constructors[type](...values);
};
export const deserializeJsonArray = (json: string): Array<GameObject> => {
return (JSON.parse(json) as Array<any>).map(([type, ...values]) => {
return new constructors[type](...values);
});
};