Skip to content

Commit

Permalink
feat(momentum): Stage/course checkpoint splits
Browse files Browse the repository at this point in the history
  • Loading branch information
Panzerhandschuh committed Nov 26, 2024
1 parent c8548e1 commit 77f9f17
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 43 deletions.
80 changes: 50 additions & 30 deletions scripts/common/timer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,30 @@ export interface RunSplits {
segments: RunSegment[];
}

export function getSplitSegmentTime(runSplits: RunSplits, segmentIndex: number, subsegmentIndex: number): number {
if (subsegmentIndex > 0) {
return (
runSplits.segments[segmentIndex].subsegments[subsegmentIndex].timeReached -
runSplits.segments[segmentIndex].subsegments[subsegmentIndex - 1].timeReached
);
}

if (segmentIndex > 0) {
return (
runSplits.segments[segmentIndex].subsegments[subsegmentIndex].timeReached -
runSplits.segments[segmentIndex - 1].subsegments.at(-1).timeReached
);
}

return 0;
}

export function getSegmentName(segmentIndex: number, subsegmentIndex: number): string {
return subsegmentIndex >= 1
? `${(segmentIndex + 1).toString()}-${subsegmentIndex.toString()}`
: segmentIndex.toString();
}

export interface RunMetadata {
filePath: string;
timestamp: number;
Expand Down Expand Up @@ -317,24 +341,35 @@ export class Comparison {

static generateSplits(baseRunSplits: RunSplits, comparisonRunSplits: RunSplits): ComparisonSplit[] {
return baseRunSplits.segments.map((_, i) => {
const baseAccumulateTime = baseRunSplits.segments[i].subsegments[0].timeReached;
const comparisonAccumulateTime = comparisonRunSplits.segments[i].subsegments[0].timeReached;
const baseSplitTime = Comparison.getSegmentTime(baseRunSplits, i);
const comparisonSplitTime = Comparison.getSegmentTime(comparisonRunSplits, i);

return {
name: i.toString(),
accumulateTime: baseAccumulateTime,
time: baseSplitTime,
diff: baseAccumulateTime - comparisonAccumulateTime,
delta: baseSplitTime - comparisonSplitTime
// statsComparisons: baseRunSplits.trackStats.map(
// (stat, j) => new RunStatsComparison(stat, compareZone.stats[j])
// )
};
// TODO: Generate subsegment splits
return this.generateSegmentSplit(baseRunSplits, comparisonRunSplits, i, 0);
});
}

static generateSegmentSplit(
baseRunSplits: RunSplits,
comparisonRunSplits: RunSplits,
segmentIndex: number,
subsegmentIndex: number
): ComparisonSplit {
const baseAccumulateTime = baseRunSplits.segments[segmentIndex].subsegments[subsegmentIndex].timeReached;
const comparisonAccumulateTime =
comparisonRunSplits.segments[segmentIndex].subsegments[subsegmentIndex].timeReached;
const baseSplitTime = getSplitSegmentTime(baseRunSplits, segmentIndex, subsegmentIndex);
const comparisonSplitTime = getSplitSegmentTime(comparisonRunSplits, segmentIndex, subsegmentIndex);

return {
name: getSegmentName(segmentIndex, subsegmentIndex),
accumulateTime: baseAccumulateTime,
time: baseSplitTime,
diff: baseAccumulateTime - comparisonAccumulateTime,
delta: baseSplitTime - comparisonSplitTime
// statsComparisons: baseRunSplits.trackStats.map(
// (stat, j) => new RunStatsComparison(stat, compareZone.stats[j])
// )
};
}

static generateFinishSplit(
baseRunTime: number,
baseRunSplits: RunSplits,
Expand All @@ -355,21 +390,6 @@ export class Comparison {
// )
};
}

static getSegmentTime(runSplits: RunSplits, segmentIndex: number): number {
if (segmentIndex < 0 || segmentIndex >= runSplits.segments.length) {
return 0;
}

if (segmentIndex === 0) {
return runSplits.segments[0].subsegments[0].timeReached;
}

return (
runSplits.segments[segmentIndex].subsegments[0].timeReached -
runSplits.segments[segmentIndex - 1].subsegments[0].timeReached
);
}
}

export interface ComparisonSplit {
Expand Down
37 changes: 24 additions & 13 deletions scripts/hud/comparisons.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PanelHandler } from 'util/module-helpers';
import { Comparison, RunMetadata, ComparisonSplit, TimerState, RunSplits } from 'common/timer';
import { Comparison, RunMetadata, ComparisonSplit, TimerState, RunSplits, getSegmentName } from 'common/timer';

@PanelHandler()
class HudComparisonsHandler {
Expand Down Expand Up @@ -50,20 +50,24 @@ class HudComparisonsHandler {
timerStatus.trackId.number === this.comparison.trackId.number;

// TODO: Unordered/optional splits
// TODO: Subsegment splits
if (timerStatus.state === TimerState.RUNNING) {
if (timerStatus.majorNum <= 1 || timerStatus.minorNum > 1) {
if (timerStatus.majorNum <= 1 && timerStatus.minorNum <= 1) {
return;
}

const split = hasCompare
? Comparison.generateSplits(runSplits, this.comparison.runSplits)[timerStatus.majorNum - 1]
? Comparison.generateSegmentSplit(
runSplits,
this.comparison.runSplits,
timerStatus.majorNum - 1,
timerStatus.minorNum - 1
)
: {
name: timerStatus.majorNum - 1,
name: getSegmentName(timerStatus.majorNum - 1, timerStatus.minorNum - 1),
accumulateTime: timerStatus.runTime
};

this.addComparisonSplit(split, timerStatus.majorNum, hasCompare);
this.addComparisonSplit(split, timerStatus.minorNum > 1, hasCompare);
} else if (timerStatus.state === TimerState.FINISHED) {
const split = hasCompare
? Comparison.generateFinishSplit(
Expand All @@ -77,11 +81,16 @@ class HudComparisonsHandler {
accumulateTime: timerStatus.runTime
};

this.addComparisonSplit(split, timerStatus.majorNum, hasCompare);
this.addComparisonSplit(split, false, hasCompare);
}
}

addComparisonSplit(split: any, majorNum: number, hasCompare: boolean): void {
addComparisonSplit(
split: ComparisonSplit | { name: string; accumulateTime: number },
isSubSplit: boolean,
hasCompare: boolean
): void {
// Remove older splits
const splitPanels = this.panels.splits.Children().reverse();
if (splitPanels.length > this.maxActiveSplits) {
for (const panel of splitPanels.filter((_, i) => splitPanels.length - i > this.maxActiveSplits)) {
Expand All @@ -93,14 +102,16 @@ class HudComparisonsHandler {
class: 'hud-comparisons__split'
});

if (majorNum > 1) {
const lastSplit = this.panels.splits.GetFirstChild().GetFirstChild();
lastSplit?.RemoveClass('split--latest');
}
const lastSplit = this.panels.splits.GetFirstChild().GetFirstChild();
lastSplit?.RemoveClass('split--latest');

this.panels.splits.MoveChildBefore(wrapper, this.panels.splits.Children()[0]);

const panel = $.CreatePanel('Split', wrapper, '', { class: 'split--hud split--latest' });
let splitClass = 'split--hud split--latest';
if (isSubSplit) {
splitClass += ' split--subsplit';
}
const panel = $.CreatePanel('Split', wrapper, '', { class: splitClass });

Object.assign(
panel,
Expand Down
4 changes: 4 additions & 0 deletions styles/components/split.scss
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@
horizontal-align: right;
}

&.split--subsplit {
margin-left: 30px;
}

&.split--latest {
& .split__left {
margin-top: 2px;
Expand Down

0 comments on commit 77f9f17

Please sign in to comment.