From 23740236675594f7eb8625ad5b98ee30d09448d3 Mon Sep 17 00:00:00 2001 From: cyclane Date: Sun, 18 Jul 2021 22:53:48 +0100 Subject: [PATCH] first somewhat stable version --- src/components/Graphs.svelte | 13 ++++- src/components/stores.ts | 27 +-------- src/lib/api.ts | 55 ++++++++++++++++--- .../[player]}/cute-name.ts | 8 ++- src/routes/graph/new.svelte | 12 +++- .../profile/[profile]/member/[member].svelte | 8 +-- .../member/[member]/graph/[graph].svelte | 41 +++++++++++++- src/routes/profile/new.svelte | 1 + src/routes/profiles.svelte | 13 ++++- 9 files changed, 132 insertions(+), 46 deletions(-) rename src/routes/api/profiles/{[player]/[profile] => [profile]/[player]}/cute-name.ts (86%) diff --git a/src/components/Graphs.svelte b/src/components/Graphs.svelte index ed0dc70..ab4ec33 100644 --- a/src/components/Graphs.svelte +++ b/src/components/Graphs.svelte @@ -1,11 +1,18 @@ @@ -23,7 +30,9 @@ {/each} - + {#if add} + + {/if} {:catch error}

{error.message}

diff --git a/src/components/stores.ts b/src/components/stores.ts index 7406971..28f638f 100644 --- a/src/components/stores.ts +++ b/src/components/stores.ts @@ -50,27 +50,6 @@ function createExtraNav() { export const extraNav = createExtraNav(); -function createCache() { - const { subscribe, set, update } = writable<{ - [key: string]: string; - }>({}); - - return { - subscribe, - set: (key: string, value: string) => - update(current => ({ - ...current, - [key]: value - })), - unset: (key: string) => - update(current => { - delete current[key]; - return current; - }), - clear: () => set({}), - update - }; -} - -export const playerCache = createCache(); -export const profileCache = createCache(); +export let playerCache: Record = {}; +export let uuidCache: Record = {}; +export let profileCache: Record = {}; diff --git a/src/lib/api.ts b/src/lib/api.ts index cc5c1ea..a1baef5 100644 --- a/src/lib/api.ts +++ b/src/lib/api.ts @@ -1,4 +1,5 @@ import type { Graph, Tracker } from "../routes/api/_types"; +import { playerCache, profileCache, uuidCache } from "../components/stores"; import type { Profile } from "./hypixel"; import { requestWithDefaults } from "./util"; @@ -60,12 +61,17 @@ export async function getProfiles( * @returns A list of profiles */ export async function getPlayerProfiles( - token: string, + token: string | undefined, player: string ): Promise { - let response = await request(`/api/profiles/${player}`, { - headers: { Authorization: token } - }); + let response = await request( + `/api/profiles/${player}`, + token + ? { + headers: { Authorization: token } + } + : {} + ); return response.json(); } @@ -79,10 +85,15 @@ export async function getProfileCuteName( player: string, profile: string ): Promise { + if (profileCache[player + profile] !== undefined) { + return profileCache[player + profile]; + } let response = await request( `/api/profiles/${player}/${profile}/cute-name` ); - return response.json(); + let cuteName = await response.json(); + profileCache[player + profile] = cuteName; + return cuteName; } /** @@ -106,8 +117,13 @@ export async function getGraphs( * @returns Player's username */ export async function getUsername(uuid: string): Promise { + if (playerCache[uuid] !== undefined) { + return playerCache[uuid]; + } let response = await request(`/api/username?uuid=${uuid}`); - return response.json(); + let username = await response.json(); + playerCache[uuid] = username; + return username; } /** @@ -116,8 +132,13 @@ export async function getUsername(uuid: string): Promise { * @returns Player's UUID */ export async function getUUID(username: string): Promise { + if (uuidCache[username] !== undefined) { + return uuidCache[username]; + } let response = await request(`/api/uuid?username=${username}`); - return response.json(); + let uuid = await response.json(); + uuidCache[username] = uuid; + return uuid; } /** @@ -155,6 +176,18 @@ export async function getPermissions(token?: string): Promise { return response.json(); } +/** + * Create a graph + * @param token Authorization token + * @param type Graph type + * @param name Graph name + * @param style Graph style + * @param value AQL code for graph + * @param xAxisName X-axis name + * @param xAxisType X-axis type + * @param yAxisName Y-axis name + * @param yAxisType Y-axis type + */ export async function putGraph( token: string | undefined, type: string, @@ -183,6 +216,14 @@ export async function putGraph( return; } +/** + * Get graph data for a profile + * @param token Authorization token + * @param graph Graph's ID + * @param profile Profile's UUID + * @param member Player's UUID + * @returns Graph data + */ export async function getGraph( token: string | undefined, graph: string, diff --git a/src/routes/api/profiles/[player]/[profile]/cute-name.ts b/src/routes/api/profiles/[profile]/[player]/cute-name.ts similarity index 86% rename from src/routes/api/profiles/[player]/[profile]/cute-name.ts rename to src/routes/api/profiles/[profile]/[player]/cute-name.ts index 945c986..69015be 100644 --- a/src/routes/api/profiles/[player]/[profile]/cute-name.ts +++ b/src/routes/api/profiles/[profile]/[player]/cute-name.ts @@ -17,7 +17,7 @@ export async function get( } let response = await getProfilesByPlayer(API_KEY || "", req.params.player); - if (response.success && response.profiles !== undefined) { + if (response.success && response.profiles) { if (profileNameCache[req.params.player] === undefined) { profileNameCache[req.params.player] = {}; } @@ -28,8 +28,10 @@ export async function get( ); res.writeHead(200); res.end( - response.profiles.find(p => p.profile_id === req.params.profile) - ?.cute_name + JSON.stringify( + response.profiles.find(p => p.profile_id === req.params.profile) + ?.cute_name + ) ); } else { res.writeHead(response._response.status); diff --git a/src/routes/graph/new.svelte b/src/routes/graph/new.svelte index 8812a7f..54de2f3 100644 --- a/src/routes/graph/new.svelte +++ b/src/routes/graph/new.svelte @@ -44,7 +44,17 @@ on:submit={async event => { event.preventDefault(); try { - await putGraph($login?.token, type, name, style, value); + await putGraph( + $login?.token, + type, + name, + style, + value, + xAxisName, + xAxisType, + yAxisName, + yAxisName + ); goto("/graphs"); } catch (e) { if ( diff --git a/src/routes/profile/[profile]/member/[member].svelte b/src/routes/profile/[profile]/member/[member].svelte index a1963d7..a5efdbb 100644 --- a/src/routes/profile/[profile]/member/[member].svelte +++ b/src/routes/profile/[profile]/member/[member].svelte @@ -15,18 +15,16 @@ import Graphs from "../../../../components/Graphs.svelte"; import { goto } from "@sapper/app"; import { extraNav, login } from "../../../../components/stores"; - import { getPlayerProfiles, getUsername } from "../../../../lib/api"; + import { getProfileCuteName, getUsername } from "../../../../lib/api"; $extraNav; $: if ($login !== undefined) { (async () => { let username = await getUsername(member); - let profiles = await getPlayerProfiles(member); + let cuteName = await getProfileCuteName(profile, member); extraNav.set([ [ - username + - " @ " + - profiles.find(p => p.profile_id === profile)?.cute_name, + `${username} @ ${cuteName}`, () => goto(`/profile/${profile}/member/${member}`) ] ]); diff --git a/src/routes/profile/[profile]/member/[member]/graph/[graph].svelte b/src/routes/profile/[profile]/member/[member]/graph/[graph].svelte index 0248901..faf9e48 100644 --- a/src/routes/profile/[profile]/member/[member]/graph/[graph].svelte +++ b/src/routes/profile/[profile]/member/[member]/graph/[graph].svelte @@ -16,13 +16,50 @@ export let graph: string; import { onMount } from "svelte"; + import { goto } from "@sapper/app"; import { init } from "echarts"; - import { getGraph, getGraphs } from "../../../../../../lib/api"; - import { login } from "../../../../../../components/stores"; + import { + getGraph, + getGraphs, + getProfileCuteName, + getUsername + } from "../../../../../../lib/api"; + import { login, extraNav } from "../../../../../../components/stores"; + import type { Graph } from "../../../../../api/_types"; + + $extraNav; + + $: if ($login !== undefined) { + (async () => { + let username = await getUsername(member); + let cuteName = await getProfileCuteName(profile, member); + extraNav.update(current => { + return [ + [ + `${username} @ ${cuteName}`, + () => goto(`/profile/${profile}/member/${member}`) + ], + ...(current || []) + ]; + }); + })(); + } onMount(async () => { let graphs = await getGraphs($login?.token); let g = graphs.find(g => g.graph === graph); + extraNav.update(current => { + return [ + ...(current || []), + [ + g?.data.name || "Unknown graph", + () => + goto( + `/profile/${profile}/member/${member}/graph/${graph}` + ) + ] + ]; + }); let data = await getGraph($login?.token, graph, profile, member); diff --git a/src/routes/profile/new.svelte b/src/routes/profile/new.svelte index 102502a..2b4d1c5 100644 --- a/src/routes/profile/new.svelte +++ b/src/routes/profile/new.svelte @@ -40,6 +40,7 @@ event.preventDefault(); try { profilesPromise = getPlayerProfiles( + $login?.token, (uuid = await getUUID(username)) ); } catch (error) { diff --git a/src/routes/profiles.svelte b/src/routes/profiles.svelte index 82917ce..208c7c4 100644 --- a/src/routes/profiles.svelte +++ b/src/routes/profiles.svelte @@ -1,10 +1,17 @@ @@ -39,7 +46,9 @@ {/each} -
+ {#if add} + + {/if} {:catch error}

{error.message}