XP calculation for repeated intervals
This commit is contained in:
parent
f134c4cf97
commit
e3ed1a2e0d
|
|
@ -2,6 +2,7 @@ import { xp } from "./xp";
|
||||||
import { Interval } from "../ast";
|
import { Interval } from "../ast";
|
||||||
import { Duration } from "../Duration";
|
import { Duration } from "../Duration";
|
||||||
import { ConstantIntensity, RangeIntensity } from "../Intensity";
|
import { ConstantIntensity, RangeIntensity } from "../Intensity";
|
||||||
|
import { RepeatedInterval } from "../detectRepeats";
|
||||||
|
|
||||||
describe("xp()", () => {
|
describe("xp()", () => {
|
||||||
describe("ConstantIntensity interval", () => {
|
describe("ConstantIntensity interval", () => {
|
||||||
|
|
@ -65,16 +66,35 @@ describe("xp()", () => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// Intervals
|
describe("Repeated interval", () => {
|
||||||
//
|
const createTestInterval = (seconds: number): RepeatedInterval => ({
|
||||||
// 4 x 15s = 1min --> 11 XP
|
type: "repeat",
|
||||||
// 4 x 30s = 2min --> 23 XP
|
times: 2,
|
||||||
// 4 x 60s = 4min --> 47 XP
|
intervals: [
|
||||||
|
{
|
||||||
|
type: "Interval",
|
||||||
|
duration: new Duration(seconds / 4),
|
||||||
|
intensity: new ConstantIntensity(80),
|
||||||
|
comments: [],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: "Interval",
|
||||||
|
duration: new Duration(seconds / 4),
|
||||||
|
intensity: new ConstantIntensity(70),
|
||||||
|
comments: [],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
comments: [],
|
||||||
|
});
|
||||||
|
|
||||||
// Other XP
|
[
|
||||||
//
|
[4 * 15, 11], // 1 minute
|
||||||
// 20 XP per km
|
[4 * 30, 23], // 2 minutes
|
||||||
|
[4 * 60, 47], // 4 minutes
|
||||||
// 221 km
|
].forEach(([seconds, expectedXp]) => {
|
||||||
// 6:42 of intervals
|
it(`${seconds}s produces ${expectedXp} XP`, () => {
|
||||||
|
expect(xp([createTestInterval(seconds)])).toEqual(expectedXp);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -1,20 +1,28 @@
|
||||||
import { Interval } from "../ast";
|
import { Interval } from "../ast";
|
||||||
import { sum } from "ramda";
|
import { sum } from "ramda";
|
||||||
import { RangeIntensity, ConstantIntensity, FreeIntensity } from "../Intensity";
|
import { RangeIntensity, ConstantIntensity, FreeIntensity } from "../Intensity";
|
||||||
|
import { RepeatedInterval } from "../detectRepeats";
|
||||||
|
import { totalDuration } from "./totalDuration";
|
||||||
|
|
||||||
const intervalXp = ({ intensity, duration }: Interval): number => {
|
const intervalXp = (interval: Interval | RepeatedInterval): number => {
|
||||||
if (intensity instanceof RangeIntensity) {
|
if (interval.type === "repeat") {
|
||||||
// 6 XP per minute (1XP for every 10 seconds)
|
// 11 XP per minute (1 XP for every 5.1 seconds)
|
||||||
return Math.floor(duration.seconds / 10);
|
const duration = totalDuration(interval.intervals).seconds * interval.times;
|
||||||
} else if (intensity instanceof ConstantIntensity) {
|
return Math.floor(duration / 5.1);
|
||||||
// 10 XP per minute (1XP for every 5.56 seconds)
|
|
||||||
return Math.floor(duration.seconds / 5.56);
|
|
||||||
} else if (intensity instanceof FreeIntensity) {
|
|
||||||
// 5 XP per minute
|
|
||||||
return Math.round(duration.seconds / 60) * 5;
|
|
||||||
} else {
|
} else {
|
||||||
throw new Error("Unknown type of intensity");
|
if (interval.intensity instanceof RangeIntensity) {
|
||||||
|
// 6 XP per minute (1XP for every 10 seconds)
|
||||||
|
return Math.floor(interval.duration.seconds / 10);
|
||||||
|
} else if (interval.intensity instanceof ConstantIntensity) {
|
||||||
|
// 10 XP per minute (1XP for every 5.56 seconds)
|
||||||
|
return Math.floor(interval.duration.seconds / 5.56);
|
||||||
|
} else if (interval.intensity instanceof FreeIntensity) {
|
||||||
|
// 5 XP per minute
|
||||||
|
return Math.round(interval.duration.seconds / 60) * 5;
|
||||||
|
} else {
|
||||||
|
throw new Error("Unknown type of intensity");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export const xp = (intervals: Interval[]): number => sum(intervals.map(intervalXp));
|
export const xp = (intervals: (Interval | RepeatedInterval)[]): number => sum(intervals.map(intervalXp));
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue