diff --git a/README.md b/README.md new file mode 100644 index 0000000..1b42b7f --- /dev/null +++ b/README.md @@ -0,0 +1,29 @@ +# HogWarp Chat + +## Usage + +### Builder + +The following codeblock describes the usage of the `ChatMessage Builder` that allows for different styles inside the same message and some more details. + +```cs +var builder = new ChatMessage.Builder(); +builder.AddIcon(ChatIcon.Gryffindor); +builder.AddText("Default ", ChatTextStyle.Default); +builder.AddText("Gryffindor ", ChatTextStyle.Gryffindor); +builder.AddText("Hufflepuff ", ChatTextStyle.Hufflepuff); +builder.AddText("Ravenclaw ", ChatTextStyle.Ravenclaw); +builder.AddText("Slytherin ", ChatTextStyle.Slytherin); +builder.AddText("Admin ", ChatTextStyle.Admin); +builder.AddText("Dev ", ChatTextStyle.Dev); +builder.AddText("Server ", ChatTextStyle.Server); +builder.AddText("Red ", ChatTextStyle.Red); +builder.AddText("Blue ", ChatTextStyle.Blue); +builder.AddText("Green ", ChatTextStyle.Green); +builder.AddText("Yellow ", ChatTextStyle.Yellow); +builder.AddText("Magenta ", ChatTextStyle.Magenta); +builder.AddText("Cyan ", ChatTextStyle.Cyan); +builder.AddSender(player.Username, ChatTextStyle.Server); +SendMessage(player, builder.Build().Message); +``` + diff --git a/Server/ChatIcon.cs b/Server/ChatIcon.cs new file mode 100644 index 0000000..e7630d9 --- /dev/null +++ b/Server/ChatIcon.cs @@ -0,0 +1,12 @@ +namespace HogWarpChat; + +/// +/// The following icons are added in front of the sender +/// +public enum ChatIcon +{ + Gryffindor, + Hufflepuff, + Ravenclaw, + Slytherin +} \ No newline at end of file diff --git a/Server/ChatMessage.cs b/Server/ChatMessage.cs new file mode 100644 index 0000000..da25b4a --- /dev/null +++ b/Server/ChatMessage.cs @@ -0,0 +1,111 @@ +namespace HogWarpChat; + +public sealed class ChatMessage +{ + private bool _includeTimestamp; + private ChatIcon? _icon; + private string _sender = string.Empty; + private string _texts = string.Empty; + + /// + /// Contains the completely builded chat message + /// + public string Message { get; private set; } = string.Empty; + + public sealed class Builder(bool includeTimestamp = true) + { + private readonly ChatMessage _chatMessage = new() { _includeTimestamp = includeTimestamp }; + + /// + /// Prepends the chat message with an icon + /// + public Builder AddIcon(ChatIcon icon) + { + _chatMessage._icon = icon; + return this; + } + + #region SENDER + + /// + /// Adds a sender to the chat message with default style + /// + public Builder AddSender(string sender) + { + _chatMessage._sender = $"{sender}:"; + return this; + } + + /// + /// Adds a sender to the chat message + /// + public Builder AddSender(string sender, ChatStyle style) + { + if (style.Style == ChatTextStyle.Default) + return AddSender(sender); + _chatMessage._sender += $"<{style.Value}>{sender}:"; + return this; + } + + /// + /// Adds a sender to the chat message with the given style + /// + public Builder AddSender(string sender, ChatTextStyle style) + { + return ChatStyles.Styles.TryGetValue(style, out var textStyle) + ? AddSender(sender, textStyle) + : AddSender(sender); + } + + #endregion + + #region TEXTS + + /// + /// Adds a default text to the message + /// + public Builder AddText(string text) + { + _chatMessage._texts += text; + return this; + } + + /// + /// Adds a text to the chat message with the given style + /// + public Builder AddText(string text, ChatStyle style) + { + if (style.Style == ChatTextStyle.Default) + return AddText(text); + _chatMessage._texts += $"<{style.Value}>{text}"; + return this; + } + + /// + /// Adds a text to the chat message with the given style + /// + public Builder AddText(string text, ChatTextStyle style) + { + return ChatStyles.Styles.TryGetValue(style, out var textStyle) + ? AddText(text, textStyle) + : AddText(text); + } + + /// + /// Builds the chat message based on the available icon, sender and texts. + /// + public ChatMessage Build() + { + if (_chatMessage._includeTimestamp) + _chatMessage.Message += DateTime.Now.ToString("[HH:mm] "); + if (_chatMessage._icon is not null) + _chatMessage.Message += $" "; + if (!string.IsNullOrEmpty(_chatMessage._sender)) + _chatMessage.Message += $"{_chatMessage._sender} "; // Just for the whitespace + _chatMessage.Message += _chatMessage._texts; + return _chatMessage; + } + + #endregion + } +} \ No newline at end of file diff --git a/Server/ChatTextStyle.cs b/Server/ChatTextStyle.cs new file mode 100644 index 0000000..00bcf7d --- /dev/null +++ b/Server/ChatTextStyle.cs @@ -0,0 +1,61 @@ +namespace HogWarpChat; + +public enum ChatTextStyle +{ + Default, + Gryffindor, + Hufflepuff, + Ravenclaw, + Slytherin, + Admin, + Dev, + Server, + Red, + Blue, + Green, + Yellow, + Magenta, + Cyan +} + +public sealed class ChatStyle(ChatTextStyle style, string value) +{ + public readonly ChatTextStyle Style = style; + public readonly string Value = value; +} + +public static class ChatStyles +{ + public static readonly ChatStyle Default = new (ChatTextStyle.Default, "Default"); + public static readonly ChatStyle Gryffindor = new (ChatTextStyle.Gryffindor, "Gryffindor"); + public static readonly ChatStyle Hufflepuff = new (ChatTextStyle.Hufflepuff, "Hufflepuff"); + public static readonly ChatStyle Ravenclaw = new (ChatTextStyle.Ravenclaw, "Ravenclaw"); + public static readonly ChatStyle Slytherin = new (ChatTextStyle.Slytherin, "Slytherin"); + public static readonly ChatStyle Admin = new (ChatTextStyle.Admin, "Admin"); + public static readonly ChatStyle Dev = new (ChatTextStyle.Dev, "Dev"); + public static readonly ChatStyle Server = new (ChatTextStyle.Server, "Server"); + public static readonly ChatStyle Red = new (ChatTextStyle.Red, "FF0000FF"); + public static readonly ChatStyle Blue = new (ChatTextStyle.Blue, "0000FFFF"); + public static readonly ChatStyle Green = new (ChatTextStyle.Green, "00FF00FF"); + public static readonly ChatStyle Yellow = new (ChatTextStyle.Yellow, "FFFF00FF"); + public static readonly ChatStyle Magenta = new (ChatTextStyle.Magenta, "FF00FFFF"); + public static readonly ChatStyle Cyan = new (ChatTextStyle.Cyan, "00FFFFFF"); + + public static Dictionary Styles = new() + { + { ChatTextStyle.Default, Default }, + { ChatTextStyle.Gryffindor, Gryffindor }, + { ChatTextStyle.Hufflepuff, Hufflepuff }, + { ChatTextStyle.Ravenclaw, Ravenclaw }, + { ChatTextStyle.Slytherin, Slytherin }, + { ChatTextStyle.Admin, Admin }, + { ChatTextStyle.Dev, Dev }, + { ChatTextStyle.Server, Server }, + { ChatTextStyle.Red, Red }, + { ChatTextStyle.Blue, Blue }, + { ChatTextStyle.Green, Green }, + { ChatTextStyle.Yellow, Yellow }, + { ChatTextStyle.Magenta, Magenta }, + { ChatTextStyle.Cyan, Cyan }, + }; +} \ No newline at end of file