Fixed various Object is possibly undefined warnings

This commit is contained in:
Dev 2024-05-28 10:25:23 +01:00
parent e0eaadbeef
commit 65f27a7d8c
17 changed files with 61 additions and 40 deletions

View File

@ -665,6 +665,7 @@
"trader-missing_trader_details_using_default_refresh_time": "Trader: {{traderId}} not found, generating temp entry with default refresh time of: {{updateTime}}",
"trader-price_multipler_is_zero_use_default": "traderPriceMultipler was 0, this is invalid, setting to 0.01",
"trader-unable_to_delete_stale_purchases": "Unable to process trader purchases in profile: {{profileId}} as trader: {{traderId}} cannot be found, skipping",
"trader-unable_to_find_profile_by_id": "Unable to find a profile with the id: %s",
"trader-unable_to_find_profile_with_id": "Unable to find profile with sessionId: %s",
"trader-unable_to_find_trader_by_id": "Unable to find trader with Id: %s",
"trader-unable_to_find_trader_in_enum": "Unable to find trader: %s in Traders enum",

View File

@ -147,8 +147,8 @@ export class GiveSptCommand implements ISptCommand
}
const localizedGlobal
= this.databaseServer.getTables().locales.global[locale]
?? this.databaseServer.getTables().locales.global.en;
= this.databaseServer.getTables().locales!.global[locale]
?? this.databaseServer.getTables().locales!.global.en;
const closestItemsMatchedByName = this.itemHelper
.getItems()
@ -191,8 +191,8 @@ export class GiveSptCommand implements ISptCommand
}
}
const localizedGlobal = this.databaseServer.getTables().locales.global[locale]
?? this.databaseServer.getTables().locales.global.en;
const localizedGlobal = this.databaseServer.getTables().locales!.global[locale]
?? this.databaseServer.getTables().locales!.global.en;
// If item is an item name, we need to search using that item name and the locale which one we want otherwise
// item is just the tplId.
const tplId = isItemName

View File

