diff --git a/config/channels.yml b/config/channels.yml index 27875e6..4764560 100644 --- a/config/channels.yml +++ b/config/channels.yml @@ -18,6 +18,7 @@ lobbyRegexes: - ^lobby[0-9][0-9]$ - ^lobby[0-9]$ - ^lobby$ + - ^lobbyNaN$ - ^test/.+$ lobbyBackdoor: lolwutsecretlobbybackdoor fullChannel: test/awkward diff --git a/src/channel/Channel.ts b/src/channel/Channel.ts index afcf00d..9787827 100644 --- a/src/channel/Channel.ts +++ b/src/channel/Channel.ts @@ -8,7 +8,7 @@ import { ServerEvents, IChannelInfo, Notification, - UserFlags + UserFlags, } from "../util/types"; import type { Socket } from "../ws/Socket"; import { validateChannelSettings } from "./settings"; @@ -18,7 +18,7 @@ import { ChannelList } from "./ChannelList"; import { config } from "./config"; import { saveChatHistory, getChatHistory } from "../data/history"; import { mixin } from "../util/helpers"; -import { readUser } from "../data/user"; +import { User } from "@prisma/client"; interface CachedKickban { userId: string; @@ -45,6 +45,7 @@ export class Channel extends EventEmitter { public logger: Logger; public bans = new Array(); + public cursorCache = new Array<{ x: string | number; y: string | number; id: string }>(); public crown?: Crown; @@ -66,8 +67,6 @@ export class Channel extends EventEmitter { // Validate settings in set // Set the verified settings - this.logger.debug("lobby me?", this.isLobby()); - if (!this.isLobby()) { if (set) { //this.logger.debug("Passed settings:", set); @@ -172,6 +171,101 @@ export class Channel extends EventEmitter { this.on("command", (msg, socket) => { // TODO commands }); + + this.on("user data update", (user: User) => { + try { + if (typeof user.name !== "string") return; + if (typeof user.color !== "string") return; + if (typeof user.id !== "string") return; + if (typeof user.tag !== "undefined" && typeof user.tag !== "string") return; + if (typeof user.flags !== "undefined" && typeof user.flags !== "string") return; + + let tag; + let flags; + + try { + tag = JSON.parse(user.tag); + } catch (err) { } + + try { + flags = JSON.parse(user.flags); + } catch (err) { } + + for (const p of this.ppl) { + if (p._id !== user.id) continue; + + p._id = user.id; + p.name = user.name; + p.color = user.color; + p.tag = tag; + p.flags = flags; + + let found; + + for (const cursor of this.cursorCache) { + if (cursor.id == p.id) { + found = cursor + } + } + + let x: string | number = "0.00"; + let y: string | number = "-10.00"; + + if (found) { + x = found.x; + y = found.y; + } + + this.sendArray( + [ + { + m: "p", + _id: p._id, + name: p.name, + color: p.color, + id: p.id, + x: x, + y: y + } + ] + ); + } + + this.emit("update", this); + } catch (err) { + this.logger.error(err); + this.logger.warn("Unable to update user"); + } + }); + + this.on("cursor", (pos: { x: string | number; y: string | number; id: string }) => { + let found; + + for (const cursor of this.cursorCache) { + if (cursor.id == pos.id) { + found = cursor; + } + } + + if (!found) { + // Cache the cursor pos + this.cursorCache.push(pos); + } else { + // Edit the cache + found.x = pos.x; + found.y = pos.y; + } + + this.sendArray([ + { + m: "m", + id: pos.id, + // not type safe + x: pos.x as string, + y: pos.y as string + } + ]); + }); } /** @@ -202,7 +296,7 @@ export class Channel extends EventEmitter { * Determine whether this channel is a lobby with the name "lobby" in it */ public isTrueLobby() { - if (this.getID().match("^lobby[0-9][0-9]$") && this.getID().match("^lobby[1-9]$")) return true; + if (this.getID().match("^lobby[0-9][0-9]$") && this.getID().match("^lobby[0-9]$") && this.getID().match("^lobby$"), "^lobbyNaN$") return true; return false; } diff --git a/src/channel/config.ts b/src/channel/config.ts index 55f8e4a..a5e83dc 100644 --- a/src/channel/config.ts +++ b/src/channel/config.ts @@ -30,7 +30,7 @@ export const config = loadConfig("config/channels.yml", { visible: true }, // Here's a terrifying fact: Brandon used parseInt to check lobby names - lobbyRegexes: ["^lobby[0-9][0-9]$", "^lobby[0-9]$", "^lobby$", "^test/.+$"], + lobbyRegexes: ["^lobby[0-9][0-9]$", "^lobby[0-9]$", "^lobby$", "^lobbyNaN$", "^test/.+$"], lobbyBackdoor: "lolwutsecretlobbybackdoor", fullChannel: "test/awkward", sendLimit: false, diff --git a/src/ws/Socket.ts b/src/ws/Socket.ts index d3bc75a..5c69ef1 100644 --- a/src/ws/Socket.ts +++ b/src/ws/Socket.ts @@ -318,14 +318,13 @@ export class Socket extends EventEmitter { const part = this.getParticipant(); if (!part) return; - ch.sendArray([ - { - m: "m", - id: part.id, - x: this.cursorPos.x, - y: this.cursorPos.y - } - ]); + let pos = { + x: this.cursorPos.x, + y: this.cursorPos.y, + id: this.getParticipantID() + }; + + ch.emit("cursor", pos); } public getCurrentChannel() { diff --git a/src/ws/events/admin/handlers/color.ts b/src/ws/events/admin/handlers/color.ts index 4d620c5..3993278 100644 --- a/src/ws/events/admin/handlers/color.ts +++ b/src/ws/events/admin/handlers/color.ts @@ -1,12 +1,11 @@ +import { ChannelList } from "../../../../channel/ChannelList"; import { readUser, updateUser } from "../../../../data/user"; import { ServerEventListener } from "../../../../util/types"; -import { findSocketsByUserID } from "../../../Socket"; +import { Logger } from "../../../../util/Logger"; export const color: ServerEventListener<"color"> = { id: "color", callback: async (msg, socket) => { - // I'm not allowed to use this feature on MPP.net for a really stupid reason - // Nevermind, fishing bot has color again const id = msg._id; const color = msg.color; @@ -19,9 +18,8 @@ export const color: ServerEventListener<"color"> = { user.color = color; await updateUser(id, user); - const toUpdate = findSocketsByUserID(id); - toUpdate.forEach(s => { - s.userset(undefined, msg.color, true); - }); + for (const ch of ChannelList.getList()) { + ch.emit("user data update", user); + } } }; diff --git a/src/ws/events/admin/handlers/name.ts b/src/ws/events/admin/handlers/name.ts index cd11201..b290354 100644 --- a/src/ws/events/admin/handlers/name.ts +++ b/src/ws/events/admin/handlers/name.ts @@ -1,3 +1,4 @@ +import { ChannelList } from "../../../../channel/ChannelList"; import { readUser, updateUser } from "../../../../data/user"; import { ServerEventListener } from "../../../../util/types"; import { findSocketsByUserID } from "../../../Socket"; @@ -18,9 +19,8 @@ export const name: ServerEventListener<"name"> = { user.name = name; await updateUser(id, user); - const toUpdate = findSocketsByUserID(id); - toUpdate.forEach(s => { - s.userset(msg.name, undefined, true); - }); + for (const ch of ChannelList.getList()) { + ch.emit("user data update", user); + } } }; diff --git a/src/ws/events/admin/handlers/user_flag.ts b/src/ws/events/admin/handlers/user_flag.ts index 42d186b..22731a5 100644 --- a/src/ws/events/admin/handlers/user_flag.ts +++ b/src/ws/events/admin/handlers/user_flag.ts @@ -1,3 +1,4 @@ +import { ChannelList } from "../../../../channel/ChannelList"; import { readUser, updateUser } from "../../../../data/user"; import { Logger } from "../../../../util/Logger"; import { ServerEventListener } from "../../../../util/types"; @@ -39,6 +40,10 @@ export const user_flag: ServerEventListener<"user_flag"> = { sock.setUserFlag(msg.key, msg.value); }); + for (const ch of ChannelList.getList()) { + ch.emit("user data update", user); + } + // socket.getCurrentChannel()?.logger.debug("socks:", socks); } };