From 110438929c855aec8fe1a4cc0b01ccad7ee3807d Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 12 Feb 2017 10:51:53 -0500 Subject: More MUD refactoring (I'm almost done!) --- ShiftOS.Server/SaveManager.cs | 135 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 135 insertions(+) (limited to 'ShiftOS.Server/SaveManager.cs') 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) { -- cgit v1.2.3