From afb8d8c740e49df85c97bf7a4c7506438a719e6b Mon Sep 17 00:00:00 2001 From: Holly Date: Thu, 26 Sep 2024 10:31:24 +0100 Subject: [PATCH] badges --- server/entities/Badge.ts | 25 +++++++++++++++++++ server/index.ts | 11 +++++++-- server/objects/RemoteUser.ts | 5 +++- server/repos/BadgeRepo.ts | 44 +++++++++++++++++++++++++++++++++ server/services/BadgeService.ts | 0 5 files changed, 82 insertions(+), 3 deletions(-) create mode 100644 server/entities/Badge.ts create mode 100644 server/repos/BadgeRepo.ts create mode 100644 server/services/BadgeService.ts diff --git a/server/entities/Badge.ts b/server/entities/Badge.ts new file mode 100644 index 0000000..c8d7fbb --- /dev/null +++ b/server/entities/Badge.ts @@ -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; + } +} \ No newline at end of file diff --git a/server/index.ts b/server/index.ts index a7cd889..0165e9d 100644 --- a/server/index.ts +++ b/server/index.ts @@ -17,9 +17,15 @@ onlineUsersUnique.setHelpText("Number of unique user connections to the websocke const dataIn = metrics.addMetric(new Counter("multiprobe_data_in")); 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")); 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 { WebSocketServer } from "ws"; import Fastify from "fastify"; @@ -194,6 +200,7 @@ websocketServer.on("connection", (socket) => { socket.on("message", async (data) => { const reader = createReader(Endian.LE, data as Buffer); dataIn.add(reader.length); + messagesIn.add(1); if (reader.length > 0 && reader.length < 1024) { switch (reader.readUByte()) { 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); } 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 { - 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()); user.send(usersToSend.toBuffer()); diff --git a/server/objects/RemoteUser.ts b/server/objects/RemoteUser.ts index b8d650d..f3c5a1f 100644 --- a/server/objects/RemoteUser.ts +++ b/server/objects/RemoteUser.ts @@ -11,6 +11,7 @@ export default class RemoteUser { 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; @@ -22,7 +23,7 @@ export default class RemoteUser { public isAfk:boolean; 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.connectionUUID = connectionUUID; this.id = RemoteUser.USER_IDS++; @@ -30,6 +31,7 @@ export default class RemoteUser { this.currentURL = currentURL; this.rawURL = rawURL; this.dataOut = dataOut; + this.messagesOut = messagesOut; this.allowedPings = 10; this.lastPingReset = Date.now(); this.userId = userId; @@ -42,6 +44,7 @@ export default class RemoteUser { send(data:Buffer) { this.dataOut.add(data.length); + this.messagesOut.add(1); this.socket.send(data); } } \ No newline at end of file diff --git a/server/repos/BadgeRepo.ts b/server/repos/BadgeRepo.ts new file mode 100644 index 0000000..b56da6d --- /dev/null +++ b/server/repos/BadgeRepo.ts @@ -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(); + + 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; +} \ No newline at end of file diff --git a/server/services/BadgeService.ts b/server/services/BadgeService.ts new file mode 100644 index 0000000..e69de29