From f5ef64e87d4ec61f630ab28235c4906bfb08a2e1 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 12 Feb 2017 14:03:46 -0500 Subject: [PATCH] Admin panel for chat frontend. --- ShiftOS.Objects/Objects.cs | 38 +++- ShiftOS.Server.WebAdmin/Program.cs | 202 +++++++++++++++++- .../Properties/Resources.Designer.cs | 28 +++ .../Properties/Resources.resx | 6 + .../Resources/ChatEditTemplate.txt | 5 + .../Resources/ChatListView.txt | 11 + .../Resources/HtmlTemplate.txt | 21 +- .../ShiftOS.Server.WebAdmin.csproj | 6 + ShiftOS.Server/Program.cs | 61 +----- 9 files changed, 302 insertions(+), 76 deletions(-) create mode 100644 ShiftOS.Server.WebAdmin/Resources/ChatEditTemplate.txt create mode 100644 ShiftOS.Server.WebAdmin/Resources/ChatListView.txt diff --git a/ShiftOS.Objects/Objects.cs b/ShiftOS.Objects/Objects.cs index 18677be..17f40ea 100644 --- a/ShiftOS.Objects/Objects.cs +++ b/ShiftOS.Objects/Objects.cs @@ -67,15 +67,49 @@ namespace ShiftOS.Objects public string OnlineChat { get; set; } } + public class FriendlyNameAttribute : Attribute + { + public FriendlyNameAttribute(string name) + { + Name = name; + } + + public string Name { get; private set; } + } + + public class FriendlyDescriptionAttribute : Attribute + { + public FriendlyDescriptionAttribute(string desc) + { + Description = desc; + } + + public string Description { get; private set; } + } + public class Channel { + [FriendlyName("Chat name")] + [FriendlyDescription("The human-readable name of your chat. This should be something small, possibly a one or two word description of your chat.")] public string Name { get; set; } + + //Don't describe this one. We want it to be hidden from the admin panel's chat editor. public string ID { get; set; } + + [FriendlyName("Chat topic")] + [FriendlyDescription("A more in-depth version of your chat name. Describe what your chat's about in a sentence.")] public string Topic { get; set; } - public int MaxUsers { get; set; } //0 for unlimited users (or the MUD maximum) - public List Users = new List(); + + [FriendlyName("Is it a Discord relay?")] + [FriendlyDescription("If checked, this channel will use a Discord bot to relay messages between ShiftOS and a chosen Discord channel. Useful if you'd like to integrate your MUD with the rest of your community.")] public bool IsDiscordProxy { get; set; } + + [FriendlyName("Discord bot token")] + [FriendlyDescription("If this is a discord relay chat, paste the token for your Discord bot here. Note: It MUST be a bot token, not a user token.")] public string DiscordBotToken { get; set; } + + [FriendlyName("Discord channel ID")] + [FriendlyDescription("If this channel is a Discord relay, paste the ID of the channel you'd like the bot to listen to here. You can get the channel ID by enabling Developer Mode in your Discord settings, then right-clicking your channel name and clicking 'Copy ID', then paste it here.")] public string DiscordChannelID { get; set; } } diff --git a/ShiftOS.Server.WebAdmin/Program.cs b/ShiftOS.Server.WebAdmin/Program.cs index 6e3661d..91e4831 100644 --- a/ShiftOS.Server.WebAdmin/Program.cs +++ b/ShiftOS.Server.WebAdmin/Program.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -122,6 +123,100 @@ namespace ShiftOS.Server.WebAdmin return false; } + public static string BuildFormFromObject(object obj) + { + StringBuilder sb = new StringBuilder(); + sb.AppendLine("
"); + foreach(var prop in obj.GetType().GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance)) + { + string name = ""; + string description = "No description."; + foreach(var attrib in prop.GetCustomAttributes(false)) + { + if(attrib is FriendlyNameAttribute) + { + name = (attrib as FriendlyNameAttribute).Name; + } + if(attrib is FriendlyDescriptionAttribute) + { + description = (attrib as FriendlyDescriptionAttribute).Description; + } + } + if (name != "") + { + sb.AppendLine(""); + + sb.AppendLine($@" +"); + } + else + { + sb.AppendLine($""); + } + } + sb.AppendLine(""); + sb.AppendLine("
+

{name}

+

{description}

+
"); + if (prop.PropertyType == typeof(bool)) + { + string isChecked = ((bool)prop.GetValue(obj) == true) ? "checked" : ""; + sb.AppendLine($""); + } + else if (prop.PropertyType == typeof(string)) + { + sb.AppendLine($""); + } + + sb.AppendLine("
"); + return sb.ToString(); + } + + public static Channel GetChat(string id) + { + if (File.Exists("chats.json")) + foreach (var channel in JsonConvert.DeserializeObject>(File.ReadAllText("chats.json"))) + { + if (channel.ID == id) + return channel; + } + return new Channel(); + } + + public static string GetAllChats() + { + StringBuilder sb = new StringBuilder(); + sb.AppendLine(""); + sb.AppendLine($@" + + + + + + "); + if (File.Exists("chats.json")) + { + foreach(var chat in JsonConvert.DeserializeObject>(File.ReadAllText("chats.json"))) + { + sb.AppendLine($@" + + + + + + + +"); + } + } + sb.AppendLine("
IDNameTopicIs Discord RelayDiscord channel IDDiscord Bot TokenActions
{chat.ID}{chat.Name}{chat.Topic}{chat.IsDiscordProxy}{chat.DiscordChannelID}{chat.DiscordBotToken} + Edit + Delete +
"); + return sb.ToString(); + } + public static string GetCPWorth() { if (System.IO.Directory.Exists("saves")) @@ -222,7 +317,7 @@ namespace ShiftOS.Server.WebAdmin Get["/logout"] = parameters => { - return this.Logout("/"); + return this.Logout("~/"); }; Post["/login"] = parameters => @@ -269,16 +364,111 @@ namespace ShiftOS.Server.WebAdmin this.RequiresClaims("Admin"); Get["/"] = _ => { - return PageBuilder.Build("status", new Dictionary{ + return statusBuilder(); + }; + Get["/status"] = _ => + { + return statusBuilder(); + }; + Get["/chats"] = _ => + { + return chatsListBuilder(); + }; + + Get["/createchat"] = _ => + { + return PageBuilder.Build("editchat", new Dictionary + { + {"{body}", Properties.Resources.ChatEditTemplate }, + {"{form}", SystemManager.BuildFormFromObject(new Channel()) } + }); + }; + + Post["/createchat"] = parameters => + { + var chat = this.Bind(); + chat.ID = chat.Name.ToLower().Replace(" ", "_"); + List chats = new List(); + if (File.Exists("chats.json")) + chats = JsonConvert.DeserializeObject>(File.ReadAllText("chats.json")); + + bool chatExists = false; + + for (int i = 0; i < chats.Count; i++) + { + if (chats[i].ID == chat.ID) + { + chats[i] = chat; + chatExists = true; + } + } + + if (!chatExists) + { + chats.Add(chat); + } + + File.WriteAllText("chats.json", JsonConvert.SerializeObject(chats, Formatting.Indented)); + + return chatsListBuilder(); + }; + + Get["/editchat/{id}"] = parameters => + { + return PageBuilder.Build("editchat", new Dictionary + { + {"{body}", Properties.Resources.ChatEditTemplate }, + {"{form}", SystemManager.BuildFormFromObject(SystemManager.GetChat(parameters.id)) } + }); + }; + + Post["/editchat/{id}"] = parameters => + { + var chat = this.Bind(); + chat.ID = chat.Name.ToLower().Replace(" ", "_"); + List chats = new List(); + if (File.Exists("chats.json")) + chats = JsonConvert.DeserializeObject>(File.ReadAllText("chats.json")); + + bool chatExists = false; + + for (int i = 0; i < chats.Count; i++) + { + if (chats[i].ID == chat.ID) + { + chats[i] = chat; + chatExists = true; + } + } + + if (!chatExists) + { + chats.Add(chat); + } + + File.WriteAllText("chats.json", JsonConvert.SerializeObject(chats, Formatting.Indented)); + return chatsListBuilder(); + }; + + } + + private string statusBuilder() + { + return PageBuilder.Build("status", new Dictionary{ { "{cp_worth}", SystemManager.GetCPWorth() }, { "{user_count}", SystemManager.GetUserCount() }, { "{system_time}", DateTime.Now.ToString() }, }); - }; - Get["/status"] = _ => + + } + + private string chatsListBuilder() + { + return PageBuilder.Build("bla", new Dictionary { - return PageBuilder.Build("status"); - }; + { "{body}", Properties.Resources.ChatListView }, + { "{chat_table}", SystemManager.GetAllChats() } + }); } } diff --git a/ShiftOS.Server.WebAdmin/Properties/Resources.Designer.cs b/ShiftOS.Server.WebAdmin/Properties/Resources.Designer.cs index cf60006..a3e1d67 100644 --- a/ShiftOS.Server.WebAdmin/Properties/Resources.Designer.cs +++ b/ShiftOS.Server.WebAdmin/Properties/Resources.Designer.cs @@ -60,6 +60,34 @@ namespace ShiftOS.Server.WebAdmin.Properties { } } + /// + /// Looks up a localized string similar to <h3>Create/edit chat</h3> + /// + ///<p>Please fill out the details below for your channel list to be modified.</p> + /// + ///{form}. + /// + internal static string ChatEditTemplate { + get { + return ResourceManager.GetString("ChatEditTemplate", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to <h3>Chats</h3> + /// + ///<p>On this page you can find a list of all chats in the system. Chats are a part of the multi-user domain that allows online players to talk to eachother in the 'MUD Chat' application.</p> + /// + ///<p>If you have a Discord server for your multi-user domain, you can also designate a ShiftOS chat to listen on a specific channel on your server. You will need to create a Discord Bot Token and specify the ID of the channel you want tolisten to.</p> + /// + ///<p>Once the chat is set up, you should see a bot [rest of string was truncated]";. + /// + internal static string ChatListView { + get { + return ResourceManager.GetString("ChatListView", resourceCulture); + } + } + /// /// Looks up a localized string similar to <html> /// <head> diff --git a/ShiftOS.Server.WebAdmin/Properties/Resources.resx b/ShiftOS.Server.WebAdmin/Properties/Resources.resx index a6e7f9b..7fa664a 100644 --- a/ShiftOS.Server.WebAdmin/Properties/Resources.resx +++ b/ShiftOS.Server.WebAdmin/Properties/Resources.resx @@ -118,6 +118,12 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + ..\Resources\ChatEditTemplate.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252 + + + ..\Resources\ChatListView.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252 + ..\Resources\HtmlTemplate.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252 diff --git a/ShiftOS.Server.WebAdmin/Resources/ChatEditTemplate.txt b/ShiftOS.Server.WebAdmin/Resources/ChatEditTemplate.txt new file mode 100644 index 0000000..0c8a7b5 --- /dev/null +++ b/ShiftOS.Server.WebAdmin/Resources/ChatEditTemplate.txt @@ -0,0 +1,5 @@ +

Create/edit chat

+ +

Please fill out the details below for your channel list to be modified.

+ +{form} \ No newline at end of file diff --git a/ShiftOS.Server.WebAdmin/Resources/ChatListView.txt b/ShiftOS.Server.WebAdmin/Resources/ChatListView.txt new file mode 100644 index 0000000..eedfc07 --- /dev/null +++ b/ShiftOS.Server.WebAdmin/Resources/ChatListView.txt @@ -0,0 +1,11 @@ +

Chats

+ +

On this page you can find a list of all chats in the system. Chats are a part of the multi-user domain that allows online players to talk to eachother in the 'MUD Chat' application.

+ +

If you have a Discord server for your multi-user domain, you can also designate a ShiftOS chat to listen on a specific channel on your server. You will need to create a Discord Bot Token and specify the ID of the channel you want tolisten to.

+ +

Once the chat is set up, you should see a bot join your Discord server. Once it does, any messages received by the server in that channel will be relayed into ShiftOS, and any messages received by the MUD in the ShiftOS channel will be relayed to Discord.

+ + Create chat + +{chat_table} \ No newline at end of file diff --git a/ShiftOS.Server.WebAdmin/Resources/HtmlTemplate.txt b/ShiftOS.Server.WebAdmin/Resources/HtmlTemplate.txt index fa0fcc6..cdb6e0d 100644 --- a/ShiftOS.Server.WebAdmin/Resources/HtmlTemplate.txt +++ b/ShiftOS.Server.WebAdmin/Resources/HtmlTemplate.txt @@ -15,25 +15,28 @@