diff options
author | mat <github@matdoes.dev> | 2022-03-05 16:25:27 -0600 |
---|---|---|
committer | mat <github@matdoes.dev> | 2022-03-05 16:25:27 -0600 |
commit | cf7c8a7b343acd7c9a4b6087f9a76b2e099889d5 (patch) | |
tree | 800bf1323cd7ffc2cc45e0f47439fc425b7f3a62 | |
parent | f256a52e6a0a9ba478cde22d96c6292842247c01 (diff) | |
download | skyblock-stats-cf7c8a7b343acd7c9a4b6087f9a76b2e099889d5.tar.gz skyblock-stats-cf7c8a7b343acd7c9a4b6087f9a76b2e099889d5.tar.bz2 skyblock-stats-cf7c8a7b343acd7c9a4b6087f9a76b2e099889d5.zip |
improve mayor timers
-rw-r--r-- | src/lib/utils.ts | 35 | ||||
-rw-r--r-- | src/routes/election.svelte | 15 |
2 files changed, 33 insertions, 17 deletions
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 @@ <div class="next-mayor-update"> <p> <b>Last API update:</b> - {millisecondsToTime(currentTime - data.last_updated * 1000)} + {millisecondsToTime(currentTime - data.last_updated * 1000, { + smallestUnit: 1, + parts: 1, + })} </p> <p> <b>Next API update:</b> - {millisecondsToTime(currentTime - data.last_updated * 1000 + 10 * 60 * 1000)} + {millisecondsToTime(currentTime - data.last_updated * 1000 + 10 * 60 * 1000, { + smallestUnit: 1, + parts: 1, + })} </p> </div> <h1>SkyBlock Mayor Election Status</h1> <p> <b>Next election:</b> - {millisecondsToTime(skyblockTime(data.previous.year + 1, 6, 27) - currentTime, { parts: 3 })} + {millisecondsToTime(skyblockTime(data.previous.year + 1, 6, 27) - currentTime, { + parts: 3, + smallestUnit: 1, + })} </p> {#if data.current} |