From 1dda9efbd7a62035f2f96b50d35f54a44dcb430e Mon Sep 17 00:00:00 2001 From: Dev Date: Sun, 25 Feb 2024 11:17:33 +0000 Subject: [PATCH] Move `ExhaustableArray` into its own file + make it implement interface --- .../generators/BotEquipmentModGenerator.ts | 3 +- .../generators/RepeatableQuestGenerator.ts | 2 +- project/src/helpers/BotGeneratorHelper.ts | 47 ---------------- .../models/spt/generators/IBotGenerator.ts | 7 --- .../src/models/spt/server/ExhaustableArray.ts | 54 +++++++++++++++++++ 5 files changed, 57 insertions(+), 56 deletions(-) create mode 100644 project/src/models/spt/server/ExhaustableArray.ts diff --git a/project/src/generators/BotEquipmentModGenerator.ts b/project/src/generators/BotEquipmentModGenerator.ts index f6f55c76..b439054f 100644 --- a/project/src/generators/BotEquipmentModGenerator.ts +++ b/project/src/generators/BotEquipmentModGenerator.ts @@ -1,6 +1,6 @@ import { inject, injectable } from "tsyringe"; -import { BotGeneratorHelper, ExhaustableArray } from "@spt-aki/helpers/BotGeneratorHelper"; +import { BotGeneratorHelper } from "@spt-aki/helpers/BotGeneratorHelper"; import { BotHelper } from "@spt-aki/helpers/BotHelper"; import { BotWeaponGeneratorHelper } from "@spt-aki/helpers/BotWeaponGeneratorHelper"; import { ItemHelper } from "@spt-aki/helpers/ItemHelper"; @@ -17,6 +17,7 @@ import { ConfigTypes } from "@spt-aki/models/enums/ConfigTypes"; import { ModSpawn } from "@spt-aki/models/enums/ModSpawn"; import { IChooseRandomCompatibleModResult } from "@spt-aki/models/spt/bots/IChooseRandomCompatibleModResult"; import { EquipmentFilterDetails, EquipmentFilters, IBotConfig } from "@spt-aki/models/spt/config/IBotConfig"; +import { ExhaustableArray } from "@spt-aki/models/spt/server/ExhaustableArray"; import { ILogger } from "@spt-aki/models/spt/utils/ILogger"; import { ConfigServer } from "@spt-aki/servers/ConfigServer"; import { DatabaseServer } from "@spt-aki/servers/DatabaseServer"; diff --git a/project/src/generators/RepeatableQuestGenerator.ts b/project/src/generators/RepeatableQuestGenerator.ts index 4c3cd9d0..f3d9181d 100644 --- a/project/src/generators/RepeatableQuestGenerator.ts +++ b/project/src/generators/RepeatableQuestGenerator.ts @@ -1,6 +1,5 @@ import { inject, injectable } from "tsyringe"; -import { ExhaustableArray } from "@spt-aki/helpers/BotGeneratorHelper"; import { HandbookHelper } from "@spt-aki/helpers/HandbookHelper"; import { ItemHelper } from "@spt-aki/helpers/ItemHelper"; import { PresetHelper } from "@spt-aki/helpers/PresetHelper"; @@ -32,6 +31,7 @@ import { IRepeatableQuestConfig, } from "@spt-aki/models/spt/config/IQuestConfig"; import { IQuestTypePool } from "@spt-aki/models/spt/repeatable/IQuestTypePool"; +import { ExhaustableArray } from "@spt-aki/models/spt/server/ExhaustableArray"; import { ILogger } from "@spt-aki/models/spt/utils/ILogger"; import { EventOutputHolder } from "@spt-aki/routers/EventOutputHolder"; import { ConfigServer } from "@spt-aki/servers/ConfigServer"; diff --git a/project/src/helpers/BotGeneratorHelper.ts b/project/src/helpers/BotGeneratorHelper.ts index 45e04bf2..4fc199be 100644 --- a/project/src/helpers/BotGeneratorHelper.ts +++ b/project/src/helpers/BotGeneratorHelper.ts @@ -16,7 +16,6 @@ import { ILogger } from "@spt-aki/models/spt/utils/ILogger"; import { ConfigServer } from "@spt-aki/servers/ConfigServer"; import { DatabaseServer } from "@spt-aki/servers/DatabaseServer"; import { LocalisationService } from "@spt-aki/services/LocalisationService"; -import { JsonUtil } from "@spt-aki/utils/JsonUtil"; import { RandomUtil } from "@spt-aki/utils/RandomUtil"; @injectable() @@ -512,49 +511,3 @@ export class BotGeneratorHelper : botRole; } } - -/** TODO - move into own class */ -export class ExhaustableArray -{ - private pool: T[]; - - constructor(private itemPool: T[], private randomUtil: RandomUtil, private jsonUtil: JsonUtil) - { - this.pool = this.jsonUtil.clone(itemPool); - } - - public getRandomValue(): T - { - if (!this.pool?.length) - { - return null; - } - - const index = this.randomUtil.getInt(0, this.pool.length - 1); - const toReturn = this.jsonUtil.clone(this.pool[index]); - this.pool.splice(index, 1); - return toReturn; - } - - public getFirstValue(): T - { - if (!this.pool?.length) - { - return null; - } - - const toReturn = this.jsonUtil.clone(this.pool[0]); - this.pool.splice(0, 1); - return toReturn; - } - - public hasValues(): boolean - { - if (this.pool?.length) - { - return true; - } - - return false; - } -} diff --git a/project/src/models/spt/generators/IBotGenerator.ts b/project/src/models/spt/generators/IBotGenerator.ts index 0fa0f3a4..f8704707 100644 --- a/project/src/models/spt/generators/IBotGenerator.ts +++ b/project/src/models/spt/generators/IBotGenerator.ts @@ -11,10 +11,3 @@ export interface IBotGenerator isPmc: boolean, ): PmcInventory; } - -export interface IExhaustableArray -{ - getRandomValue(): T; - getFirstValue(): T; - hasValues(): boolean; -} diff --git a/project/src/models/spt/server/ExhaustableArray.ts b/project/src/models/spt/server/ExhaustableArray.ts new file mode 100644 index 00000000..16bdc100 --- /dev/null +++ b/project/src/models/spt/server/ExhaustableArray.ts @@ -0,0 +1,54 @@ +import { JsonUtil } from "@spt-aki/utils/JsonUtil"; +import { RandomUtil } from "@spt-aki/utils/RandomUtil"; + +export class ExhaustableArray implements IExhaustableArray +{ + private pool: T[]; + + constructor(private itemPool: T[], private randomUtil: RandomUtil, private jsonUtil: JsonUtil) + { + this.pool = this.jsonUtil.clone(itemPool); + } + + public getRandomValue(): T + { + if (!this.pool?.length) + { + return null; + } + + const index = this.randomUtil.getInt(0, this.pool.length - 1); + const toReturn = this.jsonUtil.clone(this.pool[index]); + this.pool.splice(index, 1); + return toReturn; + } + + public getFirstValue(): T + { + if (!this.pool?.length) + { + return null; + } + + const toReturn = this.jsonUtil.clone(this.pool[0]); + this.pool.splice(0, 1); + return toReturn; + } + + public hasValues(): boolean + { + if (this.pool?.length) + { + return true; + } + + return false; + } +} + +export interface IExhaustableArray +{ + getRandomValue(): T; + getFirstValue(): T; + hasValues(): boolean; +}