moar fixes

This commit is contained in:
Alex
2025-01-24 18:37:09 +00:00
parent 9bc04ddc17
commit 53b5863650
5 changed files with 250 additions and 60 deletions
+134 -22
View File
@@ -1,54 +1,166 @@
using SptCommon.Annotations;
using Core.Models.Eft.Common.Tables;
using Core.Models.Eft.Ragfair;
using Core.Models.Enums;
using Core.Models.Spt.Config;
using Core.Models.Utils;
using Core.Servers;
using Core.Services;
using Core.Utils.Cloners;
namespace Core.Helpers;
[Injectable]
public class RagfairHelper
public class RagfairHelper(
TraderAssortHelper traderAssortHelper,
DatabaseService databaseService,
HandbookHelper handbookHelper,
ItemHelper itemHelper,
RagfairLinkedItemService ragfairLinkedItemService,
UtilityHelper utilityHelper,
ConfigServer configServer,
ICloner cloner
)
{
/// <summary>
/// Gets currency TAG from TPL
/// </summary>
/// <param name="currency">currency</param>
/// <returns>string</returns>
protected RagfairConfig ragfairConfig = configServer.GetConfig<RagfairConfig>();
/**
* Gets currency TAG from TPL
* @param {string} currency
* @returns string
*/
public string GetCurrencyTag(string currency)
{
throw new NotImplementedException();
switch (currency) {
case Money.EUROS:
return "EUR";
case Money.DOLLARS:
return "USD";
case Money.ROUBLES:
return "RUB";
case Money.GP:
return "GP";
default:
return "";
}
}
public List<string> FilterCategories(string sessionID, SearchRequestData request)
{
throw new NotImplementedException();
var result = new List<string>();
// Case: weapon builds
if (request.BuildCount != null) {
return request.BuildItems.Keys.ToList();
}
// Case: search
if (request.LinkedSearchId != null) {
var data = ragfairLinkedItemService.GetLinkedItems(request.LinkedSearchId);
result = data == null ? [] : [..data];
}
// Case: category
if (request.HandbookId != null) {
var handbook = GetCategoryList(request.HandbookId);
if (result.Count != null && result.Count > 0) {
result = utilityHelper.ArrayIntersect(result, handbook);
} else {
result = handbook;
}
}
return result;
}
public Dictionary<string, TraderAssort> GetDisplayableAssorts(string sessionID)
{
throw new NotImplementedException();
var result = new Dictionary<string, TraderAssort>();
foreach (var traderID in databaseService.GetTraders().Keys) {
if (ragfairConfig.Traders.ContainsKey(traderID)) {
result[traderID] = traderAssortHelper.GetAssort(sessionID, traderID, true);
}
}
return result;
}
protected List<string> GetCategoryList(string handbookId)
{
throw new NotImplementedException();
var result = new List<string>();
// if its "mods" great-parent category, do double recursive loop
if (handbookId == "5b5f71a686f77447ed5636ab") {
foreach (var categ in handbookHelper.ChildrenCategories(handbookId)) {
foreach (var subcateg in handbookHelper.ChildrenCategories(categ)) {
result = [..result, ..handbookHelper.TemplatesWithParent(subcateg)];
}
}
return result;
}
// item is in any other category
if (handbookHelper.IsCategory(handbookId)) {
// list all item of the category
result = handbookHelper.TemplatesWithParent(handbookId);
foreach (var categ in handbookHelper.ChildrenCategories(handbookId)) {
result = [..result, ..handbookHelper.TemplatesWithParent(categ)];
}
return result;
}
// its a specific item searched
result.Add(handbookId);
return result;
}
/// <summary>
/// Iterate over array of identical items and merge stack count
/// Ragfair allows abnormally large stacks.
/// </summary>
/**
* Iterate over array of identical items and merge stack count
* Ragfair allows abnormally large stacks.
*/
public List<Item> MergeStackable(List<Item> items)
{
throw new NotImplementedException();
var list = new List<Item>();
Item rootItem = null;
foreach (var item in items) {
var itemFixed = itemHelper.FixItemStackCount(item);
var isChild = items.Any(it => it.Id == itemFixed.ParentId);
if (!isChild) {
if (rootItem == null) {
rootItem = cloner.Clone(itemFixed);
rootItem.Upd.OriginalStackObjectsCount = rootItem.Upd.StackObjectsCount;
} else {
rootItem.Upd.StackObjectsCount += itemFixed.Upd.StackObjectsCount;
list.Add(itemFixed);
}
} else {
list.Add(itemFixed);
}
}
return [rootItem, ..list];
}
/// <summary>
/// Return the symbol for a currency
/// e.g. 5449016a4bdc2d6f028b456f return ₽
/// </summary>
/// <param name="currencyTpl">currency to get symbol for</param>
/// <returns>symbol of currency</returns>
/**
* Return the symbol for a currency
* e.g. 5449016a4bdc2d6f028b456f return ₽
* @param currencyTpl currency to get symbol for
* @returns symbol of currency
*/
public string GetCurrencySymbol(string currencyTpl)
{
throw new NotImplementedException();
return currencyTpl switch
{
Money.EUROS => "€",
Money.DOLLARS => "$",
_ => "₽"
};
}
}
@@ -49,7 +49,7 @@ public record ItemLocation
public record Upd
{
public UpdBuff? Buff { get; set; }
public int? OriginalStackObjectsCount { get; set; }
public double? OriginalStackObjectsCount { get; set; }
public UpdTogglable? Togglable { get; set; }
public UpdMap? Map { get; set; }
public UpdTag? Tag { get; set; }
@@ -64,7 +64,7 @@ public record SearchRequestData : IRequestData
public string? NeededSearchId { get; set; }
[JsonPropertyName("buildItems")]
public BuildItems? BuildItems { get; set; }
public Dictionary<string, double>? BuildItems { get; set; }
[JsonPropertyName("buildCount")]
public int? BuildCount { get; set; }
@@ -82,8 +82,3 @@ public enum OfferOwnerType
TRADEROWNERTYPE = 1,
PLAYEROWNERTYPE = 2
}
public record BuildItems
{
// Define properties for BuildItems here if needed
}
@@ -1,54 +1,137 @@
using SptCommon.Annotations;
using Core.Helpers;
using SptCommon.Annotations;
using Core.Models.Eft.Common.Tables;
using Core.Models.Enums;
using Core.Models.Utils;
using SptCommon.Extensions;
namespace Core.Services;
[Injectable(InjectionType.Singleton)]
public class RagfairLinkedItemService
public class RagfairLinkedItemService(
DatabaseService databaseService,
ItemHelper itemHelper,
ISptLogger<RagfairLinkedItemService> logger
)
{
protected Dictionary<string, HashSet<string>> linkedItemsCache = new();
public HashSet<string> GetLinkedItems(string linkedSearchId)
{
throw new NotImplementedException();
if (!linkedItemsCache.Keys.Any()) {
BuildLinkedItemTable();
}
return linkedItemsCache[linkedSearchId];
}
/// <summary>
/// Use ragfair linked item service to get an array of items that can fit on or in designated itemtpl
/// </summary>
/// <param name="itemTpl">Item to get sub-items for</param>
/// <returns>TemplateItem array</returns>
/**
* Use ragfair linked item service to get an array of items that can fit on or in designated itemtpl
* @param itemTpl Item to get sub-items for
* @returns ITemplateItem array
*/
public List<TemplateItem> GetLinkedDbItems(string itemTpl)
{
throw new NotImplementedException();
var linkedItemsToWeaponTpls = GetLinkedItems(itemTpl);
return linkedItemsToWeaponTpls.Aggregate(new List<TemplateItem>(), (result, linkedTpl) => {
var itemDetails = itemHelper.GetItem(linkedTpl);
if (itemDetails.Key) {
result.Add(itemDetails.Value);
} else {
logger.Warning($"Item {itemTpl} has invalid linked item {linkedTpl}");
}
return result;
});
}
/// <summary>
/// Create Dictionary of every item and the items associated with it
/// </summary>
/**
* Create Dictionary of every item and the items associated with it
*/
protected void BuildLinkedItemTable()
{
throw new NotImplementedException();
var linkedItems = new Dictionary<string, HashSet<string>>();
foreach (var item in databaseService.GetItems().Values) {
var itemLinkedSet = GetLinkedItems(linkedItems, item.Id);
ApplyLinkedItems(GetFilters(item, "Slots"), item, itemLinkedSet);
ApplyLinkedItems(GetFilters(item, "Chambers"), item, itemLinkedSet);
ApplyLinkedItems(GetFilters(item, "Cartridges"), item, itemLinkedSet);
// Edge case, ensure ammo for revolves is included
if (item.Parent == BaseClasses.REVOLVER) {
// Find magazine for revolver
AddRevolverCylinderAmmoToLinkedItems(item, itemLinkedSet);
}
}
linkedItemsCache = linkedItems;
}
/// <summary>
/// Add ammo to revolvers linked item dictionary
/// </summary>
/// <param name="cylinder">Revolvers cylinder</param>
/// <param name="applyLinkedItems"></param>
protected void AddRevolverCylinderAmmoToLinkedItems(
TemplateItem cylinder,
Action<List<string>> applyLinkedItems)
protected void ApplyLinkedItems(List<string> items, TemplateItem item, HashSet<string> itemLinkedSet)
{
throw new NotImplementedException();
foreach (var linkedItemId in items) {
itemLinkedSet.Add(linkedItemId);
GetLinkedItems(linkedItemId).Add(item.Id);
}
}
/// <summary>
/// Scans a given slot type for filters and returns them as a List
/// </summary>
/// <param name="item"></param>
/// <param name="slot"></param>
/// <returns>List of ids</returns>
protected HashSet<string> GetLinkedItems(Dictionary<string, HashSet<string>> linkedItems, string id)
{
if (!linkedItems.ContainsKey(id)) {
linkedItems.Add(id, []);
}
return linkedItems[id];
}
/**
* Add ammo to revolvers linked item dictionary
* @param cylinder Revolvers cylinder
* @param applyLinkedItems
*/
protected void AddRevolverCylinderAmmoToLinkedItems(TemplateItem cylinder, HashSet<string> itemLinkedSet)
{
var cylinderMod = cylinder.Properties.Slots?.FirstOrDefault((x) => x.Name == "mod_magazine");
if (cylinderMod != null) {
// Get the first cylinder filter tpl
var cylinderTpl = cylinderMod.Props?.Filters?[0].Filter?[0];
if (!string.IsNullOrEmpty(cylinderTpl)) {
// Get db data for cylinder tpl, add found slots info (camora_xxx) to linked items on revolver weapon
var cylinderItem = itemHelper.GetItem(cylinderTpl).Value;
ApplyLinkedItems(GetFilters(cylinderItem, "Slots"), cylinder, itemLinkedSet);
}
}
}
/**
* Scans a given slot type for filters and returns them as a Set
* @param item
* @param slot
* @returns array of ids
*/
protected List<string> GetFilters(TemplateItem item, string slot)
{
throw new NotImplementedException();
var properties = item.Properties.GetAllPropsAsDict();
if (!properties.TryGetValue(slot, out var value) || value == null) {
// item slot doesnt exist
return [];
}
/*
var filters = new List<string>();
// I have no fucking clue wtf is happening here... god help us all and anyone who has to read this code
foreach (var sub in properties[slot].GetAllPropsAsDict()) {
if (!("_props" in sub && "filters" in sub._props)) {
// not a filter
continue;
}
for (var filter of sub._props.filters) {
for (var f of filter.Filter) {
filters.push(f);
}
}
}
*/
return new List<string>();
}
}
+2 -2
View File
@@ -65,9 +65,9 @@ namespace SptCommon.Extensions
return result;
}
public static Dictionary<string, object> GetAllPropsAsDict(this object? obj)
public static Dictionary<string, object?> GetAllPropsAsDict(this object? obj)
{
var result = new Dictionary<string, object>();
var result = new Dictionary<string, object?>();
var props = obj.GetType().GetProperties();
foreach (var prop in props)