using SPTarkov.Common.Annotations; using SPTarkov.Common.Extensions; using SPTarkov.Server.Core.Helpers; using SPTarkov.Server.Core.Models.Eft.Common.Tables; using SPTarkov.Server.Core.Models.Enums; using SPTarkov.Server.Core.Models.Spt.Mod; using SPTarkov.Server.Core.Models.Utils; using SPTarkov.Server.Core.Utils; using SPTarkov.Server.Core.Utils.Cloners; namespace SPTarkov.Server.Core.Services.Mod; [Injectable] public class CustomItemService( ISptLogger logger, HashUtil hashUtil, DatabaseService databaseService, ItemHelper itemHelper, ItemBaseClassService itemBaseClassService, ICloner cloner, LocaleService localeService ) { /// /// Create a new item from a cloned item base
/// WARNING - If no item id is supplied, an id will be generated, this id will be random every time you add an item and will not be the same on each subsequent server start
/// Add to the items db
/// Add to the flea market
/// Add to the handbook
/// Add to the locales ///
/// Item details for the new item to be created /// tplId of the new item created public CreateItemResult CreateItemFromClone(NewItemFromCloneDetails newItemDetails) { var result = new CreateItemResult(); var tables = databaseService.GetTables(); // Generate new id for item if none supplied var newItemId = GetOrGenerateIdForItem(newItemDetails.NewId); // Fail if itemId already exists if (tables.Templates.Items.ContainsKey(newItemId)) { result.Errors.Add($"ItemId already exists. {tables.Templates.Items[newItemId].Name}"); result.Success = false; result.ItemId = newItemId; return result; } // Clone existing item var itemClone = cloner.Clone(tables.Templates.Items[newItemDetails.ItemTplToClone]); // Update id and parentId of item itemClone.Id = newItemId; itemClone.Parent = newItemDetails.ParentId; UpdateBaseItemPropertiesWithOverrides(newItemDetails.OverrideProperties, itemClone); AddToItemsDb(newItemId, itemClone); AddToHandbookDb(newItemId, newItemDetails.HandbookParentId, newItemDetails.HandbookPriceRoubles); AddToLocaleDbs(newItemDetails.Locales, newItemId); AddToFleaPriceDb(newItemId, newItemDetails.FleaPriceRoubles); itemBaseClassService.HydrateItemBaseClassCache(); if (itemHelper.IsOfBaseclass(itemClone.Id, BaseClasses.WEAPON)) { AddToWeaponShelf(newItemId); } result.Success = true; result.ItemId = newItemId; return result; } /// /// Create a new item without using an existing item as a template
/// Add to the items db
/// Add to the flea market
/// Add to the handbook
/// Add to the locales
///
/// Details on what the item to be created /// CreateItemResult containing the completed items ID public CreateItemResult CreateItem(NewItemDetails newItemDetails) { var result = new CreateItemResult(); var tables = databaseService.GetTables(); var newItem = newItemDetails.NewItem; // Fail if itemId already exists if (tables.Templates.Items.ContainsKey(newItem.Id)) { result.Errors.Add($"ItemId already exists. {tables.Templates.Items[newItem.Id].Name}"); return result; } AddToItemsDb(newItem.Id, newItem); AddToHandbookDb(newItem.Id, newItemDetails.HandbookParentId, newItemDetails.HandbookPriceRoubles); AddToLocaleDbs(newItemDetails.Locales, newItem.Id); AddToFleaPriceDb(newItem.Id, newItemDetails.FleaPriceRoubles); itemBaseClassService.HydrateItemBaseClassCache(); if (itemHelper.IsOfBaseclass(newItem.Id, BaseClasses.WEAPON)) { AddToWeaponShelf(newItem.Id); } result.ItemId = newItemDetails.NewItem.Id; result.Success = true; return result; } /// /// If the ID provided is an empty string, return a randomly generated guid, otherwise return the newId parameter /// /// ID supplied to code /// ItemID protected string GetOrGenerateIdForItem(string newId) { return newId == "" ? hashUtil.Generate() : newId; } /// /// Iterates through supplied properties and updates the cloned items properties with them /// Complex objects cannot have overrides, they must be fully hydrated with values if they are to be used /// /// New properties to apply /// Item to update protected void UpdateBaseItemPropertiesWithOverrides(Props? overrideProperties, TemplateItem itemClone) { if (overrideProperties is null) { return; } foreach (var propKey in overrideProperties.GetAllPropsAsDict()) { itemClone.Properties.GetAllPropsAsDict()[propKey.Key] = overrideProperties.GetAllPropsAsDict()[propKey.Key]; } } /// /// Add a new item object to the in-memory representation of items.json /// /// ID of the item to add to items.json /// Item to add against the new id protected void AddToItemsDb(string newItemId, TemplateItem itemToAdd) { if (!databaseService.GetItems().TryAdd(newItemId, itemToAdd)) { logger.Warning($"Unable to add: {newItemId} To Database"); } } /// /// Add a handbook price for an item /// /// ID of the item being added /// Parent ID of the item being added /// Price of the item being added protected void AddToHandbookDb(string newItemId, string parentId, double? priceRoubles) { databaseService .GetTemplates() .Handbook.Items.Add( new HandbookItem { Id = newItemId, ParentId = parentId, Price = priceRoubles } ); // TODO: would we want to keep this the same or get them to send a HandbookItem } /// /// Iterate through the passed in locale data and add to each locale in turn
/// If data is not provided for each language EFT uses, the first object will be used in its place
/// e.g.
/// en[0]
/// fr[1]
///
/// No jp provided, so english will be used as a substitute ///
/// key is language, value are the new locale details /// ID of the item being created protected void AddToLocaleDbs(Dictionary localeDetails, string newItemId) { var languages = databaseService.GetLocales().Languages; foreach (var shortNameKey in languages) { // Get locale details passed in, if not provided by caller use first record in newItemDetails.locales localeDetails.TryGetValue(shortNameKey.Key, out var newLocaleDetails); newLocaleDetails ??= localeDetails[localeDetails.Keys.FirstOrDefault()]; localeService.AddCustomClientLocale(shortNameKey.Key, $"{newItemId} Name", newLocaleDetails.Name); localeService.AddCustomClientLocale(shortNameKey.Key, $"{newItemId} ShortName", newLocaleDetails.ShortName); localeService.AddCustomClientLocale(shortNameKey.Key, $"{newItemId} Description", newLocaleDetails.Description); } } /// /// Add a price to the in-memory representation of prices.json, used to inform the flea of an items price on the market /// /// ID of the new item /// Price of the new item protected void AddToFleaPriceDb(string newItemId, double? fleaPriceRoubles) { databaseService.GetTemplates().Prices[newItemId] = fleaPriceRoubles ?? 0; } /// /// Add a weapon to the hideout weapon shelf whitelist /// /// Weapon ID to add protected void AddToWeaponShelf(string newItemId) { // Ids for wall stashes in db List wallStashIds = [ ItemTpl.HIDEOUTAREACONTAINER_WEAPONSTAND_STASH_1, ItemTpl.HIDEOUTAREACONTAINER_WEAPONSTAND_STASH_2, ItemTpl.HIDEOUTAREACONTAINER_WEAPONSTAND_STASH_3 ]; foreach (var wallId in wallStashIds) { var wall = itemHelper.GetItem(wallId); if (wall.Key) { wall.Value.Properties.Grids[0].Props.Filters[0].Filter.Add(newItemId); } } } /// /// Add a custom weapon to PMCs loadout /// /// Custom weapon tpl to add to PMCs /// The weighting for the weapon to be picked vs other weapons /// The slot the weapon should be added to (e.g. FirstPrimaryWeapon/SecondPrimaryWeapon/Holster) public void AddCustomWeaponToPMCs(string weaponTpl, double weaponWeight, string weaponSlot) { var weapon = itemHelper.GetItem(weaponTpl); if (!weapon.Key) { logger.Warning($"Unable to add custom weapon {weaponTpl} to PMCs as it cannot be found in the Item db"); return; } var baseWeaponModObject = new Dictionary?>(); // Get all slots weapon has and create a dictionary of them with possible mods that slot into each var weaponSlots = weapon.Value.Properties.Slots; foreach (var slot in weaponSlots) { baseWeaponModObject[slot.Name] = [..slot.Props.Filters[0].Filter]; } // Get PMCs var botTypes = databaseService.GetBots().Types; // Add weapon base+mods into bear/usec data botTypes["usec"].BotInventory.Mods[weaponTpl] = baseWeaponModObject; botTypes["bear"].BotInventory.Mods[weaponTpl] = baseWeaponModObject; // Add weapon to array of allowed weapons + weighting to be picked botTypes["usec"].BotInventory.Equipment[Enum.Parse(weaponSlot)][weaponTpl] = weaponWeight; botTypes["bear"].BotInventory.Equipment[Enum.Parse(weaponSlot)][weaponTpl] = weaponWeight; } }