From 7cdac4e38bcb3e6c821d946a62bb249a625b7ec9 Mon Sep 17 00:00:00 2001 From: Dev Date: Tue, 28 May 2024 14:13:44 +0100 Subject: [PATCH] Added `getTrader()` and utilise inside various classes --- project/assets/database/locales/server/en.json | 1 + .../src/controllers/CustomizationController.ts | 2 +- project/src/controllers/InraidController.ts | 2 +- project/src/controllers/InsuranceController.ts | 2 +- project/src/controllers/InventoryController.ts | 2 +- project/src/controllers/TradeController.ts | 2 +- project/src/services/DatabaseService.ts | 17 +++++++++++++++++ 7 files changed, 23 insertions(+), 5 deletions(-) diff --git a/project/assets/database/locales/server/en.json b/project/assets/database/locales/server/en.json index 6cfe67c0..058c9f07 100644 --- a/project/assets/database/locales/server/en.json +++ b/project/assets/database/locales/server/en.json @@ -66,6 +66,7 @@ "customisation-unable_to_find_suit_with_id": "Unable to find suit with offer id: %s", "customisation-unable_to_get_trader_suits": "Unable to get suits from trader: %s", "database-data_at_path_missing": "The database was unable to retreive data from: [%s] Please ensure your configs are valid and the data at the location exists", + "database-no_trader_found_with_id": "Unable to find trader: %s in database", "dialog-chatbot_id_already_exists": "Chat bot: %s being registered already exists, unable to register bot", "dialog-missing_item_template": "Unable to find item tpl {{tpl}} in db, cannot send message of type {{type}}, skipping", "dialogue-unable_to_find_dialogs_in_profile": "No dialog object in profile: {{sessionId}}", diff --git a/project/src/controllers/CustomizationController.ts b/project/src/controllers/CustomizationController.ts index f8c3ff27..acee8c02 100644 --- a/project/src/controllers/CustomizationController.ts +++ b/project/src/controllers/CustomizationController.ts @@ -39,7 +39,7 @@ export class CustomizationController { const pmcData = this.profileHelper.getPmcProfile(sessionID); const clothing = this.databaseService.getCustomization(); - const suits = this.databaseService.getTraders()[traderID].suits; + const suits = this.databaseService.getTrader(traderID).suits; // Get an inner join of clothing from templates.customization and Ragman's suits array const matchingSuits = suits?.filter((suit) => suit.suiteId in clothing); diff --git a/project/src/controllers/InraidController.ts b/project/src/controllers/InraidController.ts index 5f5e3d01..b2bc8d90 100644 --- a/project/src/controllers/InraidController.ts +++ b/project/src/controllers/InraidController.ts @@ -675,7 +675,7 @@ export class InraidController const serverProfile = this.saveServer.getProfile(sessionId); const pmcData = serverProfile.characters.pmc; - const dialogueTemplates = this.databaseService.getTraders()[traderId].dialogue; + const dialogueTemplates = this.databaseService.getTrader(traderId).dialogue; if (!dialogueTemplates) { this.logger.error(this.localisationService.getText("inraid-unable_to_deliver_item_no_trader_found", traderId)); diff --git a/project/src/controllers/InsuranceController.ts b/project/src/controllers/InsuranceController.ts index 32d2a433..90866d2f 100644 --- a/project/src/controllers/InsuranceController.ts +++ b/project/src/controllers/InsuranceController.ts @@ -586,7 +586,7 @@ export class InsuranceController const labsId = "laboratory"; // After all of the item filtering that we've done, if there are no items remaining, the insurance has // successfully "failed" to return anything and an appropriate message should be sent to the player. - const traderDialogMessages = this.databaseServer.getTraders()[insurance.traderId].dialogue; + const traderDialogMessages = this.databaseServer.getTrader(insurance.traderId).dialogue; // Map is labs + insurance is disabled in base.json if ( diff --git a/project/src/controllers/InventoryController.ts b/project/src/controllers/InventoryController.ts index 57e019fe..fb9a415d 100644 --- a/project/src/controllers/InventoryController.ts +++ b/project/src/controllers/InventoryController.ts @@ -724,7 +724,7 @@ export class InventoryController { // Not fence // get tpl from trader assort - return this.databaseService.getTraders()[request.fromOwner.id].assort.items + return this.databaseService.getTrader(request.fromOwner.id).assort.items .find((item) => item._id === request.item)._tpl; } diff --git a/project/src/controllers/TradeController.ts b/project/src/controllers/TradeController.ts index 904d0426..4e13ae7f 100644 --- a/project/src/controllers/TradeController.ts +++ b/project/src/controllers/TradeController.ts @@ -288,7 +288,7 @@ export class TradeController sessionId, this.traderHelper.getTraderById(trader), MessageType.MESSAGE_WITH_ITEMS, - this.randomUtil.getArrayValue(this.databaseService.getTraders()[trader].dialogue.soldItems), + this.randomUtil.getArrayValue(this.databaseService.getTrader(trader).dialogue.soldItems), curencyReward.flatMap((x) => x), this.timeUtil.getHoursAsSeconds(72), ); diff --git a/project/src/services/DatabaseService.ts b/project/src/services/DatabaseService.ts index 7d4748d4..fb353890 100644 --- a/project/src/services/DatabaseService.ts +++ b/project/src/services/DatabaseService.ts @@ -263,4 +263,21 @@ export class DatabaseService return this.databaseServer.getTables().traders!; } + + /** + * Get specific trader by their Id + * @param traderId Desired trader id + * @returns assets/database/traders/ + */ + public getTrader(traderId: string): ITrader + { + const traders = this.getTraders(); + const desiredTrader = traders[traderId]; + if (!desiredTrader) + { + throw new error(this.localisationService.getText("database-no_trader_found_with_id", traderId)); + } + + return desiredTrader!; + } }