Clean up Calculator/Standards servicees

This commit is contained in:
Dominic Ferrando
2026-07-04 14:21:52 -04:00
parent 01878a8e03
commit f4aad56ad3
2 changed files with 11 additions and 10 deletions
+10 -9
View File
@@ -4,28 +4,29 @@ import { db } from "../database/db";
import { Value } from "@sinclair/typebox/value";
import { log } from "../util";
let calculator = new LevelCalculator(new Standards(DEFAULT_STANDARDS_DATASET));
export class CalculatorService {
standards: StandardsSubService = new StandardsSubService();
private calculator: LevelCalculator = new LevelCalculator(new Standards(DEFAULT_STANDARDS_DATASET));
standards: StandardsService = new StandardsService(this.calculator);
calculate(player: Player, activityPerformances: ActivityPerformance[]) {
this.standards.refreshConfig({ onlyIfStale: true }).catch((err) => log.error({ err }, "failed to refresh standards config"));
return calculator.calculate(player, activityPerformances);
return this.calculator.calculate(player, activityPerformances);
}
}
class StandardsSubService {
class StandardsService {
private static readonly CONFIG_REFRESH_INTERVAL_MS = 30_000;
private cachedParametersStringified: string | null = null;
private cachedDatasetId: string | null = null;
private lastRefreshedAt = 0;
constructor(private calculator: LevelCalculator) { }
async getSelectedConfig(opt: { skipCache?: boolean } = {}): Promise<StandardsParameters> {
if (!opt.skipCache) return calculator.standards.params;
if (!opt.skipCache) return this.calculator.standards.params;
await this.refreshConfig();
return calculator.standards.params;
return this.calculator.standards.params;
}
async selectConfig(id: string) {
@@ -58,7 +59,7 @@ class StandardsSubService {
async refreshConfig(opt: { onlyIfStale?: boolean } = {}) {
if (opt.onlyIfStale)
if (Date.now() - this.lastRefreshedAt < StandardsSubService.CONFIG_REFRESH_INTERVAL_MS) return;
if (Date.now() - this.lastRefreshedAt < StandardsService.CONFIG_REFRESH_INTERVAL_MS) return;
this.lastRefreshedAt = Date.now();
@@ -78,7 +79,7 @@ class StandardsSubService {
const parametersStringified = JSON.stringify(config.parameters);
if (parametersStringified === this.cachedParametersStringified && config.datasetId === this.cachedDatasetId) return;
calculator = new LevelCalculator(new Standards(config.dataset, config.parameters));
this.calculator.standards = new Standards(config.dataset, config.parameters);
this.cachedParametersStringified = parametersStringified;
this.cachedDatasetId = config.datasetId;
}