diff --git a/scripts/common/timer.ts b/scripts/common/timer.ts index d3057adf..5530101f 100644 --- a/scripts/common/timer.ts +++ b/scripts/common/timer.ts @@ -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; @@ -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, @@ -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 { diff --git a/scripts/hud/comparisons.ts b/scripts/hud/comparisons.ts index e8d6ba2d..62648cde 100644 --- a/scripts/hud/comparisons.ts +++ b/scripts/hud/comparisons.ts @@ -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 { @@ -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( @@ -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)) { @@ -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, diff --git a/styles/components/split.scss b/styles/components/split.scss index 794ae814..018e8105 100644 --- a/styles/components/split.scss +++ b/styles/components/split.scss @@ -111,6 +111,10 @@ horizontal-align: right; } + &.split--subsplit { + margin-left: 30px; + } + &.split--latest { & .split__left { margin-top: 2px;