updates related to chat moderation

* getting viking ID from API
* distinction between VikingName and DisplayName (+ fix typo)
* set DisplayName based on SUV
* support for names with spaces in tempmute command
* list of all clients in Server (for future usage in plugins)

partially based on PR7

Co-authored-by: Hipposgrumm <hipposgrumm@gmail.com>
This commit is contained in:
Robert Paciorek 2025-06-24 18:23:03 +00:00
parent d18360ec33
commit 97ce6fdd06
10 changed files with 84 additions and 29 deletions

View File

@ -11,33 +11,35 @@ class ChatMessageHandler : CommandHandler {
string message = receivedObject.Get<NetworkObject>("p").Get<string>("chm");
if (ManagementCommandProcessor.ProcessCommand(message, client))
return Task.CompletedTask;
if (client.TempMuted) {
ClientMuted(client);
return Task.CompletedTask;
}
if (!Configuration.ServerConfiguration.EnableChat && !client.Room.AllowChatOverride) {
ChatDisabled(client);
ChatDisabled(client, message);
} else if (Configuration.ServerConfiguration.Authentication >= AuthenticationMode.RequiredForChat && client.PlayerData.VikingId != 0) {
ChatRequiredAuthentication(client, message);
} else if (client.Muted) {
ClientMuted(client, message);
} else {
Chat(client, message);
}
return Task.CompletedTask;
}
public void ChatDisabled(Client client) {
// NOTE: message is passed also for functions that refuse to send messages (this can be useful at the level of the chat moderation plugin)
public void ChatDisabled(Client client, string message) {
client.Send(Utils.BuildServerSideMessage("Unfortunately, chat has been disabled by server administrators", "Server"));
}
public void ClientMuted(Client client) {
public void ChatRequiredAuthentication(Client client, string message) {
client.Send(Utils.BuildServerSideMessage("You must be authenticated to use the chat", "Server"));
}
public void ClientMuted(Client client, string message) {
client.Send(Utils.BuildServerSideMessage("You have been muted by the moderators", "Server"));
}
public void Chat(Client client, string message) {
if (Configuration.ServerConfiguration.Authentication >= AuthenticationMode.RequiredForChat && client.PlayerData.DiplayName == "placeholder") {
client.Send(Utils.BuildServerSideMessage("You must be authenticated to use the chat", "Server"));
return;
}
client.Room.Send(Utils.BuildChatMessage(client.PlayerData.Uid, message, client.PlayerData.DiplayName), client);
client.Room.Send(Utils.BuildChatMessage(client.PlayerData.Uid, message, client.PlayerData.DisplayName), client);
// NOTE: using DisplayName not VikingName here for consistency with client behavior, preventing "placeholder" in not authenticated modes and support for changing name while logged on MMO
NetworkObject cmd = new();
NetworkObject data = new();

View File

@ -84,7 +84,8 @@ class LoginHandler : CommandHandler
AuthenticationInfo info = Utils.DeserializeXml<AuthenticationInfo>(responseString);
if (info.Authenticated) {
client.PlayerData.DiplayName = info.DisplayName;
client.PlayerData.VikingName = info.VikingName;
client.PlayerData.VikingId = info.Id;
client.PlayerData.Role = info.Role;
return true;
}

View File

@ -12,7 +12,8 @@ public class Client {
public PlayerData PlayerData { get; set; } = new();
public Room? Room { get; private set; }
public bool OldApi { get; set; } = false;
public bool TempMuted { get; set; } = false;
public bool Muted { get; set; } = false;
private readonly Socket socket;
SocketBuffer socketBuffer = new();

View File

@ -90,7 +90,7 @@ public class SpecialRoom : Room {
Console.WriteLine("Started event " +alert + " in room " + Name);
} else {
specificClient.Send(packet);
Console.WriteLine("Added " + specificClient.PlayerData.DiplayName + " to event " + alert + " with " + duration + " seconds remaining");
Console.WriteLine("Added " + specificClient.PlayerData.DisplayName + " to event " + alert + " with " + duration + " seconds remaining");
}
}

View File

@ -2,6 +2,7 @@
using System.Globalization;
using sodoffmmo.Core;
using sodoffmmo.Management;
using System.Text.Json;
namespace sodoffmmo.Data;
public class PlayerData {
@ -35,7 +36,13 @@ public class PlayerData {
// animation bitfield (animations used by avatar, e.g. mounted, swim, ...)
public int Mbf { get; set; }
public string DiplayName { get; set; } = "placeholder";
// Name of player used by MMO
public string DisplayName { get; set; } = "placeholder";
// Name of player from API database
public string VikingName { get; set; } = "";
// ID of player from API database, zero for not authenticated players
public int VikingId { get; set; } = 0;
// role of player for management
public Role Role { get; set; } = Role.User;
public long last_ue_time { get; set; } = 0;
@ -90,6 +97,18 @@ public class PlayerData {
value = FixMountState(value);
}
// update the playername (sent with avatardata) if it changes
// this is also sent when the player joins the room
if (varName == "A" && variables.GetValueOrDefault("A") != value) {
try {
string? name = JsonSerializer.Deserialize<AvatarData>(value)?.DisplayName;
if (name != null && name != DisplayName) {
DisplayName = name;
}
}
catch (JsonException) {} // Don't break if the data is malformed or something.
}
// store in directory
variables[varName] = value;
return value;

View File

@ -6,19 +6,27 @@ namespace sodoffmmo.Management.Commands;
[ManagementCommand("tempmute", Role.Moderator)]
class TempMuteCommand : IManagementCommand {
public void Handle(Client client, string[] arguments) {
if (arguments.Length != 1) {
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"));
string name = string.Join(' ', arguments);
var targets = client.Room.Clients.Where(x => x.PlayerData.DisplayName == name);
// NOTE: using DisplayName here because this is the name displayed in client-side chat history
if (targets.Count() == 0) {
client.Send(Utils.BuildServerSideMessage($"TempMute: user {name} not found", "Server"));
return;
} else if (targets.Count() > 1) {
client.Send(Utils.BuildServerSideMessage($"TempMute: found multiple users with the name: {name}", "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"));
Client target = targets.First();
if (target.Muted) {
client.Send(Utils.BuildServerSideMessage($"TempMute: {name} is already muted", "Server"));
} else {
target.Muted = true;
client.Send(Utils.BuildServerSideMessage($"TempMute: {name} has been temporarily muted", "Server"));
}
}
}

View File

@ -42,7 +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}");
Console.WriteLine($"Management command {commandName} by {client.PlayerData.VikingName} ({client.PlayerData.Uid}) in {client.Room.Name}");
command.Handle(client, arguments);
return true;
}

View File

@ -8,7 +8,10 @@ public class AuthenticationInfo {
public bool Authenticated { get; set; }
[XmlElement]
public string DisplayName { get; set; }
public int Id { get; set; }
[XmlElement]
public string VikingName { get; set; }
[XmlElement]
public Role Role { get; set; }

11
src/Schema/AvatarData.cs Normal file
View File

@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace sodoffmmo.Management;
public class AvatarData {
public string? DisplayName { get; set; }
}

View File

@ -7,18 +7,26 @@ using System.Net.Sockets;
namespace sodoffmmo;
public class Server {
static object lck = new();
static readonly List<Client> clients = new();
readonly int port;
readonly IPAddress ipAddress;
readonly bool IPv6AndIPv4;
ModuleManager moduleManager = new();
public static int TotalClientCount => clients.Count;
public Server(IPAddress ipAdress, int port, bool IPv6AndIPv4) {
this.ipAddress = ipAdress;
this.port = port;
this.IPv6AndIPv4 = IPv6AndIPv4;
}
public static Client[] GetAllClients() {
return clients.ToArray();
}
public async Task Run() {
moduleManager.RegisterModules();
ManagementCommandProcessor.Initialize();
@ -47,6 +55,7 @@ public class Server {
private async Task HandleClient(Socket handler) {
Client client = new(handler);
lock (lck) clients.Add(client);
try {
while (client.Connected) {
await client.Receive();
@ -60,6 +69,7 @@ public class Server {
try {
client.SetRoom(null);
} catch (Exception) { }
lock (lck) clients.Remove(client);
client.Disconnect();
Console.WriteLine("Socket disconnected IID: " + client.ClientID);
}