save room ownership and chats

This commit is contained in:
Soph :3 2024-07-19 20:03:15 +03:00
parent ea74b57c74
commit 9969a17590
Signed by: sophie
GPG key ID: EDA5D222A0C270F2
3 changed files with 72 additions and 28 deletions

1
.gitignore vendored
View file

@ -174,3 +174,4 @@ dist
# Finder (MacOS) folder config
.DS_Store
DATABASE.db
chats

View file

@ -4,7 +4,8 @@ import { Client } from "./Client";
import { proto } from "pianoverse";
import { addRoomBan, getRoomBans } from "./Database";
import { Server } from "./Server";
import { writeFileSync, existsSync, readFileSync } from "fs";
import { join } from "path";
interface Ban {
banTime: number;
minutes: number;
@ -19,26 +20,48 @@ export class Room {
constructor(name: string, owner: string) {
this.name = name;
if(this.name == "Lobby" || this.name == "Backrooms") {
if (Server.specialRooms.includes(this.name)) {
this.owner = "";
} else {
this.owner = owner;
}
this.systemMessage("This is a development server. Instability may occur. Written by @fucksophie")
if (existsSync("chats/" + this.name)) {
const welcome = proto.ServerMessage.fromBinary(
readFileSync("chats/" + this.name)
).welcome;
if (!welcome) return;
this.chats = welcome.chat;
this.owner = welcome.owner;
} else {
this.systemMessage(
"This is a development server. Instability may occur. Written by @fucksophie"
);
}
}
private addToChatArray(chat: proto.ServerMessage_Chat) {
this.chats.push(chat);
if (this.chats.length > 150) this.chats.shift();
writeFileSync(
join("chats", this.name),
new proto.ServerMessage({
event: proto.ServerMessage_EventType.WELCOME,
welcome: {
chat: this!.chats,
owner: this.owner,
},
}).toBinary()
);
}
systemMessage(message: string) {
const system = Server.systemMessage(message);
this.addToChatArray(system.chat!);
this.particpiants.forEach((z) => {
z.clients.forEach(g => {
z.clients.forEach((g) => {
g.send(system);
})
});
});
}
chat(ws: ServerWebSocket<Client>, message: string) {
@ -63,15 +86,15 @@ export class Room {
}
ban(ws: ServerWebSocket<Client>, minutes: number) {
if(this.name == 'Backrooms') return;
addRoomBan(ws.data.id, this.name, minutes, Date.now())
if (this.name == "Backrooms") return;
addRoomBan(ws.data.id, this.name, minutes, Date.now());
ws.data.backrooms();
}
checkBan(ws: ServerWebSocket<Client>) {
const ban = getRoomBans(this.name)?.find(z=> z.id == ws.data.id);
const ban = getRoomBans(this.name)?.find((z) => z.id == ws.data.id);
if (!ban) return false;
const time = ban.banTime - (Date.now() - (ban.minutes * 60 * 1000));
const time = ban.banTime - (Date.now() - ban.minutes * 60 * 1000);
if (time <= 0) {
return false; // Database.ts automatically removes the ban
@ -91,7 +114,8 @@ export class Room {
particpiant.addClient(ws.data);
}
ws.data.send(new proto.ServerMessage({
ws.data.send(
new proto.ServerMessage({
event: proto.ServerMessage_EventType.WELCOME,
welcome: {
id: ws.data.id,
@ -102,7 +126,8 @@ export class Room {
chat: ws.data.room!.chats,
role: particpiant.profile.role,
},
}));
})
);
if (particpiant.clients.length <= 1) {
//#region Annouce to everyone that the user has joined
const joinMessage = new proto.ServerMessage({

View file

@ -4,6 +4,7 @@ import type { ServerWebSocket } from "bun";
import { Room } from "./Room";
import { Profile } from "./Profile";
import { addSiteBan, getSiteBan, removeSiteBan } from "./Database";
import * as fs from "fs";
const CEventType = proto.ClientMessage_EventType;
const SEventType = proto.ServerMessage_EventType;
@ -13,6 +14,7 @@ export class Server {
static rooms = new Map<string, Room>();
static profiles = new Map<string, Profile>();
static specialRooms = ["Lobby", "Backrooms"];
static systemMessage(message: string): proto.ServerMessage {
return new proto.ServerMessage({
event: proto.ServerMessage_EventType.CHAT,
@ -38,6 +40,22 @@ export class Server {
}
constructor(port: number) {
try {
fs.mkdirSync("chats");
} catch {}
setInterval(() => {
let allStats: { stat: fs.Stats; name: string }[] = [];
for (const name of fs.readdirSync("chats/")) {
if (Server.specialRooms.includes(name)) return;
allStats.push({ stat: fs.statSync("chats/" + name), name });
}
allStats = allStats.sort(
(a, b) => b.stat.mtime.getTime() - a.stat.mtime.getTime()
);
const deleteThese = allStats.slice(5);
deleteThese.forEach((z) => fs.unlinkSync("chats/" + z.name));
}, 5000);
Bun.serve({
// #region WS upgrading
fetch(req, server) {