Skip to content

Commit

Permalink
Merge pull request Byte-Nova#172 from SimobiSirOP/Chat_expansion
Browse files Browse the repository at this point in the history
Adding new Chat features
  • Loading branch information
Byte-Nova authored Oct 17, 2024
2 parents 21d2d52 + 2d4b4ea commit 31976ea
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 18 deletions.
6 changes: 4 additions & 2 deletions Source/Client/Managers/ChatManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ public static class ChatManagerHelper
{ UserColor.Admin, "<color=red>" },
{ UserColor.Console, "<color=yellow>" },
{ UserColor.Private, "<color=#3ae0dd>" },
{ UserColor.Discord, "<color=#9656ce>" }
{ UserColor.Discord, "<color=#9656ce>" },
{ UserColor.Server, "<color=#6d90c9>"}
};

public static Dictionary<MessageColor, string> messageColorDictionary = new Dictionary<MessageColor, string>()
Expand All @@ -151,7 +152,8 @@ public static class ChatManagerHelper
{ MessageColor.Admin, "<color=white>" },
{ MessageColor.Console, "<color=yellow>" },
{ MessageColor.Private, "<color=#3ae0dd>" },
{ MessageColor.Discord, "<color=white>" }
{ MessageColor.Discord, "<color=white>" },
{ MessageColor.Server, " <color=white>" }
};

public static string[] GetMessageWords(string message)
Expand Down
19 changes: 17 additions & 2 deletions Source/Server/Core/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,9 @@ public static void LoadResources()
SaveValueFile(ServerFileMode.Backup, false);

LoadValueFile(ServerFileMode.Mods);
SaveValueFile(ServerFileMode.Mods, false);

LoadValueFile(ServerFileMode.Chat);
SaveValueFile(ServerFileMode.Chat, false);

LoadValueFile(ServerFileMode.World);

Expand Down Expand Up @@ -185,6 +187,10 @@ public static void SaveValueFile(ServerFileMode mode, bool broadcast = true)
pathToSave = Path.Combine(Master.corePath, "ModConfig.json");
Serializer.SerializeToFile(pathToSave, Master.modConfig);
break;
case ServerFileMode.Chat:
pathToSave = Path.Combine(Master.corePath, "ChatConfig.json");
Serializer.SerializeToFile(pathToSave, Master.chatConfig);
break;
}

if (broadcast) Logger.Warning($"Saved > '{pathToSave}'");
Expand Down Expand Up @@ -301,8 +307,17 @@ public static void LoadValueFile(ServerFileMode mode, bool broadcast = true)
Serializer.SerializeToFile(pathToLoad, Master.modConfig);
}
break;

case ServerFileMode.Chat:
pathToLoad = Path.Combine(Master.corePath, "ChatConfig.json");
if (File.Exists(pathToLoad)) Master.chatConfig = Serializer.SerializeFromFile<ChatConfigFile>(pathToLoad);
else
{
Master.chatConfig = new ChatConfigFile();
Serializer.SerializeToFile(pathToLoad, Master.chatConfig);
}
break;
}

if (broadcast) Logger.Warning($"Loaded > '{pathToLoad}'");
}

Expand Down
2 changes: 2 additions & 0 deletions Source/Server/Core/Master.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ public static class Master
public static BackupConfigFile backupConfig;

public static ModConfigFile modConfig;

public static ChatConfigFile chatConfig;

//Booleans

Expand Down
13 changes: 13 additions & 0 deletions Source/Server/Files/ChatConfigFile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace GameServer;

