Setup getConfig with typebox validations

This commit is contained in:
Dominic Ferrando
2026-07-04 01:12:15 -04:00
parent d042340c8b
commit 1da9795d84
8 changed files with 52 additions and 34 deletions
-1
View File
@@ -31,7 +31,6 @@
"kysely": "^0.29.2",
"ml-levenberg-marquardt": "^5.0.1",
"pg": "^8.22.0",
"pino": "^10.3.1",
"zipcodes-us": "^1.1.3"
},
"devDependencies": {
+17 -1
View File
@@ -1,5 +1,7 @@
import { LevelCalculator, Standards, DEFAULT_STANDARDS_DATA } from "@blade-and-brawn/calculator";
import { LevelCalculator, Standards, DEFAULT_STANDARDS_DATA, type StandardsConfig, StandardsConfigSchema } 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";
export class CalculatorService {
private readonly levelCalculator;
@@ -13,4 +15,18 @@ export class CalculatorService {
calculate(player: Player, activityPerformances: ActivityPerformance[]) {
return this.levelCalculator.calculate(player, activityPerformances)
}
async getConfig(opt: { skipCache?: boolean } = {}): Promise<StandardsConfig> {
if (!opt.skipCache) return this.levelCalculator.standards.cfg;
const result = await db.selectFrom("calculator_configs")
.select(["parameters"])
.where("is_selected", "=", true)
.limit(1)
.executeTakeFirstOrThrow({ errorConstructor: () => new Error("No calculator config selected") });
const standardsConfig = result.parameters;
Value.Assert(StandardsConfigSchema, standardsConfig);
return standardsConfig;
}
}
+5 -6
View File
@@ -4,6 +4,10 @@
"workspaces": {
"": {
"name": "blade-and-brawn",
"dependencies": {
"@sinclair/typebox": "^0.34.48",
"pino": "^10.3.1",
},
"devDependencies": {
"@types/bun": "^1.3.14",
"typescript": "^6.0.3",
@@ -23,7 +27,6 @@
"kysely": "^0.29.2",
"ml-levenberg-marquardt": "^5.0.1",
"pg": "^8.22.0",
"pino": "^10.3.1",
"zipcodes-us": "^1.1.3",
},
"devDependencies": {
@@ -60,22 +63,18 @@
"version": "0.0.0",
"dependencies": {
"@blade-and-brawn/domain": "workspace:*",
"ml-levenberg-marquardt": "^5.0.0",
"ml-levenberg-marquardt": "^5.0.1",
},
},
"packages/commerce": {
"name": "@blade-and-brawn/commerce",
"version": "0.0.0",
"dependencies": {
"pino": "^10.3.1",
},
},
"packages/domain": {
"name": "@blade-and-brawn/domain",
"version": "0.0.0",
"dependencies": {
"@blade-and-brawn/domain": "workspace:*",
"@sinclair/typebox": "^0.34.48",
},
},
},
+4
View File
@@ -14,5 +14,9 @@
"devDependencies": {
"@types/bun": "^1.3.14",
"typescript": "^6.0.3"
},
"dependencies": {
"pino": "^10.3.1",
"@sinclair/typebox": "^0.34.48"
}
}
+1 -1
View File
@@ -4,7 +4,7 @@
"version": "0.0.0",
"type": "module",
"dependencies": {
"ml-levenberg-marquardt": "^5.0.0",
"ml-levenberg-marquardt": "^5.0.1",
"@blade-and-brawn/domain": "workspace:*"
},
"exports": {
+23 -20
View File
@@ -11,6 +11,8 @@ import {
type StandardUnit,
} from "@blade-and-brawn/domain";
import { levenbergMarquardt as LM } from "ml-levenberg-marquardt";
import { Type, type Static } from "@sinclair/typebox";
import { Value } from "@sinclair/typebox/value";
import defaultConfig from "./config.json" with { type: "json" };
import defaultStandardsJson from "./data/default-standards.json" with { type: "json" };
import { getAvgWeight } from "./avg-weights";
@@ -211,26 +213,27 @@ export class LevelCalculator {
}
}
export type StandardsConfig = {
global: {
maxLevel: number;
};
activity: Record<
Activity,
{
weightModifier: number;
weightSkew: number;
ageModifier: number;
enableGeneration: boolean;
difficultyModifier: number;
peakAge: number;
stretch: {
upper: number;
lower: number;
};
}
>;
};
const StandardsActivityConfigSchema = Type.Object({
weightModifier: Type.Number(),
weightSkew: Type.Number(),
ageModifier: Type.Number(),
enableGeneration: Type.Boolean(),
difficultyModifier: Type.Number(),
peakAge: Type.Number(),
stretch: Type.Object({
upper: Type.Number(),
lower: Type.Number(),
}),
});
export const StandardsConfigSchema = Type.Object({
global: Type.Object({
maxLevel: Type.Number(),
}),
activity: Type.Record(Type.Enum(Activity), StandardsActivityConfigSchema),
});
export type StandardsConfig = Static<typeof StandardsConfigSchema>;
export class Standards {
readonly cfg: StandardsConfig;
+1 -3
View File
@@ -6,7 +6,5 @@
"exports": {
".": "./src/index.ts"
},
"dependencies": {
"pino": "^10.3.1"
}
"dependencies": {}
}
+1 -2
View File
@@ -4,8 +4,7 @@
"version": "0.0.0",
"type": "module",
"dependencies": {
"@blade-and-brawn/domain": "workspace:*",
"@sinclair/typebox": "^0.34.48"
"@blade-and-brawn/domain": "workspace:*"
},
"exports": {
".": "./src/index.ts"