Binato/server/ChatManager.ts

93 lines
3.1 KiB
TypeScript
Raw Permalink Normal View History

2023-09-10 12:59:22 +01:00
import Channel from "./objects/Channel";
2022-11-19 01:06:03 +00:00
import { ConsoleHelper } from "../ConsoleHelper";
2023-09-10 12:59:22 +01:00
import FunkyArray from "./objects/FunkyArray";
import User from "./objects/User";
import Shared from "./objects/Shared";
import osu from "../osuTyping";
import PrivateChannel from "./objects/PrivateChannel";
2022-11-19 01:06:03 +00:00
2023-09-10 12:59:22 +01:00
export default class ChatManager {
2022-11-19 01:06:03 +00:00
public chatChannels:FunkyArray<Channel> = new FunkyArray<Channel>();
2022-11-19 15:06:03 +00:00
public forceJoinChannels:FunkyArray<Channel> = new FunkyArray<Channel>();
2023-08-20 13:03:01 +01:00
private readonly shared:Shared;
2022-11-19 01:06:03 +00:00
2023-08-20 13:03:01 +01:00
public constructor(shared:Shared) {
this.shared = shared;
2022-11-19 01:06:03 +00:00
}
2022-11-20 23:37:39 +00:00
public AddChatChannel(name:string, description:string, forceJoin:boolean = false) : Channel {
2023-08-20 13:03:01 +01:00
const stream = this.shared.streams.CreateStream(`chat_channel:${name}`, false);
const channel = new Channel(this.shared, `#${name}`, description, stream);
2022-11-19 15:06:03 +00:00
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 {
2023-08-20 13:03:01 +01:00
const stream = this.shared.streams.CreateStream(`chat_channel:${streamName}`, false);
const channel = new Channel(this.shared, `#${name}`, "", stream);
2022-11-20 23:37:39 +00:00
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)
}
}
}
2023-08-20 13:03:01 +01:00
public AddPrivateChatChannel(user0:User, user1:User) {
const stream = this.shared.streams.CreateStream(`private_channel:${user0.username},${user1.username}`, true);
const channel = new PrivateChannel(user0, user1, stream);
this.chatChannels.add(channel.name, channel);
ConsoleHelper.printChat(`Created private chat channel [${channel.name}]`);
return channel;
}
2022-11-19 15:06:03 +00:00
public GetChannelByName(channelName:string) : Channel | undefined {
return this.chatChannels.getByKey(channelName);
}
2023-08-20 13:03:01 +01:00
public GetPrivateChannelByName(channelName:string) : Channel | undefined {
return this.chatChannels.getByKey(channelName);
}
2022-11-19 15:06:03 +00:00
public ForceJoinChannels(user:User) {
for (const channel of this.forceJoinChannels.getIterableItems()) {
2022-11-19 15:06:03 +00:00
channel.Join(user);
}
}
2023-08-20 13:03:01 +01:00
2022-11-19 15:06:03 +00:00
public SendChannelListing(user:User) {
2023-08-20 13:03:01 +01:00
const osuPacketWriter = osu.Bancho.Writer();
for (const 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
}