@ -63,7 +63,7 @@ export class TraderSptCommand implements ISptCommand
const command: string = result.groups.command;
const quantity: number = +result.groups.quantity;
const dbTrader = Object.values(this.databaseServer.getTables().traders).find(
const dbTrader = Object.values(this.databaseServer.getTables().traders!).find(
(t) => t.base.nickname.toLocaleLowerCase() === trader.toLocaleLowerCase(),
);
if (dbTrader === undefined)

View File

@ -453,7 +453,7 @@ export class InRaidHelper
*/
public removeSpawnedInSessionPropertyFromItems(postRaidProfile: IPostRaidPmcData): IPostRaidPmcData
{
const dbItems = this.databaseServer.getTables().templates.items;
const dbItems = this.databaseServer.getTables().templates!.items;
const itemsToRemovePropertyFrom = postRaidProfile.Inventory.items.filter((x) =>
{
// Has upd object + upd.SpawnedInSession property + not a quest item

View File

@ -398,7 +398,7 @@ export class ItemHelper
*/
public getDynamicItemPrice(tpl: string): number
{
const dynamicPrice = this.databaseServer.getTables().templates.prices[tpl];
const dynamicPrice = this.databaseServer.getTables().templates!.prices[tpl];
if (dynamicPrice)
{
return dynamicPrice;
@ -432,7 +432,7 @@ export class ItemHelper
*/
public getItems(): ITemplateItem[]
{
return this.cloner.clone(Object.values(this.databaseServer.getTables().templates.items));
return this.cloner.clone(Object.values(this.databaseServer.getTables().templates!.items));
}
/**
@ -443,9 +443,9 @@ export class ItemHelper
public getItem(tpl: string): [boolean, ITemplateItem]
{
// -> Gets item from <input: _tpl>
if (tpl in this.databaseServer.getTables().templates.items)
if (tpl in this.databaseServer.getTables().templates!.items)
{
return [true, this.databaseServer.getTables().templates.items[tpl]];
return [true, this.databaseServer.getTables().templates!.items[tpl]];
}
return [false, undefined];
@ -709,7 +709,7 @@ export class ItemHelper
*/
public isItemTplStackable(tpl: string): boolean
{
const item = this.databaseServer.getTables().templates.items[tpl];
const item = this.databaseServer.getTables().templates!.items[tpl];
if (!item)
{
return undefined;
@ -1565,7 +1565,7 @@ export class ItemHelper
public getItemTplsOfBaseType(desiredBaseType: string): string[]
{
return Object.values(this.databaseServer.getTables().templates.items)
return Object.values(this.databaseServer.getTables().templates!.items)
.filter((x) => x._parent === desiredBaseType)
.map((x) => x._id);
}

View File

@ -44,7 +44,7 @@ export class PresetHelper
{
if (!this.defaultWeaponPresets)
{
this.defaultWeaponPresets = Object.values(this.databaseServer.getTables().globals.ItemPresets)
this.defaultWeaponPresets = Object.values(this.databaseServer.getTables().globals!.ItemPresets)
.filter(
(preset) =>
preset._encyclopedia !== undefined
@ -68,7 +68,7 @@ export class PresetHelper
{
if (!this.defaultEquipmentPresets)
{
this.defaultEquipmentPresets = Object.values(this.databaseServer.getTables().globals.ItemPresets)
this.defaultEquipmentPresets = Object.values(this.databaseServer.getTables().globals!.ItemPresets)
.filter(
(preset) =>
preset._encyclopedia !== undefined
@ -86,7 +86,7 @@ export class PresetHelper
public isPreset(id: string): boolean
{
return id in this.databaseServer.getTables().globals.ItemPresets;
return id in this.databaseServer.getTables().globals!.ItemPresets;
}
/**
@ -107,12 +107,12 @@ export class PresetHelper
public getPreset(id: string): IPreset
{
return this.cloner.clone(this.databaseServer.getTables().globals.ItemPresets[id]);
return this.cloner.clone(this.databaseServer.getTables().globals!.ItemPresets[id]);
}
public getAllPresets(): IPreset[]
{
return this.cloner.clone(Object.values(this.databaseServer.getTables().globals.ItemPresets));
return this.cloner.clone(Object.values(this.databaseServer.getTables().globals!.ItemPresets));
}
public getPresets(templateId: string): IPreset[]

View File

@ -482,7 +482,7 @@ export class ProfileHelper
if (useSkillProgressRateMultipler)
{
const globals = this.databaseServer.getTables().globals;
const globals = this.databaseServer.getTables().globals!;
const skillProgressRate = globals.config.SkillsSettings.SkillProgressRate;
pointsToAddToSkill *= skillProgressRate;
}

View File

@ -770,7 +770,7 @@ export class QuestHelper
*/
public getQuestsFromDb(): IQuest[]
{
return Object.values(this.databaseServer.getTables().templates.quests);
return Object.values(this.databaseServer.getTables().templates!.quests);
}
/**
@ -781,7 +781,7 @@ export class QuestHelper
*/
public getQuestFromDb(questId: string, pmcData: IPmcData): IQuest
{
let quest = this.databaseServer.getTables().templates.quests[questId];
let quest = this.databaseServer.getTables().templates!.quests[questId];
// May be a repeatable quest
if (!quest)
@ -1001,7 +1001,7 @@ export class QuestHelper
): void
{
// Get hideout crafts and find those that match by areatype/required level/end product tpl - hope for just one match
const hideoutProductions = this.databaseServer.getTables().hideout.production;
const hideoutProductions = this.databaseServer.getTables().hideout!.production;
const matchingProductions = hideoutProductions.filter(
(x) =>
x.areaType === Number.parseInt(craftUnlockReward.traderId)
@ -1099,7 +1099,7 @@ export class QuestHelper
public addAllQuestsToProfile(pmcProfile: IPmcData, statuses: QuestStatus[]): void
{
// Iterate over all quests in db
const quests = this.databaseServer.getTables().templates.quests;
const quests = this.databaseServer.getTables().templates!.quests;
for (const questIdKey in quests)
{
// Quest from db matches quests in profile, skip

View File

@ -355,7 +355,7 @@ export class RagfairOfferHelper
*/
public increaseProfileRagfairRating(profile: ISptProfile, amountToIncrementBy: number): void
{
const ragfairConfig = this.databaseServer.getTables().globals.config.RagFair;
const ragfairConfig = this.databaseServer.getTables().globals!.config.RagFair;
profile.characters.pmc.RagfairInfo.isRatingGrowing = true;
if (Number.isNaN(amountToIncrementBy))
@ -572,7 +572,7 @@ export class RagfairOfferHelper
const isTraderOffer = offer.user.memberType === MemberCategory.TRADER;
if (
pmcData.Info.Level < this.databaseServer.getTables().globals.config.RagFair.minUserLevel
pmcData.Info.Level < this.databaseServer.getTables().globals!.config.RagFair.minUserLevel
&& isDefaultUserOffer
)
{
@ -762,7 +762,7 @@ export class RagfairOfferHelper
// handle trader items to remove items that are not available to the user right now
// required search for "lamp" shows 4 items, 3 of which are not available to a new player
// filter those out
if (offer.user.id in this.databaseServer.getTables().traders)
if (offer.user.id in this.databaseServer.getTables().traders!)
{
if (!(offer.user.id in traderAssorts))
{

View File

@ -75,7 +75,7 @@ export class RagfairSellHelper
const endTime
= startTime
+ this.timeUtil.getHoursAsSeconds(
this.databaseServer.getTables().globals.config.RagFair.offerDurationTimeInHour,
this.databaseServer.getTables().globals!.config.RagFair.offerDurationTimeInHour,
);
let sellTime = startTime;

View File

@ -144,7 +144,7 @@ export class RagfairServerHelper
*/
public isTrader(traderId: string): boolean
{
return traderId in this.databaseServer.getTables().traders;
return traderId in this.databaseServer.getTables().traders!;
}
/**
@ -161,7 +161,7 @@ export class RagfairServerHelper
RagfairServerHelper.goodsReturnedTemplate,
returnedItems,
this.timeUtil.getHoursAsSeconds(
this.databaseServer.getTables().globals.config.RagFair.yourOfferDidNotSellMaxStorageTimeInHour,
this.databaseServer.getTables().globals!.config.RagFair.yourOfferDidNotSellMaxStorageTimeInHour,
),
);
}

View File

@ -120,7 +120,7 @@ export class RepairHelper
): number
{
// Degradation value is based on the armor material
const armorMaterialSettings = this.databaseServer.getTables().globals.config.ArmorMaterials[armorMaterial];
const armorMaterialSettings = this.databaseServer.getTables().globals!.config.ArmorMaterials[armorMaterial];
const minMultiplier = isRepairKit
? armorMaterialSettings.MinRepairKitDegradation

View File

@ -68,7 +68,7 @@ export class TraderAssortHelper
return this.getRagfairDataAsTraderAssort();
}
const traderClone = this.cloner.clone(this.databaseServer.getTables().traders[traderId]);
const traderClone = this.cloner.clone(this.databaseServer.getTables().traders![traderId]);
const fullProfile = this.profileHelper.getFullProfile(sessionId);
const pmcProfile = fullProfile.characters.pmc;
@ -245,7 +245,7 @@ export class TraderAssortHelper
public traderAssortsHaveExpired(traderID: string): boolean
{
const time = this.timeUtil.getTimestamp();
const trader = this.databaseServer.getTables().traders[traderID];
const trader = this.databaseServer.getTables().traders![traderID];
return trader.base.nextResupply <= time;
}

View File

@ -1,3 +1,4 @@
import { error } from "node:console";
import { inject, injectable } from "tsyringe";
import { HandbookHelper } from "@spt/helpers/HandbookHelper";
import { ItemHelper } from "@spt/helpers/ItemHelper";
@ -59,7 +60,7 @@ export class TraderHelper
const pmcData = this.profileHelper.getPmcProfile(sessionID);
if (!pmcData)
{
this.logger.error(this.localisationService.getText("trader-unable_to_find_profile_with_id", sessionID));
throw new error(this.localisationService.getText("trader-unable_to_find_profile_with_id", sessionID));
}
// Profile has traderInfo dict (profile beyond creation stage) but no requested trader in profile
@ -129,6 +130,11 @@ export class TraderHelper
{
const db = this.databaseServer.getTables();
const fullProfile = this.profileHelper.getFullProfile(sessionID);
if (!fullProfile)
{
throw new error(this.localisationService.getText("trader-unable_to_find_profile_by_id", sessionID));
}
const pmcData = fullProfile.characters.pmc;
const rawProfileTemplate: ProfileTraderTemplate
= db.templates!.profiles[fullProfile.info.edition][pmcData.Info.Side.toLowerCase()]

View File

@ -96,8 +96,8 @@ export class BotEquipmentModPoolService
pool[item._id][slot._name].push(itemToAdd);
// Check item added into array for slots, need to iterate over those
const subItemDetails = this.databaseServer.getTables().templates.items[itemToAdd];
const hasSubItemsToAdd = subItemDetails?._props?.Slots?.length > 0;
const subItemDetails = this.databaseServer.getTables().templates!.items[itemToAdd];
const hasSubItemsToAdd = subItemDetails?._props?.Slots?.length ?? 0 > 0;
if (hasSubItemsToAdd && !pool[subItemDetails._id])
{
// Recursive call
@ -185,7 +185,7 @@ export class BotEquipmentModPoolService
*/
protected generateWeaponPool(): void
{
const weapons = Object.values(this.databaseServer.getTables().templates.items).filter(
const weapons = Object.values(this.databaseServer.getTables().templates!.items).filter(
(x) => x._type === "Item" && this.itemHelper.isOfBaseclass(x._id, BaseClasses.WEAPON),
);
this.generatePool(weapons, "weapon");
@ -199,7 +199,7 @@ export class BotEquipmentModPoolService
*/
protected generateGearPool(): void
{
const gear = Object.values(this.databaseServer.getTables().templates.items).filter(
const gear = Object.values(this.databaseServer.getTables().templates!.items).filter(
(x) =>
x._type === "Item"
&& this.itemHelper.isOfBaseclasses(x._id, [

View File

@ -70,7 +70,14 @@ export class CustomLocationWaveService
for (const mapKey in bossWavesToApply)
{
const location: ILocationBase = this.databaseServer.getTables().locations[mapKey].base;
const location: ILocationBase = this.databaseServer.getTables().locations![mapKey]?.base;
if (!location)
{
this.logger.warning(`Unable to add custom boss wave to location: ${mapKey}, location not found`);
continue;
}
for (const bossWave of bossWavesToApply[mapKey])
{
if (location.BossLocationSpawn.find((x) => x.sptId === bossWave.sptId))
@ -87,7 +94,14 @@ export class CustomLocationWaveService
for (const mapKey in normalWavesToApply)
{
const location: ILocationBase = this.databaseServer.getTables().locations[mapKey].base;
const location: ILocationBase = this.databaseServer.getTables().locations![mapKey]?.base;
if (!location)
{
this.logger.warning(`Unable to add custom wave to location: ${mapKey}, location not found`);
continue;
}
for (const normalWave of normalWavesToApply[mapKey])
{
if (location.waves.find((x) => x.sptId === normalWave.sptId))

View File

@ -266,8 +266,8 @@ export class CustomItemService
}
// Get PMCs
const usec = this.databaseServer.getTables().bots.types.usec;
const bear = this.databaseServer.getTables().bots.types.bear;
const usec = this.databaseServer.getTables().bots!.types.usec;
const bear = this.databaseServer.getTables().bots!.types.bear;
// Add weapon base+mods into bear/usec data
usec.inventory.mods[weaponTpl] = baseWeaponModObject;