This commit is contained in:
Holly Stubbs 2024-09-26 10:31:24 +01:00
parent 2c88577c2f
commit afb8d8c740
Signed by: tgpholly
GPG key ID: B8583C4B7D18119E
5 changed files with 82 additions and 3 deletions

25
server/entities/Badge.ts Normal file
View file

@ -0,0 +1,25 @@
export default class Badge {
public Id: number;
public Name: string;
public Description: string;
public ImageUrl: string;
public ForUrl: string;
public CreatedByUserId: number;
public CreatedDatetime: Date;
public LastModifiedByUserId?: number;
public LastModifiedDatetime?: Date;
public DeletedByUserId?: number;
public DeletedDatetime?: Date;
public IsDeleted: boolean;
public constructor() {
this.Id = Number.MIN_VALUE;
this.Name = "";
this.Description = "";
this.ImageUrl = "";
this.ForUrl = "";
this.CreatedByUserId = Number.MIN_VALUE;
this.CreatedDatetime = new Date(0);
this.IsDeleted = false;
}
}

View file

@ -17,9 +17,15 @@ onlineUsersUnique.setHelpText("Number of unique user connections to the websocke
const dataIn = metrics.addMetric(new Counter("multiprobe_data_in")); const dataIn = metrics.addMetric(new Counter("multiprobe_data_in"));
dataIn.setHelpText("Data received by the server in bytes"); dataIn.setHelpText("Data received by the server in bytes");
const messagesIn = metrics.addMetric(new Counter("multiprobe_msg_in"));
dataIn.setHelpText("Total messages received by the server");
const dataOut = metrics.addMetric(new Counter("multiprobe_data_out")); const dataOut = metrics.addMetric(new Counter("multiprobe_data_out"));
dataOut.setHelpText("Data sent by the server in bytes"); dataOut.setHelpText("Data sent by the server in bytes");
const messagesOut = metrics.addMetric(new Counter("multiprobe_msg_out"));
dataIn.setHelpText("Total messages sent by the server");
import { createReader, createWriter, Endian } from "bufferstuff"; import { createReader, createWriter, Endian } from "bufferstuff";
import { WebSocketServer } from "ws"; import { WebSocketServer } from "ws";
import Fastify from "fastify"; import Fastify from "fastify";
@ -194,6 +200,7 @@ websocketServer.on("connection", (socket) => {
socket.on("message", async (data) => { socket.on("message", async (data) => {
const reader = createReader(Endian.LE, data as Buffer); const reader = createReader(Endian.LE, data as Buffer);
dataIn.add(reader.length); dataIn.add(reader.length);
messagesIn.add(1);
if (reader.length > 0 && reader.length < 1024) { if (reader.length > 0 && reader.length < 1024) {
switch (reader.readUByte()) { switch (reader.readUByte()) {
case MessageType.KeepAlive: case MessageType.KeepAlive:
@ -237,9 +244,9 @@ websocketServer.on("connection", (socket) => {
usersToSend.writeUInt(otherUser.id).writeShortString(otherUser.username).writeFloat(otherUser.cursorX).writeInt(otherUser.cursorY).writeBool(otherUser.isAfk); usersToSend.writeUInt(otherUser.id).writeShortString(otherUser.username).writeFloat(otherUser.cursorX).writeInt(otherUser.cursorY).writeBool(otherUser.isAfk);
} }
if (dbParty) { if (dbParty) {
user = users.set(myUUID, new RemoteUser(socket, dataOut, myUUID, dbUser.Username, page, rawURL, dbUser.Id, dbParty.Id, dbParty.Name)); user = users.set(myUUID, new RemoteUser(socket, dataOut, messagesOut, myUUID, dbUser.Username, page, rawURL, dbUser.Id, dbParty.Id, dbParty.Name));
} else { } else {
user = users.set(myUUID, new RemoteUser(socket, dataOut, myUUID, dbUser.Username, page, rawURL, dbUser.Id, Number.MIN_VALUE, "")); user = users.set(myUUID, new RemoteUser(socket, dataOut, messagesOut, myUUID, dbUser.Username, page, rawURL, dbUser.Id, Number.MIN_VALUE, ""));
} }
sendToAllButSelf(user, createWriter(Endian.LE, 6 + dbUser.Username.length).writeByte(MessageType.ClientJoined).writeUInt(user.id).writeShortString(dbUser.Username).toBuffer()); sendToAllButSelf(user, createWriter(Endian.LE, 6 + dbUser.Username.length).writeByte(MessageType.ClientJoined).writeUInt(user.id).writeShortString(dbUser.Username).toBuffer());
user.send(usersToSend.toBuffer()); user.send(usersToSend.toBuffer());

View file

@ -11,6 +11,7 @@ export default class RemoteUser {
public readonly currentURL:string; public readonly currentURL:string;
public readonly rawURL:string = ""; public readonly rawURL:string = "";
private readonly dataOut:IMetric; private readonly dataOut:IMetric;
private readonly messagesOut:IMetric;
public cursorX:number = 0; public cursorX:number = 0;
public cursorY:number = 0; public cursorY:number = 0;
public allowedPings:number; public allowedPings:number;
@ -22,7 +23,7 @@ export default class RemoteUser {
public isAfk:boolean; public isAfk:boolean;
public timeLastMovedCursor: number; public timeLastMovedCursor: number;
constructor(socket:WebSocket, dataOut:IMetric, connectionUUID:string, username:string, currentURL:string, rawURL:string, userId:number, groupId:number, groupName:string) { 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.socket = socket;
this.connectionUUID = connectionUUID; this.connectionUUID = connectionUUID;
this.id = RemoteUser.USER_IDS++; this.id = RemoteUser.USER_IDS++;
@ -30,6 +31,7 @@ export default class RemoteUser {
this.currentURL = currentURL; this.currentURL = currentURL;
this.rawURL = rawURL; this.rawURL = rawURL;
this.dataOut = dataOut; this.dataOut = dataOut;
this.messagesOut = messagesOut;
this.allowedPings = 10; this.allowedPings = 10;
this.lastPingReset = Date.now(); this.lastPingReset = Date.now();
this.userId = userId; this.userId = userId;
@ -42,6 +44,7 @@ export default class RemoteUser {
send(data:Buffer) { send(data:Buffer) {
this.dataOut.add(data.length); this.dataOut.add(data.length);
this.messagesOut.add(1);
this.socket.send(data); this.socket.send(data);
} }
} }

44
server/repos/BadgeRepo.ts Normal file
View file

@ -0,0 +1,44 @@
import Badge from "../entities/Badge";
import Database from "../objects/Database";
import RepoBase from "./RepoBase";
export default abstract class BadgeRepo {
public static async selectAll() {
const dbUser = await Database.Instance.query("SELECT * FROM Badge WHERE IsDeleted = 0");
const badges = new Array<Badge>();
for (const row of dbUser) {
const badge = new Badge();
populateBadgeFromDB(badge, row);
badges.push(badge);
}
return badges;
}
public static async selectById(id:number) {
const dbBadge = await Database.Instance.query("SELECT * FROM Badge WHERE Id = ? AND IsDeleted = 0 LIMIT 1", [id]);
if (dbBadge == null || dbBadge.length === 0) {
return null;
} else {
const badge = new Badge();
populateBadgeFromDB(badge, dbBadge[0]);
return badge;
}
}
}
function populateBadgeFromDB(badge:Badge, dbBadge:any) {
badge.Id = dbBadge.Id;
badge.Name = dbBadge.Name;
badge.Description = dbBadge.Description;
badge.ImageUrl = dbBadge.ImageUrl;
badge.ForUrl = dbBadge.ForUrl;
badge.CreatedByUserId = dbBadge.CreatedByUserId;
badge.CreatedDatetime = new Date(dbBadge.CreatedDatetime);
badge.LastModifiedByUserId = dbBadge.LastModifiedByUserId;
badge.LastModifiedDatetime = RepoBase.convertNullableDatetimeIntToDate(dbBadge.LastModifiedDatetime);
badge.DeletedByUserId = dbBadge.DeletedByUserId;
badge.DeletedDatetime = RepoBase.convertNullableDatetimeIntToDate(dbBadge.DeletedDatetime);
badge.IsDeleted = dbBadge.IsDeleted === 1;
}

View file