Skip to content
This repository was archived by the owner on Jan 3, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all 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
120 changes: 120 additions & 0 deletions Frontend/src/lib/solveTime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import { EventId, getEventResultType } from '@wca/helpers'

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
}
}

export 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 AttemptResultToStringParams {
attemptResult: number
eventId: EventId
}

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`
}

function parseMbValue(val: number) {
let mbValue = val
const old = Math.floor(mbValue / 1000000000) !== 0
let timeSeconds
let attempted
let solved
if (old) {
timeSeconds = mbValue % 100000
mbValue = Math.floor(mbValue / 100000)
attempted = mbValue % 100
mbValue = Math.floor(mbValue / 100)
solved = 99 - (mbValue % 100)
} else {
const missed = mbValue % 100
mbValue = Math.floor(mbValue / 100)
timeSeconds = mbValue % 100000
mbValue = Math.floor(mbValue / 100000)
const difference = 99 - (mbValue % 100)

solved = difference + missed
attempted = solved + missed
}

const timeCentiseconds = timeSeconds === 99999 ? null : timeSeconds * 100
return { solved, attempted, timeCentiseconds }
}

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

interface CentiSecondsToHumanReadableParams {
c: number
options?: {
short?: boolean
}
}
export function centiSecondsToHumanReadable({
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()
}
13 changes: 11 additions & 2 deletions Frontend/src/pages/events/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import {
} from 'semantic-ui-react'
import getCompetitionWcif from '../../api/competition/get/get_competition_wcif'
import { CompetitionContext } from '../../api/helper/context/competition_context'
import {
attemptResultToString,
centiSecondsToHumanReadable,
} from '../../lib/solveTime'
import { setMessage } from '../../ui/events/messages'
import LoadingMessage from '../../ui/messages/loadingMessage'

Expand Down Expand Up @@ -73,12 +77,17 @@ export default function Events() {
<TableCell>{getFormatName(round.format)}</TableCell>
<TableCell>
{round.timeLimit &&
`${round.timeLimit.centiseconds / 100} seconds`}
centiSecondsToHumanReadable({
c: round.timeLimit.centiseconds,
})}
</TableCell>
{competitionInfo['uses_cutoff?'] && (
<TableCell>
{round.cutoff &&
`${round.cutoff?.attemptResult / 100} seconds`}
attemptResultToString({
attemptResult: round.cutoff.attemptResult,
eventId: event.id,
})}
</TableCell>
)}
<TableCell>
Expand Down