diff --git a/src/channel/Channel.ts b/src/channel/Channel.ts index eebf125..d359e40 100644 --- a/src/channel/Channel.ts +++ b/src/channel/Channel.ts @@ -15,6 +15,7 @@ import Crown from "./Crown"; import { ChannelList } from "./ChannelList"; import { config } from "./config"; import { saveChatHistory, getChatHistory } from "../data/history"; +import { mixin } from "../util/helpers"; interface CachedKickban { userId: string; @@ -23,9 +24,10 @@ interface CachedKickban { } export class Channel extends EventEmitter { - private settings: Partial = config.defaultSettings; + private settings: Partial; private ppl = new Array(); public chatHistory = new Array(); + private async loadChatHistory() { this.chatHistory = await getChatHistory(this.getID()); } @@ -44,6 +46,8 @@ export class Channel extends EventEmitter { ) { super(); + this.settings = {}; + mixin(this.settings, config.defaultSettings); this.logger = new Logger("Channel - " + _id); // Validate settings in set @@ -51,7 +55,9 @@ export class Channel extends EventEmitter { if (!this.isLobby()) { if (set) { + this.logger.debug("Passed settings:", set); const validatedSet = validateChannelSettings(set); + this.logger.debug("Validated settings:", validatedSet); for (const key of Object.keys(set)) { if ((validatedSet as any)[key] === false) continue; @@ -67,9 +73,7 @@ export class Channel extends EventEmitter { if (part) this.giveCrown(part); // } } - } - - if (this.isLobby()) { + } else { this.settings = config.lobbySettings; } @@ -212,7 +216,9 @@ export class Channel extends EventEmitter { parseInt(set.color.substring(5, 7), 16) - 0x40 ); - set.color2 = `#${r.toString(16).padStart(2, "0")}${g.toString(16).padStart(2, "0")}${b.toString(16).padStart(2, "0")}`; + set.color2 = `#${r.toString(16).padStart(2, "0")}${g + .toString(16) + .padStart(2, "0")}${b.toString(16).padStart(2, "0")}`; } if (this.isLobby() && !admin) return; @@ -305,12 +311,12 @@ export class Channel extends EventEmitter { // Send our state data back socket.sendArray([ - { - m: "ch", - ch: this.getInfo(), - p: part.id, - ppl: this.getParticipantList() - }, + // { + // m: "ch", + // ch: this.getInfo(), + // p: part.id, + // ppl: this.getParticipantList() + // }, { m: "c", c: this.chatHistory.slice(-50) @@ -324,20 +330,25 @@ export class Channel extends EventEmitter { } = socket.getCursorPos(); // Broadcast a participant update for them - this.sendArray([ - { - m: "p", - _id: part._id, - name: part.name, - color: part.color, - id: part.id, - x: cursorPos.x, - y: cursorPos.y - } - ]); + this.sendArray( + [ + { + m: "p", + _id: part._id, + name: part.name, + color: part.color, + id: part.id, + x: cursorPos.x, + y: cursorPos.y + } + ], + part.id + ); // Broadcast a channel update so everyone subscribed to the channel list can see us this.emit("update", this); + + this.logger.debug("Settings:", this.settings); } /** @@ -442,11 +453,16 @@ export class Channel extends EventEmitter { * @param arr List of events to send to clients */ public sendArray( - arr: ClientEvents[EventID][] + arr: ClientEvents[EventID][], + blockPartID?: string ) { let sentSocketIDs = new Array(); for (const p of this.ppl) { + if (blockPartID) { + if (p.id == blockPartID) continue; + } + socketLoop: for (const socket of socketsBySocketID.values()) { if (socket.isDestroyed()) continue socketLoop; if (socket.getParticipantID() != p.id) continue socketLoop; diff --git a/src/util/helpers.ts b/src/util/helpers.ts index 8b3e380..52e2f56 100644 --- a/src/util/helpers.ts +++ b/src/util/helpers.ts @@ -57,3 +57,9 @@ export function spoop_text(message: string) { } return message; } + +export function mixin(obj1: any, obj2: any) { + for (const key of Object.keys(obj2)) { + obj1[key] = obj2[key]; + } +} diff --git a/src/util/readline/commands.ts b/src/util/readline/commands.ts index 19375dc..3af3541 100644 --- a/src/util/readline/commands.ts +++ b/src/util/readline/commands.ts @@ -29,11 +29,11 @@ Command.addCommand( Command.addCommand( new Command(["memory", "mem"], "memory", msg => { const mem = process.memoryUsage(); - return `Memory: ${(mem.heapUsed / 1000 / 1000).toFixed(2)} MB used / ${( + return `Memory: ${(mem.heapUsed / 1000 / 1000).toFixed(2)} MB / ${( mem.heapTotal / 1000 / 1000 - ).toFixed(2)} MB total`; + ).toFixed(2)} MB / ${(mem.rss / 1000 / 1000).toFixed(2)} MB`; }) ); @@ -51,24 +51,22 @@ Command.addCommand( Command.addCommand( new Command(["list", "ls"], "list ", async msg => { - if(msg.args.length > 1) { - if(msg.args[1] == "channels") { + if (msg.args.length > 1) { + if (msg.args[1] == "channels") { return ( "Channels:\n- " + ChannelList.getList() .map(ch => ch.getID()) .join("\n- ") - ); + ); } else if (msg.args[1] == "users") { var user = getUsers(); var users = ""; - ((await user).users).forEach(u => { - users += `\n- [${u.id}]: ${u.name}` - }) + (await user).users.forEach(u => { + users += `\n- [${u.id}]: ${u.name}`; + }); - return ( - "Users: "+await (await user).count + users - ) + return "Users: " + (await (await user).count) + users; } else { return "list "; } diff --git a/src/ws/Socket.ts b/src/ws/Socket.ts index ff6c535..67e4c78 100644 --- a/src/ws/Socket.ts +++ b/src/ws/Socket.ts @@ -242,7 +242,7 @@ export class Socket extends EventEmitter { _id: facadeID, name: this.user.name, color: this.user.color, - id: this.id + id: this.getParticipantID() }; } else { return null;