diff --git a/calculator/calc.ts b/calculator/calc.ts index 1f5253f..4c8e4c7 100644 --- a/calculator/calc.ts +++ b/calculator/calc.ts @@ -15,6 +15,11 @@ type LevelCalculatorConfig = { expandIters?: number; } +type LevelCalculatorOutput = { + player: number, + attributes: Record +} + export class LevelCalculator { cfg: Required; standards: Standards; @@ -28,26 +33,38 @@ export class LevelCalculator { } // ------------------------------------------------------------------------------------------------- - // Level calculations (public API) + // Level calculations // ------------------------------------------------------------------------------------------------- - public calculatePlayerLevel(player: Player, activityPerformances: ActivityPerformance[]): number { + public calculate(player: Player, activityPerformances: ActivityPerformance[]): LevelCalculatorOutput { + const levels: LevelCalculatorOutput = { + player: 0, + attributes: { + [attributes.STRENGTH]: 0, + [attributes.POWER]: 0, + [attributes.ENDURANCE]: 0, + [attributes.AGILITY]: 0, + } as const + }; + for (const value of Object.values(player)) { if (!value) - return 0; + return levels; } - const attrLevels = this.calculateAllAttributeLevels(player, activityPerformances); - for (const level of Object.values(attrLevels)) { + levels.attributes = this.calculateAllAttributeLevels(player, activityPerformances); + for (const level of Object.values(levels.attributes)) { if (!level) - return 0; + return levels; } - const attrLevelsSum = Object.values(attrLevels).reduce((sum, lvl) => sum + lvl, 0); - return Math.round(attrLevelsSum / Object.keys(attributes).length); + const attrLevelsSum = Object.values(levels.attributes).reduce((sum, lvl) => sum + lvl, 0); + levels.player = Math.round(attrLevelsSum / Object.keys(attributes).length); + + return levels; } - public calculateAllAttributeLevels( + private calculateAllAttributeLevels( player: Player, activityPerformances: ActivityPerformance[], ): Record { @@ -55,7 +72,6 @@ export class LevelCalculator { [attributes.STRENGTH]: 0, [attributes.POWER]: 0, [attributes.ENDURANCE]: 0, - // [attributes.SPEED]: 0, [attributes.AGILITY]: 0, } as const; @@ -75,7 +91,7 @@ export class LevelCalculator { } - public calculateAttributeLevel( + private calculateAttributeLevel( attribute: Attribute, player: Player, activityPerformances: ActivityPerformance[], @@ -123,11 +139,12 @@ export class LevelCalculator { const standard = this.standards.byActivity(activity).getOne(); if (standard.metrics.weight === -1) return standard.levels; - const { upper: upperAgeWeightStandard, lower: lowerAgeWeightStandard } = this.standards + const { lower: lowerAgeWeightStandard, upper: upperAgeWeightStandard } = this.standards .byActivity(activity) .byGender(metrics.gender) .byAge(targetAge) .getNearest("weight", metrics.weight); + const weightLower = lowerAgeWeightStandard.metrics.weight; const weightUpper = upperAgeWeightStandard.metrics.weight; @@ -146,9 +163,13 @@ export class LevelCalculator { const ageLower = lowerAgeStandard.metrics.age; const ageUpper = upperAgeStandard.metrics.age; + console.log(activity); + console.log("AGES: ", ageLower, ageUpper); + // Interpolate by age and weight let ageRatio = ageUpper === ageLower ? 1 : (metrics.age - ageLower) / (ageUpper - ageLower); ageRatio = Math.max(0, Math.min(1, ageRatio)); + const interpolatedLevels = this.interpolateLevels( interpolateByWeight(ageLower), interpolateByWeight(ageUpper), diff --git a/calculator/test.ts b/calculator/test.ts index 0474bbf..7634238 100644 --- a/calculator/test.ts +++ b/calculator/test.ts @@ -32,7 +32,7 @@ const computedPerformances: ActivityPerformance[] = [ // ENDURANCE { activity: activities.RUN, - performance: minToMs(5) + secToMs(20), + performance: minToMs(6) + secToMs(11), }, // AGILITY { @@ -44,12 +44,7 @@ const computedPerformances: ActivityPerformance[] = [ const standards = new Standards(rawStandards as StandardsMap); const levelCalculator = new LevelCalculator(standards); -const levels = { - attributes: levelCalculator.calculateAllAttributeLevels(player, computedPerformances), - player: levelCalculator.calculatePlayerLevel(player, computedPerformances) -}; - -console.log(levels); +console.log(levelCalculator.calculate(player, computedPerformances)); // const newStandards: Standards = { // "Back Squat": [], diff --git a/main.ts b/main.ts index cbe324e..efd1a09 100644 --- a/main.ts +++ b/main.ts @@ -42,10 +42,7 @@ const server = Bun.serve({ const levelCalculator = new LevelCalculator(MAIN_STANDARDS); return ClientResponse.json({ - levels: { - attributes: levelCalculator.calculateAllAttributeLevels(player, activityPerformances), - player: levelCalculator.calculatePlayerLevel(player, activityPerformances) - } + levels: levelCalculator.calculate(player, activityPerformances) }) } },