Make use of DatabaseService inside ProfileFixerService and CustomItemService

This commit is contained in:
Dev 2024-05-28 12:27:46 +01:00
parent 4c08d64ca5
commit 9f1e0693f5
3 changed files with 73 additions and 50 deletions

View File

@ -1,7 +1,9 @@
import { error } from "console"; import { error } from "console";
import { inject, injectable } from "tsyringe"; import { inject, injectable } from "tsyringe";
import { IGlobals } from "@spt/models/eft/common/IGlobals"; import { IGlobals } from "@spt/models/eft/common/IGlobals";
import { IAchievement } from "@spt/models/eft/common/tables/IAchievement";
import { IMatch } from "@spt/models/eft/common/tables/IMatch"; import { IMatch } from "@spt/models/eft/common/tables/IMatch";
import { IQuest } from "@spt/models/eft/common/tables/IQuest";
import { ITemplateItem } from "@spt/models/eft/common/tables/ITemplateItem"; import { ITemplateItem } from "@spt/models/eft/common/tables/ITemplateItem";
import { ITrader } from "@spt/models/eft/common/tables/ITrader"; import { ITrader } from "@spt/models/eft/common/tables/ITrader";
import { IBots } from "@spt/models/spt/bots/IBots"; import { IBots } from "@spt/models/spt/bots/IBots";
@ -155,6 +157,19 @@ export class DatabaseService
return this.databaseServer.getTables().templates!; return this.databaseServer.getTables().templates!;
} }
/**
* @returns assets/database/templates/achievements.json
*/
public getAchievements(): IAchievement[]
{
if (!this.databaseServer.getTables().templates!.achievements)
{
throw new error(this.localisationService.getText("database-data_at_path_missing", "assets/database/templates/achievements.json"));
}
return this.databaseServer.getTables().templates!.achievements!;
}
/** /**
* @returns assets/database/templates/items.json * @returns assets/database/templates/items.json
*/ */
@ -168,6 +183,19 @@ export class DatabaseService
return this.databaseServer.getTables().templates!.items!; return this.databaseServer.getTables().templates!.items!;
} }
/**
* @returns assets/database/templates/items.json
*/
public getQuests(): Record<string, IQuest>
{
if (!this.databaseServer.getTables().templates!.quests)
{
throw new error(this.localisationService.getText("database-data_at_path_missing", "assets/database/templates/quests.json"));
}
return this.databaseServer.getTables().templates!.quests!;
}
/** /**
* @returns assets/database/traders/ * @returns assets/database/traders/
*/ */

View File

