From 0e3d25df031071ebe53638971601f1b89d596a64 Mon Sep 17 00:00:00 2001 From: Dev Date: Wed, 25 Oct 2023 12:18:27 +0100 Subject: [PATCH] Improve getTrader() Error when no profile found Reduce returns to 1 fold two checks into one Remove magic string reliance on TradersInfo exist check More comments --- project/src/helpers/TraderHelper.ts | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/project/src/helpers/TraderHelper.ts b/project/src/helpers/TraderHelper.ts index 4abe301c..2557b866 100644 --- a/project/src/helpers/TraderHelper.ts +++ b/project/src/helpers/TraderHelper.ts @@ -48,23 +48,34 @@ export class TraderHelper this.traderConfig = this.configServer.getConfig(ConfigTypes.TRADER); } + /** + * Get a trader base object, update profile to reflect players current standing in profile + * when trader not found in profile + * @param traderID Traders Id to get + * @param sessionID Players id + * @returns Trader base + */ public getTrader(traderID: string, sessionID: string): ITraderBase { const pmcData = this.profileHelper.getPmcProfile(sessionID); - const trader = this.databaseServer.getTables().traders[traderID].base; - - if (!("TradersInfo" in pmcData)) + if (!pmcData) { - // pmc profile wiped - return trader; + this.logger.error(`No profile with sessionId: ${sessionID}`); } - - if (!(traderID in pmcData.TradersInfo)) + + // Profile has traderInfo dict (profile beyond creation stage) but no requested trader in profile + if (pmcData.TradersInfo && !(traderID in pmcData.TradersInfo)) { - // trader doesn't exist in profile + // Add trader values to profile this.resetTrader(sessionID, traderID); this.lvlUp(traderID, pmcData); } + + const trader = this.databaseServer.getTables().traders?.[traderID]?.base; + if (!trader) + { + this.logger.error(`No trader with Id: ${traderID} found`); + } return trader; }