More work on calc config
This commit is contained in:
@@ -30,7 +30,7 @@ export async function up(db: Kysely<any>): Promise<void> {
|
||||
// TABLE: CALCULATOR_CONFIGS
|
||||
await db.schema.createTable("calculator_configs")
|
||||
.$call(addDefaultColumns)
|
||||
.addColumn("parameters", "jsonb", (cb) => cb.notNull())
|
||||
.addColumn("standards_config", "jsonb", (cb) => cb.notNull())
|
||||
.addColumn("is_selected", "boolean", (cb) => cb.notNull())
|
||||
.execute();
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@ import {
|
||||
type Levels,
|
||||
DEFAULT_STANDARDS_DATA,
|
||||
} from "@blade-and-brawn/calculator";
|
||||
import { Activity, clamp, Gender, range } from "@blade-and-brawn/calculator";
|
||||
import { Activity, clamp, Gender, range } from "@blade-and-brawn/domain";
|
||||
import { levenbergMarquardt as LM } from "ml-levenberg-marquardt";
|
||||
|
||||
for (const activity of Object.values(Activity)) {
|
||||
|
||||
@@ -114,7 +114,10 @@ export const app = new Elysia()
|
||||
return { sessionId: token.sessionId.toString() };
|
||||
})
|
||||
.post("/config", () => { })
|
||||
.get("/config", () => { })
|
||||
.get("/config", async () => {
|
||||
const config = await calculatorService.getConfig();
|
||||
return config;
|
||||
})
|
||||
)
|
||||
|
||||
// COMMERCE
|
||||
|
||||
@@ -2,31 +2,49 @@ import { LevelCalculator, Standards, DEFAULT_STANDARDS_DATA, type StandardsConfi
|
||||
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 readonly levelCalculator;
|
||||
private static readonly CONFIG_REFRESH_INTERVAL_MS = 30_000;
|
||||
|
||||
private cachedStandardsConfigData: string | null = null;
|
||||
private lastRefreshedAt = 0;
|
||||
|
||||
private levelCalculator: LevelCalculator;
|
||||
|
||||
constructor() {
|
||||
this.levelCalculator = new LevelCalculator(
|
||||
new Standards(DEFAULT_STANDARDS_DATA),
|
||||
);
|
||||
this.levelCalculator = new LevelCalculator(new Standards(DEFAULT_STANDARDS_DATA));
|
||||
}
|
||||
|
||||
calculate(player: Player, activityPerformances: ActivityPerformance[]) {
|
||||
return this.levelCalculator.calculate(player, activityPerformances)
|
||||
this.refreshConfig({ onlyIfStale: true }).catch((err) => log.error({ err }, "failed to refresh calculator config"));
|
||||
return this.levelCalculator.calculate(player, activityPerformances);
|
||||
}
|
||||
|
||||
async getConfig(opt: { skipCache?: boolean } = {}): Promise<StandardsConfig> {
|
||||
if (!opt.skipCache) return this.levelCalculator.standards.cfg;
|
||||
|
||||
await this.refreshConfig();
|
||||
return this.levelCalculator.standards.cfg;
|
||||
}
|
||||
|
||||
async refreshConfig(opt: { onlyIfStale?: boolean } = {}) {
|
||||
if (opt.onlyIfStale)
|
||||
if (Date.now() - this.lastRefreshedAt < CalculatorService.CONFIG_REFRESH_INTERVAL_MS) return;
|
||||
|
||||
this.lastRefreshedAt = Date.now();
|
||||
|
||||
const result = await db.selectFrom("calculator_configs")
|
||||
.select(["parameters"])
|
||||
.select(["standards_config"])
|
||||
.where("is_selected", "=", true)
|
||||
.limit(1)
|
||||
.executeTakeFirstOrThrow({ errorConstructor: () => new Error("No calculator config selected") });
|
||||
Value.Assert(StandardsConfigSchema, result.standards_config);
|
||||
|
||||
const standardsConfig = result.parameters;
|
||||
Value.Assert(StandardsConfigSchema, standardsConfig);
|
||||
return standardsConfig;
|
||||
const standardsConfigData = JSON.stringify(result.standards_config);
|
||||
if (standardsConfigData === this.cachedStandardsConfigData) return;
|
||||
|
||||
this.levelCalculator = new LevelCalculator(new Standards(DEFAULT_STANDARDS_DATA, result.standards_config));
|
||||
this.cachedStandardsConfigData = standardsConfigData;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user