Move attachment id logic to helpers

This commit is contained in:
Platinum 2024-02-06 17:16:43 +11:00
parent de9619a654
commit 958632e9a1
3 changed files with 58 additions and 19 deletions

38
src/helpers.ts Normal file
View File

@ -0,0 +1,38 @@
// Copyright (C) 2024 Platinum
//
// This file is part of spt-the-blacklist.
//
// spt-the-blacklist is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// spt-the-blacklist is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with spt-the-blacklist. If not, see <http://www.gnu.org/licenses/>.
import { Category } from "@spt-aki/models/eft/common/tables/IHandbookBase";
// There are so many child categories of attachments, this will return all categories using recursion so I don't have to type each ID.
export function getAttachmentCategoryIds(handbookCategories: Category[]): string[] {
const weaponPartsAndModsId = "5b5f71a686f77447ed5636ab";
const weaponPartsChildrenCategories = getChildCategoriesRecursively(handbookCategories, weaponPartsAndModsId);
const childrenIds = weaponPartsChildrenCategories.map(category => category.Id);
const attachmentCategoryIds = [weaponPartsAndModsId];
return attachmentCategoryIds.concat(childrenIds);
}
function getChildCategoriesRecursively(handbookCategories: Category[], parentId: string): Category[] {
const childCategories = handbookCategories.filter(category => category.ParentId === parentId);
const grandChildrenCategories = childCategories.reduce(
(memo, category) => memo.concat(this.getChildCategoriesRecursively(handbookCategories, category.Id)),
[]
);
return childCategories.concat(grandChildrenCategories);
}

View File

@ -28,6 +28,7 @@ import { Category } from "@spt-aki/models/eft/common/tables/IHandbookBase";
import config from "../config.json";
import advancedConfig from "../advancedConfig.json";
import { getAttachmentCategoryIds } from "./helpers";
class TheBlacklistMod implements IPostDBLoadModAsync {
private logger: ILogger;
@ -63,7 +64,7 @@ class TheBlacklistMod implements IPostDBLoadModAsync {
let attachmentPriceLimitedCount = 0;
if (config.limitMaxPriceOfAttachments) {
this.initialiseAttachmentCategoryIds(tables.templates.handbook.Categories);
this.attachmentCategoryIds = getAttachmentCategoryIds(tables.templates.handbook.Categories);
}
// Find all items to update by looping through handbook which is a better indicator of useable items.
@ -149,24 +150,7 @@ class TheBlacklistMod implements IPostDBLoadModAsync {
}
}
private initialiseAttachmentCategoryIds(handbookCategories: Category[]) {
const weaponPartsAndModsId = "5b5f71a686f77447ed5636ab";
const weaponPartsChildrenCategories = this.getChildCategoriesRecursively(handbookCategories, weaponPartsAndModsId);
const childrenIds = weaponPartsChildrenCategories.map(category => category.Id);
this.attachmentCategoryIds.push(weaponPartsAndModsId);
this.attachmentCategoryIds = this.attachmentCategoryIds.concat(childrenIds);
}
private getChildCategoriesRecursively(handbookCategories: Category[], parentId: string): Category[] {
const childCategories = handbookCategories.filter(category => category.ParentId === parentId);
const grandChildrenCategories = childCategories.reduce(
(memo, category) => memo.concat(this.getChildCategoriesRecursively(handbookCategories, category.Id)),
[]
);
return childCategories.concat(grandChildrenCategories);
}
private getUpdatedPrice(item: ITemplateItem, prices: Record<string, number>): number | undefined {
const currentFleaPrice = prices[item._id];

17
src/priceService.ts Normal file
View File

@ -0,0 +1,17 @@
// Copyright (C) 2024 Platinum
//
// This file is part of spt-the-blacklist.
//
// spt-the-blacklist is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// spt-the-blacklist is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with spt-the-blacklist. If not, see <http://www.gnu.org/licenses/>.