From c060d60ba1d5f3ad9f37b48b50b694a2d19240d1 Mon Sep 17 00:00:00 2001 From: mat Date: Tue, 15 Feb 2022 21:58:20 +0000 Subject: start adding profile --- src/app.html | 2 +- src/lib/Head.svelte | 20 ++--- src/lib/Header.svelte | 36 +++++++- src/lib/Username.svelte | 8 +- src/lib/api.ts | 8 +- src/lib/constants.ts | 11 +++ src/lib/profile.ts | 57 ++++++++++++ src/lib/utils.ts | 37 ++++---- src/routes/__layout.svelte | 6 +- src/routes/player/[player].svelte | 120 ------------------------- src/routes/player/[player]/[profile].svelte | 32 +++++++ src/routes/player/[player]/index.svelte | 132 ++++++++++++++++++++++++++++ 12 files changed, 302 insertions(+), 167 deletions(-) create mode 100644 src/lib/constants.ts create mode 100644 src/lib/profile.ts delete mode 100644 src/routes/player/[player].svelte create mode 100644 src/routes/player/[player]/[profile].svelte create mode 100644 src/routes/player/[player]/index.svelte (limited to 'src') diff --git a/src/app.html b/src/app.html index b0013f4..e0a73a9 100644 --- a/src/app.html +++ b/src/app.html @@ -9,6 +9,6 @@ %svelte.head% -
%svelte.body%
+ %svelte.body% diff --git a/src/lib/Head.svelte b/src/lib/Head.svelte index d301f14..e8032db 100644 --- a/src/lib/Head.svelte +++ b/src/lib/Head.svelte @@ -1,17 +1,17 @@ - {title} - - - + {title} + + + diff --git a/src/lib/Header.svelte b/src/lib/Header.svelte index 27c7d09..a5ec702 100644 --- a/src/lib/Header.svelte +++ b/src/lib/Header.svelte @@ -21,7 +21,7 @@ }} > + + diff --git a/src/lib/Username.svelte b/src/lib/Username.svelte index 764721c..d415511 100644 --- a/src/lib/Username.svelte +++ b/src/lib/Username.svelte @@ -26,9 +26,11 @@ {:else if headType == '2d'} {/if} - - {@html formattingCodeToHtml(player.rank.colored)} - + {#if prefix} + + {@html formattingCodeToHtml(player.rank.colored)} + + {/if} {player.username} diff --git a/src/lib/api.ts b/src/lib/api.ts index 3c1d8ec..db6bf81 100644 --- a/src/lib/api.ts +++ b/src/lib/api.ts @@ -1,7 +1 @@ -const BASE_URL = 'https://skyblock-api.matdoes.dev/' - -export async function get(path: string) { - const resp = await fetch(BASE_URL + path) - return await resp.json() -} - +export const API_URL = 'https://skyblock-api.matdoes.dev/' \ No newline at end of file diff --git a/src/lib/constants.ts b/src/lib/constants.ts new file mode 100644 index 0000000..dc84994 --- /dev/null +++ b/src/lib/constants.ts @@ -0,0 +1,11 @@ +import { API_URL } from '$lib/api' + +export let constants: any = {} + +async function updateConstants() { + constants = await fetch(API_URL + 'constants').then(r => r.json()) + console.log('updated constants') +} + +updateConstants() +setInterval(updateConstants, 60 * 60 * 1000) // update every hour \ No newline at end of file diff --git a/src/lib/profile.ts b/src/lib/profile.ts new file mode 100644 index 0000000..6388128 --- /dev/null +++ b/src/lib/profile.ts @@ -0,0 +1,57 @@ +import { constants } from './constants' +import { cleanId, millisecondsToTime } from './utils' + +/** + * Convert milliseconds since epoch into a string, but if it was within the + * past week then show the timeago + */ +function prettyTimestamp(ms: number) { + const isWithinPastWeek = Date.now() - ms < 1000 * 60 * 60 * 24 * 7 + const timeAsString = isWithinPastWeek ? (millisecondsToTime(Date.now() - ms) + ' ago') : (new Date(ms)).toUTCString() + return timeAsString +} + +export function generateMetaDescription(data) { + const result: string[] = [] + + result.push(`💾 Last save: ${prettyTimestamp(data.member.last_save * 1000)}`) + + result.push(`🚶 Profile created: ${prettyTimestamp(data.member.first_join * 1000)}`) + + result.push(`✨ Fairy souls: ${data.member.fairy_souls.total}/${constants.max_fairy_souls}`) + + if (data.profile.minion_count >= constants.max_minions) + result.push(`🤖 Minion count: ${data.profile.minion_count}`) + + let mostSignificantKillsStat = null + let mostSignificantDeathsStat = null + + for (const stat of data.member.stats) { + if ( + stat.category === 'kills' + && stat.rawName != 'kills' + && stat.value >= 200_000 + && stat.value > (mostSignificantKillsStat?.value ?? 0) + ) + mostSignificantKillsStat = stat + if ( + stat.category === 'deaths' + && stat.rawName != 'deaths' + && stat.value > 1_000_000 + && stat.value > (mostSignificantDeathsStat?.value ?? 0) + ) + mostSignificantDeathsStat = stat + } + + if (mostSignificantKillsStat) + result.push( + `⚔️ ${mostSignificantKillsStat.value.toLocaleString()} ${mostSignificantKillsStat.unit || cleanId(mostSignificantKillsStat.rawName).toLowerCase()}` + ) + + if (mostSignificantDeathsStat) + result.push( + `☠ ${mostSignificantDeathsStat.value.toLocaleString()} ${mostSignificantDeathsStat.unit || cleanId(mostSignificantDeathsStat.rawName).toLowerCase()}` + ) + + return result.join('\n') +} \ No newline at end of file diff --git a/src/lib/utils.ts b/src/lib/utils.ts index e6f85ff..5d8ecc8 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -70,12 +70,14 @@ export function formattingCodeToHtml(formatted: string): string { export function removeFormattingCode(formatted: string): string { return formatted.replace(new RegExp(colorCodeCharacter + '.', 'g'), '') } -function moveStringToEnd(word: string, thing: string) { + +function moveToEndOfId(word: string, thing: string) { if (thing.startsWith(`${word}_`)) - thing = thing.substr(`${word}_`.length) + `_${word}` + thing = thing.slice(`${word}_`.length) + `_${word}` return thing } -function millisecondsToTime(totalMilliseconds: number) { + +export function millisecondsToTime(totalMilliseconds: number) { const totalSeconds = totalMilliseconds / 1000 const totalMinutes = totalSeconds / 60 const totalHours = totalMinutes / 60 @@ -98,29 +100,20 @@ function millisecondsToTime(totalMilliseconds: number) { else if (milliseconds == 1) stringUnits.push(`${milliseconds} millisecond`) return stringUnits.slice(0, 2).join(' and ') } -export function cleanNumber(number: number, unit?: string): string { - switch (unit) { - case 'time': - return millisecondsToTime(number) - case 'date': - return (new Date(number * 1000)).toUTCString() - } - return number.toLocaleString() + (unit ? (' ' + unit) : '') -} -export function clean(thing: string | number) { - if (typeof thing === 'number') { - return cleanNumber(thing) - } else { - for (const string of ['deaths', 'kills', 'collection', 'skill']) - thing = moveStringToEnd(string, thing) - return thing - .replace(/^./, thing[0].toUpperCase()) - .replace(/_/g, ' ') - } + +export function cleanId(id: string) { + for (const string of ['deaths', 'kills', 'collection', 'skill']) + id = moveToEndOfId(string, id) + + return id + .replace(/^./, id[0].toUpperCase()) + .replace(/_/g, ' ') } + export function toRomanNumerals(number: number) { return ['', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X', 'XI', 'XII', 'XIII', 'XIV', 'XV', 'XVI', 'XVII', 'XVIII', 'XIX', 'XX'][number] } + export function shuffle(a: T[]): T[] { for (let i = a.length - 1; i > 0; i--) { const j = Math.floor(Math.random() * (i + 1)) diff --git a/src/routes/__layout.svelte b/src/routes/__layout.svelte index d1fca30..2da6384 100644 --- a/src/routes/__layout.svelte +++ b/src/routes/__layout.svelte @@ -3,6 +3,6 @@ import '../app.css' -
- -
+