Binato/server/ChatManager.ts

79 lines
2.5 KiB
TypeScript
Raw Normal View History

2022-11-19 01:06:03 +00:00
import { Channel } from "./objects/Channel";
import { ConsoleHelper } from "../ConsoleHelper";
import { FunkyArray } from "./objects/FunkyArray";
import { DataStreamArray } from "./objects/DataStreamArray";
2022-11-19 15:06:03 +00:00
import { User } from "./objects/User";
const osu = require("osu-packet");
2022-11-19 01:06:03 +00:00
export class ChatManager {
public chatChannels:FunkyArray<Channel> = new FunkyArray<Channel>();
2022-11-19 15:06:03 +00:00
public forceJoinChannels:FunkyArray<Channel> = new FunkyArray<Channel>();
2022-11-19 01:06:03 +00:00
public streams:DataStreamArray;
public constructor(streams:DataStreamArray) {
this.streams = streams;
}
2022-11-20 23:37:39 +00:00
public AddChatChannel(name:string, description:string, forceJoin:boolean = false) : Channel {
2022-11-19 01:06:03 +00:00
const stream = this.streams.CreateStream(`chat_channel:${name}`, false);
2022-11-19 15:06:03 +00:00
const channel = new Channel(`#${name}`, description, stream);
this.chatChannels.add(channel.name, channel);
if (forceJoin) {
this.forceJoinChannels.add(name, channel);
}
2022-11-19 01:06:03 +00:00
ConsoleHelper.printChat(`Created chat channel [${name}]`);
2022-11-20 23:37:39 +00:00
return channel;
}
public AddSpecialChatChannel(name:string, streamName:string, forceJoin:boolean = false) : Channel {
const stream = this.streams.CreateStream(`chat_channel:${streamName}`, false);
const channel = new Channel(`#${name}`, "", stream);
this.chatChannels.add(channel.name, channel);
if (forceJoin) {
this.forceJoinChannels.add(name, channel);
}
ConsoleHelper.printChat(`Created chat channel [${name}]`);
return channel;
2022-11-19 01:06:03 +00:00
}
2022-11-19 15:06:03 +00:00
public RemoveChatChannel(channel:Channel | string) {
if (channel instanceof Channel) {
channel.stream.Delete();
this.chatChannels.remove(channel.stream.name);
this.forceJoinChannels.remove(channel.stream.name)
} else {
const chatChannel = this.GetChannelByName(channel);
if (chatChannel instanceof Channel) {
chatChannel.stream.Delete();
this.chatChannels.remove(chatChannel.stream.name);
this.forceJoinChannels.remove(chatChannel.stream.name)
}
}
}
public GetChannelByName(channelName:string) : Channel | undefined {
return this.chatChannels.getByKey(channelName);
}
public ForceJoinChannels(user:User) {
for (let channel of this.forceJoinChannels.getIterableItems()) {
channel.Join(user);
}
}
public SendChannelListing(user:User) {
const osuPacketWriter = new osu.Bancho.Writer;
for (let channel of this.chatChannels.getIterableItems()) {
2022-11-20 23:37:39 +00:00
if (channel.isSpecial) {
continue;
}
2022-11-19 15:06:03 +00:00
osuPacketWriter.ChannelAvailable({
channelName: channel.name,
channelTopic: channel.description,
channelUserCount: channel.userCount
});
}
user.addActionToQueue(osuPacketWriter.toBuffer);
}
2022-11-19 01:06:03 +00:00
}