[Serializable]
public class ChatConfigFile
{
public bool EnableMoTD = false;

public string MessageOfTheDay = "Remember to drink water";

public bool LoginNotifications = false;

public bool DisconnectNotifications = false;
}
51 changes: 42 additions & 9 deletions Source/Server/Managers/ChatManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,20 @@ namespace GameServer
public static class ChatManager
{
private static readonly Semaphore logSemaphore = new Semaphore(1, 1);

private static readonly Semaphore commandSemaphore = new Semaphore(1, 1);

private static readonly string systemName = "CONSOLE";

private static readonly string notificationName = "SERVER";

public static readonly string[] defaultJoinMessages = new string[]
{
"Welcome to the global chat!",
"Please be considerate with others and have fun!",
"Use '/help' to check all the available commands."
};

public static readonly string[] defaultTextTools = new string[]
{
"List of available text tools:",
Expand All @@ -38,7 +42,7 @@ private static void ExecuteChatCommand(ServerClient client, string[] command)
commandSemaphore.WaitOne();

ChatCommand toFind = ChatManagerHelper.GetCommandFromName(command[0]);
if (toFind == null) SendSystemMessage(client, "Command was not found.");
if (toFind == null) SendConsoleMessage(client, "Command was not found.");
else
{
ChatCommandManager.targetClient = client;
Expand Down Expand Up @@ -88,7 +92,7 @@ public static void BroadcastDiscordMessage(string client, string message)
ChatManagerHelper.ShowChatInConsole(client, message, true);
}

public static void BroadcastServerMessage(string message)
public static void BroadcastConsoleMessage(string message)
{
ChatData chatData = new ChatData();
chatData._username = systemName;
Expand All @@ -105,7 +109,24 @@ public static void BroadcastServerMessage(string message)
ChatManagerHelper.ShowChatInConsole(chatData._username, message);
}

public static void SendSystemMessage(ServerClient client, string message)
public static void BroadcastServerNotification(string message)
{
ChatData chatData = new ChatData();
chatData._username = notificationName;
chatData._message = message;
chatData._usernameColor = UserColor.Server;
chatData._messageColor = MessageColor.Server;

Packet packet = Packet.CreatePacketFromObject(nameof(ChatManager), chatData);
NetworkHelper.SendPacketToAllClients(packet);

if (Master.discordConfig.Enabled && Master.discordConfig.ChatChannelId != 0) DiscordManager.SendMessageToChatChannel(chatData._username, message);

WriteToLogs(chatData._username, message);
ChatManagerHelper.ShowChatInConsole(chatData._username, message);
}

public static void SendConsoleMessage(ServerClient client, string message)
{
ChatData chatData = new ChatData();
chatData._username = systemName;
Expand All @@ -117,6 +138,18 @@ public static void SendSystemMessage(ServerClient client, string message)
client.listener.EnqueuePacket(packet);
}

public static void SendServerMessage(ServerClient client, string message)
{
ChatData chatData = new ChatData();
chatData._username = notificationName;
chatData._message = message;
chatData._usernameColor = UserColor.Server;
chatData._messageColor = MessageColor.Server;

Packet packet = Packet.CreatePacketFromObject(nameof(ChatManager), chatData);
client.listener.EnqueuePacket(packet);
}

private static void WriteToLogs(string username, string message)
{
logSemaphore.WaitOne();
Expand Down Expand Up @@ -183,7 +216,7 @@ private static void ChatHelpCommandAction()
List<string> messagesToSend = new List<string> { "List of available commands:" };
foreach (ChatCommand command in chatCommands) messagesToSend.Add($"{command.prefix} - {command.description}");

foreach (string str in messagesToSend) ChatManager.SendSystemMessage(targetClient, str);
foreach (string str in messagesToSend) ChatManager.SendConsoleMessage(targetClient, str);
}
}

Expand All @@ -194,15 +227,15 @@ private static void ChatToolsCommandAction()
{
foreach (string str in ChatManager.defaultTextTools)
{
ChatManager.SendSystemMessage(targetClient, str);
ChatManager.SendConsoleMessage(targetClient, str);
}
}
}

private static void ChatPingCommandAction()
{
if (targetClient == null) return;
else ChatManager.SendSystemMessage(targetClient, "Pong!");
else ChatManager.SendConsoleMessage(targetClient, "Pong!");
}

private static void ChatDisconnectCommandAction()
Expand All @@ -225,15 +258,15 @@ private static void ChatPrivateMessageCommandAction()
string message = "";
for (int i = 2; i < command.Length; i++) message += command[i] + " ";

