More models organization
This commit is contained in:
@@ -5,17 +5,16 @@ import {
|
||||
kgToLb,
|
||||
lbToKg,
|
||||
range,
|
||||
MetricsSchema,
|
||||
type Metrics,
|
||||
type ActivityPerformance,
|
||||
type Player,
|
||||
} from "@blade-and-brawn/domain";
|
||||
import { levenbergMarquardt as LM } from "ml-levenberg-marquardt";
|
||||
import { Type, type Static } from "@sinclair/typebox";
|
||||
import defaultConfig from "./default-config.json" with { type: "json" };
|
||||
import defaultStandardsDataset from "./data/default-standards.json" with { type: "json" };
|
||||
import defaultStandardsData from "./data/default-standards.json" with { type: "json" };
|
||||
import { getAvgWeight } from "./avg-weights";
|
||||
import { Value } from "@sinclair/typebox/value";
|
||||
import { StandardsParamsSchema, type LevelCalculatorOutput, type Levels, type NumberMetric, type Standard, type StandardsData, type StandardsParams } from "./models";
|
||||
|
||||
// SOURCES
|
||||
// Squat, Bench, Dead Lift:
|
||||
@@ -27,42 +26,7 @@ import { Value } from "@sinclair/typebox/value";
|
||||
// 3 Cone drill:
|
||||
// https://nflsavant.com/combine.php?utm_source=chatgpt.com
|
||||
|
||||
export type LevelCalculatorOutput = {
|
||||
player: number;
|
||||
attributes: Record<Attribute, number>;
|
||||
};
|
||||
|
||||
export const LevelsSchema = Type.Record(Type.String(), Type.Number());
|
||||
export type Levels = Static<typeof LevelsSchema>;
|
||||
|
||||
type NumberMetric = Extract<keyof Metrics, "age" | "weight">;
|
||||
|
||||
export const StandardSchema = Type.Object({
|
||||
metrics: MetricsSchema,
|
||||
levels: LevelsSchema,
|
||||
});
|
||||
export type Standard = Static<typeof StandardSchema>;
|
||||
|
||||
const GeneratorSchema = Type.Object({
|
||||
metric: Type.Union([Type.Literal("age"), Type.Literal("weight")]),
|
||||
});
|
||||
type Generator = Static<typeof GeneratorSchema>;
|
||||
|
||||
const StandardUnitSchema = Type.Union([Type.Literal("ms"), Type.Literal("cm"), Type.Literal("kg")]);
|
||||
|
||||
export const StandardsDatasetSchema = Type.Record(
|
||||
Type.Enum(Activity),
|
||||
Type.Object({
|
||||
metadata: Type.Object({
|
||||
attribute: Type.Enum(Attribute),
|
||||
generators: Type.Array(GeneratorSchema),
|
||||
unit: StandardUnitSchema,
|
||||
name: Type.String(),
|
||||
}),
|
||||
standards: Type.Array(StandardSchema),
|
||||
}),
|
||||
);
|
||||
export type StandardsDataset = Static<typeof StandardsDatasetSchema>;
|
||||
export * from "./models";
|
||||
|
||||
const metricPriority = (m: NumberMetric) => {
|
||||
if (m === "age") return 0;
|
||||
@@ -70,7 +34,7 @@ const metricPriority = (m: NumberMetric) => {
|
||||
return 2;
|
||||
};
|
||||
|
||||
export const DEFAULT_STANDARDS_DATASET = defaultStandardsDataset as StandardsDataset;
|
||||
export const DEFAULT_STANDARDS_DATA = defaultStandardsData as StandardsData;
|
||||
|
||||
export class LevelCalculator {
|
||||
Standards: Standards;
|
||||
@@ -219,38 +183,16 @@ export class LevelCalculator {
|
||||
}
|
||||
}
|
||||
|
||||
const StandardsActivityParametersSchema = 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 StandardsParametersSchema = Type.Object({
|
||||
global: Type.Object({
|
||||
maxLevel: Type.Number(),
|
||||
}),
|
||||
activity: Type.Record(Type.Enum(Activity), StandardsActivityParametersSchema),
|
||||
});
|
||||
|
||||
export type StandardsParameters = Static<typeof StandardsParametersSchema>;
|
||||
|
||||
export class Standards {
|
||||
readonly params: StandardsParameters;
|
||||
private standardsData: StandardsDataset;
|
||||
readonly params: StandardsParams;
|
||||
private data: StandardsData;
|
||||
|
||||
constructor(dataset: StandardsDataset, params?: Partial<StandardsParameters>) {
|
||||
Value.Assert(StandardsParametersSchema, defaultConfig);
|
||||
constructor(data: StandardsData, params?: Partial<StandardsParams>) {
|
||||
Value.Assert(StandardsParamsSchema, defaultConfig);
|
||||
this.params = Object.assign({}, defaultConfig, params);
|
||||
|
||||
// prepare data
|
||||
this.standardsData = structuredClone(dataset);
|
||||
this.data = structuredClone(data);
|
||||
|
||||
// STRETCH
|
||||
for (const activity of Object.values(Activity)) {
|
||||
@@ -265,7 +207,7 @@ export class Standards {
|
||||
}
|
||||
|
||||
for (const gender of Object.values(Gender)) {
|
||||
const standardsByGender = this.standardsData[
|
||||
const standardsByGender = this.data[
|
||||
activity
|
||||
].standards.filter((s) => s["metrics"].gender === gender);
|
||||
const ages = [
|
||||
@@ -363,7 +305,7 @@ export class Standards {
|
||||
|
||||
// EXPAND, COMPRESS, SKEW
|
||||
for (const activity of Object.values(Activity)) {
|
||||
for (const standard of this.standardsData[activity].standards) {
|
||||
for (const standard of this.data[activity].standards) {
|
||||
// expand
|
||||
let i = 1;
|
||||
while (
|
||||
@@ -394,7 +336,7 @@ export class Standards {
|
||||
|
||||
// generate data
|
||||
const allGenerators = this.params.activity[activity].enableGeneration
|
||||
? this.standardsData[activity].metadata.generators.sort(
|
||||
? this.data[activity].metadata.generators.sort(
|
||||
(a, b) =>
|
||||
metricPriority(a.metric) - metricPriority(b.metric),
|
||||
)
|
||||
@@ -472,7 +414,7 @@ export class Standards {
|
||||
.byMetrics(newStandard.metrics)
|
||||
.getOne();
|
||||
if (!overlappingStandard)
|
||||
this.standardsData[activity].standards.push(
|
||||
this.data[activity].standards.push(
|
||||
newStandard,
|
||||
);
|
||||
|
||||
@@ -481,7 +423,7 @@ export class Standards {
|
||||
}
|
||||
|
||||
// now that we generated metric data, remove the old data
|
||||
this.standardsData[activity].standards = this.standardsData[
|
||||
this.data[activity].standards = this.data[
|
||||
activity
|
||||
].standards.filter((s) => s.metrics.age != null);
|
||||
} else if (generator.metric === "weight") {
|
||||
@@ -538,7 +480,7 @@ export class Standards {
|
||||
.byMetrics(newStandard.metrics)
|
||||
.getOne();
|
||||
if (!overlappingStandard)
|
||||
this.standardsData[activity].standards.push(
|
||||
this.data[activity].standards.push(
|
||||
newStandard,
|
||||
);
|
||||
|
||||
@@ -548,13 +490,13 @@ export class Standards {
|
||||
}
|
||||
|
||||
// now that we generated metric data, remove the old data
|
||||
this.standardsData[activity].standards = this.standardsData[
|
||||
this.data[activity].standards = this.data[
|
||||
activity
|
||||
].standards.filter((s) => s.metrics.weight);
|
||||
}
|
||||
}
|
||||
|
||||
this.standardsData[activity].standards = this.standardsData[
|
||||
this.data[activity].standards = this.data[
|
||||
activity
|
||||
].standards.sort((a, b) => {
|
||||
const genderOrder: Record<Gender, number> = {
|
||||
@@ -581,7 +523,7 @@ export class Standards {
|
||||
exact: {
|
||||
// these will get EXACT matches
|
||||
getAll(): Standard[] {
|
||||
const src = self.standardsData[activity].standards;
|
||||
const src = self.data[activity].standards;
|
||||
|
||||
const out: Standard[] = [];
|
||||
for (let i = 0; i < src.length; i++) {
|
||||
@@ -733,7 +675,7 @@ export class Standards {
|
||||
return interpolateByAgeAndWeight(metrics);
|
||||
};
|
||||
|
||||
const getMetadata = () => this.standardsData[activity].metadata;
|
||||
const getMetadata = () => this.data[activity].metadata;
|
||||
|
||||
// QUERY METHODS
|
||||
|
||||
@@ -768,10 +710,10 @@ export class Standards {
|
||||
|
||||
public getAttributeActivities(attribute: Attribute): Activity[] {
|
||||
const activities: Activity[] = [];
|
||||
for (const activity of Object.keys(this.standardsData) as Activity[]) {
|
||||
const standardData = this.standardsData[
|
||||
for (const activity of Object.keys(this.data) as Activity[]) {
|
||||
const standardData = this.data[
|
||||
activity
|
||||
] as StandardsDataset[Activity];
|
||||
] as StandardsData[Activity];
|
||||
if (standardData.metadata.attribute === attribute) {
|
||||
activities.push(activity as Activity);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
import {
|
||||
Activity,
|
||||
Attribute,
|
||||
MetricsSchema,
|
||||
type Metrics,
|
||||
} from "@blade-and-brawn/domain";
|
||||
import { Type as t, type Static } from "@sinclair/typebox";
|
||||
|
||||
// ================
|
||||
// CALCULATOR
|
||||
// ================
|
||||
|
||||
export type LevelCalculatorOutput = {
|
||||
player: number;
|
||||
attributes: Record<Attribute, number>;
|
||||
};
|
||||
|
||||
export type NumberMetric = Extract<keyof Metrics, "age" | "weight">;
|
||||
|
||||
export const LevelsSchema = t.Record(t.String(), t.Number());
|
||||
export type Levels = Static<typeof LevelsSchema>;
|
||||
|
||||
// ================
|
||||
// STANDARDS
|
||||
// ================
|
||||
|
||||
export const StandardSchema = t.Object({
|
||||
metrics: MetricsSchema,
|
||||
levels: LevelsSchema,
|
||||
});
|
||||
export type Standard = Static<typeof StandardSchema>;
|
||||
|
||||
export const GeneratorSchema = t.Object({
|
||||
metric: t.Union([t.Literal("age"), t.Literal("weight")]),
|
||||
});
|
||||
export type Generator = Static<typeof GeneratorSchema>;
|
||||
|
||||
export const StandardsDataSchema = t.Record(
|
||||
t.Enum(Activity),
|
||||
t.Object({
|
||||
metadata: t.Object({
|
||||
attribute: t.Enum(Attribute),
|
||||
generators: t.Array(GeneratorSchema),
|
||||
unit: t.Union([t.Literal("ms"), t.Literal("cm"), t.Literal("kg")]),
|
||||
name: t.String(),
|
||||
}),
|
||||
standards: t.Array(StandardSchema),
|
||||
}),
|
||||
);
|
||||
export type StandardsData = Static<typeof StandardsDataSchema>;
|
||||
|
||||
export const StandardsParamsSchema = t.Object({
|
||||
global: t.Object({
|
||||
maxLevel: t.Number(),
|
||||
}),
|
||||
activity: t.Record(t.Enum(Activity), t.Object({
|
||||
weightModifier: t.Number(),
|
||||
weightSkew: t.Number(),
|
||||
ageModifier: t.Number(),
|
||||
enableGeneration: t.Boolean(),
|
||||
difficultyModifier: t.Number(),
|
||||
peakAge: t.Number(),
|
||||
stretch: t.Object({
|
||||
upper: t.Number(),
|
||||
lower: t.Number(),
|
||||
}),
|
||||
}))
|
||||
});
|
||||
export type StandardsParams = Static<typeof StandardsParamsSchema>;
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Type } from "@sinclair/typebox";
|
||||
import { Type as t } from "@sinclair/typebox";
|
||||
import type { Static } from "@sinclair/typebox";
|
||||
|
||||
export enum Attribute {
|
||||
@@ -22,24 +22,23 @@ export enum Gender {
|
||||
Female = "Female",
|
||||
}
|
||||
|
||||
export const MetricsSchema = Type.Object({
|
||||
age: Type.Number(),
|
||||
weight: Type.Number(),
|
||||
gender: Type.Enum(Gender),
|
||||
});
|
||||
|
||||
export const PlayerSchema = Type.Object({
|
||||
name: Type.Optional(Type.String()),
|
||||
metrics: MetricsSchema,
|
||||
});
|
||||
|
||||
export const ActivityPerformanceSchema = Type.Object({
|
||||
activity: Type.Enum(Activity),
|
||||
performance: Type.Number(),
|
||||
});
|
||||
|
||||
export type StandardUnit = "ms" | "cm" | "kg";
|
||||
|
||||
export const MetricsSchema = t.Object({
|
||||
age: t.Number(),
|
||||
weight: t.Number(),
|
||||
gender: t.Enum(Gender),
|
||||
});
|
||||
export type Metrics = Static<typeof MetricsSchema>;
|
||||
|
||||
export const PlayerSchema = t.Object({
|
||||
name: t.Optional(t.String()),
|
||||
metrics: MetricsSchema,
|
||||
});
|
||||
export type Player = Static<typeof PlayerSchema>;
|
||||
|
||||
export const ActivityPerformanceSchema = t.Object({
|
||||
activity: t.Enum(Activity),
|
||||
performance: t.Number(),
|
||||
});
|
||||
export type ActivityPerformance = Static<typeof ActivityPerformanceSchema>;
|
||||
|
||||
Reference in New Issue
Block a user