save room ownership and chats
This commit is contained in:
parent
ea74b57c74
commit
9969a17590
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -174,3 +174,4 @@ dist
|
||||||
# Finder (MacOS) folder config
|
# Finder (MacOS) folder config
|
||||||
.DS_Store
|
.DS_Store
|
||||||
DATABASE.db
|
DATABASE.db
|
||||||
|
chats
|
79
src/Room.ts
79
src/Room.ts
|
@ -4,7 +4,8 @@ import { Client } from "./Client";
|
||||||
import { proto } from "pianoverse";
|
import { proto } from "pianoverse";
|
||||||
import { addRoomBan, getRoomBans } from "./Database";
|
import { addRoomBan, getRoomBans } from "./Database";
|
||||||
import { Server } from "./Server";
|
import { Server } from "./Server";
|
||||||
|
import { writeFileSync, existsSync, readFileSync } from "fs";
|
||||||
|
import { join } from "path";
|
||||||
interface Ban {
|
interface Ban {
|
||||||
banTime: number;
|
banTime: number;
|
||||||
minutes: number;
|
minutes: number;
|
||||||
|
@ -19,32 +20,54 @@ export class Room {
|
||||||
|
|
||||||
constructor(name: string, owner: string) {
|
constructor(name: string, owner: string) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
if(this.name == "Lobby" || this.name == "Backrooms") {
|
if (Server.specialRooms.includes(this.name)) {
|
||||||
this.owner = "";
|
this.owner = "";
|
||||||
} else {
|
} else {
|
||||||
this.owner = owner;
|
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) {
|
private addToChatArray(chat: proto.ServerMessage_Chat) {
|
||||||
this.chats.push(chat);
|
this.chats.push(chat);
|
||||||
if (this.chats.length > 150) this.chats.shift();
|
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) {
|
systemMessage(message: string) {
|
||||||
const system = Server.systemMessage(message);
|
const system = Server.systemMessage(message);
|
||||||
this.addToChatArray(system.chat!);
|
this.addToChatArray(system.chat!);
|
||||||
this.particpiants.forEach((z) => {
|
this.particpiants.forEach((z) => {
|
||||||
z.clients.forEach(g => {
|
z.clients.forEach((g) => {
|
||||||
g.send(system);
|
g.send(system);
|
||||||
})
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
chat(ws: ServerWebSocket<Client>, message: string) {
|
chat(ws: ServerWebSocket<Client>, message: string) {
|
||||||
const profile = Server.profiles.get(ws.data.id);
|
const profile = Server.profiles.get(ws.data.id);
|
||||||
if(!profile) return;
|
if (!profile) return;
|
||||||
|
|
||||||
const chatPacket = new proto.ServerMessage({
|
const chatPacket = new proto.ServerMessage({
|
||||||
event: proto.ServerMessage_EventType.CHAT,
|
event: proto.ServerMessage_EventType.CHAT,
|
||||||
chat: {
|
chat: {
|
||||||
|
@ -63,17 +86,17 @@ export class Room {
|
||||||
}
|
}
|
||||||
|
|
||||||
ban(ws: ServerWebSocket<Client>, minutes: number) {
|
ban(ws: ServerWebSocket<Client>, minutes: number) {
|
||||||
if(this.name == 'Backrooms') return;
|
if (this.name == "Backrooms") return;
|
||||||
addRoomBan(ws.data.id, this.name, minutes, Date.now())
|
addRoomBan(ws.data.id, this.name, minutes, Date.now());
|
||||||
|
|
||||||
ws.data.backrooms();
|
ws.data.backrooms();
|
||||||
}
|
}
|
||||||
checkBan(ws: ServerWebSocket<Client>) {
|
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;
|
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) {
|
if (time <= 0) {
|
||||||
return false; // Database.ts automatically removes the ban
|
return false; // Database.ts automatically removes the ban
|
||||||
} else {
|
} else {
|
||||||
return time;
|
return time;
|
||||||
|
@ -91,18 +114,20 @@ export class Room {
|
||||||
particpiant.addClient(ws.data);
|
particpiant.addClient(ws.data);
|
||||||
}
|
}
|
||||||
|
|
||||||
ws.data.send(new proto.ServerMessage({
|
ws.data.send(
|
||||||
event: proto.ServerMessage_EventType.WELCOME,
|
new proto.ServerMessage({
|
||||||
welcome: {
|
event: proto.ServerMessage_EventType.WELCOME,
|
||||||
id: ws.data.id,
|
welcome: {
|
||||||
name: particpiant.profile.name,
|
id: ws.data.id,
|
||||||
color: particpiant.profile.color,
|
name: particpiant.profile.name,
|
||||||
room: ws.data.room!.name,
|
color: particpiant.profile.color,
|
||||||
owner: ws.data.room!.owner,
|
room: ws.data.room!.name,
|
||||||
chat: ws.data.room!.chats,
|
owner: ws.data.room!.owner,
|
||||||
role: particpiant.profile.role,
|
chat: ws.data.room!.chats,
|
||||||
},
|
role: particpiant.profile.role,
|
||||||
}));
|
},
|
||||||
|
})
|
||||||
|
);
|
||||||
if (particpiant.clients.length <= 1) {
|
if (particpiant.clients.length <= 1) {
|
||||||
//#region Annouce to everyone that the user has joined
|
//#region Annouce to everyone that the user has joined
|
||||||
const joinMessage = new proto.ServerMessage({
|
const joinMessage = new proto.ServerMessage({
|
||||||
|
|
|
@ -4,6 +4,7 @@ import type { ServerWebSocket } from "bun";
|
||||||
import { Room } from "./Room";
|
import { Room } from "./Room";
|
||||||
import { Profile } from "./Profile";
|
import { Profile } from "./Profile";
|
||||||
import { addSiteBan, getSiteBan, removeSiteBan } from "./Database";
|
import { addSiteBan, getSiteBan, removeSiteBan } from "./Database";
|
||||||
|
import * as fs from "fs";
|
||||||
|
|
||||||
const CEventType = proto.ClientMessage_EventType;
|
const CEventType = proto.ClientMessage_EventType;
|
||||||
const SEventType = proto.ServerMessage_EventType;
|
const SEventType = proto.ServerMessage_EventType;
|
||||||
|
@ -13,6 +14,7 @@ export class Server {
|
||||||
static rooms = new Map<string, Room>();
|
static rooms = new Map<string, Room>();
|
||||||
static profiles = new Map<string, Profile>();
|
static profiles = new Map<string, Profile>();
|
||||||
|
|
||||||
|
static specialRooms = ["Lobby", "Backrooms"];
|
||||||
static systemMessage(message: string): proto.ServerMessage {
|
static systemMessage(message: string): proto.ServerMessage {
|
||||||
return new proto.ServerMessage({
|
return new proto.ServerMessage({
|
||||||
event: proto.ServerMessage_EventType.CHAT,
|
event: proto.ServerMessage_EventType.CHAT,
|
||||||
|
@ -38,6 +40,22 @@ export class Server {
|
||||||
}
|
}
|
||||||
|
|
||||||
constructor(port: number) {
|
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({
|
Bun.serve({
|
||||||
// #region WS upgrading
|
// #region WS upgrading
|
||||||
fetch(req, server) {
|
fetch(req, server) {
|
||||||
|
@ -198,7 +216,7 @@ export class Server {
|
||||||
if (!ws.data.room) return;
|
if (!ws.data.room) return;
|
||||||
const profile = Server.profiles.get(ws.data.id);
|
const profile = Server.profiles.get(ws.data.id);
|
||||||
if (!profile) return;
|
if (!profile) return;
|
||||||
if(!ws.data.room.owner) return;
|
if (!ws.data.room.owner) return;
|
||||||
if (profile.role !== proto.Role.DEVELOPER) {
|
if (profile.role !== proto.Role.DEVELOPER) {
|
||||||
if (ws.data.room.owner != ws.data.id) return;
|
if (ws.data.room.owner != ws.data.id) return;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue