chat commands

This commit is contained in:
Spirtix 2024-04-07 15:42:36 +02:00
parent 854d1c0b0a
commit 90457b6c78
11 changed files with 98 additions and 22 deletions

View File

@ -11,7 +11,11 @@ class ChatMessageHandler : CommandHandler {
string message = receivedObject.Get<NetworkObject>("p").Get<string>("chm");
if (ManagementCommandProcessor.ProcessCommand(message, client))
return Task.CompletedTask;
if (!Configuration.ServerConfiguration.EnableChat) {
if (client.TempMuted) {
ClientMuted(client);
return Task.CompletedTask;
}
if (!Configuration.ServerConfiguration.EnableChat && !client.Room.AllowChatOverride) {
ChatDisabled(client);
} else {
Chat(client, message);
@ -20,35 +24,25 @@ class ChatMessageHandler : CommandHandler {
}
public void ChatDisabled(Client client) {
NetworkObject cmd = new();
NetworkObject data = new();
data.Add("arr", new string[] { "CMR", "-1", "-1", "1", "Unfortunately, chat has been disabled by server administrators", "", "1", "Server" });
cmd.Add("c", "CMR");
cmd.Add("p", data);
client.Send(Utils.BuildServerSideMessage("Unfortunately, chat has been disabled by server administrators", "Server"));
}
NetworkPacket packet = NetworkObject.WrapObject(1, 13, cmd).Serialize();
client.Send(packet);
public void ClientMuted(Client client) {
client.Send(Utils.BuildServerSideMessage("You have been muted by the moderators", "Server"));
}
public void Chat(Client client, string message) {
if (client.PlayerData.DiplayName == "")
if (Configuration.ServerConfiguration.Authentication && client.PlayerData.DiplayName == "placeholder")
return;
client.Room.Send(Utils.BuildChatMessage(client.PlayerData.Uid, message, client.PlayerData.DiplayName), client);
NetworkObject cmd = new();
NetworkObject data = new();
data.Add("arr", new string[] { "CMR", "-1", client.PlayerData.Uid, "1", message, "", "1", client.PlayerData.DiplayName });
cmd.Add("c", "CMR");
cmd.Add("p", data);
NetworkPacket packet = NetworkObject.WrapObject(1, 13, cmd).Serialize();
client.Room.Send(packet, client);
cmd = new();
data = new();
data.Add("arr", new string[] { "SCA", "-1", "1", message, "", "1" });
cmd.Add("c", "SCA");
cmd.Add("p", data);
packet = NetworkObject.WrapObject(1, 13, cmd).Serialize();
NetworkPacket packet = NetworkObject.WrapObject(1, 13, cmd).Serialize();
client.Send(packet);
}
}

View File

@ -90,7 +90,7 @@ class LoginHandler : CommandHandler
return false;
if (responseString == null)
throw new Exception("Response string null");
Console.WriteLine(responseString);
AuthenticationInfo info = Utils.DeserializeXml<AuthenticationInfo>(responseString);
if (info.Authenticated) {
client.PlayerData.DiplayName = info.DisplayName;

View File

@ -11,6 +11,7 @@ public class Client {
public int ClientID { get; private set; }
public PlayerData PlayerData { get; set; } = new();
public Room? Room { get; private set; }
public bool TempMuted { get; set; } = false;
private readonly Socket socket;
SocketBuffer socketBuffer = new();

View File

@ -16,6 +16,8 @@ public class Room {
public string Group { get; private set; }
public bool AutoRemove { get; private set; }
public bool IsRemoved { get; private set; } = false;
public bool AllowChatOverride { get; set; } = false;
public NetworkArray RoomVariables = new();
public Room(string? name, string? group = null, bool autoRemove = false) {

View File

@ -38,4 +38,19 @@ internal static class Utils {
using (var reader = new StringReader(xmlString))
return (T)serializer.Deserialize(reader);
}
public static NetworkPacket BuildChatMessage(string uid, string message, string displayName) {
NetworkObject cmd = new();
NetworkObject data = new();
data.Add("arr", new string[] { "CMR", "-1", uid, "1", message, "", "1", displayName });
cmd.Add("c", "CMR");
cmd.Add("p", data);
return NetworkObject.WrapObject(1, 13, cmd).Serialize();
}
public static NetworkPacket BuildServerSideMessage(string message, string displayName) {
return BuildChatMessage("-1", message, displayName);
}
}

View File

@ -33,7 +33,7 @@ public class PlayerData {
// animation bitfield (animations used by avatar, e.g. mounted, swim, ...)
public int Mbf { get; set; }
public string DiplayName { get; set; } = "";
public string DiplayName { get; set; } = "placeholder";
public Role Role { get; set; } = Role.User;
public static readonly string[] SupportedVariables = {

View File

@ -0,0 +1,15 @@
using sodoffmmo.Attributes;
using sodoffmmo.Core;
namespace sodoffmmo.Management.Commands;
[ManagementCommand("announce", Role.Admin)]
class AnnounceCommand : IManagementCommand {
public void Handle(Client client, string[] arguments) {
if (arguments.Length == 0) {
client.Send(Utils.BuildServerSideMessage("Announce: No message to announce", "Server"));
return;
}
client.Room.Send(Utils.BuildServerSideMessage(string.Join(' ', arguments), "Server"));
}
}

View File

@ -0,0 +1,12 @@
using sodoffmmo.Attributes;
using sodoffmmo.Core;
namespace sodoffmmo.Management.Commands;
[ManagementCommand("disablechat", Role.Moderator)]
class DisableChatCommand : IManagementCommand {
public void Handle(Client client, string[] arguments) {
client.Room.AllowChatOverride = false;
client.Room.Send(Utils.BuildServerSideMessage("Chat has been disabled", "Server"));
}
}

View File

@ -0,0 +1,12 @@
using sodoffmmo.Attributes;
using sodoffmmo.Core;
namespace sodoffmmo.Management.Commands;
[ManagementCommand("enablechat", Role.Moderator)]
class EnableChatCommand : IManagementCommand {
public void Handle(Client client, string[] arguments) {
client.Room.AllowChatOverride = true;
client.Room.Send(Utils.BuildServerSideMessage("Chat has been enabled", "Server"));
}
}

View File

@ -0,0 +1,24 @@
using sodoffmmo.Attributes;
using sodoffmmo.Core;
namespace sodoffmmo.Management.Commands;
[ManagementCommand("tempmute", Role.Moderator)]
class TempMuteCommand : IManagementCommand {
public void Handle(Client client, string[] arguments) {
if (arguments.Length != 1) {
client.Send(Utils.BuildServerSideMessage("TempMute: Invalid number of arguments", "Server"));
return;
}
Client? target = client.Room.Clients.FirstOrDefault(x => x.PlayerData.DiplayName == arguments[0]);
if (target == null) {
client.Send(Utils.BuildServerSideMessage($"TempMute: user {arguments[0]} not found", "Server"));
return;
}
target.TempMuted = !target.TempMuted;
if (target.TempMuted)
client.Send(Utils.BuildServerSideMessage($"TempMute: {arguments[0]} has been temporarily muted", "Server"));
else
client.Send(Utils.BuildServerSideMessage($"TempMute: {arguments[0]} has been unmuted", "Server"));
}
}

View File

@ -30,7 +30,7 @@ public class ManagementCommandProcessor {
public static bool ProcessCommand(string message, Client client) {
if (!Configuration.ServerConfiguration.Authentication || !initialized)
return false;
if (!message.StartsWith("//") && message.Length < 3)
if (!message.StartsWith("::") && message.Length < 3)
return false;
string[] parts = message.Split(' ');
@ -42,6 +42,7 @@ public class ManagementCommandProcessor {
if (commands.TryGetValue(new Tuple<string, Role>(commandName, currentRole), out Type? commandType)) {
IManagementCommand command = (IManagementCommand)Activator.CreateInstance(commandType)!;
Console.WriteLine($"Management command {commandName} by {client.PlayerData.DiplayName} ({client.PlayerData.Uid}) in {client.Room.Name}");
command.Handle(client, arguments);
return true;
}