@ -19,7 +19,7 @@ import { ICoreConfig } from "@spt/models/spt/config/ICoreConfig";
import { IRagfairConfig } from "@spt/models/spt/config/IRagfairConfig"; import { IRagfairConfig } from "@spt/models/spt/config/IRagfairConfig";
import { ILogger } from "@spt/models/spt/utils/ILogger"; import { ILogger } from "@spt/models/spt/utils/ILogger";
import { ConfigServer } from "@spt/servers/ConfigServer"; import { ConfigServer } from "@spt/servers/ConfigServer";
import { DatabaseServer } from "@spt/servers/DatabaseServer"; import { DatabaseService } from "@spt/services/DatabaseService";
import { LocalisationService } from "@spt/services/LocalisationService"; import { LocalisationService } from "@spt/services/LocalisationService";
import { ICloner } from "@spt/utils/cloners/ICloner"; import { ICloner } from "@spt/utils/cloners/ICloner";
import { HashUtil } from "@spt/utils/HashUtil"; import { HashUtil } from "@spt/utils/HashUtil";
@ -36,6 +36,7 @@ export class ProfileFixerService
constructor( constructor(
@inject("WinstonLogger") protected logger: ILogger, @inject("WinstonLogger") protected logger: ILogger,
@inject("Watermark") protected watermark: Watermark, @inject("Watermark") protected watermark: Watermark,
@inject("DatabaseService") protected databaseService: DatabaseService,
@inject("HideoutHelper") protected hideoutHelper: HideoutHelper, @inject("HideoutHelper") protected hideoutHelper: HideoutHelper,
@inject("InventoryHelper") protected inventoryHelper: InventoryHelper, @inject("InventoryHelper") protected inventoryHelper: InventoryHelper,
@inject("TraderHelper") protected traderHelper: TraderHelper, @inject("TraderHelper") protected traderHelper: TraderHelper,
@ -45,7 +46,6 @@ export class ProfileFixerService
@inject("TimeUtil") protected timeUtil: TimeUtil, @inject("TimeUtil") protected timeUtil: TimeUtil,
@inject("JsonUtil") protected jsonUtil: JsonUtil, @inject("JsonUtil") protected jsonUtil: JsonUtil,
@inject("HashUtil") protected hashUtil: HashUtil, @inject("HashUtil") protected hashUtil: HashUtil,
@inject("DatabaseServer") protected databaseServer: DatabaseServer,
@inject("ConfigServer") protected configServer: ConfigServer, @inject("ConfigServer") protected configServer: ConfigServer,
@inject("RecursiveCloner") protected cloner: ICloner, @inject("RecursiveCloner") protected cloner: ICloner,
) )
@ -74,6 +74,8 @@ export class ProfileFixerService
if (pmcProfile.Hideout) if (pmcProfile.Hideout)
{ {
const globals = this.databaseService.getGlobals();
this.migrateImprovements(pmcProfile); this.migrateImprovements(pmcProfile);
this.addMissingBonusesProperty(pmcProfile); this.addMissingBonusesProperty(pmcProfile);
this.addMissingWallImprovements(pmcProfile); this.addMissingWallImprovements(pmcProfile);
@ -88,7 +90,7 @@ export class ProfileFixerService
if (pmcProfile.Hideout.Areas.find((x) => x.type === HideoutAreas.GENERATOR)!.slots.length if (pmcProfile.Hideout.Areas.find((x) => x.type === HideoutAreas.GENERATOR)!.slots.length
< 6 < 6
+ this.databaseServer.getTables().globals!.config.SkillsSettings.HideoutManagement.EliteSlots.Generator + globals.config.SkillsSettings.HideoutManagement.EliteSlots.Generator
.Slots .Slots
) )
{ {
@ -96,7 +98,7 @@ export class ProfileFixerService
this.addEmptyObjectsToHideoutAreaSlots( this.addEmptyObjectsToHideoutAreaSlots(
HideoutAreas.GENERATOR, HideoutAreas.GENERATOR,
6 6
+ this.databaseServer.getTables().globals!.config.SkillsSettings.HideoutManagement.EliteSlots + globals.config.SkillsSettings.HideoutManagement.EliteSlots
.Generator.Slots, .Generator.Slots,
pmcProfile, pmcProfile,
); );
@ -105,7 +107,7 @@ export class ProfileFixerService
if ( if (
pmcProfile.Hideout.Areas.find((x) => x.type === HideoutAreas.WATER_COLLECTOR)!.slots.length pmcProfile.Hideout.Areas.find((x) => x.type === HideoutAreas.WATER_COLLECTOR)!.slots.length
< 1 < 1
+ this.databaseServer.getTables().globals!.config.SkillsSettings.HideoutManagement.EliteSlots + globals.config.SkillsSettings.HideoutManagement.EliteSlots
.WaterCollector.Slots .WaterCollector.Slots
) )
{ {
@ -113,7 +115,7 @@ export class ProfileFixerService
this.addEmptyObjectsToHideoutAreaSlots( this.addEmptyObjectsToHideoutAreaSlots(
HideoutAreas.WATER_COLLECTOR, HideoutAreas.WATER_COLLECTOR,
1 1
+ this.databaseServer.getTables().globals!.config.SkillsSettings.HideoutManagement.EliteSlots + globals.config.SkillsSettings.HideoutManagement.EliteSlots
.WaterCollector.Slots, .WaterCollector.Slots,
pmcProfile, pmcProfile,
); );
@ -122,7 +124,7 @@ export class ProfileFixerService
if ( if (
pmcProfile.Hideout.Areas.find((x) => x.type === HideoutAreas.AIR_FILTERING)!.slots.length pmcProfile.Hideout.Areas.find((x) => x.type === HideoutAreas.AIR_FILTERING)!.slots.length
< 3 < 3
+ this.databaseServer.getTables().globals!.config.SkillsSettings.HideoutManagement.EliteSlots + globals.config.SkillsSettings.HideoutManagement.EliteSlots
.AirFilteringUnit.Slots .AirFilteringUnit.Slots
) )
{ {
@ -130,7 +132,7 @@ export class ProfileFixerService
this.addEmptyObjectsToHideoutAreaSlots( this.addEmptyObjectsToHideoutAreaSlots(
HideoutAreas.AIR_FILTERING, HideoutAreas.AIR_FILTERING,
3 3
+ this.databaseServer.getTables().globals!.config.SkillsSettings.HideoutManagement.EliteSlots + globals.config.SkillsSettings.HideoutManagement.EliteSlots
.AirFilteringUnit.Slots, .AirFilteringUnit.Slots,
pmcProfile, pmcProfile,
); );
@ -140,7 +142,7 @@ export class ProfileFixerService
if ( if (
pmcProfile.Hideout.Areas.find((x) => x.type === HideoutAreas.BITCOIN_FARM)!.slots.length pmcProfile.Hideout.Areas.find((x) => x.type === HideoutAreas.BITCOIN_FARM)!.slots.length
< 50 < 50
+ this.databaseServer.getTables().globals!.config.SkillsSettings.HideoutManagement.EliteSlots + globals.config.SkillsSettings.HideoutManagement.EliteSlots
.BitcoinFarm.Slots .BitcoinFarm.Slots
) )
{ {
@ -148,7 +150,7 @@ export class ProfileFixerService
this.addEmptyObjectsToHideoutAreaSlots( this.addEmptyObjectsToHideoutAreaSlots(
HideoutAreas.BITCOIN_FARM, HideoutAreas.BITCOIN_FARM,
50 50
+ this.databaseServer.getTables().globals!.config.SkillsSettings.HideoutManagement.EliteSlots + globals.config.SkillsSettings.HideoutManagement.EliteSlots
.BitcoinFarm.Slots, .BitcoinFarm.Slots,
pmcProfile, pmcProfile,
); );
@ -178,9 +180,9 @@ export class ProfileFixerService
return; return;
} }
const db = this.databaseServer.getTables(); const hideout = this.databaseService.getHideout();
const hideoutStandAreaDb = db.hideout!.areas.find((x) => x.type === HideoutAreas.WEAPON_STAND)!; const hideoutStandAreaDb = hideout.areas.find((area) => area.type === HideoutAreas.WEAPON_STAND)!;
const hideoutStandSecondaryAreaDb = db.hideout!.areas.find((x) => x.parentArea === hideoutStandAreaDb._id)!; const hideoutStandSecondaryAreaDb = hideout.areas.find((x) => x.parentArea === hideoutStandAreaDb._id)!;
const stageCurrentAt = hideoutStandAreaDb.stages[weaponStandArea.level]; const stageCurrentAt = hideoutStandAreaDb.stages[weaponStandArea.level];
const hideoutStandStashId = pmcProfile.Inventory.hideoutAreaStashes[HideoutAreas.WEAPON_STAND]; const hideoutStandStashId = pmcProfile.Inventory.hideoutAreaStashes[HideoutAreas.WEAPON_STAND];
const hideoutSecondaryStashId = pmcProfile.Inventory.hideoutAreaStashes[HideoutAreas.WEAPON_STAND_SECONDARY]; const hideoutSecondaryStashId = pmcProfile.Inventory.hideoutAreaStashes[HideoutAreas.WEAPON_STAND_SECONDARY];
@ -281,8 +283,8 @@ export class ProfileFixerService
return; return;
} }
const db = this.databaseServer.getTables(); const placeOfFameAreaDb = this.databaseService.getHideout().areas
const placeOfFameAreaDb = db.hideout!.areas.find((area) => area.type === HideoutAreas.PLACE_OF_FAME); .find((area) => area.type === HideoutAreas.PLACE_OF_FAME);
if (!placeOfFameAreaDb) if (!placeOfFameAreaDb)
{ {
return; return;
@ -464,7 +466,7 @@ export class ProfileFixerService
{ {
const taskConditionKeysToRemove: string[] = []; const taskConditionKeysToRemove: string[] = [];
const activeRepeatableQuests = this.getActiveRepeatableQuests(pmcProfile.RepeatableQuests); const activeRepeatableQuests = this.getActiveRepeatableQuests(pmcProfile.RepeatableQuests);
const achievements = this.databaseServer.getTables().templates!.achievements; const achievements = this.databaseService.getAchievements();
// Loop over TaskConditionCounters objects and add once we want to remove to counterKeysToRemove // Loop over TaskConditionCounters objects and add once we want to remove to counterKeysToRemove
for (const [key, taskConditionCounter] of Object.entries(pmcProfile.TaskConditionCounters)) for (const [key, taskConditionCounter] of Object.entries(pmcProfile.TaskConditionCounters))
@ -648,9 +650,8 @@ export class ProfileFixerService
protected addMissingWallImprovements(pmcProfile: IPmcData): void protected addMissingWallImprovements(pmcProfile: IPmcData): void
{ {
const profileWallArea = pmcProfile.Hideout.Areas.find((x) => x.type === HideoutAreas.EMERGENCY_WALL)!; const profileWallArea = pmcProfile.Hideout.Areas.find((x) => x.type === HideoutAreas.EMERGENCY_WALL)!;
const wallDb = this.databaseServer const wallDb = this.databaseService.getHideout().areas
.getTables() .find((x) => x.type === HideoutAreas.EMERGENCY_WALL);
.hideout!.areas.find((x) => x.type === HideoutAreas.EMERGENCY_WALL);
if (profileWallArea.level > 0) if (profileWallArea.level > 0)
{ {
@ -779,7 +780,7 @@ export class ProfileFixerService
{ {
const profileHideoutAreas = pmcProfile.Hideout.Areas; const profileHideoutAreas = pmcProfile.Hideout.Areas;
const profileBonuses = pmcProfile.Bonuses; const profileBonuses = pmcProfile.Bonuses;
const dbHideoutAreas = this.databaseServer.getTables().hideout!.areas; const dbHideoutAreas = this.databaseService.getHideout().areas;
for (const area of profileHideoutAreas) for (const area of profileHideoutAreas)
{ {
@ -870,7 +871,7 @@ export class ProfileFixerService
*/ */
public checkForOrphanedModdedItems(sessionId: string, fullProfile: ISptProfile): void public checkForOrphanedModdedItems(sessionId: string, fullProfile: ISptProfile): void
{ {
const itemsDb = this.databaseServer.getTables().templates!.items; const itemsDb = this.databaseService.getItems();
const pmcProfile = fullProfile.characters.pmc; const pmcProfile = fullProfile.characters.pmc;
// Get items placed in root of stash // Get items placed in root of stash
@ -972,7 +973,7 @@ export class ProfileFixerService
} }
} }
const clothing = this.databaseServer.getTables().templates!.customization; const clothing = this.databaseService.getTemplates().customization;
for (const [_, suitId] of Object.entries(fullProfile.suits)) for (const [_, suitId] of Object.entries(fullProfile.suits))
{ {
if (!clothing[suitId]) if (!clothing[suitId])
@ -1196,8 +1197,8 @@ export class ProfileFixerService
} }
// Iterate over clothing // Iterate over clothing
const customizationDb = this.databaseServer.getTables().templates!.customization; const customizationDb = this.databaseService.getTemplates().customization;
const customizationDbArray = Object.values(this.databaseServer.getTables().templates!.customization); const customizationDbArray = Object.values(customizationDb);
const playerIsUsec = pmcProfile.Info.Side.toLowerCase() === "usec"; const playerIsUsec = pmcProfile.Info.Side.toLowerCase() === "usec";
// Check Head // Check Head
@ -1265,7 +1266,7 @@ export class ProfileFixerService
return; return;
} }
const profileTemplates = this.databaseServer.getTables().templates?.profiles[fullProfile.info.edition]; const profileTemplates = this.databaseService.getTemplates().profiles[fullProfile.info.edition];
if (!profileTemplates) if (!profileTemplates)
{ {
return; return;
@ -1363,7 +1364,7 @@ export class ProfileFixerService
} }
// Bonus lacks id, find matching hideout area / stage / bonus // Bonus lacks id, find matching hideout area / stage / bonus
for (const area of this.databaseServer.getTables().hideout!.areas) for (const area of this.databaseService.getHideout().areas)
{ {
// TODO: skip if no stages // TODO: skip if no stages
for (const stageIndex in area.stages) for (const stageIndex in area.stages)
@ -1428,7 +1429,7 @@ export class ProfileFixerService
*/ */
protected removeOrphanedQuests(pmcProfile: IPmcData): void protected removeOrphanedQuests(pmcProfile: IPmcData): void
{ {
const quests = this.databaseServer.getTables().templates!.quests; const quests = this.databaseService.getQuests();
const profileQuests = pmcProfile.Quests; const profileQuests = pmcProfile.Quests;
const repeatableQuests: IRepeatableQuest[] = []; const repeatableQuests: IRepeatableQuest[] = [];

View File

@ -8,9 +8,8 @@ import {
NewItemDetails, NewItemDetails,
NewItemFromCloneDetails, NewItemFromCloneDetails,
} from "@spt/models/spt/mod/NewItemDetails"; } from "@spt/models/spt/mod/NewItemDetails";
import { IDatabaseTables } from "@spt/models/spt/server/IDatabaseTables";
import { ILogger } from "@spt/models/spt/utils/ILogger"; import { ILogger } from "@spt/models/spt/utils/ILogger";
import { DatabaseServer } from "@spt/servers/DatabaseServer"; import { DatabaseService } from "@spt/services/DatabaseService";
import { ItemBaseClassService } from "@spt/services/ItemBaseClassService"; import { ItemBaseClassService } from "@spt/services/ItemBaseClassService";
import { ICloner } from "@spt/utils/cloners/ICloner"; import { ICloner } from "@spt/utils/cloners/ICloner";
import { HashUtil } from "@spt/utils/HashUtil"; import { HashUtil } from "@spt/utils/HashUtil";
@ -18,18 +17,15 @@ import { HashUtil } from "@spt/utils/HashUtil";
@injectable() @injectable()
export class CustomItemService export class CustomItemService
{ {
protected tables: IDatabaseTables;
constructor( constructor(
@inject("WinstonLogger") protected logger: ILogger, @inject("WinstonLogger") protected logger: ILogger,
@inject("HashUtil") protected hashUtil: HashUtil, @inject("HashUtil") protected hashUtil: HashUtil,
@inject("DatabaseServer") protected databaseServer: DatabaseServer, @inject("DatabaseService") protected databaseServer: DatabaseService,
@inject("ItemHelper") protected itemHelper: ItemHelper, @inject("ItemHelper") protected itemHelper: ItemHelper,
@inject("ItemBaseClassService") protected itemBaseClassService: ItemBaseClassService, @inject("ItemBaseClassService") protected itemBaseClassService: ItemBaseClassService,
@inject("RecursiveCloner") protected cloner: ICloner, @inject("RecursiveCloner") protected cloner: ICloner,
) )
{ {
this.tables = this.databaseServer.getTables();
} }
/** /**
@ -165,7 +161,7 @@ export class CustomItemService
*/ */
protected addToItemsDb(newItemId: string, itemToAdd: ITemplateItem): void protected addToItemsDb(newItemId: string, itemToAdd: ITemplateItem): void
{ {
this.tables.templates.items[newItemId] = itemToAdd; this.databaseServer.getItems()[newItemId] = itemToAdd;
} }
/** /**
@ -176,7 +172,7 @@ export class CustomItemService
*/ */
protected addToHandbookDb(newItemId: string, parentId: string, priceRoubles: number): void protected addToHandbookDb(newItemId: string, parentId: string, priceRoubles: number): void
{ {
this.tables.templates.handbook.Items.push({ Id: newItemId, ParentId: parentId, Price: priceRoubles }); this.databaseServer.getTemplates().handbook.Items.push({ Id: newItemId, ParentId: parentId, Price: priceRoubles });
} }
/** /**
@ -192,7 +188,7 @@ export class CustomItemService
*/ */
protected addToLocaleDbs(localeDetails: Record<string, LocaleDetails>, newItemId: string): void protected addToLocaleDbs(localeDetails: Record<string, LocaleDetails>, newItemId: string): void
{ {
const languages = this.tables.locales.languages; const languages = this.databaseServer.getLocales().languages;
for (const shortNameKey in languages) for (const shortNameKey in languages)
{ {
// Get locale details passed in, if not provided by caller use first record in newItemDetails.locales // Get locale details passed in, if not provided by caller use first record in newItemDetails.locales
@ -203,9 +199,10 @@ export class CustomItemService
} }
// Create new record in locale file // Create new record in locale file
this.tables.locales.global[shortNameKey][`${newItemId} Name`] = newLocaleDetails.name; const globals = this.databaseServer.getLocales();
this.tables.locales.global[shortNameKey][`${newItemId} ShortName`] = newLocaleDetails.shortName; globals.global[shortNameKey][`${newItemId} Name`] = newLocaleDetails.name;
this.tables.locales.global[shortNameKey][`${newItemId} Description`] = newLocaleDetails.description; globals.global[shortNameKey][`${newItemId} ShortName`] = newLocaleDetails.shortName;
globals.global[shortNameKey][`${newItemId} Description`] = newLocaleDetails.description;
} }
} }
@ -216,7 +213,7 @@ export class CustomItemService
*/ */
protected addToFleaPriceDb(newItemId: string, fleaPriceRoubles: number): void protected addToFleaPriceDb(newItemId: string, fleaPriceRoubles: number): void
{ {
this.tables.templates.prices[newItemId] = fleaPriceRoubles; this.databaseServer.getTemplates().prices[newItemId] = fleaPriceRoubles;
} }
/** /**
@ -225,8 +222,6 @@ export class CustomItemService
*/ */
protected addToWeaponShelf(newItemId: string): void protected addToWeaponShelf(newItemId: string): void
{ {
this.databaseServer.getTables().templates.items;
// Ids for wall stashes in db // Ids for wall stashes in db
const wallStashIds = ["6401c7b213d9b818bf0e7dd7", "64381b582bb1c5dedd0fc925", "64381b6e44b37a080d0245b9"]; const wallStashIds = ["6401c7b213d9b818bf0e7dd7", "64381b582bb1c5dedd0fc925", "64381b6e44b37a080d0245b9"];
for (const wallId of wallStashIds) for (const wallId of wallStashIds)
@ -259,22 +254,21 @@ export class CustomItemService
const baseWeaponModObject = {}; const baseWeaponModObject = {};
// Get all slots weapon has and create a dictionary of them with possible mods that slot into each // Get all slots weapon has and create a dictionary of them with possible mods that slot into each
const weaponSltos = weapon[1]._props.Slots; const weaponSlots = weapon[1]._props.Slots;
for (const slot of weaponSltos) for (const slot of weaponSlots)
{ {
baseWeaponModObject[slot._name] = slot._props.filters[0].Filter; baseWeaponModObject[slot._name] = slot._props.filters[0].Filter;
} }
// Get PMCs // Get PMCs
const usec = this.databaseServer.getTables().bots!.types.usec; const botTypes = this.databaseServer.getBots().types;
const bear = this.databaseServer.getTables().bots!.types.bear;
// Add weapon base+mods into bear/usec data // Add weapon base+mods into bear/usec data
usec.inventory.mods[weaponTpl] = baseWeaponModObject; botTypes.usec.inventory.mods[weaponTpl] = baseWeaponModObject;
bear.inventory.mods[weaponTpl] = baseWeaponModObject; botTypes.bear.inventory.mods[weaponTpl] = baseWeaponModObject;
// Add weapon to array of allowed weapons + weighting to be picked // Add weapon to array of allowed weapons + weighting to be picked
usec.inventory.equipment[weaponSlot][weaponTpl] = weaponWeight; botTypes.usec.inventory.equipment[weaponSlot][weaponTpl] = weaponWeight;
bear.inventory.equipment[weaponSlot][weaponTpl] = weaponWeight; botTypes.bear.inventory.equipment[weaponSlot][weaponTpl] = weaponWeight;
} }
} }