Restructuring default config for standards
This commit is contained in:
@@ -1,6 +1,5 @@
|
|||||||
import {
|
import {
|
||||||
DEFAULT_STANDARDS_DATA,
|
DEFAULT_STANDARDS_CONFIG,
|
||||||
DEFAULT_STANDARDS_PARAMS,
|
|
||||||
StandardsDataSchema,
|
StandardsDataSchema,
|
||||||
StandardsParamsSchema,
|
StandardsParamsSchema,
|
||||||
} from "@blade-and-brawn/calculator";
|
} from "@blade-and-brawn/calculator";
|
||||||
@@ -20,9 +19,9 @@ async function seedStandardsDataset(): Promise<string> {
|
|||||||
return existing.id;
|
return existing.id;
|
||||||
}
|
}
|
||||||
|
|
||||||
Value.Assert(StandardsDataSchema, DEFAULT_STANDARDS_DATA);
|
Value.Assert(StandardsDataSchema, DEFAULT_STANDARDS_CONFIG.data);
|
||||||
const result = await db.insertInto("standards_datasets")
|
const result = await db.insertInto("standards_datasets")
|
||||||
.values({ name: DEFAULT_NAME, data: DEFAULT_STANDARDS_DATA })
|
.values({ name: DEFAULT_NAME, data: DEFAULT_STANDARDS_CONFIG.data })
|
||||||
.returning("id")
|
.returning("id")
|
||||||
.executeTakeFirstOrThrow();
|
.executeTakeFirstOrThrow();
|
||||||
log.info({ id: result.id }, "seeded default standards dataset");
|
log.info({ id: result.id }, "seeded default standards dataset");
|
||||||
@@ -44,12 +43,12 @@ async function seedStandardsConfig(datasetId: string): Promise<void> {
|
|||||||
.where("is_selected", "=", true)
|
.where("is_selected", "=", true)
|
||||||
.executeTakeFirst();
|
.executeTakeFirst();
|
||||||
|
|
||||||
Value.Assert(StandardsParamsSchema, DEFAULT_STANDARDS_PARAMS);
|
Value.Assert(StandardsParamsSchema, DEFAULT_STANDARDS_CONFIG.params);
|
||||||
const result = await db.insertInto("standards_configs")
|
const result = await db.insertInto("standards_configs")
|
||||||
.values({
|
.values({
|
||||||
name: DEFAULT_NAME,
|
name: DEFAULT_NAME,
|
||||||
dataset_id: datasetId,
|
dataset_id: datasetId,
|
||||||
params: DEFAULT_STANDARDS_PARAMS,
|
params: DEFAULT_STANDARDS_CONFIG.params,
|
||||||
is_selected: !hasSelected,
|
is_selected: !hasSelected,
|
||||||
})
|
})
|
||||||
.returning("id")
|
.returning("id")
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import {
|
import {
|
||||||
type Levels,
|
type Levels,
|
||||||
DEFAULT_STANDARDS_DATA,
|
DEFAULT_STANDARDS_CONFIG,
|
||||||
} from "@blade-and-brawn/calculator";
|
} from "@blade-and-brawn/calculator";
|
||||||
import { Activity, clamp, Gender, range } from "@blade-and-brawn/domain";
|
import { Activity, clamp, Gender, range } from "@blade-and-brawn/domain";
|
||||||
import { levenbergMarquardt as LM } from "ml-levenberg-marquardt";
|
import { levenbergMarquardt as LM } from "ml-levenberg-marquardt";
|
||||||
@@ -15,7 +15,7 @@ for (const activity of Object.values(Activity)) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (const gender of Object.values(Gender)) {
|
for (const gender of Object.values(Gender)) {
|
||||||
const standardsByGender = DEFAULT_STANDARDS_DATA[
|
const standardsByGender = DEFAULT_STANDARDS_CONFIG.data[
|
||||||
activity
|
activity
|
||||||
].standards.filter((s) => s["metrics"].gender === gender);
|
].standards.filter((s) => s["metrics"].gender === gender);
|
||||||
const ages = [...new Set(standardsByGender.map((s) => s.metrics.age))];
|
const ages = [...new Set(standardsByGender.map((s) => s.metrics.age))];
|
||||||
|
|||||||
@@ -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 type { ActivityPerformance, Player } from "@blade-and-brawn/domain";
|
||||||
import { db } from "../database/db";
|
import { db } from "../database/db";
|
||||||
import { Value } from "@sinclair/typebox/value";
|
import { Value } from "@sinclair/typebox/value";
|
||||||
import { log } from "../util";
|
import { log } from "../util";
|
||||||
|
|
||||||
export class CalculatorService {
|
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);
|
Standards: StandardsService = new StandardsService(this.Calculator);
|
||||||
|
|
||||||
calculate(player: Player, activityPerformances: ActivityPerformance[]) {
|
calculate(player: Player, activityPerformances: ActivityPerformance[]) {
|
||||||
@@ -37,11 +37,11 @@ class StandardsConfigService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** Retrieves the currently selected configuration */
|
/** Retrieves the currently selected configuration */
|
||||||
async get(opt: { skipCache?: boolean } = {}): Promise<StandardsParams> {
|
async get(opt: { skipCache?: boolean } = {}): Promise<StandardsConfig> {
|
||||||
if (!opt.skipCache) return this.Calculator.Standards.params;
|
if (!opt.skipCache) return this.Calculator.Standards.cfg;
|
||||||
|
|
||||||
await this.refresh();
|
await this.refresh();
|
||||||
return this.Calculator.Standards.params;
|
return this.Calculator.Standards.cfg;
|
||||||
}
|
}
|
||||||
|
|
||||||
async update(id: string, datasetId: string, params: StandardsParams) {
|
async update(id: string, datasetId: string, params: StandardsParams) {
|
||||||
@@ -58,7 +58,7 @@ class StandardsConfigService {
|
|||||||
|
|
||||||
this.lastRefreshedAt = Date.now();
|
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")
|
.innerJoin("standards_datasets", "standards_datasets.id", "standards_configs.dataset_id")
|
||||||
.select([
|
.select([
|
||||||
"standards_configs.dataset_id as datasetId",
|
"standards_configs.dataset_id as datasetId",
|
||||||
@@ -68,15 +68,18 @@ class StandardsConfigService {
|
|||||||
.where("standards_configs.is_selected", "=", true)
|
.where("standards_configs.is_selected", "=", true)
|
||||||
.limit(1)
|
.limit(1)
|
||||||
.executeTakeFirstOrThrow({ errorConstructor: () => new Error("No standards config selected") });
|
.executeTakeFirstOrThrow({ errorConstructor: () => new Error("No standards config selected") });
|
||||||
Value.Assert(StandardsParamsSchema, config.params);
|
Value.Assert(StandardsParamsSchema, result.params);
|
||||||
Value.Assert(StandardsDataSchema, config.data);
|
Value.Assert(StandardsDataSchema, result.data);
|
||||||
|
|
||||||
const parametersStringified = JSON.stringify(config.params);
|
const parametersStringified = JSON.stringify(result.params);
|
||||||
if (parametersStringified === this.cachedParametersStringified && config.datasetId === this.cachedDatasetId) return;
|
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.cachedParametersStringified = parametersStringified;
|
||||||
this.cachedDatasetId = config.datasetId;
|
this.cachedDatasetId = result.datasetId;
|
||||||
}
|
}
|
||||||
|
|
||||||
async switch(id: string) {
|
async switch(id: string) {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import {
|
import {
|
||||||
DEFAULT_STANDARDS_DATA,
|
DEFAULT_STANDARDS_CONFIG,
|
||||||
Standards,
|
Standards,
|
||||||
type StandardsParams,
|
type StandardsParams,
|
||||||
} from "@blade-and-brawn/calculator";
|
} from "@blade-and-brawn/calculator";
|
||||||
@@ -15,8 +15,6 @@
|
|||||||
|
|
||||||
let activeTab = $state("standards");
|
let activeTab = $state("standards");
|
||||||
|
|
||||||
const defaultStandards = new Standards(DEFAULT_STANDARDS_DATA);
|
|
||||||
|
|
||||||
let input = $state({
|
let input = $state({
|
||||||
activity: Activity.BenchPress,
|
activity: Activity.BenchPress,
|
||||||
metrics: {
|
metrics: {
|
||||||
@@ -24,7 +22,9 @@
|
|||||||
age: null as number | null,
|
age: null as number | null,
|
||||||
weight: null as number | null, // lb
|
weight: null as number | null, // lb
|
||||||
},
|
},
|
||||||
params: structuredClone(defaultStandards.params) as StandardsParams,
|
params: structuredClone(
|
||||||
|
DEFAULT_STANDARDS_CONFIG.params,
|
||||||
|
) as StandardsParams,
|
||||||
});
|
});
|
||||||
|
|
||||||
const selected = $derived({
|
const selected = $derived({
|
||||||
@@ -38,7 +38,10 @@
|
|||||||
});
|
});
|
||||||
|
|
||||||
const allStandards = $derived(
|
const allStandards = $derived(
|
||||||
new Standards(DEFAULT_STANDARDS_DATA, selected.params),
|
new Standards({
|
||||||
|
data: DEFAULT_STANDARDS_CONFIG.data,
|
||||||
|
params: selected.params,
|
||||||
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
$effect(() => {
|
$effect(() => {
|
||||||
|
|||||||
@@ -10,11 +10,11 @@ import {
|
|||||||
type Player,
|
type Player,
|
||||||
} from "@blade-and-brawn/domain";
|
} from "@blade-and-brawn/domain";
|
||||||
import { levenbergMarquardt as LM } from "ml-levenberg-marquardt";
|
import { levenbergMarquardt as LM } from "ml-levenberg-marquardt";
|
||||||
import defaultConfig from "./default-config.json" with { type: "json" };
|
import defaultStandardsParamsJson from "./data/standards/default-params.json" with { type: "json" };
|
||||||
import defaultStandardsData from "./data/default-standards.json" with { type: "json" };
|
import defaultStandardsDataJson from "./data/standards/default-data.json" with { type: "json" };
|
||||||
import { getAvgWeight } from "./avg-weights";
|
import { getAvgWeight } from "./avg-weights";
|
||||||
|
import { StandardsDataSchema, StandardsParamsSchema, type LevelCalculatorOutput, type Levels, type NumberMetric, type Standard, type StandardsConfig, type StandardsData } from "./models";
|
||||||
import { Value } from "@sinclair/typebox/value";
|
import { Value } from "@sinclair/typebox/value";
|
||||||
import { StandardsParamsSchema, type LevelCalculatorOutput, type Levels, type NumberMetric, type Standard, type StandardsData, type StandardsParams } from "./models";
|
|
||||||
|
|
||||||
// SOURCES
|
// SOURCES
|
||||||
// Squat, Bench, Dead Lift:
|
// Squat, Bench, Dead Lift:
|
||||||
@@ -34,8 +34,12 @@ const metricPriority = (m: NumberMetric) => {
|
|||||||
return 2;
|
return 2;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const DEFAULT_STANDARDS_DATA = defaultStandardsData as StandardsData;
|
Value.Assert(StandardsDataSchema, defaultStandardsDataJson);
|
||||||
export const DEFAULT_STANDARDS_PARAMS = defaultConfig as StandardsParams;
|
Value.Assert(StandardsParamsSchema, defaultStandardsParamsJson);
|
||||||
|
export const DEFAULT_STANDARDS_CONFIG: StandardsConfig = {
|
||||||
|
data: defaultStandardsDataJson,
|
||||||
|
params: defaultStandardsParamsJson
|
||||||
|
};
|
||||||
|
|
||||||
export class LevelCalculator {
|
export class LevelCalculator {
|
||||||
Standards: Standards;
|
Standards: Standards;
|
||||||
@@ -185,15 +189,11 @@ export class LevelCalculator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export class Standards {
|
export class Standards {
|
||||||
readonly params: StandardsParams;
|
|
||||||
private data: StandardsData;
|
private data: StandardsData;
|
||||||
|
|
||||||
constructor(data: StandardsData, params?: Partial<StandardsParams>) {
|
constructor(readonly cfg: StandardsConfig) {
|
||||||
Value.Assert(StandardsParamsSchema, defaultConfig);
|
// prepare internal data for modification
|
||||||
this.params = Object.assign({}, defaultConfig, params);
|
this.data = structuredClone(cfg.data);
|
||||||
|
|
||||||
// prepare data
|
|
||||||
this.data = structuredClone(data);
|
|
||||||
|
|
||||||
// STRETCH
|
// STRETCH
|
||||||
for (const activity of Object.values(Activity)) {
|
for (const activity of Object.values(Activity)) {
|
||||||
@@ -203,7 +203,7 @@ export class Standards {
|
|||||||
return (i: number) => A * Math.exp(-B * i) + C;
|
return (i: number) => A * Math.exp(-B * i) + C;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!this.params.activity[activity].enableGeneration) {
|
if (!cfg.params.activity[activity].enableGeneration) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -263,13 +263,13 @@ export class Standards {
|
|||||||
const newLevels: Levels = {};
|
const newLevels: Levels = {};
|
||||||
const newLowerLevelCount = Math.max(
|
const newLowerLevelCount = Math.max(
|
||||||
Math.round(
|
Math.round(
|
||||||
this.params.activity[activity].stretch.lower,
|
cfg.params.activity[activity].stretch.lower,
|
||||||
),
|
),
|
||||||
0,
|
0,
|
||||||
);
|
);
|
||||||
const newUpperLevelCount = Math.max(
|
const newUpperLevelCount = Math.max(
|
||||||
Math.round(
|
Math.round(
|
||||||
this.params.activity[activity].stretch.upper,
|
cfg.params.activity[activity].stretch.upper,
|
||||||
),
|
),
|
||||||
0,
|
0,
|
||||||
);
|
);
|
||||||
@@ -311,7 +311,7 @@ export class Standards {
|
|||||||
let i = 1;
|
let i = 1;
|
||||||
while (
|
while (
|
||||||
Object.keys(standard.levels).length <
|
Object.keys(standard.levels).length <
|
||||||
this.params.global.maxLevel
|
cfg.params.global.maxLevel
|
||||||
) {
|
) {
|
||||||
standard.levels = this.expandLevels(standard.levels, i++);
|
standard.levels = this.expandLevels(standard.levels, i++);
|
||||||
}
|
}
|
||||||
@@ -319,11 +319,11 @@ export class Standards {
|
|||||||
// compress
|
// compress
|
||||||
if (
|
if (
|
||||||
Object.keys(standard.levels).length >
|
Object.keys(standard.levels).length >
|
||||||
this.params.global.maxLevel
|
cfg.params.global.maxLevel
|
||||||
) {
|
) {
|
||||||
standard.levels = this.compressLevels(
|
standard.levels = this.compressLevels(
|
||||||
standard.levels,
|
standard.levels,
|
||||||
this.params.global.maxLevel,
|
cfg.params.global.maxLevel,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -331,12 +331,12 @@ export class Standards {
|
|||||||
for (const level in standard.levels) {
|
for (const level in standard.levels) {
|
||||||
standard.levels[level] =
|
standard.levels[level] =
|
||||||
standard.levels[level] *
|
standard.levels[level] *
|
||||||
(1 + this.params.activity[activity].difficultyModifier);
|
(1 + cfg.params.activity[activity].difficultyModifier);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// generate data
|
// generate data
|
||||||
const allGenerators = this.params.activity[activity].enableGeneration
|
const allGenerators = cfg.params.activity[activity].enableGeneration
|
||||||
? this.data[activity].metadata.generators.sort(
|
? this.data[activity].metadata.generators.sort(
|
||||||
(a, b) =>
|
(a, b) =>
|
||||||
metricPriority(a.metric) - metricPriority(b.metric),
|
metricPriority(a.metric) - metricPriority(b.metric),
|
||||||
@@ -346,7 +346,7 @@ export class Standards {
|
|||||||
for (const generator of allGenerators) {
|
for (const generator of allGenerators) {
|
||||||
if (generator.metric === "age") {
|
if (generator.metric === "age") {
|
||||||
for (const gender of Object.values(Gender)) {
|
for (const gender of Object.values(Gender)) {
|
||||||
const peakAge = this.params.activity[activity].peakAge;
|
const peakAge = cfg.params.activity[activity].peakAge;
|
||||||
const ageStep = 10;
|
const ageStep = 10;
|
||||||
|
|
||||||
const minAge = 0;
|
const minAge = 0;
|
||||||
@@ -376,7 +376,7 @@ export class Standards {
|
|||||||
if (base == null) continue;
|
if (base == null) continue;
|
||||||
|
|
||||||
const ageModifier =
|
const ageModifier =
|
||||||
this.params.activity[activity].ageModifier;
|
cfg.params.activity[activity].ageModifier;
|
||||||
|
|
||||||
const leftSpan = peakAge - minAge;
|
const leftSpan = peakAge - minAge;
|
||||||
const rightSpan = maxAge - peakAge;
|
const rightSpan = maxAge - peakAge;
|
||||||
@@ -464,7 +464,7 @@ export class Standards {
|
|||||||
continue;
|
continue;
|
||||||
|
|
||||||
const scalingExponent =
|
const scalingExponent =
|
||||||
this.params.activity[activity]
|
cfg.params.activity[activity]
|
||||||
.weightModifier;
|
.weightModifier;
|
||||||
const coefficient =
|
const coefficient =
|
||||||
currWeight / referenceWeight;
|
currWeight / referenceWeight;
|
||||||
|
|||||||
@@ -67,3 +67,9 @@ export const StandardsParamsSchema = t.Object({
|
|||||||
}))
|
}))
|
||||||
});
|
});
|
||||||
export type StandardsParams = Static<typeof StandardsParamsSchema>;
|
export type StandardsParams = Static<typeof StandardsParamsSchema>;
|
||||||
|
|
||||||
|
export const StandardsConfigSchema = t.Object({
|
||||||
|
data: StandardsDataSchema,
|
||||||
|
params: StandardsParamsSchema
|
||||||
|
});
|
||||||
|
export type StandardsConfig = Static<typeof StandardsConfigSchema>;
|
||||||
|
|||||||
Reference in New Issue
Block a user