From 81543e5307c98df0d44c1cf553bcdfefbfe4ea7f Mon Sep 17 00:00:00 2001
From: mat
Date: Wed, 16 Feb 2022 22:12:59 +0000
Subject: improvements
---
src/lib/constants.ts | 11 -----
src/lib/profile.ts | 7 ++--
src/lib/utils.ts | 15 +++++++
src/routes/player/[player]/[profile].svelte | 62 +++++++++++++++++++++++++----
src/routes/player/[player]/index.svelte | 26 ++++++++----
src/routes/player/index.ts | 2 +-
6 files changed, 91 insertions(+), 32 deletions(-)
delete mode 100644 src/lib/constants.ts
(limited to 'src')
diff --git a/src/lib/constants.ts b/src/lib/constants.ts
deleted file mode 100644
index dc84994..0000000
--- a/src/lib/constants.ts
+++ /dev/null
@@ -1,11 +0,0 @@
-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
index 6388128..973a6da 100644
--- a/src/lib/profile.ts
+++ b/src/lib/profile.ts
@@ -1,17 +1,16 @@
-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) {
+export 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) {
+export function generateInfobox(data, constants, opts: { meta: boolean }): string[] {
const result: string[] = []
result.push(`💾 Last save: ${prettyTimestamp(data.member.last_save * 1000)}`)
@@ -53,5 +52,5 @@ export function generateMetaDescription(data) {
`☠${mostSignificantDeathsStat.value.toLocaleString()} ${mostSignificantDeathsStat.unit || cleanId(mostSignificantDeathsStat.rawName).toLowerCase()}`
)
- return result.join('\n')
+ return result
}
\ No newline at end of file
diff --git a/src/lib/utils.ts b/src/lib/utils.ts
index f93f68e..c2a955a 100644
--- a/src/lib/utils.ts
+++ b/src/lib/utils.ts
@@ -132,4 +132,19 @@ export function twemojiHtml(s: string) {
return `
`
})
return asTwemoji
+}
+
+export function formatNumber(n: number, digits = 3) {
+ // from https://stackoverflow.com/a/9462382 with some modifications
+ const numberSymbolsLookup = [
+ { value: 1, symbol: '' },
+ { value: 1e3, symbol: 'k' },
+ { value: 1e6, symbol: 'M' },
+ { value: 1e9, symbol: 'G' },
+ { value: 1e12, symbol: 'T' },
+ { value: 1e15, symbol: 'P' },
+ { value: 1e18, symbol: 'E' },
+ ]
+ const item = numberSymbolsLookup.slice().reverse().find(item => n >= item.value)
+ return (n / item.value).toPrecision(digits).replace(/\.0+$|(\.[0-9]*[1-9])0+$/, '$1') + item.symbol
}
\ No newline at end of file
diff --git a/src/routes/player/[player]/[profile].svelte b/src/routes/player/[player]/[profile].svelte
index dfb01ab..9c3fb31 100644
--- a/src/routes/player/[player]/[profile].svelte
+++ b/src/routes/player/[player]/[profile].svelte
@@ -5,31 +5,60 @@
export const load: Load = async ({ params, fetch }) => {
const player: string = params.player
const profile: string = params.profile
- const res = await fetch(`${API_URL}player/${player}/${profile}?customization=true`).then(r =>
+ const data = await fetch(`${API_URL}player/${player}/${profile}?customization=true`).then(r =>
r.json()
)
+
+ const constants = await fetch('/constants.json').then(r => r.json())
+
return {
props: {
- data: res,
+ data,
+ constants,
},
}
}
+
+ {@html bodyStyle}
+
+
@@ -43,4 +72,21 @@
{/if}
({data.member.profileName})
+
+
+
+
+
+ {#if data.member.skills.length > 0}
+
+ {/if}
+
diff --git a/src/routes/player/[player]/index.svelte b/src/routes/player/[player]/index.svelte
index 907761d..8704b09 100644
--- a/src/routes/player/[player]/index.svelte
+++ b/src/routes/player/[player]/index.svelte
@@ -4,23 +4,29 @@
export const load: Load = async ({ params, fetch }) => {
const player: string = params.player
- const res = await fetch(`${API_URL}player/${player}?customization=true`).then(r => r.json())
- console.log('res', res)
+ const data = await fetch(`${API_URL}player/${player}?customization=true`).then(r => r.json())
- if (!res.player) {
+ if (!data.player) {
return { fallthrough: true } as unknown
}
- if (res.player.username !== player) {
+ // this should happen instantly
+ // const constants = await fetch('/_constants.json').then(r => r.json())
+
+ // console.log('constants', constants)
+
+ if (data.player.username !== player) {
return {
- redirect: `/player/${res.player.username}`,
+ redirect: `/player/${data.player.username}`,
status: 302,
}
}
return {
- props: { data: res },
+ props: {
+ data,
+ },
}
}
@@ -31,6 +37,7 @@
import Head from '$lib/Head.svelte'
export let data
+ // export let constants
let activeProfile = null
let activeProfileLastSave: number = 0
@@ -53,7 +60,6 @@
{@html bodyStyle}
-