From fdafa1d2028c2aab0544d33eb50ba1a13617e889 Mon Sep 17 00:00:00 2001 From: NetDwarf Date: Sat, 7 Jan 2023 11:01:38 +0100 Subject: [PATCH] Accept currency item if ID is the same (#433) Separate ToText() and ID Determine ToText() by ItemTemplate.Name of Item.Id_nb == ItemCurrency.id Fix wrong display Copper MerchantCatalogPages that follow other ones --- GameServer/Finance/Currency.cs | 30 ++++++++++++++++------- GameServer/Finance/Wallet.cs | 4 +-- GameServer/gameobjects/GameMerchant.cs | 14 +++++------ GameServer/gameutils/MerchantCatalog.cs | 16 ++++++------ GameServer/packets/Server/PacketLib168.cs | 2 +- 5 files changed, 40 insertions(+), 26 deletions(-) diff --git a/GameServer/Finance/Currency.cs b/GameServer/Finance/Currency.cs index a64fd78279..b6f6264b1d 100644 --- a/GameServer/Finance/Currency.cs +++ b/GameServer/Finance/Currency.cs @@ -1,10 +1,10 @@ using System; +using DOL.Database; namespace DOL.GS.Finance { public abstract class Currency { - public virtual string Name { get; } public bool IsItemCurrency => this is ItemCurrency; protected Currency() { } @@ -12,11 +12,21 @@ protected Currency() { } public static Currency Copper { get; } = new CopperCurrency(); public static Currency BountyPoints { get; } = new BountyPointsCurrency(); public static Currency Mithril { get; } = new MithrilCurrency(); - public static Currency Item(string currencyId) => new ItemCurrency(currencyId); + public static Currency Item(string currencyId, string textRepresentation = "") => new ItemCurrency(currencyId, textRepresentation); public Money Mint(long value) => Money.Mint(value, this); - public virtual string ToText() => Name; + public abstract string ToText(); + + public bool IsSameCurrencyItem(ItemTemplate item){ + if (this is ItemCurrency itemCurrency) + { + var isEquivalentItem = item.ClassType.ToLower() == $"currency.{itemCurrency.ID.ToLower()}"; + var isOriginalItem = item.Id_nb.ToLower() == itemCurrency.ID.ToLower(); + return isEquivalentItem || isOriginalItem; + } + return false; + } public override bool Equals(object obj) => obj.GetType().Equals(this.GetType()); @@ -26,32 +36,34 @@ public override bool Equals(object obj) #region Currency implementations private class CopperCurrency : Currency { - public override string Name => "money"; + public override string ToText() => "money"; } private class BountyPointsCurrency : Currency { - public override string Name => "bounty points"; + public override string ToText() => "bounty points"; } private class MithrilCurrency : Currency { - public override string Name => "Mithril"; + public override string ToText() => "Mithril"; } private class ItemCurrency : Currency { private readonly string id; + private readonly string textRepresentation; - public override string Name => id; + public string ID => id; - public ItemCurrency(string id) + public ItemCurrency(string id, string textRepresentation) { if (string.IsNullOrEmpty(id)) throw new ArgumentException("The ID of an ItemCurrency may not be null nor empty."); this.id = id.ToLower(); + this.textRepresentation = textRepresentation; } - public override string ToText() => $"units of {Name}"; + public override string ToText() => textRepresentation == "" ? id : textRepresentation; public override bool Equals(object obj) { diff --git a/GameServer/Finance/Wallet.cs b/GameServer/Finance/Wallet.cs index 68649c2509..69013d370d 100644 --- a/GameServer/Finance/Wallet.cs +++ b/GameServer/Finance/Wallet.cs @@ -23,7 +23,7 @@ public Money GetBalance(Currency currency) lock (owner.Inventory) { return currency.Mint(owner.Inventory.GetItemRange(eInventorySlot.FirstBackpack, eInventorySlot.LastBackpack) - .Where(i => i.Template.ClassType.ToLower() == $"Currency.{currency.Name}".ToLower()) + .Where(i => currency.IsSameCurrencyItem(i.Template)) .Aggregate(0, (acc, i) => acc + i.Count)); } } @@ -60,7 +60,7 @@ public bool RemoveMoney(Money money) if (GetBalance(currency).Amount < money.Amount) return false; var validCurrencyItemsInventory = owner.Inventory.GetItemRange(eInventorySlot.FirstBackpack, eInventorySlot.LastBackpack) - .Where(i => i.Template.ClassType.ToLower() == $"Currency.{currency.Name}".ToLower()); + .Where(i => currency.IsSameCurrencyItem(i.Template)); var remainingDue = (int)money.Amount; foreach (var currencyItem in validCurrencyItemsInventory) { diff --git a/GameServer/gameobjects/GameMerchant.cs b/GameServer/gameobjects/GameMerchant.cs index 782562d486..465eefa1e5 100644 --- a/GameServer/gameobjects/GameMerchant.cs +++ b/GameServer/gameobjects/GameMerchant.cs @@ -500,48 +500,48 @@ public override bool ReceiveItem(GameLiving source, InventoryItem item) public class GameBloodSealsMerchant : GameItemCurrencyMerchant { protected override Currency Currency - => Currency.Item("BloodSeal"); + => Currency.Item("BloodSeal", "Blood Seals"); } [Obsolete("This is going to be removed. See GameItemCurrencyMerchant's obsolete message for more details.")] public class GameDiamondSealsMerchant : GameItemCurrencyMerchant { protected override Currency Currency - => Currency.Item("DiamondSeal"); + => Currency.Item("DiamondSeal", "Diamond Seals"); } [Obsolete("This is going to be removed. See GameItemCurrencyMerchant's obsolete message for more details.")] public class GameSapphireSealsMerchant : GameItemCurrencyMerchant { protected override Currency Currency - => Currency.Item("SapphireSeal"); + => Currency.Item("SapphireSeal", "Sapphire Seals"); } [Obsolete("This is going to be removed. See GameItemCurrencyMerchant's obsolete message for more details.")] public class GameEmeraldSealsMerchant : GameItemCurrencyMerchant { protected override Currency Currency - => Currency.Item("EmeraldSeal"); + => Currency.Item("EmeraldSeal", "Emerald Seals"); } [Obsolete("This is going to be removed. See GameItemCurrencyMerchant's obsolete message for more details.")] public class GameAuruliteMerchant : GameItemCurrencyMerchant { protected override Currency Currency - => Currency.Item("aurulite"); + => Currency.Item("aurulite", "units of aurulite"); } [Obsolete("This is going to be removed. See GameItemCurrencyMerchant's obsolete message for more details.")] public class GameAtlanteanGlassMerchant : GameItemCurrencyMerchant { protected override Currency Currency - => Currency.Item("atlanteanglass"); + => Currency.Item("atlanteanglass", "Atlantean Glass"); } [Obsolete("This is going to be removed. See GameItemCurrencyMerchant's obsolete message for more details.")] public class GameDragonMerchant : GameItemCurrencyMerchant { protected override Currency Currency - => Currency.Item("dragonscales"); + => Currency.Item("dragonscales", "Dragon Scales"); } } \ No newline at end of file diff --git a/GameServer/gameutils/MerchantCatalog.cs b/GameServer/gameutils/MerchantCatalog.cs index 3ce19bcbea..a9488f59b7 100644 --- a/GameServer/gameutils/MerchantCatalog.cs +++ b/GameServer/gameutils/MerchantCatalog.cs @@ -29,7 +29,7 @@ public class MerchantCatalogEntry public byte SlotPosition { get; } = 0; public byte Page { get; } = 0; public ItemTemplate Item { get; } - public long CurrencyAmount {get; } = 0; + public long CurrencyAmount { get; } = 0; public MerchantCatalogEntry(byte slotPosition, byte page, ItemTemplate itemTemplate, long currencyAmount) { @@ -140,10 +140,7 @@ public static MerchantCatalog LoadFromDatabase(string itemListId) var page = catalog.GetPage(dbMerchantItem.PageNumber); if (dbMerchantItem.SlotPosition == currencySlotPosition) { - var currencyId = (byte)dbMerchantItem.Price; - var itemTemplateId = dbMerchantItem.ItemTemplateID; - var pageCurrency = CreateCurrencyFromId(currencyId, itemTemplateId); - page.SetCurrency(pageCurrency); + page.SetCurrency(GetCurrencyFrom(dbMerchantItem)); } var itemTemplate = GameServer.Database.FindObjectByKey(dbMerchantItem.ItemTemplateID); if (itemTemplate == null) continue; @@ -154,12 +151,17 @@ public static MerchantCatalog LoadFromDatabase(string itemListId) return catalog; } - private static Currency CreateCurrencyFromId(byte currencyId, string itemCurrencyId = null) + private static Currency GetCurrencyFrom(MerchantItem merchantItem) { + var currencyId = (byte)merchantItem.Price; + var itemCurrencyId = merchantItem.ItemTemplateID; switch (currencyId) { case 1: return Currency.Copper; - case 2: return Currency.Item(itemCurrencyId); + case 2: + var itemTemplate = GameServer.Database.FindObjectByKey(itemCurrencyId); + if (itemTemplate == null) return Currency.Item(itemCurrencyId, $"units of {itemCurrencyId}"); + else return Currency.Item(itemCurrencyId, itemTemplate.Name); case 3: return Currency.BountyPoints; case 4: return Currency.Mithril; default: throw new System.NotImplementedException($"Currency with id {currencyId} is not implemented."); diff --git a/GameServer/packets/Server/PacketLib168.cs b/GameServer/packets/Server/PacketLib168.cs index 2a81366f04..87fc14ee7d 100644 --- a/GameServer/packets/Server/PacketLib168.cs +++ b/GameServer/packets/Server/PacketLib168.cs @@ -1850,7 +1850,7 @@ public virtual void SendMerchantWindow(MerchantCatalog catalog, eMerchantWindowT { foreach (var page in catalog.GetAllPages()) { - if (page.Currency.Equals(Currency.Copper) == false) windowType = ConvertCurrencyToMerchantWindowType(page.Currency); + windowType = ConvertCurrencyToMerchantWindowType(page.Currency); using (GSTCPPacketOut pak = new GSTCPPacketOut(GetPacketCode(eServerPackets.MerchantWindow))) { pak.WriteByte((byte)page.EntryCount); //Item count on this page