pianoverse_server/src/Client.ts
2024-07-19 03:07:27 +03:00

53 lines
1.1 KiB
TypeScript

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<Client>;
constructor(ws: ServerWebSocket<Client>) {
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();
}
}