Server/project/src/services/RagfairCategoriesService.ts
Refringe 4ac12ef70a Formatting/Linting Changes (!168)
These are the formatting & linting configuration changes from the `3.8.0` branch and the changes that they make to the overall project.

The majority of these changes are from running two commands:

`npm run lint:fix`
`npm run style:fix`

This has already been run on the `3.8.0` branch and this PR should make `master` play nicer when it comes to merges going forward.

There are now four VSCode plugins recommended for server development. They've been added to the workspace file and a user should get a UI notification when the workspace is opened if they're not installed.

The four plugins are:
https://marketplace.visualstudio.com/items?itemName=EditorConfig.EditorConfig
https://marketplace.visualstudio.com/items?itemName=dprint.dprint
https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint
https://marketplace.visualstudio.com/items?itemName=biomejs.biome

Once installed they should just work within the workspace.

Also, be sure to `npm i` to get the new dprint application.

Co-authored-by: Refringe <brownelltyler@gmail.com>
Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/168
2023-11-16 21:42:06 +00:00

97 lines
2.6 KiB
TypeScript

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<string, number> = {};
constructor(@inject("WinstonLogger") protected logger: ILogger)
{}
/**
* Get all flea categories and their count of offers
* @returns item categories and count
*/
public getAllCategories(): Record<string, number>
{
return this.categories;
}
/**
* With the supplied items, get custom categories
* @returns a custom list of categories
*/
public getBespokeCategories(offers: IRagfairOffer[]): Record<string, number>
{
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<string, number>
{
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<string, number>, 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]--;
}
}