2020-09-16 16:06:43 +02:00
|
|
|
import { Group } from "./Group";
|
|
|
|
import { PointInterface } from "./Websocket/PointInterface";
|
2021-06-24 10:09:10 +02:00
|
|
|
import { Zone } from "_Model/Zone";
|
|
|
|
import { Movable } from "_Model/Movable";
|
|
|
|
import { PositionNotifier } from "_Model/PositionNotifier";
|
|
|
|
import { ServerDuplexStream } from "grpc";
|
|
|
|
import {
|
|
|
|
BatchMessage,
|
2021-12-18 11:30:58 +01:00
|
|
|
CompanionMessage,
|
|
|
|
FollowAbortMessage,
|
|
|
|
FollowConfirmationMessage,
|
2021-06-24 10:09:10 +02:00
|
|
|
PusherToBackMessage,
|
|
|
|
ServerToClientMessage,
|
|
|
|
SubMessage,
|
|
|
|
} from "../Messages/generated/messages_pb";
|
|
|
|
import { CharacterLayer } from "_Model/Websocket/CharacterLayer";
|
2020-11-13 18:00:22 +01:00
|
|
|
|
|
|
|
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;
|
2021-12-18 11:30:58 +01:00
|
|
|
private _following: User | undefined;
|
2021-12-17 18:26:17 +01:00
|
|
|
private followedBy: Set<User> = new Set<User>();
|
2020-09-16 16:06:43 +02:00
|
|
|
|
|
|
|
public constructor(
|
2020-09-18 13:57:38 +02:00
|
|
|
public id: number,
|
2020-11-13 18:00:22 +01:00
|
|
|
public readonly uuid: string,
|
2021-01-15 03:19:58 +01:00
|
|
|
public readonly IPAddress: string,
|
2020-09-25 15:25:06 +02:00
|
|
|
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,
|
2020-11-13 18:00:22 +01:00
|
|
|
public readonly socket: UserSocket,
|
|
|
|
public readonly tags: string[],
|
2021-06-24 10:09:10 +02:00
|
|
|
public readonly visitCardUrl: string | null,
|
2020-11-13 18:00:22 +01:00
|
|
|
public readonly name: string,
|
2021-04-02 21:21:11 +02:00
|
|
|
public readonly characterLayers: CharacterLayer[],
|
2021-04-06 18:54:45 +02:00
|
|
|
public readonly companion?: CompanionMessage
|
2020-09-16 16:06:43 +02:00
|
|
|
) {
|
|
|
|
this.listenedZones = new Set<Zone>();
|
2020-09-25 15:25:06 +02:00
|
|
|
|
|
|
|
this.positionNotifier.enter(this);
|
2020-09-16 16:06:43 +02:00
|
|
|
}
|
|
|
|
|
2020-09-25 15:25:06 +02:00
|
|
|
public getPosition(): PointInterface {
|
2020-09-16 16:06:43 +02:00
|
|
|
return this.position;
|
|
|
|
}
|
2020-09-25 15:25:06 +02:00
|
|
|
|
|
|
|
public setPosition(position: PointInterface): void {
|
|
|
|
const oldPosition = this.position;
|
|
|
|
this.position = position;
|
|
|
|
this.positionNotifier.updatePosition(this, position, oldPosition);
|
|
|
|
}
|
2020-11-13 18:00:22 +01:00
|
|
|
|
2021-12-17 18:26:17 +01:00
|
|
|
public addFollower(follower: User): void {
|
|
|
|
this.followedBy.add(follower);
|
|
|
|
follower._following = this;
|
|
|
|
|
|
|
|
const message = new FollowConfirmationMessage();
|
|
|
|
message.setFollower(follower.id);
|
|
|
|
message.setLeader(this.id);
|
|
|
|
const clientMessage = new ServerToClientMessage();
|
|
|
|
clientMessage.setFollowconfirmationmessage(message);
|
|
|
|
this.socket.write(clientMessage);
|
|
|
|
}
|
|
|
|
|
|
|
|
public delFollower(follower: User): void {
|
|
|
|
this.followedBy.delete(follower);
|
|
|
|
follower._following = undefined;
|
|
|
|
|
|
|
|
const message = new FollowAbortMessage();
|
|
|
|
message.setFollower(follower.id);
|
|
|
|
message.setLeader(this.id);
|
|
|
|
const clientMessage = new ServerToClientMessage();
|
|
|
|
clientMessage.setFollowabortmessage(message);
|
|
|
|
this.socket.write(clientMessage);
|
|
|
|
follower.socket.write(clientMessage);
|
|
|
|
}
|
|
|
|
|
|
|
|
public hasFollowers(): boolean {
|
|
|
|
return this.followedBy.size !== 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
get following(): User | undefined {
|
|
|
|
return this._following;
|
2021-12-13 05:06:52 +01:00
|
|
|
}
|
|
|
|
|
2021-12-17 18:26:17 +01:00
|
|
|
public stopLeading(): void {
|
|
|
|
for (const follower of this.followedBy) {
|
|
|
|
this.delFollower(follower);
|
2021-12-13 05:06:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-13 18:00:22 +01:00
|
|
|
private batchedMessages: BatchMessage = new BatchMessage();
|
2021-06-24 10:09:10 +02:00
|
|
|
private batchTimeout: NodeJS.Timeout | null = null;
|
2020-11-13 18:00:22 +01:00
|
|
|
|
|
|
|
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
|
|
|
}
|