From 9705e9889f6d45f6a7ce52676069a66e18efd0a0 Mon Sep 17 00:00:00 2001 From: SilicaAndPina Date: Fri, 6 Nov 2020 21:22:15 +1300 Subject: [PATCH] Add shop info . --- DataCollection/gamedata.json | 14 +++- .../Horse Isle Server/Game/Messages.cs | 47 +++++++++++ .../Horse Isle Server/Game/Meta.cs | 82 ++++++++++++++----- .../Horse Isle Server/Game/Shop.cs | 40 ++++++--- .../Horse Isle Server/Game/ShopInventory.cs | 82 +++++++++++++++++-- .../Horse Isle Server/Player/User.cs | 1 + .../Horse Isle Server/Server/Database.cs | 2 +- .../Horse Isle Server/Server/Gamedata.cs | 28 ++++++- 8 files changed, 253 insertions(+), 43 deletions(-) diff --git a/DataCollection/gamedata.json b/DataCollection/gamedata.json index 8256df9..d9f9aae 100644 --- a/DataCollection/gamedata.json +++ b/DataCollection/gamedata.json @@ -54,6 +54,11 @@ "end_of_meta":"^Z", "back_to_map":"^M", "long_full_line":"^L", + "shop":{ + "selling":"^LThings I am selling:", + "sell_me":"^LThings you can SELL to me:", + "infinity":"~", + }, "hay_pile":{ "no_pitchfork":"Unfortunately you do not have a pitchfork to gather hay!", "pitchfork":"You have a pitchfork, would you like to gather some hay?^I267^T6Get to work Gathering Hay ^BMpitchfork^R1" @@ -69,12 +74,19 @@ "inventory":{ "header_format":"^ATYour Inventory^HYou are carrying the following %ITEMCOUNT% different items: (%MAXITEMS% max)", "item_entry":"^I%ICONID%^T4(%COUNT%) %TITLE%", + "shop_entry":"^I%ICONID%^T4(%COUNT%) %TITLE% $%PRICE%", "item_drop_button":"^B4D%RANDOMID%", "item_info_button":"^B4LE%RANDOMID%", + "item_info_itemid_button":"^B4LN%ITEMID%", "item_consume_button":"^B4E%RANDOMID%", "item_throw_button":"^B4T%RANDOMID%", "item_use_button":"^B4UD%RANDOMID%", - "item_read_button":"^B4VL%RANDOMID%" + "item_read_button":"^B4VL%RANDOMID%", + "buy_button":"^B4BN%ITEMID%", + "buy_5_button":"^B4B5%ITEMID%", + "buy_25_button":"^B4B2%ITEMID%", + "sell_button":"^B4S%RANDOMID%", + "sell_all_button":"^B4A%ITEMID%", }, "dropped_items":{ "nothing_message":"^LYou see nothing on the ground of interest.^R1", diff --git a/Horse Isle Server/Horse Isle Server/Game/Messages.cs b/Horse Isle Server/Horse Isle Server/Game/Messages.cs index 0930c1f..14d6fac 100644 --- a/Horse Isle Server/Horse Isle Server/Game/Messages.cs +++ b/Horse Isle Server/Horse Isle Server/Game/Messages.cs @@ -74,15 +74,30 @@ namespace HISP.Game // Inventory public static string InventoryItemFormat; + public static string InventoryHeaderFormat; public static string ItemDropButton; public static string ItemInformationButton; + public static string ItemInformationByIdButton; public static string ItemConsumeButton; public static string ItemThrowButton; public static string ItemUseButton; public static string ItemReadButton; + public static string ShopEntryFormat; + public static string ShopBuyButton; + public static string ShopBuy5Button; + public static string ShopBuy25Button; + + public static string SellButton; + public static string SellAllButton; + + // Shop + public static string ThingsIAmSelling; + public static string ThingsYouSellMe; + public static string InfinitySign; + // Npc public static string NpcStartChatFormat; public static string NpcChatpointFormat; @@ -123,6 +138,38 @@ namespace HISP.Game public static string WagonCutscene; public static string BallonCutscene; + + + public static string FormatShopEntry(int iconid, string count, string name, int price) + { + return ShopEntryFormat.Replace("%ICONID%", iconid.ToString()).Replace("%COUNT%", count).Replace("%TITLE%", name).Replace("%PRICE%", price.ToString()); + } + + public static string FormatItemInformationByIdButton(int itemId) + { + return ItemInformationByIdButton.Replace("%ITEMID%", itemId.ToString()); + } + public static string FormatBuyItemButton(int itemId) + { + return ShopBuyButton.Replace("%ITEMID%", itemId.ToString()); + } + public static string FormatBuy5ItemButton(int itemId) + { + return ShopBuy5Button.Replace("%ITEMID%", itemId.ToString()); + } + public static string FormatBuy25ItemButton(int itemId) + { + return ShopBuy25Button.Replace("%ITEMID%", itemId.ToString()); + } + public static string ForamtSellButton(int randomId) + { + return SellButton.Replace("%RANDOMID%", randomId.ToString()); + } + public static string FormatSellAllButton(int itemId) + { + return SellAllButton.Replace("%ITEMID%", itemId.ToString()); + } + public static string FormatNpcInformation(string name, string description) { return NpcInformationFormat.Replace("%NAME%", name).Replace("%DESCRIPTION%", description); diff --git a/Horse Isle Server/Horse Isle Server/Game/Meta.cs b/Horse Isle Server/Horse Isle Server/Game/Meta.cs index 1613c0c..af319be 100644 --- a/Horse Isle Server/Horse Isle Server/Game/Meta.cs +++ b/Horse Isle Server/Horse Isle Server/Game/Meta.cs @@ -74,6 +74,37 @@ namespace HISP.Game return playersNearby; } + private static string buildShopInfo(Shop shop) + { + string message = ""; + InventoryItem[] itemList = shop.Inventory.GetItemList(); + + message += Messages.ThingsIAmSelling; + foreach (InventoryItem item in itemList) + { + message += "^R1"; + Item.ItemInformation itemInfo = Item.GetItemById(item.ItemId); + + int count = item.ItemInstances.Count; + string countStr = count.ToString(); + if (item.Infinite) + countStr = Messages.InfinitySign; + + + message += Messages.FormatShopEntry(itemInfo.IconId, countStr, itemInfo.Name, shop.CalculateBuyCost(itemInfo)); + + message += Messages.FormatBuyItemButton(itemInfo.Id); + if (count >= 5) + message += Messages.FormatBuy5ItemButton(itemInfo.Id); + if (count >= 25) + message += Messages.FormatBuy25ItemButton(itemInfo.Id); + + message += Messages.FormatItemInformationByIdButton(itemInfo.Id); + + } + return message; + } + private static string buildCommonInfo(int x, int y) { string message = ""; @@ -174,34 +205,41 @@ namespace HISP.Game if (specialTile.Code == null) message += buildCommonInfo(specialTile.X, specialTile.Y); else + { + user.MetaPriority = true; - string TileCode = specialTile.Code; - string TileArg = ""; - if (TileCode.Contains("-")) - { + string TileCode = specialTile.Code; - TileCode = TileCode.Split('-')[0]; - TileArg = TileCode.Split('-')[1]; - } + string TileArg = ""; + if (TileCode.Contains("-")) + { + TileArg = TileCode.Split('-')[1]; + TileCode = TileCode.Split('-')[0]; + } - if (TileCode == "TRANSPORT") - { - Transport.TransportPoint point = Transport.GetTransportPoint(specialTile.X, specialTile.Y); - message += Meta.BuildTransportInfo(point)+ "^R1"; - } - - if (TileCode == "STRAWPILE") - { - if (user.Inventory.HasItemId(Item.Pitchfork)) - message += Messages.HasPitchforkMeta; - else - message += Messages.NoPitchforkMeta; - } + if (TileCode == "TRANSPORT") + { + Transport.TransportPoint point = Transport.GetTransportPoint(specialTile.X, specialTile.Y); + message += Meta.BuildTransportInfo(point) + "^R1"; + } - if(TileCode == "STORE") - { + if (TileCode == "STRAWPILE") + { + if (user.Inventory.HasItemId(Item.Pitchfork)) + message += Messages.HasPitchforkMeta; + else + message += Messages.NoPitchforkMeta; + } + if (TileCode == "STORE") + { + int ShopID = int.Parse(TileArg); + Shop shop = Shop.GetShopById(ShopID); + user.LastShoppedAt = shop; + message += buildShopInfo(shop); + + } } diff --git a/Horse Isle Server/Horse Isle Server/Game/Shop.cs b/Horse Isle Server/Horse Isle Server/Game/Shop.cs index 2603137..7eec1c7 100644 --- a/Horse Isle Server/Horse Isle Server/Game/Shop.cs +++ b/Horse Isle Server/Horse Isle Server/Game/Shop.cs @@ -1,4 +1,5 @@ -using System; +using HISP.Server; +using System; using System.Collections.Generic; namespace HISP.Game @@ -10,14 +11,22 @@ namespace HISP.Game public string[] BuysItemTypes; public int BuyPricePercentage; public int SellPricePercentage; - public int[] InfniteStocks; - ShopInventory Inventory; + public ShopInventory Inventory; - public Shop() + public Shop(int[] infiniteStocks) { - Id = shopList.Count; - Inventory = new ShopInventory(this); - shopList.Add(this); + Id = ShopList.Count+1; + this.Inventory = new ShopInventory(this); + + + foreach(int stock in infiniteStocks) + { + if (Item.ItemIdExist(stock)) + this.Inventory.AddInfinity(Item.GetItemById(stock)); + else + Logger.WarnPrint("Item ID: " + stock + " Does not exist."); + } + Shop.ShopList.Add(this); } public int CalculateBuyCost(Item.ItemInformation item) @@ -29,13 +38,22 @@ namespace HISP.Game return Math.Abs(item.SellPrice * (100 / SellPricePercentage)); } - - + public bool CanSell(Item.ItemInformation item) + { + foreach(string ItemType in BuysItemTypes) + { + if(ItemType == item.Type) + { + return true; + } + } + return false; + } // Static Functions - private static List shopList = new List(); + public static List ShopList = new List(); public static Shop GetShopById(int id) { - return shopList[id]; + return ShopList[id-1]; } } diff --git a/Horse Isle Server/Horse Isle Server/Game/ShopInventory.cs b/Horse Isle Server/Horse Isle Server/Game/ShopInventory.cs index 7fd10be..ba04df7 100644 --- a/Horse Isle Server/Horse Isle Server/Game/ShopInventory.cs +++ b/Horse Isle Server/Horse Isle Server/Game/ShopInventory.cs @@ -1,6 +1,7 @@ using HISP.Server; using System; using System.Collections.Generic; +using System.Linq; namespace HISP.Game { @@ -20,6 +21,8 @@ namespace HISP.Game baseShop = shopkeeper; ItemInstance[] instances = Database.GetShopInventory(baseShop.Id).ToArray(); + inventoryItems = new List(); + foreach (ItemInstance instance in instances) { addItem(instance, false); @@ -43,6 +46,7 @@ namespace HISP.Game InventoryItem inventoryItem = new InventoryItem(); inventoryItem.ItemId = item.ItemId; + inventoryItem.Infinite = false; inventoryItem.ItemInstances.Add(item); inventoryItems.Add(inventoryItem); } @@ -52,8 +56,11 @@ namespace HISP.Game InventoryItem inventoryItem = new InventoryItem(); inventoryItem.ItemId = itemInfo.Id; inventoryItem.Infinite = true; + for(int i = 0; i < 25; i++) // add 25 inventoryItem.ItemInstances.Add(new ItemInstance(inventoryItem.ItemId)); + + inventoryItems.Add(inventoryItem); } public void Add(ItemInstance item) { @@ -62,32 +69,93 @@ namespace HISP.Game public InventoryItem GetItemByItemId(int itemId) { - throw new NotImplementedException(); + InventoryItem[] items = GetItemList(); + foreach (InventoryItem item in items) + { + if (item.ItemId == itemId) + { + return item; + } + } + throw new KeyNotFoundException("id: " + itemId + " not found in shop inventory"); } public InventoryItem GetItemByRandomid(int randomId) { - throw new NotImplementedException(); + InventoryItem[] items = GetItemList(); + foreach (InventoryItem item in items) + { + ItemInstance[] instances = item.ItemInstances.ToArray(); + foreach (ItemInstance instance in instances) + { + if (instance.RandomId == randomId) + return item; + } + } + throw new KeyNotFoundException("random id: " + randomId + " not found in shop inventory"); } - public InventoryItem[] GetItemList() { - throw new NotImplementedException(); + return inventoryItems.OrderBy(o => o.ItemInstances[0].GetItemInfo().SortBy).OrderBy(o => o.Infinite).ToArray(); } public bool HasItem(int randomId) { - throw new NotImplementedException(); + InventoryItem[] items = GetItemList(); + foreach (InventoryItem item in items) + { + ItemInstance[] instances = item.ItemInstances.ToArray(); + foreach (ItemInstance instance in instances) + { + if (instance.RandomId == randomId) + return true; + } + } + return false; } public bool HasItemId(int itemId) { - throw new NotImplementedException(); + InventoryItem[] items = GetItemList(); + foreach (InventoryItem item in items) + { + if (item.ItemId == itemId) + { + return true; + } + } + return false; } + public void Remove(ItemInstance item) { - throw new NotImplementedException(); + + foreach (InventoryItem inventoryItem in inventoryItems) + { + if (item.ItemId == inventoryItem.ItemId) + { + foreach (ItemInstance instance in inventoryItem.ItemInstances) + { + if (instance.RandomId == item.RandomId) + { + inventoryItem.ItemInstances.Remove(instance); + + if (inventoryItem.ItemInstances.Count <= 0) + inventoryItems.Remove(inventoryItem); + + + if (!inventoryItem.Infinite) // no need to bug the database. + Database.RemoveItemFromShopInventory(baseShop.Id, item); + else + inventoryItem.ItemInstances.Add(new ItemInstance(inventoryItem.ItemId)); // Gen new item in inventory to replace it. + return; + } + } + } + } + + Logger.ErrorPrint("Tried to remove item : " + item.RandomId + " from inventory when it was not in it"); } } } diff --git a/Horse Isle Server/Horse Isle Server/Player/User.cs b/Horse Isle Server/Horse Isle Server/Player/User.cs index d9e3c46..00428de 100644 --- a/Horse Isle Server/Horse Isle Server/Player/User.cs +++ b/Horse Isle Server/Horse Isle Server/Player/User.cs @@ -34,6 +34,7 @@ namespace HISP.Player public string Password; // For chat filter. public PlayerInventory Inventory; public Npc.NpcEntry LastTalkedToNpc; + public Shop LastShoppedAt; public PlayerQuests Quests; public int ChatViolations { diff --git a/Horse Isle Server/Horse Isle Server/Server/Database.cs b/Horse Isle Server/Horse Isle Server/Server/Database.cs index ac408d3..02c0104 100644 --- a/Horse Isle Server/Horse Isle Server/Server/Database.cs +++ b/Horse Isle Server/Horse Isle Server/Server/Database.cs @@ -21,7 +21,7 @@ namespace HISP.Server string BuddyTable = "CREATE TABLE BuddyList(Id INT, IdFriend INT, Pending BOOL)"; string WorldTable = "CREATE TABLE World(Time INT,Day INT, Year INT, Weather TEXT(64))"; string InventoryTable = "CREATE TABLE Inventory(PlayerID INT, RandomID INT, ItemID INT)"; - string ShopInventory = "CREATE TABLE ShopInventroy(ShopID INT, RandomID INT, ItemID INT)"; + string ShopInventory = "CREATE TABLE ShopInventory(ShopID INT, RandomID INT, ItemID INT)"; string DroppedItems = "CREATE TABLE DroppedItems(X INT, Y INT, RandomID INT, ItemID INT, DespawnTimer INT)"; string TrackedQuest = "CREATE TABLE TrackedQuest(playerId INT, questId INT, timesCompleted INT)"; diff --git a/Horse Isle Server/Horse Isle Server/Server/Gamedata.cs b/Horse Isle Server/Horse Isle Server/Server/Gamedata.cs index f29cb25..06655f5 100644 --- a/Horse Isle Server/Horse Isle Server/Server/Gamedata.cs +++ b/Horse Isle Server/Horse Isle Server/Server/Gamedata.cs @@ -342,6 +342,19 @@ namespace HISP.Server Logger.DebugPrint("Registered Quest: " + quest.Id); Quest.QuestList.Add(quest); } + + int totalShops = gameData.shop_list.Count; + for(int i = 0; i < totalShops; i++) + { + + Shop shop = new Shop(gameData.shop_list[i].stocks_itemids.ToObject()); + shop.BuyPricePercentage = gameData.shop_list[i].buy_percent; + shop.SellPricePercentage = gameData.shop_list[i].sell_percent; + shop.BuysItemTypes = gameData.shop_list[i].buys_item_types.ToObject(); + + Logger.DebugPrint("Registered Shop ID: "+ shop.Id + " Selling items at " + shop.SellPricePercentage + "% and buying at " + shop.BuyPricePercentage); + } + Item.Present = gameData.item.special.present; Item.MailMessage = gameData.item.special.mail_message; Item.DorothyShoes = gameData.item.special.dorothy_shoes; @@ -414,6 +427,11 @@ namespace HISP.Server Messages.RakeNothing = gameData.messages.tools.rake; Messages.ShovelNothing = gameData.messages.tools.shovel; + // Shop + Messages.ThingsIAmSelling = gameData.messages.meta.shop.selling; + Messages.ThingsYouSellMe = gameData.messages.meta.shop.sell_me; + Messages.InfinitySign = gameData.messages.meta.shop.infinity; + // Meta Format Messages.LocationFormat = gameData.messages.meta.location_format; @@ -445,14 +463,22 @@ namespace HISP.Server Messages.InventoryHeaderFormat = gameData.messages.meta.inventory.header_format; Messages.InventoryItemFormat = gameData.messages.meta.inventory.item_entry; - + Messages.ShopEntryFormat = gameData.messages.meta.inventory.shop_entry; Messages.ItemInformationButton = gameData.messages.meta.inventory.item_info_button; + Messages.ItemInformationByIdButton = gameData.messages.meta.inventory.item_info_itemid_button; + Messages.ItemDropButton = gameData.messages.meta.inventory.item_drop_button; Messages.ItemThrowButton = gameData.messages.meta.inventory.item_throw_button; Messages.ItemConsumeButton = gameData.messages.meta.inventory.item_consume_button; Messages.ItemUseButton = gameData.messages.meta.inventory.item_use_button; Messages.ItemReadButton = gameData.messages.meta.inventory.item_read_button; + Messages.ShopBuyButton = gameData.messages.meta.inventory.buy_button; + Messages.ShopBuy5Button = gameData.messages.meta.inventory.buy_5_button; + Messages.ShopBuy25Button = gameData.messages.meta.inventory.buy_25_button; + + Messages.SellButton = gameData.messages.meta.inventory.sell_button; + Messages.SellAllButton = gameData.messages.meta.inventory.sell_all_button; // Npc Messages.NpcStartChatFormat = gameData.messages.meta.npc.start_chat_format;