Finish basic setup for configuration loading
This commit is contained in:
@@ -1,14 +1,25 @@
|
||||
import { LevelCalculator, Standards, DEFAULT_STANDARDS_CONFIG, type StandardsParams, StandardsParamsSchema, StandardsDataSchema, type StandardsConfig } from "@blade-and-brawn/calculator";
|
||||
import { LevelCalculator, Standards, FALLBACK_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_CONFIG));
|
||||
private Calculator = new LevelCalculator(new Standards(FALLBACK_STANDARDS_CONFIG));
|
||||
Standards: StandardsService = new StandardsService(this.Calculator);
|
||||
private ready: Promise<void>;
|
||||
|
||||
calculate(player: Player, activityPerformances: ActivityPerformance[]) {
|
||||
constructor() {
|
||||
// Load the selected config on startup instead of waiting for the
|
||||
// first `calculate()` call. If it fails (e.g. no config selected
|
||||
// yet, DB unreachable at boot), fall back to DEFAULT_STANDARDS_CONFIG
|
||||
// and keep serving — it'll retry on the next refresh.
|
||||
this.ready = this.Standards.Config.refresh()
|
||||
.catch((err) => log.error({ err }, "failed to load initial standards config, falling back to defaults"));
|
||||
}
|
||||
|
||||
async calculate(player: Player, activityPerformances: ActivityPerformance[]) {
|
||||
await this.ready;
|
||||
this.Standards.Config.refresh({ onlyIfStale: true }).catch((err) => log.error({ err }, "failed to refresh standards config"));
|
||||
return this.Calculator.calculate(player, activityPerformances);
|
||||
}
|
||||
@@ -92,7 +103,7 @@ class StandardsConfigService {
|
||||
.set({ is_selected: true })
|
||||
.where("id", "=", id)
|
||||
.execute();
|
||||
if (result[0]?.numUpdatedRows === 0n) throw new Error(`No config with id "${id}"`);
|
||||
if (result[0]?.numUpdatedRows === 0n) throw new Error(`No standards config with id "${id}"`);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user