import { inject, injectable } from "tsyringe"; import { IRagfairOffer } from "@spt-aki/models/eft/ragfair/IRagfairOffer"; import { ILogger } from "@spt-aki/models/spt/utils/ILogger"; @injectable() export class RagfairCategoriesService { protected categories: Record = {}; constructor(@inject("WinstonLogger") protected logger: ILogger) {} /** * Get all flea categories and their count of offers * @returns item categories and count */ public getAllCategories(): Record { return this.categories; } /** * With the supplied items, get custom categories * @returns a custom list of categories */ public getBespokeCategories(offers: IRagfairOffer[]): Record { return this.processOffersIntoCategories(offers); } /** * Take an array of ragfair offers and create a dictionary of items with thier corrisponding offer count * @param offers ragfair offers * @returns categories and count */ protected processOffersIntoCategories(offers: IRagfairOffer[]): Record { const result = {}; for (const offer of offers) { this.addOrIncrementCategory(offer, result); } return result; } /** * Increment or decrement a category array * @param offer Offer to process * @param categories Categories to update * @param increment (Optional) Should item be incremented or decremented */ protected addOrIncrementCategory(offer: IRagfairOffer, categories: Record, increment = true): void { const itemId = offer.items[0]._tpl; if (increment) { categories[itemId] = categories[itemId] ? categories[itemId] + 1 : 1; } else { // No category, no work to do if (categories[itemId]) { categories[itemId]--; // Remove category entirely as its 0 or less if (categories[itemId] < 1) { delete categories[itemId]; } } } } /** * Increase category count by 1 * @param offer */ public incrementCategory(offer: IRagfairOffer): void { this.addOrIncrementCategory(offer, this.categories); this.categories[offer.items[0]._tpl]++; } /** * Reduce category count by 1 * @param offer */ public decrementCategory(offer: IRagfairOffer): void { this.addOrIncrementCategory(offer, this.categories, false); this.categories[offer.items[0]._tpl]--; } }