33 lines
1.2 KiB
TypeScript
33 lines
1.2 KiB
TypeScript
import { LevelCalculator, Standards, DEFAULT_STANDARDS_DATA, type StandardsConfig, StandardsConfigSchema } from "@blade-and-brawn/calculator";
|
|
import type { ActivityPerformance, Player } from "@blade-and-brawn/domain";
|
|
import { db } from "../database/db";
|
|
import { Value } from "@sinclair/typebox/value";
|
|
|
|
export class CalculatorService {
|
|
private readonly levelCalculator;
|
|
|
|
constructor() {
|
|
this.levelCalculator = new LevelCalculator(
|
|
new Standards(DEFAULT_STANDARDS_DATA),
|
|
);
|
|
}
|
|
|
|
calculate(player: Player, activityPerformances: ActivityPerformance[]) {
|
|
return this.levelCalculator.calculate(player, activityPerformances)
|
|
}
|
|
|
|
async getConfig(opt: { skipCache?: boolean } = {}): Promise<StandardsConfig> {
|
|
if (!opt.skipCache) return this.levelCalculator.standards.cfg;
|
|
|
|
const result = await db.selectFrom("calculator_configs")
|
|
.select(["parameters"])
|
|
.where("is_selected", "=", true)
|
|
.limit(1)
|
|
.executeTakeFirstOrThrow({ errorConstructor: () => new Error("No calculator config selected") });
|
|
|
|
const standardsConfig = result.parameters;
|
|
Value.Assert(StandardsConfigSchema, standardsConfig);
|
|
return standardsConfig;
|
|
}
|
|
}
|