264 lines
9.0 KiB
C#
Raw Normal View History

2022-07-09 12:15:57 +01:00
using Aki.Common.Http;
2022-05-14 20:06:24 +01:00
using Aki.Common.Utils;
using EFT.InventoryLogic;
using System;
using System.Collections.Generic;
2022-07-09 12:15:57 +01:00
using System.Reflection;
using System.Net;
using System.Threading;
using UnityEngine;
using System.IO;
2022-05-14 20:06:24 +01:00
namespace itemValueMod
{
public class ItemValue
{
public static void AddItemValue<T>(ref T __instance, string id, ItemTemplate template) where T : Item
{
2022-12-25 15:41:48 +00:00
var atts = new List<ItemAttributeClass>();
2022-05-14 20:06:24 +01:00
atts.AddRange(__instance.Attributes);
__instance.Attributes = atts;
2022-12-25 15:41:48 +00:00
ItemAttributeClass attr1 = new ItemAttributeClass(EItemAttributeId.MoneySum)
2022-05-14 20:06:24 +01:00
{
StringValue = new Func<string>(__instance.TraderPrice),
FullStringValue = new Func<string>(__instance.TraderName),
Name = "TRADER",
2022-05-14 20:06:24 +01:00
DisplayType = new Func<EItemAttributeDisplayType>(() => EItemAttributeDisplayType.Compact)
};
__instance.Attributes.Add(attr1);
2022-05-14 20:06:24 +01:00
}
}
public static class ValueExtension
{
static public Dictionary<string, JsonClass> itemDictionary = new Dictionary<string, JsonClass>();
2022-06-25 22:30:13 +01:00
static object lockObject = new object();
public static JsonClass GetData(String itemId)
{
var json = RequestHandler.GetJson($"/cwx/seeitemvalue/{itemId}");
var jsonClass = Json.Deserialize<JsonClass>(json);
itemDictionary.Add(itemId, jsonClass);
return jsonClass;
}
public static string TraderPrice(this Item item)
2022-05-14 20:06:24 +01:00
{
string itemId = item.Template._id;
2022-06-25 22:30:13 +01:00
JsonClass jsonClass;
bool lockWasTaken = false;
double alteredPrice = 1;
2022-05-14 20:06:24 +01:00
try
2022-05-14 20:06:24 +01:00
{
Monitor.Enter(lockObject, ref lockWasTaken);
if(!itemDictionary.TryGetValue(itemId, out jsonClass))
2022-06-25 22:30:13 +01:00
{
jsonClass = GetData(item.Template._id);
}
}
catch (WebException)
{
return $"[SeeItemValue] Issue happened whilst getting Item from server";
}
finally
{
if (lockWasTaken)
{
Monitor.Exit(lockObject);
}
}
if (jsonClass.price != 1)
{
alteredPrice = DurabilityCheck(item, jsonClass);
}
double _price = alteredPrice * jsonClass.multiplier;
return Math.Round(_price).ToString();
}
public static string TraderName(this Item item)
{
string itemId = item.Template._id;
JsonClass jsonClass;
bool lockWasTaken = false;
try
{
Monitor.Enter(lockObject, ref lockWasTaken);
if (!itemDictionary.TryGetValue(itemId, out jsonClass))
{
jsonClass = GetData(item.Template._id);
}
}
catch (WebException)
{
return $"[SeeItemValue] Issue happened whilst getting Item from server";
}
finally
{
if (lockWasTaken)
{
Monitor.Exit(lockObject);
2022-07-09 12:15:57 +01:00
}
}
return jsonClass.traderName;
}
public static double DurabilityCheck(this Item item, JsonClass jsonClass)
{
double editedPrice = jsonClass.price;
double originalMax = jsonClass.originalMax;
DebugMode($" Entered DurabilityCheck() - starting price is: {editedPrice}");
2022-07-09 12:15:57 +01:00
var medKit = item.GetItemComponent<MedKitComponent>();
if (medKit != null && medKit.HpResource != 0 && medKit.MaxHpResource != 0)
{
DebugMode($" Medkit Check - HpResource is: {medKit.HpResource}");
DebugMode($" Medkit Check - MaxHpResource is: {medKit.MaxHpResource}");
2022-07-09 12:15:57 +01:00
editedPrice *= medKit.HpResource / medKit.MaxHpResource;
}
DebugMode($" After Medkit Check - price is: {editedPrice}");
2022-07-09 12:15:57 +01:00
var repair = item.GetItemComponent<RepairableComponent>();
if (repair != null)
{
if (repair.Durability > 0)
{
DebugMode($" repairable Check - Durability is: {repair.Durability}");
DebugMode($" Medkit Check - originalMax is: {originalMax}");
2022-07-09 12:15:57 +01:00
editedPrice *= repair.Durability / originalMax;
2022-06-25 22:30:13 +01:00
}
2022-07-09 12:15:57 +01:00
else
{
DebugMode($" repairable Check - Durability is 0");
2022-07-09 12:15:57 +01:00
editedPrice = 1;
}
}
DebugMode($" After repairable Check - price is: {editedPrice}");
2022-07-09 12:15:57 +01:00
var dogtag = item.GetItemComponent<DogtagComponent>();
if (dogtag != null && dogtag.Level != 0)
{
DebugMode($" dogtag Check - level is: {dogtag.Level}");
2022-07-09 12:15:57 +01:00
editedPrice *= dogtag.Level;
2022-06-25 22:30:13 +01:00
}
2022-05-14 20:06:24 +01:00
DebugMode($" After dogtag Check - price is: {editedPrice}");
2022-07-09 12:15:57 +01:00
var repairKit = item.GetItemComponent<RepairKitComponent>();
if (repairKit != null)
{
if (repairKit.Resource > 0)
{
DebugMode($" repairkit Check - Resource is: {repairKit.Resource}");
DebugMode($" repairkit Check - originalMax is: {originalMax}");
2022-07-09 12:15:57 +01:00
editedPrice *= repairKit.Resource / originalMax;
}
else
{
DebugMode($" repairkit Check - Resource is 0");
2022-07-09 12:15:57 +01:00
editedPrice = 1;
}
}
DebugMode($" After repairkit Check - price is: {editedPrice}");
2022-07-09 12:15:57 +01:00
var resource = item.GetItemComponent<ResourceComponent>();
if (resource != null && resource.Value != 0 && resource.MaxResource != 0)
{
DebugMode($" resource Check - Resource is: {resource.Value}");
DebugMode($" resource Check - MaxResource is: {resource.MaxResource}");
2022-07-09 12:15:57 +01:00
editedPrice *= resource.Value / resource.MaxResource;
}
DebugMode($" After resource Check - price is: {editedPrice}");
2022-07-09 12:15:57 +01:00
var foodDrink = item.GetItemComponent<FoodDrinkComponent>();
if (foodDrink != null && foodDrink.HpPercent != 0)
{
2022-12-25 15:41:48 +00:00
GInterface211 ginterface211_0 = (GInterface211)foodDrink.GetType().GetField("ginterface211_0", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(foodDrink);
2022-07-09 12:15:57 +01:00
DebugMode($" foodDrink Check - HpPercent is: {foodDrink.HpPercent}");
2022-12-25 15:41:48 +00:00
DebugMode($" foodDrink Check - MaxResource is: {ginterface211_0.MaxResource}");
2022-12-25 15:41:48 +00:00
editedPrice *= foodDrink.HpPercent / ginterface211_0.MaxResource;
2022-07-09 12:15:57 +01:00
}
DebugMode($" After foodDrink Check - price is: {editedPrice}");
2022-07-09 12:15:57 +01:00
var keys = item.GetItemComponent<KeyComponent>();
if (keys != null)
{
2022-12-25 15:41:48 +00:00
GInterface215 gInterface215_0 = (GInterface215)keys.GetType().GetField("Template", BindingFlags.Public | BindingFlags.Instance).GetValue(keys);
2022-07-09 12:15:57 +01:00
if (keys.NumberOfUsages > 0)
{
2022-12-25 15:41:48 +00:00
double totalMinusUsed = Convert.ToDouble(gInterface215_0.MaximumNumberOfUsage - keys.NumberOfUsages);
double multi = totalMinusUsed / gInterface215_0.MaximumNumberOfUsage;
2022-07-09 12:15:57 +01:00
DebugMode($" foodDrink Check - totalMinusUsed is: {totalMinusUsed}");
DebugMode($" foodDrink Check - multi is: {multi}");
2022-07-09 12:15:57 +01:00
editedPrice *= multi;
}
}
DebugMode($" After keys Check - price is: {editedPrice}");
2022-07-09 12:15:57 +01:00
var sideEffect = item.GetItemComponent<SideEffectComponent>();
if (sideEffect != null && sideEffect.Value != 0)
{
DebugMode($" sideEffect Check - resource is: {sideEffect.Value}");
DebugMode($" sideEffect Check - MaxResource is: {sideEffect.MaxResource}");
2022-07-09 12:15:57 +01:00
editedPrice *= sideEffect.Value / sideEffect.MaxResource;
}
DebugMode($" After sideEffect Check - price is: {editedPrice}");
DebugMode($"Ending price: {editedPrice}");
2022-07-09 12:15:57 +01:00
return editedPrice;
2022-05-14 20:06:24 +01:00
}
public static void DebugMode(String str)
{
var directory = Directory.GetCurrentDirectory();
var modDirectory = new DirectoryInfo(directory + "/user/mods/");
DirectoryInfo[] dirsInDir = modDirectory.GetDirectories("*" + "SeeItemValue" + "*.*");
if (dirsInDir.Length == 1)
{
var json = File.ReadAllText(dirsInDir[0].ToString() + "/src/config.json");
var configJson = Json.Deserialize<ConfigClass>(json);
if (configJson != null && configJson.DebugMode)
{
Debug.LogError(str);
}
}
}
2022-05-14 20:06:24 +01:00
}
}