From 110438929c855aec8fe1a4cc0b01ccad7ee3807d Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 12 Feb 2017 10:51:53 -0500 Subject: [PATCH] More MUD refactoring (I'm almost done!) --- ShiftOS.Server/LegionManager.cs | 132 ++++++ ShiftOS.Server/PongHighscores.cs | 63 +++ ShiftOS.Server/Program.cs | 612 +-------------------------- ShiftOS.Server/RemoteTerminal.cs | 22 + ShiftOS.Server/SaveManager.cs | 135 ++++++ ShiftOS.Server/ShiftOS.Server.csproj | 3 + ShiftOS.Server/ShopBackend.cs | 158 +++++++ 7 files changed, 515 insertions(+), 610 deletions(-) create mode 100644 ShiftOS.Server/LegionManager.cs create mode 100644 ShiftOS.Server/PongHighscores.cs create mode 100644 ShiftOS.Server/ShopBackend.cs diff --git a/ShiftOS.Server/LegionManager.cs b/ShiftOS.Server/LegionManager.cs new file mode 100644 index 0000000..9f2de5f --- /dev/null +++ b/ShiftOS.Server/LegionManager.cs @@ -0,0 +1,132 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.IO; +using Newtonsoft.Json; +using ShiftOS.Objects; +using static ShiftOS.Server.Program; +using NetSockets; + +namespace ShiftOS.Server +{ + public static class LegionManager + { + [MudRequest("legion_createnew")] + public static void CreateLegion(string guid, object contents) + { + List legions = new List(); + if (File.Exists("legions.json")) + legions = JsonConvert.DeserializeObject>(File.ReadAllText("legions.json")); + + var l = JsonConvert.DeserializeObject(contents as string); + bool legionExists = false; + + foreach (var legion in legions) + { + if (legion.ShortName == l.ShortName) + legionExists = true; + } + + if (legionExists == false) + { + legions.Add(l); + server.DispatchTo(new Guid(msg.GUID), new NetObject("test", new ServerMessage + { + Name = "legion_create_ok", + GUID = "server" + })); + + } + else + { + server.DispatchTo(new Guid(msg.GUID), new NetObject("test", new ServerMessage + { + Name = "legion_alreadyexists", + GUID = "server" + })); + } + + File.WriteAllText("legions.json", JsonConvert.SerializeObject(legions, Formatting.Indented)); + + } + + [MudRequest("legion_getall")] + public static void GetAllLegions(string guid, object contents) + { + List allLegions = new List(); + + if (File.Exists("legions.json")) + allLegions = JsonConvert.DeserializeObject>(File.ReadAllText("legions.json")); + + server.DispatchTo(new Guid(msg.GUID), new NetObject("alllegions", new ServerMessage + { + Name = "legion_all", + GUID = "server", + Contents = JsonConvert.SerializeObject(allLegions) + })); + + } + + [MudRequest("legion_get_users")] + public static void GetLegionUsers(string guid, object contents) + { + var lgn = JsonConvert.DeserializeObject(contents as string); + + List userIDs = new List(); + + foreach (var savfile in Directory.GetFiles("saves")) + { + try + { + var savefilecontents = JsonConvert.DeserializeObject(File.ReadAllText(savfile)); + if (savefilecontents.CurrentLegions.Contains(lgn.ShortName)) + { + userIDs.Add($"{savefilecontents.Username}@{savefilecontents.SystemName}"); + } + } + catch { } + } + + server.DispatchTo(new Guid(msg.GUID), new NetObject("userlist", new ServerMessage + { + Name = "legion_users_found", + GUID = "server", + Contents = JsonConvert.SerializeObject(userIDs) + })); + + } + + [MudRequest("user_get_legion")] + public static void GetUserLegion(string guid, object contents) + { + var userSave = JsonConvert.DeserializeObject(contents as string); + + if (File.Exists("legions.json")) + { + var legionList = JsonConvert.DeserializeObject>(File.ReadAllText("legions.json")); + foreach (var legion in legionList) + { + if (userSave.CurrentLegions.Contains(legion.ShortName)) + { + server.DispatchTo(new Guid(msg.GUID), new NetObject("reply", new ServerMessage + { + Name = "user_legion", + GUID = "server", + Contents = JsonConvert.SerializeObject(legion) + })); + return; + } + } + } + + server.DispatchTo(new Guid(msg.GUID), new NetObject("fuck", new ServerMessage + { + Name = "user_not_found_in_legion", + GUID = "server" + })); + + } + } +} diff --git a/ShiftOS.Server/PongHighscores.cs b/ShiftOS.Server/PongHighscores.cs new file mode 100644 index 0000000..596e9f6 --- /dev/null +++ b/ShiftOS.Server/PongHighscores.cs @@ -0,0 +1,63 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ShiftOS.Objects; +using Newtonsoft.Json; +using System.IO; +using static ShiftOS.Server.Program; + +namespace ShiftOS.Server +{ + public static class PongHighscores + { + [MudRequest("pong_gethighscores")] + public static void GetHighScores(string guid, object contents) + { + if (File.Exists("pong_highscores.json")) + { + server.DispatchTo(new Guid(msg.GUID), new NetObject("pongstuff", new ServerMessage + { + Name = "pong_highscores", + GUID = "server", + Contents = File.ReadAllText("pong_highscores.json") + })); + } + + } + + [MudRequest("pong_sethighscores")] + public static void PostHighscores(string guid, object contents) + { + var hs = new List(); + if (File.Exists("pong_highscores.json")) + hs = JsonConvert.DeserializeObject>(File.ReadAllText("pong_highscores.json")); + + var newHS = JsonConvert.DeserializeObject(contents as string); + for (int i = 0; i <= hs.Count; i++) + { + try + { + if (hs[i].UserName == newHS.UserName) + { + if (newHS.HighestLevel > hs[i].HighestLevel) + hs[i].HighestLevel = newHS.HighestLevel; + if (newHS.HighestCodepoints > hs[i].HighestCodepoints) + hs[i].HighestCodepoints = newHS.HighestCodepoints; + File.WriteAllText("pong_highscores.json", JsonConvert.SerializeObject(hs)); + return; + + } + } + catch + { + + } + } + hs.Add(newHS); + File.WriteAllText("pong_highscores.json", JsonConvert.SerializeObject(hs)); + + } + } +} diff --git a/ShiftOS.Server/Program.cs b/ShiftOS.Server/Program.cs index b06b9d5..cb896cd 100644 --- a/ShiftOS.Server/Program.cs +++ b/ShiftOS.Server/Program.cs @@ -332,415 +332,7 @@ namespace ShiftOS.Server switch (msg.Name) { - case "trm_invcmd": - Console.WriteLine("Before arg check"); - args = JsonConvert.DeserializeObject>(msg.Contents); - if(args["guid"] != null && args["command"] != null) - { - Console.WriteLine("arg check finished"); - string cmd = args["command"] as string; - string cGuid = args["guid"] as string; - Console.WriteLine("Before dispatch"); - server.DispatchTo(new Guid(cGuid), new NetObject("trminvoke", new ServerMessage - { - Name = "trm_invokecommand", - GUID = "server", - Contents = cmd - })); - Console.WriteLine("After dispatch"); - } - break; - case "update_shop_by_user": - List shopList = new List(); - if (File.Exists("shops.json")) - shopList = JsonConvert.DeserializeObject>(File.ReadAllText("shops.json")); - - var username = args["username"] as string; - var updateShop = JsonConvert.DeserializeObject(msg.Contents); - - for(int i = 0; i < shopList.Count; i++) - { - if(shopList[i].Owner == username) - { - shopList[i] = updateShop; - } - } - - File.WriteAllText("shops.json", JsonConvert.SerializeObject(shopList, Formatting.Indented)); - - server.DispatchTo(new Guid(msg.GUID), new NetObject("nametaken", new ServerMessage - { - Name = "shop_added", - GUID = "server", - })); - - - break; - case "create_shop": - List shopFile = new List(); - if (File.Exists("shops.json")) - shopFile = JsonConvert.DeserializeObject>(File.ReadAllText("shops.json")); - - var newShop = JsonConvert.DeserializeObject(msg.Contents); - - foreach (var shop in shopFile) - { - if (shop.Name == newShop.Name) - { - server.DispatchTo(new Guid(msg.GUID), new NetObject("nametaken", new ServerMessage - { - Name = "shop_taken", - GUID = "server", - })); - return; - } - } - - shopFile.Add(newShop); - File.WriteAllText("shops.json", JsonConvert.SerializeObject(shopFile, Formatting.Indented)); - - server.DispatchTo(new Guid(msg.GUID), new NetObject("nametaken", new ServerMessage - { - Name = "shop_added", - GUID = "server", - })); - - break; - case "user_shop_check": - List allshops = new List(); - if (File.Exists("shops.json")) - allshops = JsonConvert.DeserializeObject>(File.ReadAllText("shops.json")); - - int res = 0; - - foreach(var shop in allshops) - { - if(shop.Owner == args["username"] as string) - { - res = 1; - } - } - - server.DispatchTo(new Guid(msg.GUID), new NetObject("hahhhhhhh", new ServerMessage - { - Name = "user_shop_check_result", - GUID = "server", - Contents = res.ToString() - })); - - break; - case "shop_getitems": - var shopName = args["shopname"] as string; - Shop tempShop = null; - foreach(var item in JsonConvert.DeserializeObject>(File.ReadAllText("shops.json"))) - { - if(item.Name == shopName) - { - tempShop = item; - } - } - - if(tempShop != null) - foreach(var item in tempShop.Items) - { - server.DispatchTo(new Guid(msg.GUID), new NetObject("item", new ServerMessage - { - Name = "shop_additem", - GUID = "server", - Contents = JsonConvert.SerializeObject(new - { - shop = shopName, - itemdata = Compress(Compress(JsonConvert.SerializeObject(item))) - }) - })); - } - - break; - case "shop_getall": - List shops = new List(); - if (File.Exists("shops.json")) - shops = JsonConvert.DeserializeObject>(File.ReadAllText("shops.json")); - //Purge all items in all shops temporarily. - //This is to save on network bandwidth as it will take a long time to send everyone's shops down if we don't purge the stock. - //And with high bandwidth usage, we may end up DOSing our clients when too many people upload too many things. - //Furthermore, this'll make the MUD Control Centre seem faster... - for (int i = 0; i < shops.Count; i++) - { - shops[i].Items = new List(); - } - server.DispatchTo(new Guid(msg.GUID), new NetObject("ladouceur", new ServerMessage - { - Name = "shop_all", - GUID = "server", - Contents = JsonConvert.SerializeObject(shops) - })); - break; - case "shop_requestdownload": - string download = args["download"] as string; - if (File.Exists(download) && download.StartsWith("shopDownloads/")) - { - server.DispatchTo(new Guid(msg.GUID), new NetObject("shop_download_meta", new ServerMessage - { - Name = "shop_download_meta", - GUID = "server", - Contents = JsonConvert.SerializeObject(File.ReadAllBytes(download)) - })); - } - break; - case "usr_givecp": - if (args["username"] != null && args["amount"] != null) - { - string userName = args["username"] as string; - int amount = (int)args["amount"]; - - if (Directory.Exists("saves")) - { - foreach(var saveFile in Directory.GetFiles("saves")) - { - var saveFileContents = JsonConvert.DeserializeObject(ReadEncFile(saveFile)); - if(saveFileContents.Username == userName) - { - saveFileContents.Codepoints += amount; - WriteEncFile(saveFile, JsonConvert.SerializeObject(saveFileContents, Formatting.Indented)); - server.DispatchAll(new NetObject("pikachu_use_thunderbolt_oh_yeah_and_if_you_happen_to_be_doing_backend_and_see_this_post_a_picture_of_ash_ketchum_from_the_unova_series_in_the_discord_dev_room_holy_crap_this_is_a_long_snake_case_thing_about_ash_ketchum_and_pikachu", new ServerMessage - { - Name = "update_your_cp", - GUID = "server", - Contents = $@"{{ - username: ""{userName}"", - amount: {amount} -}}" - })); - return; - } - } - } - } - break; - case "mud_login": - if (args["username"] != null && args["password"] != null) - { - foreach(var savefile in Directory.GetFiles("saves")) - { - try - { - var save = JsonConvert.DeserializeObject(ReadEncFile(savefile)); - - - if(save.Username == args["username"].ToString() && save.Password == args["password"].ToString()) - { - - server.DispatchTo(new Guid(msg.GUID), new NetObject("mud_savefile", new ServerMessage - { - Name = "mud_savefile", - GUID = "server", - Contents = JsonConvert.SerializeObject(save) - })); - return; - } - } - catch { } - } - server.DispatchTo(new Guid(msg.GUID), new NetObject("auth_failed", new ServerMessage - { - Name = "mud_login_denied", - GUID = "server" - })); - } - else - { - server.DispatchTo(new Guid(msg.GUID), new NetObject("auth_failed", new ServerMessage - { - Name = "mud_login_denied", - GUID = "server" - })); - } - break; - case "legion_createnew": - List legions = new List(); - if (File.Exists("legions.json")) - legions = JsonConvert.DeserializeObject>(File.ReadAllText("legions.json")); - - var l = JsonConvert.DeserializeObject(msg.Contents); - bool legionExists = false; - - foreach (var legion in legions) - { - if (legion.ShortName == l.ShortName) - legionExists = true; - } - - if (legionExists == false) - { - legions.Add(l); - server.DispatchTo(new Guid(msg.GUID), new NetObject("test", new ServerMessage - { - Name = "legion_create_ok", - GUID = "server" - })); - - } - else - { - server.DispatchTo(new Guid(msg.GUID), new NetObject("test", new ServerMessage - { - Name = "legion_alreadyexists", - GUID = "server" - })); - } - - File.WriteAllText("legions.json", JsonConvert.SerializeObject(legions, Formatting.Indented)); - break; - case "legion_get_all": - List allLegions = new List(); - - if (File.Exists("legions.json")) - allLegions = JsonConvert.DeserializeObject>(File.ReadAllText("legions.json")); - - server.DispatchTo(new Guid(msg.GUID), new NetObject("alllegions", new ServerMessage - { - Name = "legion_all", - GUID = "server", - Contents = JsonConvert.SerializeObject(allLegions) - })); - break; - case "legion_get_users": - var lgn = JsonConvert.DeserializeObject(msg.Contents); - - List userIDs = new List(); - - foreach (var savfile in Directory.GetFiles("saves")) - { - try - { - var savefilecontents = JsonConvert.DeserializeObject(File.ReadAllText(savfile)); - if (savefilecontents.CurrentLegions.Contains(lgn.ShortName)) - { - userIDs.Add($"{savefilecontents.Username}@{savefilecontents.SystemName}"); - } - } - catch { } - } - - server.DispatchTo(new Guid(msg.GUID), new NetObject("userlist", new ServerMessage - { - Name = "legion_users_found", - GUID = "server", - Contents = JsonConvert.SerializeObject(userIDs) - })); - break; - case "user_get_legion": - var userSave = JsonConvert.DeserializeObject(msg.Contents); - - if (File.Exists("legions.json")) - { - var legionList = JsonConvert.DeserializeObject>(File.ReadAllText("legions.json")); - foreach (var legion in legionList) - { - if (userSave.CurrentLegions.Contains(legion.ShortName)) - { - server.DispatchTo(new Guid(msg.GUID), new NetObject("reply", new ServerMessage - { - Name = "user_legion", - GUID = "server", - Contents = JsonConvert.SerializeObject(legion) - })); - return; - } - } - } - - server.DispatchTo(new Guid(msg.GUID), new NetObject("fuck", new ServerMessage - { - Name = "user_not_found_in_legion", - GUID = "server" - })); - - break; - case "mud_save": - var sav = JsonConvert.DeserializeObject(msg.Contents); - - WriteEncFile("saves/" + sav.Username + ".save", JsonConvert.SerializeObject(sav, Formatting.Indented)); - - server.DispatchTo(new Guid(msg.GUID), new NetObject("auth_failed", new ServerMessage - { - Name = "mud_saved", - GUID = "server" - })); - - break; - case "mud_checkuserexists": - if (args["username"] != null && args["password"] != null) - { - foreach (var savefile in Directory.GetFiles("saves")) - { - try - { - var save = JsonConvert.DeserializeObject(ReadEncFile(savefile)); - - - if (save.Username == args["username"].ToString() && save.Password == args["password"].ToString()) - { - server.DispatchTo(new Guid(msg.GUID), new NetObject("mud_savefile", new ServerMessage - { - Name = "mud_found", - GUID = "server", - })); - return; - } - } - catch { } - } - server.DispatchTo(new Guid(msg.GUID), new NetObject("auth_failed", new ServerMessage - { - Name = "mud_notfound", - GUID = "server" - })); - } - else - { - server.DispatchTo(new Guid(msg.GUID), new NetObject("auth_failed", new ServerMessage - { - Name = "mud_notfound", - GUID = "server" - })); - } - break; - case "user_get_shop": - string shopOwner = msg.Contents; - if (File.Exists("shops.json")) - foreach (var shop in JsonConvert.DeserializeObject>(File.ReadAllText("shops.json"))) - { - if (shop.Owner == shopOwner) - { - server.DispatchTo(new Guid(msg.GUID), new NetObject("ruecuodaL", new ServerMessage - { - Name = "user_shop", - GUID = "server", - Contents = JsonConvert.SerializeObject(shop) - })); - return; - } - } - - server.DispatchTo(new Guid(msg.GUID), new NetObject("ruecuodaL", new ServerMessage - { - Name = "user_noshop", - GUID = "server", - })); - - break; - case "pong_gethighscores": - if (File.Exists("pong_highscores.json")) - { - server.DispatchTo(new Guid(msg.GUID), new NetObject("pongstuff", new ServerMessage - { - Name = "pong_highscores", - GUID = "server", - Contents = File.ReadAllText("pong_highscores.json") - })); - } - break; + case "get_memos_for_user": if(args["username"] != null) { @@ -779,68 +371,7 @@ namespace ShiftOS.Server break; - case "pong_sethighscore": - var hs = new List(); - if (File.Exists("pong_highscores.json")) - hs = JsonConvert.DeserializeObject>(File.ReadAllText("ponghighscores.json")); - - var newHS = JsonConvert.DeserializeObject(msg.Contents); - for (int i = 0; i <= hs.Count; i++) - { - try - { - if (hs[i].UserName == newHS.UserName) - { - if (newHS.HighestLevel > hs[i].HighestLevel) - hs[i].HighestLevel = newHS.HighestLevel; - if (newHS.HighestCodepoints > hs[i].HighestCodepoints) - hs[i].HighestCodepoints = newHS.HighestCodepoints; - File.WriteAllText("pong_highscores.json", JsonConvert.SerializeObject(hs)); - return; - - } - } - catch - { - - } - } - hs.Add(newHS); - File.WriteAllText("pong_highscores.json", JsonConvert.SerializeObject(hs)); - return; - case "getvirusdb": - if (!File.Exists("virus.db")) - File.WriteAllText("virus.db", "{}"); - - server.DispatchTo(new Guid(msg.GUID), new NetObject("vdb", new ServerMessage - { - Name = "virusdb", - GUID = "server", - Contents = File.ReadAllText("virus.db") - })); - break; - case "getvirus": - Dictionary virusDB = new Dictionary(); - - if (File.Exists("virus.db")) - virusDB = JsonConvert.DeserializeObject>(File.ReadAllText("virus.db")); - - foreach (var kv in virusDB) - { - if (kv.Key == msg.Contents) - { - server.DispatchTo(new Guid(msg.GUID), new NetObject("response", new ServerMessage - { - Name = "mud_virus", - GUID = "server", - Contents = kv.Value, - })); - return; - } - } - - break; - case "download_start": + case "download_start": if (!msg.Contents.StartsWith("shiftnet/")) { server.DispatchTo(new Guid(msg.GUID), new NetObject("shiftnet_got", new ServerMessage @@ -928,56 +459,6 @@ The page you requested at was not found on this multi-user domain." } break; - case "mud_scanvirus": - Dictionary _virusDB = new Dictionary(); - - bool addIfNotFound = true; - - if (msg.Contents.Contains("||scanonly")) - addIfNotFound = false; - - msg.Contents = msg.Contents.Replace("||scanonly", ""); - - if(File.Exists("virus.db")) - _virusDB = JsonConvert.DeserializeObject>(File.ReadAllText("virus.db")); - - foreach(var kv in _virusDB) - { - if(kv.Value == msg.Contents) - { - server.DispatchTo(new Guid(msg.GUID), new NetObject("response", new ServerMessage - { - Name = "mud_virus_signature", - GUID = "server", - Contents = kv.Key, - })); - return; - } - } - - if (addIfNotFound == true) - { - string newguid = Guid.NewGuid().ToString(); - _virusDB.Add(newguid, msg.Contents); - File.WriteAllText("virus.db", JsonConvert.SerializeObject(_virusDB, Formatting.Indented)); - server.DispatchTo(new Guid(msg.GUID), new NetObject("response", new ServerMessage - { - Name = "mud_virus_signature", - GUID = "server", - Contents = newguid, - })); - } - else - { - server.DispatchTo(new Guid(msg.GUID), new NetObject("response", new ServerMessage - { - Name = "mud_virus_signature", - GUID = "server", - Contents = "unknown", - })); - } - return; - case "chat_join": if (args.ContainsKey("id")) { @@ -1179,93 +660,6 @@ The page you requested at was not found on this multi-user domain." })); } break; - case "lua_up": - string lua = msg.Contents; - string firstLine = lua.Split(new[] { Environment.NewLine }, StringSplitOptions.None)[0]; - firstLine = firstLine.Remove(0, 3); //delete the comment - string[] a = firstLine.Split('.'); - if(!Directory.Exists("scripts/" + a[0])) - { - Directory.CreateDirectory($"scripts/{a[0]}"); - } - File.WriteAllText($"scripts/{a[0]}/{a[1]}.lua", lua); - break; - case "mudhack_init": - if (MUDHackPasswords.ContainsKey(msg.GUID)) - MUDHackPasswords.Remove(msg.GUID); - - MUDHackPasswords.Add(msg.GUID, GenerateRandomPassword()); - - - server.DispatchTo(new Guid(msg.GUID), new NetObject("mudhack_init", new ServerMessage - { - Name = "mudhack_init", - GUID = "SERVER", - Contents = MUDHackPasswords[msg.GUID], - })); - - break; - case "mudhack_verify": - if (!MUDHackPasswords.ContainsKey(msg.GUID)) - { - - server.DispatchTo(new Guid(msg.GUID), new NetObject("mudhack_init", new ServerMessage - { - Name = "server_error", - GUID = "SERVER", - Contents = "{SRV_HACK_NOT_INITIATED}", - })); - return; - } - - string pass = ""; - if (args.ContainsKey("pass")) - pass = args["pass"] as string; - - if(pass == MUDHackPasswords[msg.GUID]) - { - server.DispatchTo(new Guid(msg.GUID), new NetObject("mudhack_init", new ServerMessage - { - Name = "mudhack_granted", - GUID = "SERVER", - })); - - } - else - { - - server.DispatchTo(new Guid(msg.GUID), new NetObject("mudhack_init", new ServerMessage - { - Name = "mudhack_denied", - GUID = "SERVER", - })); - } - break; - case "mudhack_killpass": - if (MUDHackPasswords.ContainsKey(msg.GUID)) - MUDHackPasswords.Remove(msg.GUID); - break; - case "mudhack_getallusers": - List users = new List(); - - foreach (var chat in chats) - { - foreach(var usr in chat.Users) - { - var ousr = new OnlineUser(); - ousr.Username = usr.Username; - ousr.OnlineChat = chat.ID; - users.Add(ousr); - } - } - - server.DispatchTo(new Guid(msg.GUID), new NetObject("mudhack_users", new ServerMessage - { - Name = "mudhack_users", - GUID = "SERVER", - Contents = JsonConvert.SerializeObject(users), - })); - break; case "getguid_reply": msg.GUID = "server"; //The message's GUID was manipulated by the client to send to another client. @@ -1317,8 +711,6 @@ The page you requested at was not found on this multi-user domain." throw new Exception($"{user}.{script}: Script not found."); } break; - default: - throw new Exception($"Server couldn't decipher this message:\n\n{JsonConvert.SerializeObject(msg)}"); } } catch(Exception ex) diff --git a/ShiftOS.Server/RemoteTerminal.cs b/ShiftOS.Server/RemoteTerminal.cs index d28cf8a..6742e44 100644 --- a/ShiftOS.Server/RemoteTerminal.cs +++ b/ShiftOS.Server/RemoteTerminal.cs @@ -26,6 +26,28 @@ namespace ShiftOS.Server } } + [MudRequest("trm_invcmd")] + public static void InvokeCommand(string guid, object contents) + { + Console.WriteLine("Before arg check"); + var args = contents as Dictionary; + if (args["guid"] != null && args["command"] != null) + { + Console.WriteLine("arg check finished"); + string cmd = args["command"] as string; + string cGuid = args["guid"] as string; + Console.WriteLine("Before dispatch"); + Program.ClientDispatcher.Server.DispatchTo(new Guid(cGuid), new NetObject("trminvoke", new ServerMessage + { + Name = "trm_invokecommand", + GUID = "server", + Contents = cmd + })); + Console.WriteLine("After dispatch"); + } + + } + [MudRequest("trm_handshake_request")] public static void RequestHandshake(string guid, object contents) { diff --git a/ShiftOS.Server/SaveManager.cs b/ShiftOS.Server/SaveManager.cs index 4bca36d..505abe6 100644 --- a/ShiftOS.Server/SaveManager.cs +++ b/ShiftOS.Server/SaveManager.cs @@ -6,6 +6,8 @@ using System.Threading.Tasks; using ShiftOS.Objects; using System.IO; using Newtonsoft.Json; +using NetSockets; +using static ShiftOS.Server.Program; namespace ShiftOS.Server { @@ -32,6 +34,139 @@ namespace ShiftOS.Server } + [MudRequest("mud_login")] + public static void UserLogin(string guid, object contents) + { + var args = contents as Dictionary; + if (args["username"] != null && args["password"] != null) + { + foreach (var savefile in Directory.GetFiles("saves")) + { + try + { + var save = JsonConvert.DeserializeObject(ReadEncFile(savefile)); + + + if (save.Username == args["username"].ToString() && save.Password == args["password"].ToString()) + { + + Program.server.DispatchTo(new Guid(msg.GUID), new NetObject("mud_savefile", new ServerMessage + { + Name = "mud_savefile", + GUID = "server", + Contents = JsonConvert.SerializeObject(save) + })); + return; + } + } + catch { } + } + Program.server.DispatchTo(new Guid(msg.GUID), new NetObject("auth_failed", new ServerMessage + { + Name = "mud_login_denied", + GUID = "server" + })); + } + else + { + Program.server.DispatchTo(new Guid(msg.GUID), new NetObject("auth_failed", new ServerMessage + { + Name = "mud_login_denied", + GUID = "server" + })); + } + + } + + [MudRequest("mud_checkuserexists")] + public static void CheckUserExists(string guid, object contents) + { + var args = contents as Dictionary; + if (args["username"] != null && args["password"] != null) + { + foreach (var savefile in Directory.GetFiles("saves")) + { + try + { + var save = JsonConvert.DeserializeObject(ReadEncFile(savefile)); + + + if (save.Username == args["username"].ToString() && save.Password == args["password"].ToString()) + { + server.DispatchTo(new Guid(msg.GUID), new NetObject("mud_savefile", new ServerMessage + { + Name = "mud_found", + GUID = "server", + })); + return; + } + } + catch { } + } + server.DispatchTo(new Guid(msg.GUID), new NetObject("auth_failed", new ServerMessage + { + Name = "mud_notfound", + GUID = "server" + })); + } + else + { + server.DispatchTo(new Guid(msg.GUID), new NetObject("auth_failed", new ServerMessage + { + Name = "mud_notfound", + GUID = "server" + })); + } + + } + + [MudRequest("mud_save")] + public static void SaveGame(string guid, object contents) + { + var sav = JsonConvert.DeserializeObject(contents as string); + + WriteEncFile("saves/" + sav.Username + ".save", JsonConvert.SerializeObject(sav, Formatting.Indented)); + + Program.server.DispatchTo(new Guid(msg.GUID), new NetObject("auth_failed", new ServerMessage + { + Name = "mud_saved", + GUID = "server" + })); + + } + + [MudRequest("usr_givecp")] + public static void GiveCodepoints(string guid, object contents) + { + var args = contents as Dictionary; + if (args["username"] != null && args["amount"] != null) + { + string userName = args["username"] as string; + int cpAmount = (int)args["amount"]; + + if (Directory.Exists("saves")) + { + foreach (var saveFile in Directory.GetFiles("saves")) + { + var saveFileContents = JsonConvert.DeserializeObject(ReadEncFile(saveFile)); + if (saveFileContents.Username == userName) + { + saveFileContents.Codepoints += amount; + WriteEncFile(saveFile, JsonConvert.SerializeObject(saveFileContents, Formatting.Indented)); + Program.ClientDispatcher.Broadcast("update_your_cp", new + { + username = userName, + amount = cpAmount + }); + + return; + } + } + } + } + + } + [MudRequest("usr_takecp")] public static void TakeCodepoints(string guid, object contents) { diff --git a/ShiftOS.Server/ShiftOS.Server.csproj b/ShiftOS.Server/ShiftOS.Server.csproj index 8654d82..dce2f20 100644 --- a/ShiftOS.Server/ShiftOS.Server.csproj +++ b/ShiftOS.Server/ShiftOS.Server.csproj @@ -53,6 +53,8 @@ + + @@ -62,6 +64,7 @@ + diff --git a/ShiftOS.Server/ShopBackend.cs b/ShiftOS.Server/ShopBackend.cs new file mode 100644 index 0000000..b59da39 --- /dev/null +++ b/ShiftOS.Server/ShopBackend.cs @@ -0,0 +1,158 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Newtonsoft.Json; +using ShiftOS.Objects; +using System.IO; +using static ShiftOS.Server.Program; + +namespace ShiftOS.Server +{ + public static class ShopBackend + { + [MudRequest("update_shop_by_user")] + public static void UpdateShopByUser(string guid, object contents) + { + var args = contents as Dictionary; + List shopList = new List(); + if (File.Exists("shops.json")) + shopList = JsonConvert.DeserializeObject>(File.ReadAllText("shops.json")); + + var username = args["username"] as string; + var updateShop = JsonConvert.DeserializeObject(msg.Contents); + + for (int i = 0; i < shopList.Count; i++) + { + if (shopList[i].Owner == username) + { + shopList[i] = updateShop; + } + } + + File.WriteAllText("shops.json", JsonConvert.SerializeObject(shopList, Formatting.Indented)); + + Program.ClientDispatcher.DispatchTo("shop_added", guid, ""); + } + + [MudRequest("create_shop")] + public static void CreateShop(string guid, object contents) + { + var args = contents as Dictionary; + List shopFile = new List(); + if (File.Exists("shops.json")) + shopFile = JsonConvert.DeserializeObject>(File.ReadAllText("shops.json")); + + var newShop = JsonConvert.DeserializeObject(msg.Contents); + + foreach (var shop in shopFile) + { + if (shop.Name == newShop.Name) + { + Program.ClientDispatcher.DispatchTo("shop_taken", guid, ""); + return; + } + } + + shopFile.Add(newShop); + File.WriteAllText("shops.json", JsonConvert.SerializeObject(shopFile, Formatting.Indented)); + + + Program.ClientDispatcher.DispatchTo("shop_added", guid, ""); + + } + + [MudRequest("user_shop_check")] + public static void UserShopCheck(string guid, object contents) + { + var args = contents as Dictionary; + List allshops = new List(); + if (File.Exists("shops.json")) + allshops = JsonConvert.DeserializeObject>(File.ReadAllText("shops.json")); + + int res = 0; + + foreach (var shop in allshops) + { + if (shop.Owner == args["username"] as string) + { + res = 1; + } + } + + Program.ClientDispatcher.DispatchTo("user_shop_check_result", guid, res.ToString()); + } + + [MudRequest("shop_getitems")] + public static void GetShopItems(string guid, object contents) + { + var args = contents as Dictionary; + var shopName = args["shopname"] as string; + Shop tempShop = null; + foreach (var item in JsonConvert.DeserializeObject>(File.ReadAllText("shops.json"))) + { + if (item.Name == shopName) + { + tempShop = item; + } + } + + if (tempShop != null) + foreach (var item in tempShop.Items) + { + Program.ClientDispatcher.DispatchTo("shop_additem", guid, new + { + shop = shopName, + itemdata = Program.Compress(Program.Compress(JsonConvert.SerializeObject(item))) + }); + } + + } + + [MudRequest("shop_getall")] + public static void GetAllShops(string guid, object contents) + { + var args = contents as Dictionary; + List shops = new List(); + if (File.Exists("shops.json")) + shops = JsonConvert.DeserializeObject>(File.ReadAllText("shops.json")); + //Purge all items in all shops temporarily. + //This is to save on network bandwidth as it will take a long time to send everyone's shops down if we don't purge the stock. + //And with high bandwidth usage, we may end up DOSing our clients when too many people upload too many things. + //Furthermore, this'll make the MUD Control Centre seem faster... + for (int i = 0; i < shops.Count; i++) + { + shops[i].Items = new List(); + } + Program.ClientDispatcher.DispatchTo("shop_all", guid, shops); + } + + [MudRequest("user_get_shop")] + public static void GetShop(string guid, object contents) + { + string shopOwner = contents as string; + if (File.Exists("shops.json")) + foreach (var shop in JsonConvert.DeserializeObject>(File.ReadAllText("shops.json"))) + { + if (shop.Owner == shopOwner) + { + server.DispatchTo(new Guid(msg.GUID), new NetObject("ruecuodaL", new ServerMessage + { + Name = "user_shop", + GUID = "server", + Contents = JsonConvert.SerializeObject(shop) + })); + return; + } + } + + server.DispatchTo(new Guid(msg.GUID), new NetObject("ruecuodaL", new ServerMessage + { + Name = "user_noshop", + GUID = "server", + })); + + } + } +}