WutheringWaves/GameServer/Controllers/ChatController.cs

94 lines
3.2 KiB
C#
Raw Normal View History

2024-02-14 21:22:21 +00:00
using System.Text;
using System.Text.RegularExpressions;
2024-02-14 21:22:21 +00:00
using GameServer.Controllers.Attributes;
using GameServer.Controllers.ChatCommands;
using GameServer.Models;
using GameServer.Models.Chat;
using GameServer.Network;
using GameServer.Network.Messages;
using Protocol;
namespace GameServer.Controllers;
internal partial class ChatController : Controller
2024-02-14 21:22:21 +00:00
{
private readonly ModelManager _modelManager;
public ChatController(PlayerSession session, ModelManager modelManager) : base(session)
{
_modelManager = modelManager;
}
[NetEvent(MessageId.PrivateChatDataRequest)]
public async Task<RpcResult> OnPrivateChatDataRequest()
2024-02-14 21:22:21 +00:00
{
if (!_modelManager.Chat.AllChatRooms.Any())
{
ChatRoom chatRoom = _modelManager.Chat.GetChatRoom(1338); // Reversed Helper
chatRoom.AddMessage(1338, (int)ChatContentType.Text, BuildWelcomeMessage());
}
await PushPrivateChatHistory();
return Response(MessageId.PrivateChatDataResponse, new PrivateChatDataResponse()); // Response doesn't contain any useful info, everything is in notifies.
}
[NetEvent(MessageId.PrivateChatRequest)]
public async Task<RpcResult> OnPrivateChatRequest(PrivateChatRequest request, ChatCommandManager chatCommandManager)
2024-02-14 21:22:21 +00:00
{
ChatRoom chatRoom = _modelManager.Chat.GetChatRoom(1338);
chatRoom.AddMessage(_modelManager.Player.Id, request.ChatContentType, request.Content);
if (!request.Content.StartsWith('/'))
{
chatRoom.AddMessage(1338, 0, "huh?");
}
else
{
string content = MultipleSpacesRegex().Replace(request.Content, " ");
string[] split = content[1..].Split(' ');
2024-02-14 21:22:21 +00:00
if (split.Length >= 2)
{
await chatCommandManager.InvokeCommandAsync(split[0], split[1], split[2..]);
}
}
await PushPrivateChatHistory();
return Response(MessageId.PrivateChatResponse, new PrivateChatResponse());
}
[NetEvent(MessageId.PrivateChatOperateRequest)]
public RpcResult OnPrivateChatOperateRequest() => Response(MessageId.PrivateChatOperateResponse, new PrivateChatOperateResponse());
2024-02-14 21:22:21 +00:00
private async Task PushPrivateChatHistory()
{
await Session.Push(MessageId.PrivateChatHistoryNotify, new PrivateChatHistoryNotify
{
AllChats =
{
_modelManager.Chat.AllChatRooms
.Select(chatRoom => new PrivateChatHistoryContentProto
{
TargetUid = chatRoom.TargetUid,
Chats = { chatRoom.ChatHistory }
})
}
});
}
private static string BuildWelcomeMessage()
{
StringBuilder builder = new();
builder.AppendLine("Welcome to ReversedRooms WutheringWaves private server!\nTo get support, join:\ndiscord.gg/reversedrooms");
builder.AppendLine("List of all commands:");
foreach (string description in ChatCommandManager.CommandDescriptions)
{
builder.AppendLine(description);
}
return builder.ToString();
}
[GeneratedRegex(@"\s+")]
private static partial Regex MultipleSpacesRegex();
2024-02-14 21:22:21 +00:00
}