if (string.IsNullOrWhiteSpace(message)) ChatManager.SendSystemMessage(targetClient, "Message was empty.");
if (string.IsNullOrWhiteSpace(message)) ChatManager.SendConsoleMessage(targetClient, "Message was empty.");
else
{
ServerClient toFind = ChatManagerHelper.GetUserFromName(ChatManagerHelper.GetUsernameFromMention(command[1]));
if (toFind == null) ChatManager.SendSystemMessage(targetClient, "User was not found.");
if (toFind == null) ChatManager.SendConsoleMessage(targetClient, "User was not found.");
else
{
//Don't allow players to send wispers to themselves
if (toFind == targetClient) ChatManager.SendSystemMessage(targetClient, "Can't send a whisper to yourself.");
if (toFind == targetClient) ChatManager.SendConsoleMessage(targetClient, "Can't send a whisper to yourself.");
else
{
ChatData chatData = new ChatData();
Expand Down
6 changes: 5 additions & 1 deletion Source/Server/Managers/LoginManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,11 @@ private static void PostLogin(ServerClient client)

GlobalDataManager.SendServerGlobalData(client);

foreach(string str in ChatManager.defaultJoinMessages) ChatManager.SendSystemMessage(client, str);
foreach(string str in ChatManager.defaultJoinMessages) ChatManager.SendConsoleMessage(client, str);

if (Master.chatConfig.EnableMoTD) ChatManager.SendServerMessage(client, $"MoTD > {Master.chatConfig.MessageOfTheDay}");

if (Master.chatConfig.LoginNotifications) ChatManager.BroadcastServerNotification($"{client.userFile.Username} has logged in!");

if (WorldManager.CheckIfWorldExists())
{
Expand Down
2 changes: 1 addition & 1 deletion Source/Server/Misc/Commands/CommandStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ private static void ServerMessageCommandAction()
}
fullText = fullText.Remove(fullText.Length - 1, 1);

ChatManager.BroadcastServerMessage(fullText);
ChatManager.BroadcastConsoleMessage(fullText);

Logger.Title($"Sent chat: '{fullText}'");
}
Expand Down
1 change: 1 addition & 0 deletions Source/Server/Network/Network.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public static void KickClient(ServerClient client)
Main_.ChangeTitle();
UserManager.SendPlayerRecount();
Logger.Message($"[Disconnect] > {client.userFile.Username} | {client.userFile.SavedIP}");
if (Master.chatConfig.DisconnectNotifications) ChatManager.BroadcastServerNotification($"{client.userFile.Username} has left the server!");
}
catch { Logger.Warning($"Error disconnecting user {client.userFile.Username}, this will cause memory overhead"); }
}
Expand Down
6 changes: 3 additions & 3 deletions Source/Shared/Misc/CommonEnumerators.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public enum ResponseStepMode { IllegalAction, UserUnavailable, Pop }

public enum SaveStepMode { Send, Receive, Reset }

public enum ServerFileMode { Configs, Actions, Sites, Roads, World, Whitelist, Difficulty, Market, Discord, Backup, Mods }
public enum ServerFileMode { Configs, Actions, Sites, Roads, World, Whitelist, Difficulty, Market, Discord, Backup, Mods, Chat }

public enum LogMode { Message, Warning, Error, Title, Outsider }

Expand Down Expand Up @@ -70,9 +70,9 @@ public enum WorldStepMode { Required, Existing }

public enum SaveMode { Disconnect, Autosave, Strict }

public enum UserColor { Normal, Admin, Console, Private, Discord }
public enum UserColor { Normal, Admin, Console, Private, Discord, Server }

public enum MessageColor { Normal, Admin, Console, Private, Discord }
public enum MessageColor { Normal, Admin, Console, Private, Discord, Server }

public enum LoginMode { Login, Register }

Expand Down

0 comments on commit 31976ea

Please sign in to comment.