From f0c3c6032f1abdd8a010897869ad947f65943529 Mon Sep 17 00:00:00 2001 From: Archangel Date: Tue, 26 Nov 2024 15:32:48 +0100 Subject: [PATCH] Run startup of mod asynchronously --- src/LotsofLoot.ts | 4 +- src/controllers/LotsofLootController.ts | 6 +- .../LotsofLootMarkedRoomController.ts | 68 ++++++------ src/helpers/LotsofLootHelper.ts | 104 +++++++++--------- 4 files changed, 95 insertions(+), 87 deletions(-) diff --git a/src/LotsofLoot.ts b/src/LotsofLoot.ts index 972f981..f13d599 100644 --- a/src/LotsofLoot.ts +++ b/src/LotsofLoot.ts @@ -34,8 +34,8 @@ export class LotsofLoot { } public async postDBLoadAsync(_container: DependencyContainer): Promise { - this.lotsofLootController.applyLotsOfLootModifications(); - this.lotsofLootMarkedRoomController.adjustMarkedRoomItems(); + await this.lotsofLootController.applyLotsOfLootModifications(); + await this.lotsofLootMarkedRoomController.adjustMarkedRoomItems(); this.logger.logInfo(`Finished loading`); } diff --git a/src/controllers/LotsofLootController.ts b/src/controllers/LotsofLootController.ts index 7365e0f..c5e07dd 100644 --- a/src/controllers/LotsofLootController.ts +++ b/src/controllers/LotsofLootController.ts @@ -30,7 +30,7 @@ export class LotsofLootController { ) { } - public applyLotsOfLootModifications(): void { + public async applyLotsOfLootModifications(): Promise { const lotsofLootConfig = this.config.getConfig(); const tables = this.databaseService.getTables(); const locations = tables.locations; @@ -75,11 +75,11 @@ export class LotsofLootController { } for (const itemId in lotsofLootConfig.changeRelativeProbabilityInPool) { - this.lotsofLootHelper.changeRelativeProbabilityInPool(itemId, lotsofLootConfig.changeRelativeProbabilityInPool[itemId]); + await this.lotsofLootHelper.changeRelativeProbabilityInPoolAsync(itemId, lotsofLootConfig.changeRelativeProbabilityInPool[itemId]); } for (const itemId in lotsofLootConfig.changeProbabilityOfPool) { - this.lotsofLootHelper.changeProbabilityOfPool(itemId, lotsofLootConfig.changeProbabilityOfPool[itemId]); + await this.lotsofLootHelper.changeProbabilityOfPoolAsync(itemId, lotsofLootConfig.changeProbabilityOfPool[itemId]); } if (lotsofLootConfig.general.disableFleaRestrictions) { diff --git a/src/controllers/LotsofLootMarkedRoomController.ts b/src/controllers/LotsofLootMarkedRoomController.ts index 12dc1f4..beacc0d 100644 --- a/src/controllers/LotsofLootMarkedRoomController.ts +++ b/src/controllers/LotsofLootMarkedRoomController.ts @@ -22,7 +22,7 @@ export class LotsofLootMarkedRoomController { ) { } - public adjustMarkedRoomItems(): void { + public async adjustMarkedRoomItems(): Promise { const spawnPointsCustoms = this.databaseService.getTables().locations.bigmap.looseLoot.spawnpoints; const spawnPointsReserve = this.databaseService.getTables().locations.rezervbase.looseLoot.spawnpoints; const spawnPointsStreets = this.databaseService.getTables().locations.tarkovstreets.looseLoot.spawnpoints; @@ -33,8 +33,8 @@ export class LotsofLootMarkedRoomController { if (spawnpoint.template.Position.x > 180 && spawnpoint.template.Position.x < 185 && spawnpoint.template.Position.z > 180 && spawnpoint.template.Position.z < 185 && spawnpoint.template.Position.y > 6 && spawnpoint.template.Position.y < 7) { this.logger.debug(`Marked room (Customs) ${spawnpoint.template.Id}`); spawnpoint.probability *= this.config.getConfig().markedRoom.multiplier.customs; - this.markedAddExtraItems(spawnpoint); - this.markedItemGroups(spawnpoint); + await this.markedAddExtraItemsAsync(spawnpoint); + await this.adjustMarkedItemGroupsAsync(spawnpoint); } } @@ -42,18 +42,18 @@ export class LotsofLootMarkedRoomController { if (spawnpoint.template.Position.x > -125 && spawnpoint.template.Position.x < -120 && spawnpoint.template.Position.z > 25 && spawnpoint.template.Position.z < 30 && spawnpoint.template.Position.y > -15 && spawnpoint.template.Position.y < -14) { this.logger.debug(`Marked room (Reserve) ${spawnpoint.template.Id}`); spawnpoint.probability *= this.config.getConfig().markedRoom.multiplier.reserve; - this.markedAddExtraItems(spawnpoint); - this.markedItemGroups(spawnpoint); + await this.markedAddExtraItemsAsync(spawnpoint); + await this.adjustMarkedItemGroupsAsync(spawnpoint); } else if (spawnpoint.template.Position.x > -155 && spawnpoint.template.Position.x < -150 && spawnpoint.template.Position.z > 70 && spawnpoint.template.Position.z < 75 && spawnpoint.template.Position.y > -9 && spawnpoint.template.Position.y < -8) { this.logger.debug(`Marked room (Reserve) ${spawnpoint.template.Id}`); spawnpoint.probability *= this.config.getConfig().markedRoom.multiplier.reserve; - this.markedAddExtraItems(spawnpoint); - this.markedItemGroups(spawnpoint); + await this.markedAddExtraItemsAsync(spawnpoint); + await this.adjustMarkedItemGroupsAsync(spawnpoint); } else if (spawnpoint.template.Position.x > 190 && spawnpoint.template.Position.x < 195 && spawnpoint.template.Position.z > -230 && spawnpoint.template.Position.z < -225 && spawnpoint.template.Position.y > -6 && spawnpoint.template.Position.y < -5) { this.logger.debug(`Marked room (Reserve) ${spawnpoint.template.Id}`); spawnpoint.probability *= this.config.getConfig().markedRoom.multiplier.reserve; - this.markedAddExtraItems(spawnpoint); - this.markedItemGroups(spawnpoint); + await this.markedAddExtraItemsAsync(spawnpoint); + await this.adjustMarkedItemGroupsAsync(spawnpoint); } } @@ -62,15 +62,15 @@ export class LotsofLootMarkedRoomController { if (spawnpoint.template.Position.x > -133 && spawnpoint.template.Position.x < -129 && spawnpoint.template.Position.z > 265 && spawnpoint.template.Position.z < 275 && spawnpoint.template.Position.y > 8.5 && spawnpoint.template.Position.y < 11) { this.logger.debug(`Marked room (Streets) ${spawnpoint.template.Id}`); spawnpoint.probability *= this.config.getConfig().markedRoom.multiplier.streets; - this.markedAddExtraItems(spawnpoint); - this.markedItemGroups(spawnpoint); + await this.markedAddExtraItemsAsync(spawnpoint); + await this.adjustMarkedItemGroupsAsync(spawnpoint); } //Chek 13 Marked Room else if (spawnpoint.template.Position.x > 186 && spawnpoint.template.Position.x < 191 && spawnpoint.template.Position.z > 224 && spawnpoint.template.Position.z < 229 && spawnpoint.template.Position.y > -0.5 && spawnpoint.template.Position.y < 1.5) { this.logger.debug(`Marked room (Streets) ${spawnpoint.template.Id}`); spawnpoint.probability *= this.config.getConfig().markedRoom.multiplier.streets; - this.markedAddExtraItems(spawnpoint); - this.markedItemGroups(spawnpoint); + await this.markedAddExtraItemsAsync(spawnpoint); + await this.adjustMarkedItemGroupsAsync(spawnpoint); } } @@ -79,46 +79,52 @@ export class LotsofLootMarkedRoomController { if (spawnpoint.template.Position.x > 319 && spawnpoint.template.Position.x < 330 && spawnpoint.template.Position.z > 482 && spawnpoint.template.Position.z < 489 && spawnpoint.template.Position.y > 5 && spawnpoint.template.Position.y < 6.5) { this.logger.debug(`Marked room (Lighthouse) ${spawnpoint.template.Id}`); spawnpoint.probability *= this.config.getConfig().markedRoom.multiplier.lighthouse; - this.markedAddExtraItems(spawnpoint); - this.markedItemGroups(spawnpoint); + await this.markedAddExtraItemsAsync(spawnpoint); + await this.adjustMarkedItemGroupsAsync(spawnpoint); } } } - private markedAddExtraItems(spawnpoint: ISpawnpoint): void { - for (const item of Object.entries(this.config.getConfig().markedRoom.extraItems)) { - if (spawnpoint.template.Items.find((x) => x._tpl === item[0])) { - continue; + private async markedAddExtraItemsAsync(spawnpoint: ISpawnpoint): Promise { + const extraItems = Object.entries(await this.config.getConfig().markedRoom.extraItems); + + await Promise.all(extraItems.map(async ([itemTpl, relativeProbability]) => { + if (spawnpoint.template.Items.find((x) => x._tpl === itemTpl)) { + return; // Skip if the item already exists } const key = this.hashUtil.generate(); spawnpoint.template.Items.push({ _id: key, - _tpl: item[0], + _tpl: itemTpl, }); spawnpoint.itemDistribution.push({ composedKey: { key: key }, - relativeProbability: item[1], + relativeProbability: relativeProbability, }); - this.logger.debug(`Added ${item[0]} to ${spawnpoint.template.Id}`); - } + this.logger.debug(`Added ${itemTpl} to ${spawnpoint.template.Id}`); + })); } + - private markedItemGroups(spawnpoint: ISpawnpoint): void { - for (const item of spawnpoint.template.Items) { - for (const group in this.config.getConfig().markedRoom.itemGroups) { + private async adjustMarkedItemGroupsAsync(spawnpoint: ISpawnpoint): Promise { + const itemGroups = this.config.getConfig().markedRoom.itemGroups; + + await Promise.all(spawnpoint.template.Items.map(async (item) => { + await Promise.all(Object.keys(itemGroups).map(async (group) => { if (this.itemHelper.isOfBaseclass(item._tpl, group)) { - for (const dist of spawnpoint.itemDistribution) { + await Promise.all(spawnpoint.itemDistribution.map(async (dist) => { if (dist.composedKey.key === item._id) { - dist.relativeProbability *= this.config.getConfig().markedRoom.itemGroups[group]; + dist.relativeProbability *= itemGroups[group]; + this.logger.debug(`markedItemGroups: Changed ${item._tpl} to ${dist.relativeProbability}`); } - } + })); } - } - } + })); + })); } } diff --git a/src/helpers/LotsofLootHelper.ts b/src/helpers/LotsofLootHelper.ts index 5f9f256..86d25f0 100644 --- a/src/helpers/LotsofLootHelper.ts +++ b/src/helpers/LotsofLootHelper.ts @@ -29,62 +29,64 @@ export class LotsofLootHelper { } } - public changeRelativeProbabilityInPool(itemtpl: string, mult: number): void { + public async changeRelativeProbabilityInPoolAsync(itemtpl: string, mult: number): Promise { const locations = this.databaseService.getTables().locations; - - for (const locationId in locations) { - if (locations.hasOwnProperty(locationId)) { - const location: ILocation = locations[locationId]; - - if (!location.looseLoot) { - this.logger.debug(`Skipping ${locationId} as it has no loose loot!`); - continue; - } - - location.looseLoot.spawnpoints.forEach((spawnpoint) => { - const item = spawnpoint.template.Items.find((i) => i._tpl == itemtpl); - - if (item) { - const itemDistribution = spawnpoint.itemDistribution.find((i) => i.composedKey.key == item._id); - - if (itemDistribution) { - itemDistribution.relativeProbability *= mult; - - this.logger.debug(`${locationId}, ${spawnpoint.template.Id}, ${item._tpl}, ${itemDistribution.relativeProbability}`); - } - } - }); + const locationIds = Object.keys(locations); + + //Process each location asynchronously + await Promise.all(locationIds.map(async (locationId) => { + const location: ILocation = locations[locationId]; + + if (!location.looseLoot) { + this.logger.debug(`Skipping ${locationId} as it has no loose loot!`); + return; //Return skips this location, keeps the loop going. } - } + + //Process spawnpoints asynchronously + await Promise.all(location.looseLoot.spawnpoints.map(async (spawnpoint) => { + const item = spawnpoint.template.Items.find((i) => i._tpl == itemtpl); + + if (item) { + const itemDistribution = spawnpoint.itemDistribution.find((i) => i.composedKey.key == item._id); + + if (itemDistribution) { + itemDistribution.relativeProbability *= mult; + + this.logger.debug(`${locationId}, ${spawnpoint.template.Id}, ${item._tpl}, ${itemDistribution.relativeProbability}`); + } + } + })); + })); } - public changeProbabilityOfPool(itemtpl: string, mult: number): void { + public async changeProbabilityOfPoolAsync(itemtpl: string, mult: number): Promise { const locations = this.databaseService.getTables().locations; - - for (const locationId in locations) { - if (locations.hasOwnProperty(locationId)) { - const location: ILocation = locations[locationId]; - - if (!location.looseLoot) { - this.logger.debug(`Skipping ${locationId} as it has no loose loot!`); - continue; - } - - location.looseLoot.spawnpoints.forEach((spawnpoint) => { - const item = spawnpoint.template.Items.find((i) => i._tpl == itemtpl); - - if (item) { - spawnpoint.probability *= mult; - - //Clamp probability back down to 1 - if (spawnpoint.probability > 1) { - spawnpoint.probability = 1; - } - - this.logger.debug(`${locationId}, Pool:${spawnpoint.template.Id}, Chance:${spawnpoint.probability}`); - } - }); + const locationIds = Object.keys(locations); + + //Process each location asynchronously + await Promise.all(locationIds.map(async (locationId) => { + const location: ILocation = locations[locationId]; + + if (!location.looseLoot) { + this.logger.debug(`Skipping ${locationId} as it has no loose loot!`); + return; //Return skips this location, keeps the loop going. } - } + + //Process spawnpoints asynchronously + await Promise.all(location.looseLoot.spawnpoints.map(async (spawnpoint) => { + const item = spawnpoint.template.Items.find((i) => i._tpl == itemtpl); + + if (item) { + spawnpoint.probability *= mult; + + //Clamp probability to 1 if it exceeds + if (spawnpoint.probability > 1) { + spawnpoint.probability = 1; + } + + this.logger.debug(`${locationId}, Pool:${spawnpoint.template.Id}, Chance:${spawnpoint.probability}`); + } + })); + })); } }