More MUD refactoring (I'm almost done!)

This commit is contained in:
Michael 2017-02-12 10:51:53 -05:00
parent 88e84f74e1
commit 110438929c
7 changed files with 515 additions and 610 deletions

View file

@ -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<Legion> legions = new List<Legion>();
if (File.Exists("legions.json"))
legions = JsonConvert.DeserializeObject<List<Legion>>(File.ReadAllText("legions.json"));
var l = JsonConvert.DeserializeObject<Legion>(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<Legion> allLegions = new List<Legion>();
if (File.Exists("legions.json"))
allLegions = JsonConvert.DeserializeObject<List<Legion>>(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<Legion>(contents as string);
List<string> userIDs = new List<string>();
foreach (var savfile in Directory.GetFiles("saves"))
{
try
{
var savefilecontents = JsonConvert.DeserializeObject<Save>(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<Save>(contents as string);
if (File.Exists("legions.json"))
{
var legionList = JsonConvert.DeserializeObject<List<Legion>>(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"
}));
}
}
}

View file

@ -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<PongHighscore>();
if (File.Exists("pong_highscores.json"))
hs = JsonConvert.DeserializeObject<List<PongHighscore>>(File.ReadAllText("pong_highscores.json"));
var newHS = JsonConvert.DeserializeObject<PongHighscore>(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));
}
}
}

View file

@ -332,415 +332,7 @@ namespace ShiftOS.Server
switch (msg.Name)
{
case "trm_invcmd":
Console.WriteLine("Before arg check");
args = JsonConvert.DeserializeObject<Dictionary<string, object>>(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<Shop> shopList = new List<Shop>();
if (File.Exists("shops.json"))
shopList = JsonConvert.DeserializeObject<List<Shop>>(File.ReadAllText("shops.json"));
var username = args["username"] as string;
var updateShop = JsonConvert.DeserializeObject<Shop>(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<Shop> shopFile = new List<Shop>();
if (File.Exists("shops.json"))
shopFile = JsonConvert.DeserializeObject<List<Shop>>(File.ReadAllText("shops.json"));
var newShop = JsonConvert.DeserializeObject<Shop>(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<Shop> allshops = new List<Shop>();
if (File.Exists("shops.json"))
allshops = JsonConvert.DeserializeObject<List<Shop>>(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<List<Shop>>(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<Shop> shops = new List<Shop>();
if (File.Exists("shops.json"))
shops = JsonConvert.DeserializeObject<List<Shop>>(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<ShopItem>();
}
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<Save>(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<Save>(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<Legion> legions = new List<Legion>();
if (File.Exists("legions.json"))
legions = JsonConvert.DeserializeObject<List<Legion>>(File.ReadAllText("legions.json"));
var l = JsonConvert.DeserializeObject<Legion>(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<Legion> allLegions = new List<Legion>();
if (File.Exists("legions.json"))
allLegions = JsonConvert.DeserializeObject<List<Legion>>(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<Legion>(msg.Contents);
List<string> userIDs = new List<string>();
foreach (var savfile in Directory.GetFiles("saves"))
{
try
{
var savefilecontents = JsonConvert.DeserializeObject<Save>(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<Save>(msg.Contents);
if (File.Exists("legions.json"))
{
var legionList = JsonConvert.DeserializeObject<List<Legion>>(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<Save>(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<Save>(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<List<Shop>>(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<PongHighscore>();
if (File.Exists("pong_highscores.json"))
hs = JsonConvert.DeserializeObject<List<PongHighscore>>(File.ReadAllText("ponghighscores.json"));
var newHS = JsonConvert.DeserializeObject<PongHighscore>(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<string, string> virusDB = new Dictionary<string, string>();
if (File.Exists("virus.db"))
virusDB = JsonConvert.DeserializeObject<Dictionary<string, string>>(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<string, string> _virusDB = new Dictionary<string, string>();
bool addIfNotFound = true;
if (msg.Contents.Contains("||scanonly"))
addIfNotFound = false;
msg.Contents = msg.Contents.Replace("||scanonly", "");
if(File.Exists("virus.db"))
_virusDB = JsonConvert.DeserializeObject<Dictionary<string, string>>(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<OnlineUser> users = new List<OnlineUser>();
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)

View file

@ -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<string, object>;
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)
{

View file

@ -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<string, object>;
if (args["username"] != null && args["password"] != null)
{
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())
{
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<string, object>;
if (args["username"] != null && args["password"] != null)
{
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())
{
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<Save>(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<string, object>;
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<Save>(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)
{

View file

@ -53,6 +53,8 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="LegionManager.cs" />
<Compile Include="PongHighscores.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Properties\Resources.Designer.cs">
@ -62,6 +64,7 @@
</Compile>
<Compile Include="RemoteTerminal.cs" />
<Compile Include="SaveManager.cs" />
<Compile Include="ShopBackend.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />

View file

@ -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<string, object>;
List<Shop> shopList = new List<Shop>();
if (File.Exists("shops.json"))
shopList = JsonConvert.DeserializeObject<List<Shop>>(File.ReadAllText("shops.json"));
var username = args["username"] as string;
var updateShop = JsonConvert.DeserializeObject<Shop>(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<string, object>;
List<Shop> shopFile = new List<Shop>();
if (File.Exists("shops.json"))
shopFile = JsonConvert.DeserializeObject<List<Shop>>(File.ReadAllText("shops.json"));
var newShop = JsonConvert.DeserializeObject<Shop>(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<string, object>;
List<Shop> allshops = new List<Shop>();
if (File.Exists("shops.json"))
allshops = JsonConvert.DeserializeObject<List<Shop>>(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<string, object>;
var shopName = args["shopname"] as string;
Shop tempShop = null;
foreach (var item in JsonConvert.DeserializeObject<List<Shop>>(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<string, object>;
List<Shop> shops = new List<Shop>();
if (File.Exists("shops.json"))
shops = JsonConvert.DeserializeObject<List<Shop>>(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<ShopItem>();
}
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<List<Shop>>(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",
}));
}
}
}