Skip to content

Commit

Permalink
Accept currency item if ID is the same (#433)
Browse files Browse the repository at this point in the history
Separate ToText() and ID
Determine ToText() by ItemTemplate.Name of Item.Id_nb == ItemCurrency.id
Fix wrong display Copper MerchantCatalogPages that follow other ones
  • Loading branch information
NetDwarf authored Jan 7, 2023
1 parent 87224cb commit fdafa1d
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 26 deletions.
30 changes: 21 additions & 9 deletions GameServer/Finance/Currency.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,32 @@
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() { }

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());
Expand All @@ -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)
{
Expand Down
4 changes: 2 additions & 2 deletions GameServer/Finance/Wallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
Expand Down Expand Up @@ -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)
{
Expand Down
14 changes: 7 additions & 7 deletions GameServer/gameobjects/GameMerchant.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
16 changes: 9 additions & 7 deletions GameServer/gameutils/MerchantCatalog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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<ItemTemplate>(dbMerchantItem.ItemTemplateID);
if (itemTemplate == null) continue;
Expand All @@ -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<ItemTemplate>(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.");
Expand Down
2 changes: 1 addition & 1 deletion GameServer/packets/Server/PacketLib168.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit fdafa1d

Please sign in to comment.