From eb4d375ccd7bc603d516284065fa551e999f2d26 Mon Sep 17 00:00:00 2001 From: rafi1212122 Date: Thu, 1 Jun 2023 07:58:46 +0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=A4=AF=20chatters?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- GameServer/Game/Chatrooms/Chatroom.cs | 114 ++++++++++++++++++ GameServer/Game/Chatrooms/WorldChatroom.cs | 6 + .../Handlers/EnterWorldChatroomReqHandler.cs | 27 +++++ .../Handlers/SendChatMsgNotifyHandler.cs | 15 +++ 4 files changed, 162 insertions(+) create mode 100644 GameServer/Game/Chatrooms/Chatroom.cs create mode 100644 GameServer/Game/Chatrooms/WorldChatroom.cs create mode 100644 GameServer/Handlers/EnterWorldChatroomReqHandler.cs create mode 100644 GameServer/Handlers/SendChatMsgNotifyHandler.cs diff --git a/GameServer/Game/Chatrooms/Chatroom.cs b/GameServer/Game/Chatrooms/Chatroom.cs new file mode 100644 index 0000000..4a9cec0 --- /dev/null +++ b/GameServer/Game/Chatrooms/Chatroom.cs @@ -0,0 +1,114 @@ +using Common; +using Common.Database; +using Common.Resources.Proto; +using PemukulPaku.GameServer.Commands; + +namespace PemukulPaku.GameServer.Game.Chatrooms +{ + public class Chatroom + { + public readonly uint Id; + public readonly List Members = new(); + public readonly List Messages = new(); + + public Chatroom(uint id) + { + Id = id; + } + + public void Join(in Session session) + { + Members.Add(session); + } + + public void OnSendChat(in Session session, ChatMsg chatMsg) + { + string? StringMsg = chatMsg.Content.Items.Where(item => item.MsgStr != null).FirstOrDefault()?.MsgStr; + + if (StringMsg != null) + { + // do we need cmds? + Command? Cmd = CommandFactory.Commands.Find(cmd => StringMsg.Split(' ').ToList()[0] == cmd.Name.ToLower()); + if (Cmd != null) + { + + } + + chatMsg.CheckResult = new() + { + ShieldType = 0, + NumberCheck = 0, + RewriteText = StringMsg + }; + chatMsg.Msg = StringMsg; + } + + UserScheme User = session.Player.User; + + chatMsg.Uid = User.Uid; + chatMsg.Nickname = User.Nick; + chatMsg.Time = (uint)Global.GetUnixInSeconds(); + chatMsg.AvatarId = (uint)User.AssistantAvatarId; + chatMsg.DressId = session.Player.AvatarList.Where(avatar => avatar.AvatarId == User.AssistantAvatarId).First().DressId; + chatMsg.FrameId = 200001; + chatMsg.CustomHeadId = 161001; + + Broadcast(chatMsg); + Messages.Add(chatMsg); + } + + public void Broadcast(ChatMsg chatMsg) + { + foreach (Session session in Members) + { + RecvChatMsgNotify notify = new() { }; + notify.ChatMsgLists.Add(chatMsg); + + session.Send(Packet.FromProto(notify, CmdId.RecvChatMsgNotify)); + } + } + } + + public abstract class BaseChatroom where TSelf : BaseChatroom + { + public readonly Dictionary Chatrooms = new(); + private static TSelf? Instance; + + public static TSelf GetInstance() + { + return Instance ??= Activator.CreateInstance(); + } + + public virtual Chatroom Join(in Session session, uint Id) + { + Chatrooms.TryGetValue(Id, out Chatroom? chatroom); + if(chatroom != null) + { + chatroom.Join(session); + } + else + { + Chatrooms.Add(Id, new Chatroom(Id)); + return Join(session, Id); + } + return chatroom; + } + + public virtual void Left(Session session) + { + Chatroom? chatroom = Chatrooms.Values.Where(chatr => chatr.Members.Contains(session)).FirstOrDefault(); + if (chatroom != null) + { + chatroom.Members.Remove(session); + if (chatroom.Members.Count < 1) + Chatrooms.Remove(chatroom.Id); + } + } + + public virtual Chatroom GetChatroom(Session session) + { + Chatroom? chatroom = Chatrooms.Values.Where(chatr => chatr.Members.Contains(session)).FirstOrDefault(); + return chatroom ?? Join(session, 1); + } + } +} diff --git a/GameServer/Game/Chatrooms/WorldChatroom.cs b/GameServer/Game/Chatrooms/WorldChatroom.cs new file mode 100644 index 0000000..5632a2e --- /dev/null +++ b/GameServer/Game/Chatrooms/WorldChatroom.cs @@ -0,0 +1,6 @@ +namespace PemukulPaku.GameServer.Game.Chatrooms +{ + public class WorldChatroom : BaseChatroom + { + } +} diff --git a/GameServer/Handlers/EnterWorldChatroomReqHandler.cs b/GameServer/Handlers/EnterWorldChatroomReqHandler.cs new file mode 100644 index 0000000..2906de0 --- /dev/null +++ b/GameServer/Handlers/EnterWorldChatroomReqHandler.cs @@ -0,0 +1,27 @@ +using Common.Resources.Proto; +using PemukulPaku.GameServer.Game.Chatrooms; + +namespace PemukulPaku.GameServer.Handlers +{ + [PacketCmdId(CmdId.EnterWorldChatroomReq)] + internal class EnterWorldChatroomReqHandler : IPacketHandler + { + public void Handle(Session session, Packet packet) + { + EnterWorldChatroomReq Data = packet.GetDecodedBody(); + WorldChatroom.GetInstance().Left(session); + Chatroom JoinedChatroom = WorldChatroom.GetInstance().Join(session, Data.ChatroomId == 0 ? 1 : Data.ChatroomId); + + EnterWorldChatroomRsp Rsp = new() + { + retcode = EnterWorldChatroomRsp.Retcode.Succ, + ChatroomId = JoinedChatroom.Id, + ActivityType = ActivityWorldChatroomType.ActivityWorldChatroomTypeNone, + PlayerNum = (uint)JoinedChatroom.Members.Count + }; + Rsp.HisChatMsgLists.AddRange(JoinedChatroom.Messages.Skip(JoinedChatroom.Messages.Count - 5).ToList()); + + session.Send(Packet.FromProto(Rsp, CmdId.EnterWorldChatroomRsp)); + } + } +} diff --git a/GameServer/Handlers/SendChatMsgNotifyHandler.cs b/GameServer/Handlers/SendChatMsgNotifyHandler.cs new file mode 100644 index 0000000..fd94277 --- /dev/null +++ b/GameServer/Handlers/SendChatMsgNotifyHandler.cs @@ -0,0 +1,15 @@ +using Common.Resources.Proto; +using PemukulPaku.GameServer.Game.Chatrooms; + +namespace PemukulPaku.GameServer.Handlers +{ + [PacketCmdId(CmdId.SendChatMsgNotify)] + internal class SendChatMsgNotifyHandler : IPacketHandler + { + public void Handle(Session session, Packet packet) + { + SendChatMsgNotify Data = packet.GetDecodedBody(); + WorldChatroom.GetInstance().GetChatroom(session).OnSendChat(session, Data.ChatMsg); + } + } +}