This repository has been archived on 2025-01-01. You can view files and clone it, but cannot push or open issues or pull requests.
ShiftOS_TheReturn/ShiftOS.Server/SaveManager.cs

375 lines
14 KiB
C#
Raw Normal View History

2017-03-06 12:01:16 -05:00
/*
2017-02-24 20:24:57 -05:00
* MIT License
*
* Copyright (c) 2017 Michael VanOverbeek and ShiftOS devs
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
using System;
2017-02-11 11:48:26 -05:00
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ShiftOS.Objects;
using System.IO;
using Newtonsoft.Json;
using NetSockets;
using static ShiftOS.Server.Program;
2017-04-30 11:44:29 -04:00
using System.Net;
2017-02-11 11:48:26 -05:00
namespace ShiftOS.Server
{
public static class SaveManager
{
2017-02-15 16:30:58 -05:00
[MudRequest("usr_getcp", typeof(Dictionary<string, object>))]
2017-02-11 11:48:26 -05:00
public static void GetCodepoints(string guid, object contents)
{
var args = contents as Dictionary<string, object>;
if (!args.ContainsKey("username"))
throw new MudException("No 'username' argument supplied.");
2017-04-10 16:46:36 -04:00
args["username"] = args["username"].ToString().ToLower();
2017-02-11 11:48:26 -05:00
foreach(var savefile in Directory.GetFiles("saves"))
{
var save = ReadSave(savefile);
if(save.Username == args["username"] as string)
{
Program.ClientDispatcher.DispatchTo("usr_codepoints", guid, save.Codepoints);
return;
}
}
throw new MudException("User " + args["username"] as string + " not found on this multi-user domain.");
}
2017-02-15 16:30:58 -05:00
[MudRequest("mud_login", typeof(Dictionary<string, object>))]
public static void UserLogin(string guid, object contents)
{
var args = contents as Dictionary<string, object>;
if (args["username"] != null && args["password"] != null)
{
2017-04-10 16:46:36 -04:00
args["username"] = args["username"].ToString().ToLower();
foreach (var savefile in Directory.GetFiles("saves"))
{
try
{
var save = JsonConvert.DeserializeObject<Save>(ReadEncFile(savefile));
if (save.Username == args["username"].ToString() && save.Password == args["password"].ToString())
{
2017-03-06 12:01:16 -05:00
if(save.ID == new Guid())
{
save.ID = Guid.NewGuid();
WriteEncFile(savefile, JsonConvert.SerializeObject(save));
}
2017-02-12 10:56:24 -05:00
Program.server.DispatchTo(new Guid(guid), new NetObject("mud_savefile", new ServerMessage
{
Name = "mud_savefile",
GUID = "server",
Contents = JsonConvert.SerializeObject(save)
}));
return;
}
}
catch { }
}
2017-02-26 13:09:59 -05:00
try
{
2017-02-26 13:09:59 -05:00
Program.server.DispatchTo(new Guid(guid), new NetObject("auth_failed", new ServerMessage
{
Name = "mud_login_denied",
GUID = "server"
}));
}
catch { }
}
else
{
2017-02-26 13:09:59 -05:00
try
{
2017-02-26 13:09:59 -05:00
Program.server.DispatchTo(new Guid(guid), new NetObject("auth_failed", new ServerMessage
{
Name = "mud_login_denied",
GUID = "server"
}));
}
catch { }
}
}
2017-02-15 16:30:58 -05:00
[MudRequest("mud_checkuserexists", typeof(Dictionary<string, object>))]
public static void CheckUserExists(string guid, object contents)
{
var args = contents as Dictionary<string, object>;
2017-04-17 15:10:34 -04:00
if (args["username"] != null)
{
2017-04-10 16:46:36 -04:00
args["username"] = args["username"].ToString().ToLower();
foreach (var savefile in Directory.GetFiles("saves"))
{
try
{
var save = JsonConvert.DeserializeObject<Save>(ReadEncFile(savefile));
2017-04-17 15:10:34 -04:00
if (save.Username == args["username"].ToString())
{
2017-02-12 10:56:24 -05:00
server.DispatchTo(new Guid(guid), new NetObject("mud_savefile", new ServerMessage
{
Name = "mud_found",
GUID = "server",
}));
return;
}
}
catch { }
}
2017-02-12 10:56:24 -05:00
server.DispatchTo(new Guid(guid), new NetObject("auth_failed", new ServerMessage
{
Name = "mud_notfound",
GUID = "server"
}));
}
else
{
2017-02-12 10:56:24 -05:00
server.DispatchTo(new Guid(guid), new NetObject("auth_failed", new ServerMessage
{
Name = "mud_notfound",
GUID = "server"
}));
}
}
2017-02-15 16:30:58 -05:00
[MudRequest("mud_save", typeof(Save))]
public static void SaveGame(string guid, object contents)
{
2017-02-15 16:30:58 -05:00
var sav = contents as Save;
2017-04-17 15:13:58 -04:00
if (string.IsNullOrWhiteSpace(sav.Username))
return;
2017-04-10 16:46:36 -04:00
sav.Username = sav.Username.ToLower();
WriteEncFile("saves/" + sav.Username + ".save", JsonConvert.SerializeObject(sav, Formatting.Indented));
2017-02-13 13:12:05 -05:00
try
{
2017-04-30 11:44:29 -04:00
2017-02-13 13:12:05 -05:00
Program.server.DispatchTo(new Guid(guid), new NetObject("auth_failed", new ServerMessage
{
Name = "mud_saved",
GUID = "server"
}));
}
catch { }
2017-04-30 11:44:29 -04:00
try
{
//Update the shiftos website with the user's codepoints.
if (!string.IsNullOrWhiteSpace(sav.UniteAuthToken))
{
2017-05-21 08:21:41 -04:00
var wreq = WebRequest.Create(UserConfig.Get().UniteUrl + "/API/SetCodepoints/" + sav.Codepoints.ToString());
2017-04-30 11:44:29 -04:00
wreq.Headers.Add("Authentication: Token " + sav.UniteAuthToken);
wreq.GetResponse();
}
}
catch { }
}
2017-04-30 12:01:48 -04:00
[MudRequest("mud_token_login", typeof(string))]
public static void TokenLogin(string guid, string token)
{
foreach (var savefile in Directory.GetFiles("saves"))
{
try
{
var save = JsonConvert.DeserializeObject<Save>(ReadEncFile(savefile));
if (save.UniteAuthToken==token)
{
if (save.ID == new Guid())
{
save.ID = Guid.NewGuid();
WriteEncFile(savefile, JsonConvert.SerializeObject(save));
}
var wr = HttpWebRequest.Create(UserConfig.Get().UniteUrl + "/API/GetCodepoints");
wr.Headers.Add("Authentication: Token " + save.UniteAuthToken);
try
{
using(var resp = wr.GetResponse())
{
using(var str = resp.GetResponseStream())
{
using(var reader = new StreamReader(str))
{
Console.WriteLine("This user has " + reader.ReadToEnd() + " Codepoint(s).");
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
Program.server.DispatchTo(new Guid(guid), new NetObject("auth_failed", new ServerMessage
{
Name = "mud_login_denied",
GUID = "server"
}));
return;
}
2017-04-30 12:01:48 -04:00
Program.server.DispatchTo(new Guid(guid), new NetObject("mud_savefile", new ServerMessage
{
Name = "mud_savefile",
GUID = "server",
Contents = JsonConvert.SerializeObject(save)
}));
return;
}
}
catch { }
}
try
{
Program.server.DispatchTo(new Guid(guid), new NetObject("auth_failed", new ServerMessage
{
Name = "mud_login_denied",
GUID = "server"
}));
}
catch { }
}
2017-02-23 17:05:09 -05:00
[MudRequest("delete_save", typeof(ClientSave))]
public static void DeleteSave(string guid, object contents)
{
var cSave = contents as ClientSave;
2017-04-10 16:46:36 -04:00
cSave.Username = cSave.Username.ToLower();
2017-02-23 17:05:09 -05:00
foreach(var saveFile in Directory.GetFiles("saves"))
{
try
{
var save = JsonConvert.DeserializeObject<Save>(ReadEncFile(saveFile));
if(save.Username == cSave.Username && save.Password == cSave.Password)
{
File.Delete(saveFile);
return;
}
}
catch { }
}
}
2017-02-15 16:30:58 -05:00
[MudRequest("usr_givecp", typeof(Dictionary<string, object>))]
public static void GiveCodepoints(string guid, object contents)
{
var args = contents as Dictionary<string, object>;
if (args["username"] != null && args["amount"] != null)
{
2017-04-10 16:46:36 -04:00
args["username"] = args["username"].ToString().ToLower();
string userName = args["username"] as string;
long cpAmount = (long)args["amount"];
if (Directory.Exists("saves"))
{
foreach (var saveFile in Directory.GetFiles("saves"))
{
var saveFileContents = JsonConvert.DeserializeObject<Save>(ReadEncFile(saveFile));
if (saveFileContents.Username == userName)
{
2017-02-12 10:56:24 -05:00
saveFileContents.Codepoints += cpAmount;
WriteEncFile(saveFile, JsonConvert.SerializeObject(saveFileContents, Formatting.Indented));
Program.ClientDispatcher.Broadcast("update_your_cp", new
{
username = userName,
amount = cpAmount
});
return;
}
}
}
}
}
2017-02-15 16:30:58 -05:00
[MudRequest("usr_takecp", typeof(Dictionary<string, object>))]
2017-02-11 11:48:26 -05:00
public static void TakeCodepoints(string guid, object contents)
{
var args = contents as Dictionary<string, object>;
if (args["username"] != null && args["password"] != null && args["amount"] != null && args["yourusername"] != null)
{
2017-04-10 16:46:36 -04:00
args["username"] = args["username"].ToString().ToLower();
2017-02-11 11:48:26 -05:00
string userName = args["username"] as string;
string passw = args["password"] as string;
int cpAmount = (int)args["amount"];
if (Directory.Exists("saves"))
{
foreach (var saveFile in Directory.GetFiles("saves"))
{
var saveFileContents = JsonConvert.DeserializeObject<Save>(ReadEncFile(saveFile));
if (saveFileContents.Username == userName && saveFileContents.Password == passw)
{
saveFileContents.Codepoints += cpAmount;
WriteEncFile(saveFile, JsonConvert.SerializeObject(saveFileContents, Formatting.Indented));
Program.ClientDispatcher.Broadcast("update_your_cp", new {
username = userName,
amount = -cpAmount
});
Program.ClientDispatcher.DispatchTo("update_your_cp", guid, new
{
username = args["yourusername"].ToString(),
amount = cpAmount
});
return;
}
}
}
}
}
private static Save ReadSave(string fPath)
{
return Newtonsoft.Json.JsonConvert.DeserializeObject<Save>(ReadEncFile(fPath));
}
private static string ReadEncFile(string fPath)
{
return Encryption.Decrypt(File.ReadAllText(fPath));
}
private static void WriteEncFile(string fPath, string contents)
{
File.WriteAllText(fPath, Encryption.Encrypt(contents));
}
}
}