Server/project/src/helpers/ProbabilityHelper.ts

26 lines
742 B
TypeScript
Raw Normal View History

2023-03-03 16:23:46 +01:00
import { inject, injectable } from "tsyringe";
import { ILogger } from "@spt-aki/models/spt/utils/ILogger";
import { RandomUtil } from "@spt-aki/utils/RandomUtil";
2023-03-03 16:23:46 +01:00
@injectable()
export class ProbabilityHelper
{
constructor(
@inject("WinstonLogger") protected logger: ILogger,
@inject("RandomUtil") protected randomUtil: RandomUtil,
2023-03-03 16:23:46 +01:00
)
{}
2023-03-03 16:23:46 +01:00
/**
* Chance to roll a number out of 100
* @param chance Percentage chance roll should success
* @param scale scale of chance to allow support of numbers > 1-100
* @returns true if success
*/
public rollChance(chance: number, scale = 1): boolean
{
return (this.randomUtil.getInt(1, 100 * scale) / (1 * scale)) <= chance;
2023-03-03 16:23:46 +01:00
}
}