diff options
| author | Michael <[email protected]> | 2017-02-12 11:21:20 -0500 |
|---|---|---|
| committer | Michael <[email protected]> | 2017-02-12 11:21:20 -0500 |
| commit | 7f8f8b1c483e04abde8e849e8054a5ff4b651051 (patch) | |
| tree | 955e3bca44bc0f51459f9faf1beb1c485cafa18c | |
| parent | 177c22a24b419144af7a00d5970d04dc1618e0d7 (diff) | |
| download | shiftos_thereturn-7f8f8b1c483e04abde8e849e8054a5ff4b651051.tar.gz shiftos_thereturn-7f8f8b1c483e04abde8e849e8054a5ff4b651051.tar.bz2 shiftos_thereturn-7f8f8b1c483e04abde8e849e8054a5ff4b651051.zip | |
Finish MUD Refactoring
| -rw-r--r-- | ShiftOS.Server/Core.cs | 86 | ||||
| -rw-r--r-- | ShiftOS.Server/MemoManager.cs | 60 | ||||
| -rw-r--r-- | ShiftOS.Server/Program.cs | 384 | ||||
| -rw-r--r-- | ShiftOS.Server/ShiftOS.Server.csproj | 3 | ||||
| -rw-r--r-- | ShiftOS.Server/ShiftnetBackend.cs | 114 |
5 files changed, 263 insertions, 384 deletions
diff --git a/ShiftOS.Server/Core.cs b/ShiftOS.Server/Core.cs new file mode 100644 index 0000000..e761a53 --- /dev/null +++ b/ShiftOS.Server/Core.cs @@ -0,0 +1,86 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ShiftOS.Objects; +using NetSockets; +using Newtonsoft.Json; +using System.IO; +using static ShiftOS.Server.Program; + + +namespace ShiftOS.Server +{ + public static class Core + { + [MudRequest("getguid_reply")] + public static void GuidBounce(string guid, object contents) + { + //The message's GUID was manipulated by the client to send to another client. + //So we can just bounce back the message to the other client. + server.DispatchTo(new Guid(guid), new NetObject("bounce", new ServerMessage + { + Name = "getguid_reply", + GUID = guid, + Contents = contents as string + })); + + } + + [MudRequest("getguid_send")] + public static void GuidReceiver(string guid, object contents) + { + string usrname = contents as string; + server.DispatchAll(new NetObject("are_you_this_guy", new ServerMessage + { + Name = "getguid_fromserver", + GUID = guid, + Contents = usrname, + })); + + } + + [MudRequest("script")] + public static void RunScript(string guid, object contents) + { + var args = contents as Dictionary<string, string>; + + string user = ""; + string script = ""; + string sArgs = ""; + + if (!args.ContainsKey("user")) + throw new MudException("No 'user' arg specified in message to server"); + + if (!args.ContainsKey("script")) + throw new MudException("No 'script' arg specified in message to server"); + + if (!args.ContainsKey("args")) + throw new MudException("No 'args' arg specified in message to server"); + + user = args["user"] as string; + script = args["script"] as string; + sArgs = args["args"] as string; + + if (File.Exists($"scripts/{user}/{script}.lua")) + { + var script_arguments = JsonConvert.DeserializeObject<Dictionary<string, object>>(sArgs); + server.DispatchTo(new Guid(guid), new NetObject("runme", new ServerMessage + { + Name = "run", + GUID = "Server", + Contents = $@"{{ + script:""{File.ReadAllText($"scripts/{user}/{script}.lua").Replace("\"", "\\\"")}"", + args:""{sArgs}"" + }}" + })); + } + else + { + throw new MudException($"{user}.{script}: Script not found."); + } + + } + } +} diff --git a/ShiftOS.Server/MemoManager.cs b/ShiftOS.Server/MemoManager.cs new file mode 100644 index 0000000..ed0a2e9 --- /dev/null +++ b/ShiftOS.Server/MemoManager.cs @@ -0,0 +1,60 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ShiftOS.Objects; +using NetSockets; +using Newtonsoft.Json; +using System.IO; +using static ShiftOS.Server.Program; + +namespace ShiftOS.Server +{ + public static class MemoManager + { + [MudRequest("get_memos_for_user")] + public static void GetMemosForUser(string guid, object contents) + { + var args = contents as Dictionary<string, object>; + if (args["username"] != null) + { + string usrnme = args["username"].ToString(); + + List<MUDMemo> mmos = new List<MUDMemo>(); + + if (File.Exists("memos.json")) + { + foreach (var mmo in JsonConvert.DeserializeObject<MUDMemo[]>(File.ReadAllText("memos.json"))) + { + if (mmo.UserTo == usrnme) + { + mmos.Add(mmo); + } + } + } + + server.DispatchTo(new Guid(guid), new NetObject("mud_memos", new ServerMessage + { + Name = "mud_usermemos", + GUID = "server", + Contents = JsonConvert.SerializeObject(mmos) + })); + } + + } + + [MudRequest("mud_postmemo")] + public static void PostMemo(string guid, object contents) + { + MUDMemo memo = JsonConvert.DeserializeObject<MUDMemo>(contents as string); + List<MUDMemo> memos = new List<MUDMemo>(); + + if (File.Exists("memos.json")) + memos = JsonConvert.DeserializeObject<List<MUDMemo>>(File.ReadAllText("memos.json")); + + memos.Add(memo); + File.WriteAllText("memos.txt", JsonConvert.SerializeObject(memos)); + } + } +} diff --git a/ShiftOS.Server/Program.cs b/ShiftOS.Server/Program.cs index cb896cd..46d5ab3 100644 --- a/ShiftOS.Server/Program.cs +++ b/ShiftOS.Server/Program.cs @@ -328,390 +328,6 @@ namespace ShiftOS.Server } ClientDispatcher.DispatchTo("Error", msg.GUID, new MudRequestHandlerNotFoundException()); - - - switch (msg.Name) - { - - case "get_memos_for_user": - if(args["username"] != null) - { - string usrnme = args["username"].ToString(); - - List<MUDMemo> mmos = new List<MUDMemo>(); - - if (File.Exists("memos.json")) - { - foreach(var mmo in JsonConvert.DeserializeObject<MUDMemo[]>(File.ReadAllText("memos.json"))) - { - if(mmo.UserTo == usrnme) - { - mmos.Add(mmo); - } - } - } - - server.DispatchTo(new Guid(msg.GUID), new NetObject("mud_memos", new ServerMessage - { - Name = "mud_usermemos", - GUID = "server", - Contents = JsonConvert.SerializeObject(mmos) - })); - } - break; - case "mud_post_memo": - MUDMemo memo = JsonConvert.DeserializeObject<MUDMemo>(msg.Contents); - List<MUDMemo> memos = new List<MUDMemo>(); - - if (File.Exists("memos.json")) - memos = JsonConvert.DeserializeObject<List<MUDMemo>>(File.ReadAllText("memos.json")); - - memos.Add(memo); - File.WriteAllText("memos.txt", JsonConvert.SerializeObject(memos)); - - - break; - case "download_start": - if (!msg.Contents.StartsWith("shiftnet/")) - { - server.DispatchTo(new Guid(msg.GUID), new NetObject("shiftnet_got", new ServerMessage - { - Name = "shiftnet_file", - GUID = "server", - Contents = (File.Exists("badrequest.md") == true) ? File.ReadAllText("badrequest.md") : @"# Bad request. - -You have sent a bad request to the multi-user domain. Please try again." - })); - return; - } - - if (File.Exists(msg.Contents)) - { - server.DispatchTo(new Guid(msg.GUID), new NetObject("download", new ServerMessage - { - Name = "download_meta", - GUID = "server", - Contents = JsonConvert.SerializeObject(File.ReadAllBytes(msg.Contents)) - })); - } - else - { - server.DispatchTo(new Guid(msg.GUID), new NetObject("shiftnet_got", new ServerMessage - { - Name = "shiftnet_file", - GUID = "server", - Contents = (File.Exists("notfound.md") == true) ? File.ReadAllText("notfound.md") : @"# Not found. - -The page you requested at was not found on this multi-user domain." - })); - - } - break; - case "shiftnet_get": - string surl = args["url"] as string; - while (surl.EndsWith("/")) - { - surl = surl.Remove(surl.Length - 1, 1); - } - if (!surl.StartsWith("shiftnet/")) - { - server.DispatchTo(new Guid(msg.GUID), new NetObject("shiftnet_got", new ServerMessage - { - Name = "shiftnet_file", - GUID = "server", - Contents = (File.Exists("badrequest.md") == true) ? File.ReadAllText("badrequest.md") : @"# Bad request. - -You have sent a bad request to the multi-user domain. Please try again." - })); - - return; - } - - if (File.Exists(surl)) - { - server.DispatchTo(new Guid(msg.GUID), new NetObject("shiftnet_got", new ServerMessage - { - Name = "shiftnet_file", - GUID = "server", - Contents = File.ReadAllText(surl) - })); - } - else if (File.Exists(surl + "/home.rnp")) - { - server.DispatchTo(new Guid(msg.GUID), new NetObject("shiftnet_got", new ServerMessage - { - Name = "shiftnet_file", - GUID = "server", - Contents = File.ReadAllText(surl + "/home.rnp") - })); - - } - else - { - server.DispatchTo(new Guid(msg.GUID), new NetObject("shiftnet_got", new ServerMessage - { - Name = "shiftnet_file", - GUID = "server", - Contents = (File.Exists("notfound.md") == true) ? File.ReadAllText("notfound.md") : @"# Not found. - -The page you requested at was not found on this multi-user domain." - })); - - } - break; - case "chat_join": - if (args.ContainsKey("id")) - { - var cuser = new Save(); - if (args.ContainsKey("user")) - { - cuser = JsonConvert.DeserializeObject<Save>(JsonConvert.SerializeObject(args["user"])); - - - } - int index = -1; - string chat_id = args["id"] as string; - foreach(var chat in chats) - { - if(chat.ID == chat_id) - { - if(chat.Users.Count < chat.MaxUsers || chat.MaxUsers == 0) - { - //user can join chat. - if(cuser != null) - { - index = chats.IndexOf(chat); - } - } - } - } - if(index > -1) - { - chats[index].Users.Add(cuser); - server.DispatchAll(new NetObject("broadcast", new ServerMessage - { - Name = "cbroadcast", - GUID = "server", - Contents = $"{chat_id}: {cuser.Username} {{CHAT_HAS_JOINED}}" - })); - } - else - { - server.DispatchTo(new Guid(msg.GUID), new NetObject("broadcast", new ServerMessage - { - Name = "cbroadcast", - GUID = "server", - Contents = $"{chat_id}: {{CHAT_NOT_FOUND_OR_TOO_MANY_MEMBERS}}" - })); - } - } - break; - case "chat": - if (args.ContainsKey("id")) - { - var cuser = new Save(); - if (args.ContainsKey("user")) - { - cuser = JsonConvert.DeserializeObject<Save>(JsonConvert.SerializeObject(args["user"])); - - - } - string message = ""; - if (args.ContainsKey("msg")) - message = args["msg"] as string; - - int index = -1; - string chat_id = args["id"] as string; - foreach (var chat in chats) - { - if (chat.ID == chat_id) - { - if (cuser != null && !string.IsNullOrWhiteSpace(message) && UserInChat(chat, cuser)) - { - index = chats.IndexOf(chat); - } - } - } - if (index > -1) - { - server.DispatchAll(new NetObject("broadcast", new ServerMessage - { - Name = "cbroadcast", - GUID = "server", - Contents = $"{chat_id}/{cuser.Username}: {message}" - })); - } - else - { - server.DispatchTo(new Guid(msg.GUID), new NetObject("broadcast", new ServerMessage - { - Name = "cbroadcast", - GUID = "server", - Contents = $"{chats[index].ID}: {{CHAT_NOT_FOUND_OR_NOT_IN_CHAT}}" - })); - } - } - - break; - case "chat_leave": - if (args.ContainsKey("id")) - { - var cuser = new Save(); - if (args.ContainsKey("user")) - { - cuser = JsonConvert.DeserializeObject<Save>(JsonConvert.SerializeObject(args["user"])); - - - } - int index = -1; - string chat_id = args["id"] as string; - foreach (var chat in chats) - { - if (chat.ID == chat_id) - { - if (cuser != null && UserInChat(chat, cuser)) - { - index = chats.IndexOf(chat); - } - } - } - if (index > -1) - { - server.DispatchAll(new NetObject("broadcast", new ServerMessage - { - Name = "cbroadcast", - GUID = "server", - Contents = $"{chats[index].ID}: {cuser.Username} {{HAS_LEFT_CHAT}}" - })); - } - else - { - server.DispatchTo(new Guid(msg.GUID), new NetObject("broadcast", new ServerMessage - { - Name = "cbroadcast", - GUID = "server", - Contents = $"{chat_id}: {{CHAT_NOT_FOUND_OR_NOT_IN_CHAT}}" - })); - } - } - break; - case "chat_create": - string id = ""; - string topic = ""; - string name = ""; - int max_users = 0; - - if (args.ContainsKey("id")) - id = args["id"] as string; - if (args.ContainsKey("topic")) - name = args["topic"] as string; - if (args.ContainsKey("name")) - topic = args["name"] as string; - if (args.ContainsKey("max_users")) - max_users = Convert.ToInt32(args["max_users"].ToString()); - - bool id_taken = false; - - foreach(var chat in chats) - { - if (chat.ID == id) - id_taken = true; - } - - if (id_taken == false) - { - chats.Add(new Channel - { - ID = id, - Name = name, - Topic = topic, - MaxUsers = max_users, - Users = new List<Save>() - }); - SaveChats(); - server.DispatchTo(new Guid(msg.GUID), new NetObject("broadcast", new ServerMessage - { - Name = "cbroadcast", - GUID = "server", - Contents = $"{id}: {{SUCCESSFULLY_CREATED_CHAT}}" - })); - } - else - { - server.DispatchTo(new Guid(msg.GUID), new NetObject("broadcast", new ServerMessage - { - Name = "cbroadcast", - GUID = "server", - Contents = $"{id}: {{ID_TAKEN}}" - })); - } - - break; - case "broadcast": - string text = msg.Contents; - if (!string.IsNullOrWhiteSpace(text)) - { - server.DispatchTo(new Guid(msg.GUID), new NetObject("runme", new ServerMessage - { - Name = "broadcast", - GUID = "Server", - Contents = text - - })); - } - break; - case "getguid_reply": - msg.GUID = "server"; - //The message's GUID was manipulated by the client to send to another client. - //So we can just bounce back the message to the other client. - server.DispatchTo(new Guid(msg.GUID), new NetObject("bounce", msg)); - break; - case "getguid_send": - string usrname = msg.Contents; - string guid = msg.GUID; - server.DispatchAll(new NetObject("are_you_this_guy", new ServerMessage - { - Name = "getguid_fromserver", - GUID = guid, - Contents = usrname, - })); - break; - case "script": - string user = ""; - string script = ""; - string sArgs = ""; - - if (!args.ContainsKey("user")) - throw new Exception("No 'user' arg specified in message to server"); - - if (!args.ContainsKey("script")) - throw new Exception("No 'script' arg specified in message to server"); - - if (!args.ContainsKey("args")) - throw new Exception("No 'args' arg specified in message to server"); - - user = args["user"] as string; - script = args["script"] as string; - sArgs = args["args"] as string; - - if(File.Exists($"scripts/{user}/{script}.lua")) - { - var script_arguments = JsonConvert.DeserializeObject<Dictionary<string, object>>(sArgs); - server.DispatchTo(new Guid(msg.GUID), new NetObject("runme", new ServerMessage { - Name="run", - GUID="Server", - Contents = $@"{{ - script:""{File.ReadAllText($"scripts/{user}/{script}.lua").Replace("\"", "\\\"")}"", - args:""{sArgs}"" - }}" - })); - } - else - { - throw new Exception($"{user}.{script}: Script not found."); - } - break; - } } catch(Exception ex) { diff --git a/ShiftOS.Server/ShiftOS.Server.csproj b/ShiftOS.Server/ShiftOS.Server.csproj index dce2f20..ee12b2e 100644 --- a/ShiftOS.Server/ShiftOS.Server.csproj +++ b/ShiftOS.Server/ShiftOS.Server.csproj @@ -53,7 +53,9 @@ </Reference> </ItemGroup> <ItemGroup> + <Compile Include="Core.cs" /> <Compile Include="LegionManager.cs" /> + <Compile Include="MemoManager.cs" /> <Compile Include="PongHighscores.cs" /> <Compile Include="Program.cs" /> <Compile Include="Properties\AssemblyInfo.cs" /> @@ -64,6 +66,7 @@ </Compile> <Compile Include="RemoteTerminal.cs" /> <Compile Include="SaveManager.cs" /> + <Compile Include="ShiftnetBackend.cs" /> <Compile Include="ShopBackend.cs" /> </ItemGroup> <ItemGroup> diff --git a/ShiftOS.Server/ShiftnetBackend.cs b/ShiftOS.Server/ShiftnetBackend.cs new file mode 100644 index 0000000..701021d --- /dev/null +++ b/ShiftOS.Server/ShiftnetBackend.cs @@ -0,0 +1,114 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ShiftOS.Objects; +using NetSockets; +using Newtonsoft.Json; +using System.IO; +using static ShiftOS.Server.Program; + +namespace ShiftOS.Server +{ + public static class ShiftnetBackend + { + [MudRequest("download_start")] + public static void StartDownload(string guid, object contents) + { + string url = contents as string; + if (!url.StartsWith("shiftnet/")) + { + server.DispatchTo(new Guid(guid), new NetObject("shiftnet_got", new ServerMessage + { + Name = "shiftnet_file", + GUID = "server", + Contents = (File.Exists("badrequest.md") == true) ? File.ReadAllText("badrequest.md") : @"# Bad request. + +You have sent a bad request to the multi-user domain. Please try again." + })); + return; + } + + if (File.Exists(url)) + { + server.DispatchTo(new Guid(guid), new NetObject("download", new ServerMessage + { + Name = "download_meta", + GUID = "server", + Contents = JsonConvert.SerializeObject(File.ReadAllBytes(url)) + })); + } + else + { + server.DispatchTo(new Guid(guid), new NetObject("shiftnet_got", new ServerMessage + { + Name = "shiftnet_file", + GUID = "server", + Contents = (File.Exists("notfound.md") == true) ? File.ReadAllText("notfound.md") : @"# Not found. + +The page you requested at was not found on this multi-user domain." + })); + + } + + } + + [MudRequest("shiftnet_get")] + public static void GetPage(string guid, object contents) + { + var args = contents as Dictionary<string, object>; + string surl = args["url"] as string; + while (surl.EndsWith("/")) + { + surl = surl.Remove(surl.Length - 1, 1); + } + if (!surl.StartsWith("shiftnet/")) + { + server.DispatchTo(new Guid(guid), new NetObject("shiftnet_got", new ServerMessage + { + Name = "shiftnet_file", + GUID = "server", + Contents = (File.Exists("badrequest.md") == true) ? File.ReadAllText("badrequest.md") : @"# Bad request. + +You have sent a bad request to the multi-user domain. Please try again." + })); + + return; + } + + if (File.Exists(surl)) + { + server.DispatchTo(new Guid(guid), new NetObject("shiftnet_got", new ServerMessage + { + Name = "shiftnet_file", + GUID = "server", + Contents = File.ReadAllText(surl) + })); + } + else if (File.Exists(surl + "/home.rnp")) + { + server.DispatchTo(new Guid(guid), new NetObject("shiftnet_got", new ServerMessage + { + Name = "shiftnet_file", + GUID = "server", + Contents = File.ReadAllText(surl + "/home.rnp") + })); + + } + else + { + server.DispatchTo(new Guid(guid), new NetObject("shiftnet_got", new ServerMessage + { + Name = "shiftnet_file", + GUID = "server", + Contents = (File.Exists("notfound.md") == true) ? File.ReadAllText("notfound.md") : @"# Not found. + +The page you requested at was not found on this multi-user domain." + })); + + } + + } + } +} |
