t00-multiuser/server/objects/RemoteUser.ts

57 lines
No EOL
2 KiB
TypeScript

import { createWriter, Endian } from "bufferstuff";
import IMetric from "simple-prom/lib/interfaces/IMetric";
import { WebSocket } from "ws";
import { MessageType } from "../enums/MessageType";
export default class RemoteUser {
private static USER_IDS = 0;
public readonly socket:WebSocket;
public readonly connectionUUID:string;
public readonly id:number;
public readonly username:string;
public readonly currentURL:string;
public readonly rawURL:string = "";
private readonly dataOut:IMetric;
private readonly messagesOut:IMetric;
public cursorX:number = 0;
public cursorY:number = 0;
public allowedPings:number;
public lastPingReset:number;
public userId:number;
public groupId:number = Number.MIN_VALUE;
public groupName:string;
public lastKeepAliveTime:number;
public isAfk:boolean;
public timeLastMovedCursor: number;
constructor(socket:WebSocket, dataOut:IMetric, messagesOut:IMetric, connectionUUID:string, username:string, currentURL:string, rawURL:string, userId:number, groupId:number, groupName:string) {
this.socket = socket;
this.connectionUUID = connectionUUID;
this.id = RemoteUser.USER_IDS++;
this.username = username;
this.currentURL = currentURL;
this.rawURL = rawURL;
this.dataOut = dataOut;
this.messagesOut = messagesOut;
this.allowedPings = 10;
this.lastPingReset = Date.now();
this.userId = userId;
this.groupId = groupId;
this.groupName = groupName;
this.lastKeepAliveTime = Date.now();
this.isAfk = false;
this.timeLastMovedCursor = Date.now();
}
send(data:Buffer) {
this.dataOut.add(data.length);
this.messagesOut.add(1);
this.socket.send(data);
}
sendBadge(name:string, description:string, imageUrl:string) {
const formattedDescription = description.replaceAll("\r", "").replaceAll("\n", "<br>");
this.send(createWriter(Endian.LE, 7 + name.length * 2 + formattedDescription.length * 2 + imageUrl.length * 2).writeByte(MessageType.BadgeUnlock).writeString16(name).writeString16(formattedDescription).writeString16(imageUrl).toBuffer());
}
}