From cf7c8a7b343acd7c9a4b6087f9a76b2e099889d5 Mon Sep 17 00:00:00 2001 From: mat Date: Sat, 5 Mar 2022 16:25:27 -0600 Subject: improve mayor timers --- src/lib/utils.ts | 35 +++++++++++++++++++++-------------- src/routes/election.svelte | 15 ++++++++++++--- 2 files changed, 33 insertions(+), 17 deletions(-) (limited to 'src') diff --git a/src/lib/utils.ts b/src/lib/utils.ts index d6e8e13..c871387 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -82,10 +82,19 @@ function moveToEndOfId(word: string, thing: string) { } interface MillisecondsToTimeOpts { - parts: number + parts?: number + /** + * 0: All units + * 1: Don't show milliseconds + * 2: Don't show seconds + * 3: Don't show minutes + * 4: Don't show hours + * 5: Don't show days + */ + smallestUnit?: number } -export function millisecondsToTime(totalMilliseconds: number, opts: MillisecondsToTimeOpts = { parts: 2 }) { +export function millisecondsToTime(totalMilliseconds: number, opts: MillisecondsToTimeOpts = {}) { const totalSeconds = totalMilliseconds / 1000 const totalMinutes = totalSeconds / 60 const totalHours = totalMinutes / 60 @@ -99,20 +108,18 @@ export function millisecondsToTime(totalMilliseconds: number, opts: Milliseconds const stringUnits: string[] = [] - if (totalDays > 1) stringUnits.push(`${days} days`) - else if (totalDays == 1) stringUnits.push(`${days} day`) - if (totalHours > 1) stringUnits.push(`${hours} hours`) - else if (totalHours == 1) stringUnits.push(`${hours} hour`) - if (totalMinutes > 1) stringUnits.push(`${minutes} minutes`) - else if (totalMinutes == 1) stringUnits.push(`${minutes} minute`) - if (totalSeconds > 1) stringUnits.push(`${seconds} seconds`) - else if (totalSeconds == 1) stringUnits.push(`${seconds} second`) - if (totalMilliseconds > 1) stringUnits.push(`${milliseconds} milliseconds`) - else if (totalMilliseconds == 1) stringUnits.push(`${milliseconds} millisecond`) + const smallestUnit = opts.smallestUnit ?? 0 + + if (totalDays > 1 && smallestUnit <= 4) stringUnits.push(days === 1 ? `${days} day` : `${days} days`) + if (totalHours > 1 && smallestUnit <= 3) stringUnits.push(hours === 1 ? `${hours} hour` : `${hours} hours`) + if (totalMinutes > 1 && smallestUnit <= 2) stringUnits.push(minutes === 1 ? `${minutes} minute` : `${minutes} minutes`) + if (totalSeconds > 1 && smallestUnit <= 1) stringUnits.push(seconds === 1 ? `${seconds} second` : `${seconds} seconds`) + if (totalMilliseconds > 0 && smallestUnit <= 0) stringUnits.push(`${milliseconds} ms`) + // comma separated, "and" before last - let partsCount = opts.parts - if (stringUnits.length < opts.parts) partsCount = stringUnits.length + let partsCount = opts.parts ?? 2 + if (stringUnits.length < partsCount) partsCount = stringUnits.length if (partsCount === 1) return stringUnits[0] return stringUnits.slice(0, partsCount - 1).join(', ') + ' and ' + stringUnits[partsCount - 1] } diff --git a/src/routes/election.svelte b/src/routes/election.svelte index 69f4812..7b156b6 100644 --- a/src/routes/election.svelte +++ b/src/routes/election.svelte @@ -73,17 +73,26 @@

Last API update: - {millisecondsToTime(currentTime - data.last_updated * 1000)} + {millisecondsToTime(currentTime - data.last_updated * 1000, { + smallestUnit: 1, + parts: 1, + })}

Next API update: - {millisecondsToTime(currentTime - data.last_updated * 1000 + 10 * 60 * 1000)} + {millisecondsToTime(currentTime - data.last_updated * 1000 + 10 * 60 * 1000, { + smallestUnit: 1, + parts: 1, + })}

SkyBlock Mayor Election Status

Next election: - {millisecondsToTime(skyblockTime(data.previous.year + 1, 6, 27) - currentTime, { parts: 3 })} + {millisecondsToTime(skyblockTime(data.previous.year + 1, 6, 27) - currentTime, { + parts: 3, + smallestUnit: 1, + })}

{#if data.current} -- cgit