first somewhat stable version

This commit is contained in:
Gleb Koval 2021-07-18 22:53:48 +01:00
parent d96fb38aa1
commit 2374023667
No known key found for this signature in database
GPG Key ID: 120F2F6DA9D995FB
9 changed files with 132 additions and 46 deletions

View File

@ -1,11 +1,18 @@
<script lang="ts"> <script lang="ts">
export let hrefPrefix = ""; export let hrefPrefix = "";
import { login } from "./stores"; import { login } from "./stores";
import { getGraphs } from "../lib/api"; import { checkPermissions, Permissions } from "../lib/permissions";
import { getGraphs, getPermissions } from "../lib/api";
let add = false;
let graphsPromise: ReturnType<typeof getGraphs> = new Promise(() => {}); let graphsPromise: ReturnType<typeof getGraphs> = new Promise(() => {});
$: if ($login !== undefined) { $: if ($login !== undefined) {
graphsPromise = getGraphs($login?.token); graphsPromise = getGraphs($login?.token);
getPermissions($login?.token).then(permissions => {
if (checkPermissions([Permissions.ADD_GRAPH], permissions)) {
add = true;
}
});
} }
</script> </script>
@ -23,7 +30,9 @@
</a> </a>
{/each} {/each}
<!-- svelte-ignore a11y-missing-content --> <!-- svelte-ignore a11y-missing-content -->
<a class="item add" href="/graph/new" /> {#if add}
<a class="item add" href="/graph/new" />
{/if}
{:catch error} {:catch error}
<span class="item"> <span class="item">
<h2 style="color: red">{error.message}</h2> <h2 style="color: red">{error.message}</h2>

View File

@ -50,27 +50,6 @@ function createExtraNav() {
export const extraNav = createExtraNav(); export const extraNav = createExtraNav();
function createCache() { export let playerCache: Record<string, string> = {};
const { subscribe, set, update } = writable<{ export let uuidCache: Record<string, string> = {};
[key: string]: string; export let profileCache: Record<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();

View File

@ -1,4 +1,5 @@
import type { Graph, Tracker } from "../routes/api/_types"; import type { Graph, Tracker } from "../routes/api/_types";
import { playerCache, profileCache, uuidCache } from "../components/stores";
import type { Profile } from "./hypixel"; import type { Profile } from "./hypixel";
import { requestWithDefaults } from "./util"; import { requestWithDefaults } from "./util";
@ -60,12 +61,17 @@ export async function getProfiles(
* @returns A list of profiles * @returns A list of profiles
*/ */
export async function getPlayerProfiles( export async function getPlayerProfiles(
token: string, token: string | undefined,
player: string player: string
): Promise<Profile[]> { ): Promise<Profile[]> {
let response = await request(`/api/profiles/${player}`, { let response = await request(
headers: { Authorization: token } `/api/profiles/${player}`,
}); token
? {
headers: { Authorization: token }
}
: {}
);
return response.json(); return response.json();
} }
@ -79,10 +85,15 @@ export async function getProfileCuteName(
player: string, player: string,
profile: string profile: string
): Promise<string> { ): Promise<string> {
if (profileCache[player + profile] !== undefined) {
return profileCache[player + profile];
}
let response = await request( let response = await request(
`/api/profiles/${player}/${profile}/cute-name` `/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 * @returns Player's username
*/ */
export async function getUsername(uuid: string): Promise<string> { export async function getUsername(uuid: string): Promise<string> {
if (playerCache[uuid] !== undefined) {
return playerCache[uuid];
}
let response = await request(`/api/username?uuid=${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<string> {
* @returns Player's UUID * @returns Player's UUID
*/ */
export async function getUUID(username: string): Promise<string> { export async function getUUID(username: string): Promise<string> {
if (uuidCache[username] !== undefined) {
return uuidCache[username];
}
let response = await request(`/api/uuid?username=${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<string[]> {
return response.json(); 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( export async function putGraph(
token: string | undefined, token: string | undefined,
type: string, type: string,
@ -183,6 +216,14 @@ export async function putGraph(
return; 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( export async function getGraph(
token: string | undefined, token: string | undefined,
graph: string, graph: string,

View File

@ -17,7 +17,7 @@ export async function get(
} }
let response = await getProfilesByPlayer(API_KEY || "", req.params.player); 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) { if (profileNameCache[req.params.player] === undefined) {
profileNameCache[req.params.player] = {}; profileNameCache[req.params.player] = {};
} }
@ -28,8 +28,10 @@ export async function get(
); );
res.writeHead(200); res.writeHead(200);
res.end( res.end(
response.profiles.find(p => p.profile_id === req.params.profile) JSON.stringify(
?.cute_name response.profiles.find(p => p.profile_id === req.params.profile)
?.cute_name
)
); );
} else { } else {
res.writeHead(response._response.status); res.writeHead(response._response.status);

View File

@ -44,7 +44,17 @@
on:submit={async event => { on:submit={async event => {
event.preventDefault(); event.preventDefault();
try { try {
await putGraph($login?.token, type, name, style, value); await putGraph(
$login?.token,
type,
name,
style,
value,
xAxisName,
xAxisType,
yAxisName,
yAxisName
);
goto("/graphs"); goto("/graphs");
} catch (e) { } catch (e) {
if ( if (

View File

@ -15,18 +15,16 @@
import Graphs from "../../../../components/Graphs.svelte"; import Graphs from "../../../../components/Graphs.svelte";
import { goto } from "@sapper/app"; import { goto } from "@sapper/app";
import { extraNav, login } from "../../../../components/stores"; import { extraNav, login } from "../../../../components/stores";
import { getPlayerProfiles, getUsername } from "../../../../lib/api"; import { getProfileCuteName, getUsername } from "../../../../lib/api";
$extraNav; $extraNav;
$: if ($login !== undefined) { $: if ($login !== undefined) {
(async () => { (async () => {
let username = await getUsername(member); let username = await getUsername(member);
let profiles = await getPlayerProfiles(member); let cuteName = await getProfileCuteName(profile, member);
extraNav.set([ extraNav.set([
[ [
username + `${username} @ ${cuteName}`,
" @ " +
profiles.find(p => p.profile_id === profile)?.cute_name,
() => goto(`/profile/${profile}/member/${member}`) () => goto(`/profile/${profile}/member/${member}`)
] ]
]); ]);

View File

@ -16,13 +16,50 @@
export let graph: string; export let graph: string;
import { onMount } from "svelte"; import { onMount } from "svelte";
import { goto } from "@sapper/app";
import { init } from "echarts"; import { init } from "echarts";
import { getGraph, getGraphs } from "../../../../../../lib/api"; import {
import { login } from "../../../../../../components/stores"; 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 () => { onMount(async () => {
let graphs = await getGraphs($login?.token); let graphs = await getGraphs($login?.token);
let g = graphs.find(g => g.graph === graph); 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); let data = await getGraph($login?.token, graph, profile, member);

View File

@ -40,6 +40,7 @@
event.preventDefault(); event.preventDefault();
try { try {
profilesPromise = getPlayerProfiles( profilesPromise = getPlayerProfiles(
$login?.token,
(uuid = await getUUID(username)) (uuid = await getUUID(username))
); );
} catch (error) { } catch (error) {

View File

@ -1,10 +1,17 @@
<script lang="ts"> <script lang="ts">
import { login } from "../components/stores"; import { login } from "../components/stores";
import { getProfiles, getUsername } from "../lib/api"; import { getPermissions, getProfiles, getUsername } from "../lib/api";
import { checkPermissions, Permissions } from "../lib/permissions";
let add = false;
let profilesPromise: ReturnType<typeof getProfiles> = new Promise(() => {}); let profilesPromise: ReturnType<typeof getProfiles> = new Promise(() => {});
$: if ($login !== undefined) { $: if ($login !== undefined) {
profilesPromise = getProfiles($login?.token); profilesPromise = getProfiles($login?.token);
getPermissions($login?.token).then(permissions => {
if (checkPermissions([Permissions.ADD_GRAPH], permissions)) {
add = true;
}
});
} }
</script> </script>
@ -39,7 +46,9 @@
</button> </button>
{/each} {/each}
<!-- svelte-ignore a11y-missing-content --> <!-- svelte-ignore a11y-missing-content -->
<a class="item add" href="/profile/new" /> {#if add}
<a class="item add" href="/profile/new" />
{/if}
{:catch error} {:catch error}
<div class="item"> <div class="item">
<h2 style="color: red">{error.message}</h2> <h2 style="color: red">{error.message}</h2>