calculator players
This commit is contained in:
@@ -0,0 +1,292 @@
|
||||
<script lang="ts">
|
||||
import {
|
||||
LevelCalculator,
|
||||
type LevelCalculatorOutput,
|
||||
type Standards,
|
||||
} from "$lib/services/calculator/main";
|
||||
import {
|
||||
Activity,
|
||||
Attribute,
|
||||
Gender,
|
||||
lbToKg,
|
||||
type ActivityPerformance,
|
||||
type Player,
|
||||
} from "$lib/services/calculator/util";
|
||||
|
||||
interface CalcData {
|
||||
levels?: LevelCalculatorOutput;
|
||||
player: Player;
|
||||
activityPerformances: ActivityPerformance[];
|
||||
}
|
||||
|
||||
interface Props {
|
||||
allStandards: Standards;
|
||||
}
|
||||
|
||||
const { allStandards }: Props = $props();
|
||||
|
||||
const levelCalculator = $derived(new LevelCalculator(allStandards));
|
||||
|
||||
let calculations = $state([] as CalcData[]);
|
||||
|
||||
$effect(() => {
|
||||
for (const calculation of calculations) {
|
||||
calculation.levels = levelCalculator.calculate(
|
||||
calculation.player,
|
||||
calculation.activityPerformances,
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
$effect(() => {
|
||||
const savedCalcs = localStorage.getItem("calculations");
|
||||
if (savedCalcs) calculations = JSON.parse(savedCalcs);
|
||||
});
|
||||
|
||||
$effect(() => {
|
||||
localStorage.setItem("calculations", JSON.stringify(calculations));
|
||||
});
|
||||
|
||||
const createCalculation = function (name: string) {
|
||||
return {
|
||||
player: {
|
||||
name: name,
|
||||
metrics: {
|
||||
age: 18,
|
||||
weight: 180,
|
||||
gender: Gender.Male,
|
||||
},
|
||||
},
|
||||
activityPerformances: Object.values(Activity).map((a) => ({
|
||||
activity: a,
|
||||
performance: 0,
|
||||
})),
|
||||
};
|
||||
};
|
||||
|
||||
function downloadObject(obj: unknown, filename = "data.json") {
|
||||
const json = JSON.stringify(obj, null, 2);
|
||||
const blob = new Blob([json], { type: "application/json" });
|
||||
const url = URL.createObjectURL(blob);
|
||||
|
||||
const a = document.createElement("a");
|
||||
a.href = url;
|
||||
a.download = filename;
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
|
||||
a.remove();
|
||||
URL.revokeObjectURL(url);
|
||||
}
|
||||
|
||||
const formatMs = (ms: number) => {
|
||||
const total = Math.floor(ms / 1000);
|
||||
const m = Math.floor(total / 60);
|
||||
const s = total % 60;
|
||||
return `${m}m ${s}s`;
|
||||
};
|
||||
const inchesToFeetInches = (inches: number) => {
|
||||
const ft = Math.floor(inches / 12);
|
||||
const rem = inches - ft * 12;
|
||||
const whole = Math.floor(rem);
|
||||
const isHalf = Math.abs(rem - whole - 0.5) < 1e-9;
|
||||
const inchLabel = isHalf ? `${whole}½` : `${whole}`;
|
||||
return `${ft}' ${inchLabel}"`;
|
||||
};
|
||||
|
||||
const formatSeconds = (ms: number) => (ms / 1000).toFixed(2) + " seconds";
|
||||
|
||||
const performanceOptionsFromActivity = function (activity: Activity) {
|
||||
const options: { name: string; value: string }[] = [];
|
||||
switch (activity) {
|
||||
case Activity.BackSquat:
|
||||
case Activity.Deadlift:
|
||||
case Activity.BenchPress:
|
||||
for (let i = 0; i < 600; ++i) {
|
||||
options.push({
|
||||
name: String(i) + " lb",
|
||||
value: String(lbToKg(i)),
|
||||
});
|
||||
}
|
||||
break;
|
||||
|
||||
case Activity.Run: {
|
||||
const MAX_MIN = 30;
|
||||
for (let ms = 0; ms <= MAX_MIN * 60_000; ms += 1_000) {
|
||||
options.push({ name: formatMs(ms), value: String(ms) });
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case Activity.BroadJump: {
|
||||
const MAX_INCHES = 15 * 12 + 5;
|
||||
for (let halfStep = 0; halfStep <= MAX_INCHES * 2; halfStep++) {
|
||||
const inches = halfStep / 2;
|
||||
options.push({
|
||||
name: inchesToFeetInches(inches),
|
||||
value: (inches * 2.54).toFixed(1),
|
||||
});
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case Activity.ConeDrill: {
|
||||
const MIN_MS = 5_000;
|
||||
const MAX_MS = 20_000;
|
||||
for (let ms = MIN_MS; ms <= MAX_MS; ms += 10) {
|
||||
options.push({
|
||||
name: formatSeconds(ms),
|
||||
value: String(ms),
|
||||
});
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
return options;
|
||||
};
|
||||
</script>
|
||||
|
||||
<div class="mt-5 mb-5 flex w-full">
|
||||
<div class="ml-auto">
|
||||
<button
|
||||
onclick={() =>
|
||||
calculations.push(
|
||||
createCalculation(String(calculations.length)),
|
||||
)}
|
||||
class="btn btn-primary">New</button
|
||||
>
|
||||
<button
|
||||
onclick={() => downloadObject(calculations)}
|
||||
class="btn btn-secondary">Export</button
|
||||
>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<section
|
||||
class="w-full px-3 grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-4"
|
||||
>
|
||||
{#each calculations as calculation, index}
|
||||
<div
|
||||
class="card card-compact bg-base-200 p-4 shadow-lg max-w-sm w-full"
|
||||
>
|
||||
<div class="card-body gap-4 p-4">
|
||||
<input
|
||||
class="input input-bordered input-sm w-full"
|
||||
type="text"
|
||||
bind:value={calculation.player.name}
|
||||
placeholder="Player name"
|
||||
/>
|
||||
|
||||
<ul
|
||||
class="bg-base-100 rounded-box shadow-xs divide-y divide-base-300"
|
||||
>
|
||||
<li class="p-3">
|
||||
<div class="flex items-center justify-between">
|
||||
<div class="font-semibold text-sm">OVERALL</div>
|
||||
<div class="badge badge-neutral badge-xs">
|
||||
{calculation?.levels?.player || "N/A"}
|
||||
</div>
|
||||
</div>
|
||||
</li>
|
||||
|
||||
{#each Object.values(Attribute) as attribute}
|
||||
<li class="p-3">
|
||||
<div class="flex items-center justify-between">
|
||||
<div class="opacity-80 text-sm">
|
||||
{attribute}
|
||||
</div>
|
||||
<div class="badge badge-ghost badge-xs">
|
||||
{calculation?.levels?.attributes?.[
|
||||
attribute
|
||||
] || "N/A"}
|
||||
</div>
|
||||
</div>
|
||||
</li>
|
||||
{/each}
|
||||
</ul>
|
||||
|
||||
<fieldset
|
||||
class="fieldset bg-base-200/60 rounded-lg"
|
||||
onchange={() =>
|
||||
(calculation.levels = levelCalculator.calculate(
|
||||
calculation.player,
|
||||
calculation.activityPerformances,
|
||||
))}
|
||||
>
|
||||
<div class="grid grid-cols-1 md:grid-cols-3 gap-3">
|
||||
<label class="form-control">
|
||||
<span class="label mb-1 text-xs">Gender</span>
|
||||
<select
|
||||
class="select select-bordered select-sm w-full"
|
||||
bind:value={calculation.player.metrics.gender}
|
||||
>
|
||||
{#each Object.values(Gender) as gender}
|
||||
<option value={gender}>{gender}</option>
|
||||
{/each}
|
||||
</select>
|
||||
</label>
|
||||
|
||||
<label class="form-control">
|
||||
<span class="label mb-1 text-xs">Age</span>
|
||||
<input
|
||||
class="input input-bordered input-sm w-full"
|
||||
type="number"
|
||||
min="1"
|
||||
max="100"
|
||||
step="1"
|
||||
bind:value={calculation.player.metrics.age}
|
||||
/>
|
||||
</label>
|
||||
|
||||
<label class="form-control">
|
||||
<span class="label mb-1 text-xs">Weight</span>
|
||||
<input
|
||||
class="input input-bordered input-sm w-full"
|
||||
type="number"
|
||||
min="1"
|
||||
max="500"
|
||||
step="1"
|
||||
bind:value={calculation.player.metrics.weight}
|
||||
/>
|
||||
</label>
|
||||
</div>
|
||||
</fieldset>
|
||||
|
||||
<fieldset
|
||||
class="fieldset bg-base-200/60 rounded-lg"
|
||||
onchange={() =>
|
||||
(calculation.levels = levelCalculator.calculate(
|
||||
calculation.player,
|
||||
calculation.activityPerformances,
|
||||
))}
|
||||
>
|
||||
<div class="grid grid-cols-2 gap-3">
|
||||
{#each calculation.activityPerformances as activityPerformance}
|
||||
<label class="form-control space-y-1">
|
||||
<span class="label mb-1 text-xs">
|
||||
{activityPerformance.activity}
|
||||
</span>
|
||||
<select
|
||||
class="select select-bordered select-sm w-full"
|
||||
bind:value={activityPerformance.performance}
|
||||
>
|
||||
{#each performanceOptionsFromActivity(activityPerformance.activity) as option}
|
||||
<option value={option.value}
|
||||
>{option.name}</option
|
||||
>
|
||||
{/each}
|
||||
</select>
|
||||
</label>
|
||||
{/each}
|
||||
</div>
|
||||
</fieldset>
|
||||
</div>
|
||||
<button
|
||||
onclick={() =>
|
||||
confirm(`Delete ${calculation.player.name}?`) &&
|
||||
calculations.splice(index, 1)}
|
||||
class="btn btn-error btn-sm w-1/4">Delete</button
|
||||
>
|
||||
</div>
|
||||
{/each}
|
||||
</section>
|
||||
@@ -0,0 +1,127 @@
|
||||
<script lang="ts">
|
||||
import type {
|
||||
Metrics,
|
||||
Standard,
|
||||
Standards,
|
||||
StandardsConfig,
|
||||
} from "$lib/services/calculator/main";
|
||||
import {
|
||||
Activity,
|
||||
cmToIn,
|
||||
kgToLb,
|
||||
msToTime,
|
||||
range,
|
||||
} from "$lib/services/calculator/util";
|
||||
|
||||
interface Props {
|
||||
allStandards: Standards;
|
||||
selected: {
|
||||
activity: Activity;
|
||||
metrics: Metrics;
|
||||
cfg: StandardsConfig;
|
||||
};
|
||||
}
|
||||
|
||||
const { selected, allStandards }: Props = $props();
|
||||
|
||||
const getLvlValue = (
|
||||
activity: Activity,
|
||||
standard: Standard,
|
||||
lvl: number,
|
||||
): string => {
|
||||
const rawValue = standard.levels[lvl];
|
||||
const unit = allStandards.byActivity(activity).getMetadata().unit;
|
||||
switch (unit) {
|
||||
case "ms":
|
||||
return msToTime(rawValue, activity === Activity.ConeDrill);
|
||||
case "cm": {
|
||||
return String(Math.round(cmToIn(rawValue) * 10) / 10);
|
||||
}
|
||||
case "kg":
|
||||
return String(Math.round(kgToLb(rawValue)));
|
||||
default:
|
||||
return "";
|
||||
}
|
||||
};
|
||||
</script>
|
||||
|
||||
<section class="w-full">
|
||||
<div>
|
||||
<div class="flex items-center">
|
||||
<p class="text-xs label">
|
||||
{#if !selected.cfg.disableGeneration && allStandards
|
||||
.byActivity(selected.activity)
|
||||
.getMetadata().generators.length}
|
||||
(Generated data: {allStandards
|
||||
.byActivity(selected.activity)
|
||||
.getMetadata()
|
||||
.generators.map((g) => g.metric)
|
||||
.join(", ")})
|
||||
{:else}
|
||||
(Generated data: NONE)
|
||||
{/if}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{#if !selected.metrics.age}
|
||||
{#each allStandards.agesFor(selected.activity, selected.metrics.gender) as age}
|
||||
{@render standardsTable(selected.activity, {
|
||||
...selected.metrics,
|
||||
age,
|
||||
})}
|
||||
{/each}
|
||||
{:else}
|
||||
{@render standardsTable(selected.activity, selected.metrics)}
|
||||
{/if}
|
||||
</section>
|
||||
|
||||
{#snippet standardsTable(activity: Activity, metrics: Metrics)}
|
||||
{#snippet standardsTableRow(standard: Standard)}
|
||||
<tr class="hover:bg-base-300">
|
||||
<th>
|
||||
{standard.metrics.weight
|
||||
? parseFloat(kgToLb(standard.metrics.weight).toFixed(2))
|
||||
: "None"}</th
|
||||
>
|
||||
{#each range(selected.cfg.maxLevel) as lvl}
|
||||
<td>{getLvlValue(activity, standard, lvl)}</td>
|
||||
{/each}
|
||||
</tr>
|
||||
{/snippet}
|
||||
<div class="mt-5">
|
||||
<h3 class="text-lg italic font-semibold">{metrics.age}</h3>
|
||||
<div class="overflow-x-auto">
|
||||
<table class="table table-zebra table-pin-cols">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Body weight</th>
|
||||
{#each range(selected.cfg.maxLevel) as lvl}
|
||||
<td>{lvl}</td>
|
||||
{/each}
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{#if metrics.weight}
|
||||
{@const standard = allStandards
|
||||
.byActivity(activity)
|
||||
.byGender(metrics.gender)
|
||||
.byAge(metrics.age)
|
||||
.byWeight(metrics.weight)
|
||||
.getOneInterpolated()}
|
||||
{@render standardsTableRow(standard)}
|
||||
{:else}
|
||||
{@const standards = allStandards
|
||||
.byActivity(activity)
|
||||
.byGender(metrics.gender)
|
||||
.byAge(metrics.age)
|
||||
.getAllInterpolated({ normalizeForLb: true })}
|
||||
{#each standards as standard}
|
||||
{@render standardsTableRow(standard)}
|
||||
{/each}
|
||||
{/if}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
{/snippet}
|
||||
@@ -15,7 +15,7 @@ type LevelCalculatorConfig = {
|
||||
compressTo?: number;
|
||||
}
|
||||
|
||||
type LevelCalculatorOutput = {
|
||||
export type LevelCalculatorOutput = {
|
||||
player: number,
|
||||
attributes: Record<Attribute, number>
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import avgWeightDataRaw from "$lib/data/avg-weights.json"
|
||||
// -------------------------------------------------------------------------------------------------
|
||||
|
||||
export type Player = {
|
||||
name?: string;
|
||||
metrics: Metrics;
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
import "../app.css";
|
||||
|
||||
const links = [
|
||||
{ name: "Standards", path: "/standards" },
|
||||
{ name: "Calculator", path: "/calculator" },
|
||||
{ name: "Commerce", path: "/commerce" },
|
||||
];
|
||||
|
||||
|
||||
@@ -2,5 +2,5 @@ import { redirect } from '@sveltejs/kit';
|
||||
import type { PageServerLoad } from './$types';
|
||||
|
||||
export const load: PageServerLoad = ({ }) => {
|
||||
redirect(308, '/standards');
|
||||
redirect(308, '/calculator');
|
||||
};
|
||||
|
||||
@@ -0,0 +1,242 @@
|
||||
<script lang="ts">
|
||||
import {
|
||||
Standards,
|
||||
type ActivityStandards,
|
||||
type Metrics,
|
||||
type StandardsConfig,
|
||||
} from "$lib/services/calculator/main";
|
||||
import allStandardsRaw from "$lib/data/standards.json" assert { type: "json" };
|
||||
import { Activity, Gender, lbToKg } from "$lib/services/calculator/util";
|
||||
import StandardsTable from "$lib/components/StandardsTable.svelte";
|
||||
import PlayersTable from "$lib/components/PlayersTable.svelte";
|
||||
|
||||
let activeTab = $state("standards");
|
||||
|
||||
let input = $state({
|
||||
activity: Activity.BenchPress,
|
||||
metrics: {
|
||||
gender: Gender.Male,
|
||||
age: "",
|
||||
weight: "",
|
||||
},
|
||||
cfg: {
|
||||
enableGeneration: true,
|
||||
maxLevel: "5",
|
||||
weightModfier: ".1",
|
||||
weightSkew: ".4",
|
||||
ageModifier: ".1",
|
||||
},
|
||||
});
|
||||
|
||||
const selected = $derived({
|
||||
activity: input.activity,
|
||||
metrics: {
|
||||
gender: input.metrics.gender,
|
||||
age: +input.metrics.age,
|
||||
weight: lbToKg(+input.metrics.weight),
|
||||
} as Metrics,
|
||||
cfg: {
|
||||
maxLevel: +input.cfg.maxLevel,
|
||||
weightModifier: +input.cfg.weightModfier,
|
||||
weightSkew: +input.cfg.weightSkew,
|
||||
ageModifier: +input.cfg.ageModifier,
|
||||
disableGeneration: !input.cfg.enableGeneration,
|
||||
} as StandardsConfig,
|
||||
});
|
||||
|
||||
const allStandards = $derived(
|
||||
new Standards(allStandardsRaw as ActivityStandards, selected.cfg),
|
||||
);
|
||||
|
||||
$effect(() => {
|
||||
console.log(selected);
|
||||
if (!input.metrics.age) input.metrics.weight = "";
|
||||
});
|
||||
|
||||
$effect(() => {
|
||||
const savedParameters = localStorage.getItem("parameters");
|
||||
if (savedParameters) input = JSON.parse(savedParameters);
|
||||
});
|
||||
|
||||
$effect(() => {
|
||||
localStorage.setItem("parameters", JSON.stringify(input));
|
||||
});
|
||||
</script>
|
||||
|
||||
<section class="w-full flex justify-between gap-2 pt-10 items-center">
|
||||
{#if activeTab === "standards"}
|
||||
<fieldset class="fieldset bg-base-200 p-6 max-w-lg">
|
||||
<legend class="fieldset-legend text-lg font-semibold">
|
||||
Metrics
|
||||
</legend>
|
||||
<div class="grid grid-cols-1 md:grid-cols-2 gap-4">
|
||||
<label>
|
||||
<span class="label mb-1">Activity</span>
|
||||
<select
|
||||
class="select w-full"
|
||||
name="activities"
|
||||
bind:value={input.activity}
|
||||
>
|
||||
{#each Object.values(Activity) as activity}
|
||||
<option value={activity}>
|
||||
{allStandards.byActivity(activity).getMetadata()
|
||||
.name}
|
||||
</option>
|
||||
{/each}
|
||||
</select>
|
||||
</label>
|
||||
|
||||
<label>
|
||||
<span class="label mb-1">Gender</span>
|
||||
<select
|
||||
class="select w-full"
|
||||
name="genders"
|
||||
bind:value={input.metrics.gender}
|
||||
>
|
||||
{#each Object.values(Gender) as gender}
|
||||
<option value={gender}>{gender}</option>
|
||||
{/each}
|
||||
</select>
|
||||
</label>
|
||||
|
||||
<label>
|
||||
<span class="label mb-1">Age</span>
|
||||
<input
|
||||
class="input w-full"
|
||||
type="number"
|
||||
min="0"
|
||||
max="100"
|
||||
placeholder="18"
|
||||
bind:value={input.metrics.age}
|
||||
/>
|
||||
</label>
|
||||
|
||||
<label>
|
||||
<span class="label mb-1">Weight (lb)</span>
|
||||
<input
|
||||
class="input w-full"
|
||||
type="number"
|
||||
min="0"
|
||||
max="600"
|
||||
placeholder={selected.metrics.age
|
||||
? "170"
|
||||
: "Requires age"}
|
||||
bind:value={input.metrics.weight}
|
||||
disabled={!selected.metrics.age}
|
||||
/>
|
||||
</label>
|
||||
</div>
|
||||
</fieldset>
|
||||
{/if}
|
||||
|
||||
<fieldset class="fieldset bg-base-200 p-6 max-w-lg">
|
||||
<legend class="fieldset-legend text-lg font-semibold"> Data </legend>
|
||||
|
||||
<div class="flex gap-4">
|
||||
<fieldset class="fieldset bg-base-300 p-3 max-w-xl">
|
||||
<legend class="fieldset-legend text-sm font-semibold">
|
||||
General
|
||||
</legend>
|
||||
|
||||
<div class="grid grid-cols-1 gap-4">
|
||||
<label>
|
||||
<span class="label mb-1">Max level</span>
|
||||
<input
|
||||
class="input w-full"
|
||||
type="number"
|
||||
min="1"
|
||||
max="100"
|
||||
placeholder="5"
|
||||
bind:value={input.cfg.maxLevel}
|
||||
/>
|
||||
</label>
|
||||
</div>
|
||||
</fieldset>
|
||||
|
||||
<fieldset class="fieldset bg-base-300 p-3 max-w-3xs">
|
||||
<legend class="fieldset-legend text-sm font-semibold">
|
||||
Data Generation
|
||||
</legend>
|
||||
|
||||
<div class="grid grid-cols-1 md:grid-cols-2 gap-4">
|
||||
<label class="label">
|
||||
<input
|
||||
type="checkbox"
|
||||
bind:checked={input.cfg.enableGeneration}
|
||||
class="toggle toggle-lg m-auto"
|
||||
/>
|
||||
</label>
|
||||
<label>
|
||||
<span class="label mb-1">Weight modifier</span>
|
||||
<input
|
||||
class="input w-full"
|
||||
type="number"
|
||||
min="0"
|
||||
max="1"
|
||||
step=".05"
|
||||
placeholder=".1"
|
||||
bind:value={input.cfg.weightModfier}
|
||||
disabled={selected.cfg.disableGeneration}
|
||||
/>
|
||||
</label>
|
||||
<label>
|
||||
<span class="label mb-1">Weight skew</span>
|
||||
<input
|
||||
class="input w-full"
|
||||
type="number"
|
||||
min="0"
|
||||
max="1"
|
||||
step=".05"
|
||||
placeholder=".1"
|
||||
bind:value={input.cfg.weightSkew}
|
||||
disabled={selected.cfg.disableGeneration}
|
||||
/>
|
||||
</label>
|
||||
<label>
|
||||
<span class="label mb-1">Age modifier</span>
|
||||
<input
|
||||
class="input w-full"
|
||||
type="number"
|
||||
min="0"
|
||||
max="1"
|
||||
step=".05"
|
||||
placeholder=".1"
|
||||
bind:value={input.cfg.ageModifier}
|
||||
disabled={selected.cfg.disableGeneration}
|
||||
/>
|
||||
</label>
|
||||
</div>
|
||||
</fieldset>
|
||||
</div>
|
||||
</fieldset>
|
||||
</section>
|
||||
|
||||
<div class="divider"></div>
|
||||
|
||||
<div role="tablist" class="tabs tabs-border">
|
||||
<button
|
||||
role="tab"
|
||||
class="tab"
|
||||
class:tab-active={activeTab === "standards"}
|
||||
onclick={() => (activeTab = "standards")}
|
||||
>
|
||||
Standards
|
||||
</button>
|
||||
|
||||
<button
|
||||
role="tab"
|
||||
class="tab"
|
||||
class:tab-active={activeTab === "players"}
|
||||
onclick={() => (activeTab = "players")}
|
||||
>
|
||||
Players
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{#if activeTab === "standards"}
|
||||
<StandardsTable {selected} {allStandards} />
|
||||
{/if}
|
||||
|
||||
{#if activeTab === "players"}
|
||||
<PlayersTable {allStandards} />
|
||||
{/if}
|
||||
@@ -1,307 +0,0 @@
|
||||
<script lang="ts">
|
||||
import {
|
||||
Standards,
|
||||
type ActivityStandards,
|
||||
type Metrics,
|
||||
type Standard,
|
||||
type StandardsConfig,
|
||||
} from "$lib/services/calculator/main";
|
||||
import allStandardsRaw from "$lib/data/standards.json" assert { type: "json" };
|
||||
import {
|
||||
Activity,
|
||||
cmToIn,
|
||||
Gender,
|
||||
kgToLb,
|
||||
lbToKg,
|
||||
msToTime,
|
||||
range,
|
||||
} from "$lib/services/calculator/util";
|
||||
|
||||
let input = $state({
|
||||
activity: Activity.BenchPress,
|
||||
metrics: {
|
||||
gender: Gender.Male,
|
||||
age: "",
|
||||
weight: "",
|
||||
},
|
||||
cfg: {
|
||||
enableGeneration: true,
|
||||
maxLevel: "5",
|
||||
weightModfier: ".1",
|
||||
weightSkew: ".4",
|
||||
ageModifier: ".1",
|
||||
},
|
||||
});
|
||||
|
||||
const selected = $derived({
|
||||
activity: input.activity,
|
||||
metrics: {
|
||||
gender: input.metrics.gender,
|
||||
age: +input.metrics.age,
|
||||
weight: lbToKg(+input.metrics.weight),
|
||||
} as Metrics,
|
||||
cfg: {
|
||||
maxLevel: +input.cfg.maxLevel,
|
||||
weightModifier: +input.cfg.weightModfier,
|
||||
weightSkew: +input.cfg.weightSkew,
|
||||
ageModifier: +input.cfg.ageModifier,
|
||||
disableGeneration: !input.cfg.enableGeneration,
|
||||
} as StandardsConfig,
|
||||
});
|
||||
|
||||
$effect(() => {
|
||||
console.log(selected);
|
||||
if (!input.metrics.age) input.metrics.weight = "";
|
||||
});
|
||||
|
||||
const allStandards = $derived(
|
||||
new Standards(allStandardsRaw as ActivityStandards, selected.cfg),
|
||||
);
|
||||
|
||||
const getLvlValue = (
|
||||
activity: Activity,
|
||||
standard: Standard,
|
||||
lvl: number,
|
||||
): string => {
|
||||
const rawValue = standard.levels[lvl];
|
||||
const unit = allStandards.byActivity(activity).getMetadata().unit;
|
||||
switch (unit) {
|
||||
case "ms":
|
||||
return msToTime(rawValue, activity === Activity.ConeDrill);
|
||||
case "cm": {
|
||||
return String(Math.round(cmToIn(rawValue) * 10) / 10);
|
||||
}
|
||||
case "kg":
|
||||
return String(Math.round(kgToLb(rawValue)));
|
||||
default:
|
||||
return "";
|
||||
}
|
||||
};
|
||||
</script>
|
||||
|
||||
<section class="w-full flex justify-between gap-2 pt-10 items-center">
|
||||
<fieldset class="fieldset bg-base-200 p-6 max-w-lg">
|
||||
<legend class="fieldset-legend text-lg font-semibold"> Metrics </legend>
|
||||
<div class="grid grid-cols-1 md:grid-cols-2 gap-4">
|
||||
<label>
|
||||
<span class="label mb-1">Activity</span>
|
||||
<select
|
||||
class="select w-full"
|
||||
name="activities"
|
||||
bind:value={input.activity}
|
||||
>
|
||||
{#each Object.values(Activity) as activity}
|
||||
<option value={activity}>
|
||||
{allStandards.byActivity(activity).getMetadata()
|
||||
.name}
|
||||
</option>
|
||||
{/each}
|
||||
</select>
|
||||
</label>
|
||||
|
||||
<label>
|
||||
<span class="label mb-1">Gender</span>
|
||||
<select
|
||||
class="select w-full"
|
||||
name="genders"
|
||||
bind:value={input.metrics.gender}
|
||||
>
|
||||
{#each Object.values(Gender) as gender}
|
||||
<option value={gender}>{gender}</option>
|
||||
{/each}
|
||||
</select>
|
||||
</label>
|
||||
|
||||
<label>
|
||||
<span class="label mb-1">Age</span>
|
||||
<input
|
||||
class="input w-full"
|
||||
type="number"
|
||||
min="0"
|
||||
max="100"
|
||||
placeholder="18"
|
||||
bind:value={input.metrics.age}
|
||||
/>
|
||||
</label>
|
||||
|
||||
<label>
|
||||
<span class="label mb-1">Weight (lb)</span>
|
||||
<input
|
||||
class="input w-full"
|
||||
type="number"
|
||||
min="0"
|
||||
max="600"
|
||||
placeholder={selected.metrics.age ? "170" : "Requires age"}
|
||||
bind:value={input.metrics.weight}
|
||||
disabled={!selected.metrics.age}
|
||||
/>
|
||||
</label>
|
||||
</div>
|
||||
</fieldset>
|
||||
|
||||
<fieldset class="fieldset bg-base-200 p-6 max-w-lg">
|
||||
<legend class="fieldset-legend text-lg font-semibold">
|
||||
Parameters
|
||||
</legend>
|
||||
|
||||
<div class="flex gap-4">
|
||||
<fieldset class="fieldset bg-base-300 p-3 max-w-xl">
|
||||
<legend class="fieldset-legend text-sm font-semibold">
|
||||
General
|
||||
</legend>
|
||||
|
||||
<div class="grid grid-cols-1 gap-4">
|
||||
<label>
|
||||
<span class="label mb-1">Max level</span>
|
||||
<input
|
||||
class="input w-full"
|
||||
type="number"
|
||||
min="1"
|
||||
max="100"
|
||||
placeholder="5"
|
||||
bind:value={input.cfg.maxLevel}
|
||||
/>
|
||||
</label>
|
||||
</div>
|
||||
</fieldset>
|
||||
|
||||
<fieldset class="fieldset bg-base-300 p-3 max-w-3xs">
|
||||
<legend class="fieldset-legend text-sm font-semibold">
|
||||
Data Generation
|
||||
</legend>
|
||||
|
||||
<div class="grid grid-cols-1 md:grid-cols-2 gap-4">
|
||||
<label class="label">
|
||||
<input
|
||||
type="checkbox"
|
||||
bind:checked={input.cfg.enableGeneration}
|
||||
class="toggle toggle-lg m-auto"
|
||||
/>
|
||||
</label>
|
||||
<label>
|
||||
<span class="label mb-1">Weight modifier</span>
|
||||
<input
|
||||
class="input w-full"
|
||||
type="number"
|
||||
min="0"
|
||||
max="1"
|
||||
step=".05"
|
||||
placeholder=".1"
|
||||
bind:value={input.cfg.weightModfier}
|
||||
disabled={selected.cfg.disableGeneration}
|
||||
/>
|
||||
</label>
|
||||
<label>
|
||||
<span class="label mb-1">Weight skew</span>
|
||||
<input
|
||||
class="input w-full"
|
||||
type="number"
|
||||
min="0"
|
||||
max="1"
|
||||
step=".05"
|
||||
placeholder=".1"
|
||||
bind:value={input.cfg.weightSkew}
|
||||
disabled={selected.cfg.disableGeneration}
|
||||
/>
|
||||
</label>
|
||||
<label>
|
||||
<span class="label mb-1">Age modifier</span>
|
||||
<input
|
||||
class="input w-full"
|
||||
type="number"
|
||||
min="0"
|
||||
max="1"
|
||||
step=".05"
|
||||
placeholder=".1"
|
||||
bind:value={input.cfg.ageModifier}
|
||||
disabled={selected.cfg.disableGeneration}
|
||||
/>
|
||||
</label>
|
||||
</div>
|
||||
</fieldset>
|
||||
</div>
|
||||
</fieldset>
|
||||
</section>
|
||||
|
||||
<div class="divider"></div>
|
||||
|
||||
<section class="w-full">
|
||||
<div>
|
||||
<div class="flex items-center">
|
||||
<p class="text-xs label">
|
||||
{#if !selected.cfg.disableGeneration && allStandards
|
||||
.byActivity(selected.activity)
|
||||
.getMetadata().generators.length}
|
||||
(Generated data: {allStandards
|
||||
.byActivity(selected.activity)
|
||||
.getMetadata()
|
||||
.generators.map((g) => g.metric)
|
||||
.join(", ")})
|
||||
{:else}
|
||||
(Generated data: NONE)
|
||||
{/if}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{#if !selected.metrics.age}
|
||||
{#each allStandards.agesFor(selected.activity, selected.metrics.gender) as age}
|
||||
{@render standardsTable(selected.activity, {
|
||||
...selected.metrics,
|
||||
age,
|
||||
})}
|
||||
{/each}
|
||||
{:else}
|
||||
{@render standardsTable(selected.activity, selected.metrics)}
|
||||
{/if}
|
||||
</section>
|
||||
|
||||
{#snippet standardsTable(activity: Activity, metrics: Metrics)}
|
||||
{#snippet standardsTableRow(standard: Standard)}
|
||||
<tr class="hover:bg-base-300">
|
||||
<th>
|
||||
{standard.metrics.weight
|
||||
? parseFloat(kgToLb(standard.metrics.weight).toFixed(2))
|
||||
: "None"}</th
|
||||
>
|
||||
{#each range(selected.cfg.maxLevel) as lvl}
|
||||
<td>{getLvlValue(activity, standard, lvl)}</td>
|
||||
{/each}
|
||||
</tr>
|
||||
{/snippet}
|
||||
<div class="mt-5">
|
||||
<h3 class="text-lg italic font-semibold">{metrics.age}</h3>
|
||||
<div class="overflow-x-auto">
|
||||
<table class="table table-zebra table-pin-cols">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Body weight</th>
|
||||
{#each range(selected.cfg.maxLevel) as lvl}
|
||||
<td>{lvl}</td>
|
||||
{/each}
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{#if metrics.weight}
|
||||
{@const standard = allStandards
|
||||
.byActivity(activity)
|
||||
.byGender(metrics.gender)
|
||||
.byAge(metrics.age)
|
||||
.byWeight(metrics.weight)
|
||||
.getOneInterpolated()}
|
||||
{@render standardsTableRow(standard)}
|
||||
{:else}
|
||||
{@const standards = allStandards
|
||||
.byActivity(activity)
|
||||
.byGender(metrics.gender)
|
||||
.byAge(metrics.age)
|
||||
.getAllInterpolated({ normalizeForLb: true })}
|
||||
{#each standards as standard}
|
||||
{@render standardsTableRow(standard)}
|
||||
{/each}
|
||||
{/if}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
{/snippet}
|
||||
Reference in New Issue
Block a user