Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion src/helpers/result.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { AttemptResult } from '../models/attemptResult';
import { formatCentiseconds } from './time';
import { centiSecondsToHumanReadable, formatCentiseconds } from './time';
import { EventId } from '../models';
import { getEventResultType } from './event';

type DnfMultiResult = { isDnf: true };
type DnsMultiResult = { isDns: true };
Expand Down Expand Up @@ -120,3 +122,28 @@ function decodeNewMultiResult(result: AttemptResult): DecodedMultiResult {

return res;
}

interface AttemptResultToStringParams {
attemptResult: number;
eventId: EventId;
}

function attemptResultToMbPoints(mbValue: number) {
const { solved, attempted } = decodeMultiResult(mbValue);
const missed = attempted - solved;
return solved - missed;
}

export function attemptResultToString({
attemptResult,
eventId,
}: AttemptResultToStringParams) {
const type = getEventResultType(eventId);
if (type === 'time') {
return centiSecondsToHumanReadable({ c: attemptResult });
}
if (type === 'number') {
return `${attemptResult} moves`;
}
return `${attemptResultToMbPoints(attemptResult)} points`;
}
71 changes: 71 additions & 0 deletions src/helpers/time.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import { getEventResultType } from './event';
import { EventId } from '../models';
import { decodeMultiResult } from './result';

export function formatCentiseconds(centiTime: number): string {
if (centiTime === -1) {
return 'DNF';
Expand All @@ -20,3 +24,70 @@ function prefix(n: number): string {
}
return `${n}`;
}

export const SECOND_IN_CS = 100;
export const MINUTE_IN_CS = 60 * SECOND_IN_CS;
export const HOUR_IN_CS = 60 * MINUTE_IN_CS;

interface PluralizeParams {
count: number;
word: string;
options?: {
fixed?: number;
abbreviate?: boolean;
};
}

function pluralize({ count, word, options = {} }: PluralizeParams) {
const countStr =
options.fixed && count % 1 > 0 ? count.toFixed(options.fixed) : count;
const countDesc = options.abbreviate
? word[0]
: ` ${count === 1 ? word : `${word}s`}`;
return countStr + countDesc;
}

interface CentiSecondsToHumanReadableParams {
c: number;
options?: {
short?: boolean;
};
}
export function centiSecondsToHumanReadable({
Comment thread
coder13 marked this conversation as resolved.
c,
options = {},
}: CentiSecondsToHumanReadableParams) {
let centiseconds = c;
let str = '';

const hours = centiseconds / HOUR_IN_CS;
centiseconds %= HOUR_IN_CS;
if (hours >= 1) {
str += `${pluralize({
count: Math.floor(hours),
word: 'hour',
options: { abbreviate: options.short },
})} `;
}

const minutes = centiseconds / MINUTE_IN_CS;
centiseconds %= MINUTE_IN_CS;
if (minutes >= 1) {
str += `${pluralize({
count: Math.floor(minutes),
word: 'minute',
options: { abbreviate: options.short },
})} `;
}

const seconds = centiseconds / SECOND_IN_CS;
if (seconds > 0 || str.length === 0) {
str += `${pluralize({
count: seconds,
word: 'second',
options: { fixed: 2, abbreviate: options.short },
})} `;
}

return str.trim();
}