From c544e4081d53bcd888c8fce459c7e02f8516f5a8 Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 9 Feb 2017 18:32:48 -0500 Subject: [PATCH] Dramatic shop optimizations. --- ShiftOS.Server/Program.cs | 36 ++++++++++++++++++- .../Applications/MUDControlCentre.cs | 32 +++++++++++++---- 2 files changed, 61 insertions(+), 7 deletions(-) diff --git a/ShiftOS.Server/Program.cs b/ShiftOS.Server/Program.cs index 9ac5702..bdcf10e 100644 --- a/ShiftOS.Server/Program.cs +++ b/ShiftOS.Server/Program.cs @@ -443,12 +443,46 @@ Contents: 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 = 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", diff --git a/ShiftOS.WinForms/Applications/MUDControlCentre.cs b/ShiftOS.WinForms/Applications/MUDControlCentre.cs index 5e93e2f..927f50d 100644 --- a/ShiftOS.WinForms/Applications/MUDControlCentre.cs +++ b/ShiftOS.WinForms/Applications/MUDControlCentre.cs @@ -140,6 +140,15 @@ namespace ShiftOS.WinForms.Applications ShowShop(JsonConvert.DeserializeObject(msg.Contents)); })); } + else if(msg.Name == "shop_additem") + { + var contents = JsonConvert.DeserializeObject>(msg.Contents); + if((string)contents["shop"] == CurrentShop.Name) + { + CurrentShop.Items.Add((ShopItem)contents["itemdata"]); + this.Invoke(new Action(PopulateShopView)); + } + } else if(msg.Name == "user_noshop") { this.Invoke(new Action(() => @@ -181,7 +190,7 @@ namespace ShiftOS.WinForms.Applications public void PopulateShopEditor() { txtshopdescription.Text = editingShop.Description; - txtshopname.Text = editingShop.Description; + txtshopname.Text = editingShop.Name; lbeditingshopitems.Items.Clear(); foreach(var item in editingShop.Items) { @@ -247,6 +256,17 @@ namespace ShiftOS.WinForms.Applications } + public void PopulateShopView() + { + lbupgrades.Items.Clear(); + shopItems = CurrentShop.Items; + foreach (var item in CurrentShop.Items) + { + lbupgrades.Items.Add(item.Name); + } + + } + public void ShowShop(Shop shop) { shop_view.BringToFront(); @@ -257,12 +277,11 @@ namespace ShiftOS.WinForms.Applications lbprice.Text = "Select an item from the list on the left."; btnbuy.Hide(); - lbupgrades.Items.Clear(); - shopItems = shop.Items; - foreach (var item in shop.Items) + ServerManager.SendMessage("shop_getitems", JsonConvert.SerializeObject(new { - lbupgrades.Items.Add(item.Name); - } + shopname = CurrentShop.Name + })); + lbupgrades.SelectedIndexChanged += (o, a) => { item = shopItems[lbupgrades.SelectedIndex]; @@ -715,6 +734,7 @@ Current legions: {legionname}"; AppearanceManager.SetupWindow(new ShopItemCreator(new ShopItem(), new Action((item) => { editingShop.Items.Add(item); + PopulateShopEditor(); }))); }