partey_workadventure/back/src/Model/User.ts

67 lines
2.3 KiB
TypeScript
Raw Normal View History

2020-09-16 16:06:43 +02:00
import { Group } from "./Group";
import { PointInterface } from "./Websocket/PointInterface";
import {Zone} from "_Model/Zone";
import {Movable} from "_Model/Movable";
import {PositionNotifier} from "_Model/PositionNotifier";
2020-12-04 15:58:31 +01:00
import {ServerDuplexStream} from "grpc";
2021-04-02 21:21:11 +02:00
import {BatchMessage, Companion, PusherToBackMessage, ServerToClientMessage, SubMessage} from "../Messages/generated/messages_pb";
2020-12-04 15:58:31 +01:00
import {CharacterLayer} from "_Model/Websocket/CharacterLayer";
export type UserSocket = ServerDuplexStream<PusherToBackMessage, ServerToClientMessage>;
2020-09-16 16:06:43 +02:00
export class User implements Movable {
public listenedZones: Set<Zone>;
public group?: Group;
public constructor(
public id: number,
public readonly uuid: string,
2021-01-15 03:19:58 +01:00
public readonly IPAddress: string,
private position: PointInterface,
2020-09-16 16:06:43 +02:00
public silent: boolean,
2020-09-29 16:01:22 +02:00
private positionNotifier: PositionNotifier,
public readonly socket: UserSocket,
public readonly tags: string[],
public readonly name: string,
2021-04-02 21:21:11 +02:00
public readonly characterLayers: CharacterLayer[],
public readonly companion?: Companion
2020-09-16 16:06:43 +02:00
) {
this.listenedZones = new Set<Zone>();
this.positionNotifier.enter(this);
2020-09-16 16:06:43 +02:00
}
public getPosition(): PointInterface {
2020-09-16 16:06:43 +02:00
return this.position;
}
public setPosition(position: PointInterface): void {
const oldPosition = this.position;
this.position = position;
this.positionNotifier.updatePosition(this, position, oldPosition);
}
private batchedMessages: BatchMessage = new BatchMessage();
private batchTimeout: NodeJS.Timeout|null = null;
public emitInBatch(payload: SubMessage): void {
this.batchedMessages.addPayload(payload);
if (this.batchTimeout === null) {
this.batchTimeout = setTimeout(() => {
/*if (socket.disconnecting) {
return;
}*/
const serverToClientMessage = new ServerToClientMessage();
serverToClientMessage.setBatchmessage(this.batchedMessages);
this.socket.write(serverToClientMessage);
this.batchedMessages = new BatchMessage();
this.batchTimeout = null;
}, 100);
}
}
2020-09-16 16:06:43 +02:00
}