feat: get best trader offer for items
TODO: Make a better conversion for USD/EUR to RUB
This commit is contained in:
44
Plugin/Utils/EconomyUtils.cs
Normal file
44
Plugin/Utils/EconomyUtils.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Comfort.Common;
|
||||
using EFT.InventoryLogic;
|
||||
using SPT.Reflection.Utils;
|
||||
using EFTCurrencyUtils = GClass2531;
|
||||
|
||||
namespace LootValueEX.Utils
|
||||
{
|
||||
internal class EconomyUtils
|
||||
{
|
||||
|
||||
public static float ConvertToRuble(string id, float amount)
|
||||
{
|
||||
if (!EFTCurrencyUtils.TryGetCurrencyType(id, out ECurrencyType currencyType))
|
||||
return 0f;
|
||||
if (currencyType.Equals(ECurrencyType.RUB))
|
||||
return amount;
|
||||
return amount * (float)Singleton<HandbookClass>.Instance.GetBasePrice(id);
|
||||
}
|
||||
|
||||
public static Structs.TradeOfferStruct GetTraderItemOffer(TraderClass trader, Item item)
|
||||
{
|
||||
Plugin.Log.LogDebug($"GetTraderItemOffer: {item.LocalizedName()} - {trader.LocalizedName}");
|
||||
TraderClass.GStruct244? tradeOffer = trader.GetUserItemPrice(item);
|
||||
|
||||
if (tradeOffer == null)
|
||||
{
|
||||
Plugin.Log.LogDebug("GetTraderItemOffer: tradeOffer == null");
|
||||
return default;
|
||||
}
|
||||
if(tradeOffer.Value.Amount > 0)
|
||||
{
|
||||
Plugin.Log.LogDebug($"{trader.LocalizedName}\n\tCurrencyID: {tradeOffer.Value.CurrencyId}\n\tValue: {tradeOffer.Value.Amount}\n\tPrice in RB: {EconomyUtils.ConvertToRuble(tradeOffer.Value.CurrencyId, tradeOffer.Value.Amount)}");
|
||||
return new Structs.TradeOfferStruct(trader.Id, tradeOffer.Value.CurrencyId, tradeOffer.Value.Amount, EconomyUtils.ConvertToRuble(tradeOffer.Value.CurrencyId, tradeOffer.Value.Amount));
|
||||
}
|
||||
Plugin.Log.LogDebug("GetTraderItemOffer: no value");
|
||||
return default;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,6 @@
|
||||
using EFT.InventoryLogic;
|
||||
using LootValueEX.Extensions;
|
||||
using SPT.Reflection.Utils;
|
||||
|
||||
namespace LootValueEX.Utils
|
||||
{
|
||||
@@ -13,5 +15,23 @@ namespace LootValueEX.Utils
|
||||
}
|
||||
return totalDurability;
|
||||
}
|
||||
|
||||
public static Task<Structs.TradeOfferStruct> GetBestTraderValueAsync(Item item)
|
||||
{
|
||||
Structs.TradeOfferStruct bestOffer = new();
|
||||
item = item.UnstackItem();
|
||||
foreach (TraderClass trader in ClientAppUtils.GetMainApp().GetClientBackEndSession().DisplayableTraders)
|
||||
{
|
||||
Structs.TradeOfferStruct currentOffer = EconomyUtils.GetTraderItemOffer(trader, item);
|
||||
|
||||
if (currentOffer.Equals(default) || currentOffer.Price <= 0)
|
||||
continue;
|
||||
|
||||
if (currentOffer.PriceInRouble > bestOffer.PriceInRouble)
|
||||
bestOffer = currentOffer;
|
||||
}
|
||||
return Task.FromResult(bestOffer);
|
||||
}
|
||||
public static Structs.TradeOfferStruct GetBestTraderValue(Item item) => Task.Run(() => GetBestTraderValueAsync(item)).Result;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user