Skip to content

Commit

Permalink
Run startup of mod asynchronously
Browse files Browse the repository at this point in the history
  • Loading branch information
ArchangelWTF committed Nov 26, 2024
1 parent 0124c37 commit f0c3c60
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 87 deletions.
4 changes: 2 additions & 2 deletions src/LotsofLoot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ export class LotsofLoot {
}

public async postDBLoadAsync(_container: DependencyContainer): Promise<void> {
this.lotsofLootController.applyLotsOfLootModifications();
this.lotsofLootMarkedRoomController.adjustMarkedRoomItems();
await this.lotsofLootController.applyLotsOfLootModifications();
await this.lotsofLootMarkedRoomController.adjustMarkedRoomItems();

this.logger.logInfo(`Finished loading`);
}
Expand Down
6 changes: 3 additions & 3 deletions src/controllers/LotsofLootController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class LotsofLootController {
) {
}

public applyLotsOfLootModifications(): void {
public async applyLotsOfLootModifications(): Promise<void> {
const lotsofLootConfig = this.config.getConfig();
const tables = this.databaseService.getTables();
const locations = tables.locations;
Expand Down Expand Up @@ -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) {
Expand Down
68 changes: 37 additions & 31 deletions src/controllers/LotsofLootMarkedRoomController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class LotsofLootMarkedRoomController {
) {
}

public adjustMarkedRoomItems(): void {
public async adjustMarkedRoomItems(): Promise<void> {
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;
Expand All @@ -33,27 +33,27 @@ 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);
}
}

for (const spawnpoint of spawnPointsReserve) {
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);
}
}

Expand All @@ -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);
}
}

Expand All @@ -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<void> {
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<void> {
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}`);
}
}
}));
}
}
}
}));
}));
}
}
104 changes: 53 additions & 51 deletions src/helpers/LotsofLootHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,62 +29,64 @@ export class LotsofLootHelper {
}
}

public changeRelativeProbabilityInPool(itemtpl: string, mult: number): void {
public async changeRelativeProbabilityInPoolAsync(itemtpl: string, mult: number): Promise<void> {
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<void> {
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}`);
}
}));
}));
}
}

0 comments on commit f0c3c60

Please sign in to comment.