From f4aad56ad3c59b0fb2b69930f6854ec98223e287 Mon Sep 17 00:00:00 2001 From: Dominic Ferrando Date: Sat, 4 Jul 2026 14:15:01 -0400 Subject: [PATCH] Clean up Calculator/Standards servicees --- apps/api/src/services/calculator.ts | 19 ++++++++++--------- packages/calculator/src/index.ts | 2 +- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/apps/api/src/services/calculator.ts b/apps/api/src/services/calculator.ts index 4bfb63c..c49d618 100644 --- a/apps/api/src/services/calculator.ts +++ b/apps/api/src/services/calculator.ts @@ -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 { - 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; } diff --git a/packages/calculator/src/index.ts b/packages/calculator/src/index.ts index bf3f42c..6f2b6bf 100644 --- a/packages/calculator/src/index.ts +++ b/packages/calculator/src/index.ts @@ -73,7 +73,7 @@ const metricPriority = (m: NumberMetric) => { export const DEFAULT_STANDARDS_DATASET = defaultStandardsDataset as StandardsDataset; export class LevelCalculator { - readonly standards: Standards; + standards: Standards; public constructor(standards: Standards) { this.standards = standards;