import type { ServerWebSocket } from "bun"; import { Room } from "./Room"; import { proto } from "pianoverse"; import { Server } from "./Server"; export class Client { id!: string; room?: Room; uniqWsID: string; private ws: ServerWebSocket; constructor(ws: ServerWebSocket) { this.ws = ws; this.id = [...Bun.SHA256.hash(ws.remoteAddress + process.env.HASH)] .slice(0, 7) .map((z) => z.toString(16)) .join(""); this.uniqWsID = crypto.randomUUID(); } send(message: proto.ServerMessage) { this.ws.send(message.toBinary()); } alert(message: string) { this.send( new proto.ServerMessage({ event: proto.ServerMessage_EventType.MESSAGE, message: message, }) ); } backrooms() { const part = this.room?.particpiants.get(this.id); if (part) { part.removeClient(this); } else { return; } let room = Server.rooms.get("Backrooms"); if (!room) { let owner = this.id; room = new Room("Backrooms", owner); Server.rooms.set(room.name, room); } room.addClient(this.ws); Server.updateRooms(); } }