From f5ef64e87d4ec61f630ab28235c4906bfb08a2e1 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 12 Feb 2017 14:03:46 -0500 Subject: Admin panel for chat frontend. --- ShiftOS.Server.WebAdmin/Program.cs | 202 ++++++++++++++++++++- .../Properties/Resources.Designer.cs | 28 +++ ShiftOS.Server.WebAdmin/Properties/Resources.resx | 6 + .../Resources/ChatEditTemplate.txt | 5 + ShiftOS.Server.WebAdmin/Resources/ChatListView.txt | 11 ++ ShiftOS.Server.WebAdmin/Resources/HtmlTemplate.txt | 17 +- .../ShiftOS.Server.WebAdmin.csproj | 6 + 7 files changed, 262 insertions(+), 13 deletions(-) create mode 100644 ShiftOS.Server.WebAdmin/Resources/ChatEditTemplate.txt create mode 100644 ShiftOS.Server.WebAdmin/Resources/ChatListView.txt (limited to 'ShiftOS.Server.WebAdmin') 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 @@