Restructuring default config for standards

This commit is contained in:
Dominic Ferrando
2026-07-04 17:41:08 -04:00
parent 376af8ea45
commit a888d24bfd
8 changed files with 59 additions and 48 deletions
+15 -12
View File
@@ -1,11 +1,11 @@
import { LevelCalculator, Standards, DEFAULT_STANDARDS_DATA, type StandardsParams, StandardsParamsSchema, StandardsDataSchema } from "@blade-and-brawn/calculator";
import { LevelCalculator, Standards, DEFAULT_STANDARDS_CONFIG, type StandardsParams, StandardsParamsSchema, StandardsDataSchema, type StandardsConfig } 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";
import { log } from "../util";
export class CalculatorService {
private Calculator = new LevelCalculator(new Standards(DEFAULT_STANDARDS_DATA));
private Calculator = new LevelCalculator(new Standards(DEFAULT_STANDARDS_CONFIG));
Standards: StandardsService = new StandardsService(this.Calculator);
calculate(player: Player, activityPerformances: ActivityPerformance[]) {
@@ -37,11 +37,11 @@ class StandardsConfigService {
}
/** Retrieves the currently selected configuration */
async get(opt: { skipCache?: boolean } = {}): Promise<StandardsParams> {
if (!opt.skipCache) return this.Calculator.Standards.params;
async get(opt: { skipCache?: boolean } = {}): Promise<StandardsConfig> {
if (!opt.skipCache) return this.Calculator.Standards.cfg;
await this.refresh();
return this.Calculator.Standards.params;
return this.Calculator.Standards.cfg;
}
async update(id: string, datasetId: string, params: StandardsParams) {
@@ -58,7 +58,7 @@ class StandardsConfigService {
this.lastRefreshedAt = Date.now();
const config = await db.selectFrom("standards_configs")
const result = await db.selectFrom("standards_configs")
.innerJoin("standards_datasets", "standards_datasets.id", "standards_configs.dataset_id")
.select([
"standards_configs.dataset_id as datasetId",
@@ -68,15 +68,18 @@ class StandardsConfigService {
.where("standards_configs.is_selected", "=", true)
.limit(1)
.executeTakeFirstOrThrow({ errorConstructor: () => new Error("No standards config selected") });
Value.Assert(StandardsParamsSchema, config.params);
Value.Assert(StandardsDataSchema, config.data);
Value.Assert(StandardsParamsSchema, result.params);
Value.Assert(StandardsDataSchema, result.data);
const parametersStringified = JSON.stringify(config.params);
if (parametersStringified === this.cachedParametersStringified && config.datasetId === this.cachedDatasetId) return;
const parametersStringified = JSON.stringify(result.params);
if (parametersStringified === this.cachedParametersStringified && result.datasetId === this.cachedDatasetId) return;
this.Calculator.Standards = new Standards(config.data, config.params);
this.Calculator.Standards = new Standards({
data: result.data,
params: result.params
});
this.cachedParametersStringified = parametersStringified;
this.cachedDatasetId = config.datasetId;
this.cachedDatasetId = result.datasetId;
}
async switch(id: string) {