From 3a364092ebb73fb759d94ccfe4029c532a350ac7 Mon Sep 17 00:00:00 2001 From: not-nullptr Date: Thu, 14 Mar 2024 15:58:52 +0000 Subject: [PATCH] fix starcount --- src/routes/+page.server.ts | 72 +++++++++++++++++++------------------- src/routes/+page.svelte | 8 ++--- 2 files changed, 40 insertions(+), 40 deletions(-) diff --git a/src/routes/+page.server.ts b/src/routes/+page.server.ts index d5a7357..839dc53 100644 --- a/src/routes/+page.server.ts +++ b/src/routes/+page.server.ts @@ -4,51 +4,51 @@ import { DISCORD_USER_TOKEN, GITLAB_API_TOKEN } from "$env/static/private"; let memberCount = 0; let starCount = 0; let roleMembers = { - "1214817156420862012": 50, + "1214817156420862012": 50, }; -async function setMemberCount() { - console.log("Fetching member count"); - - const promises = [ - fetch("https://discord.com/api/v9/invites/suyu?with_counts=true&with_expiration=true"), - DISCORD_USER_TOKEN - ? fetch("https://discord.com/api/v9/guilds/1214371687114477618/roles/member-counts", { - headers: { - Authorization: DISCORD_USER_TOKEN, - }, - }) - : Promise.resolve({ json: () => roleMembers }), - GITLAB_API_TOKEN - ? fetch("https://gitlab.com/api/v4/projects/suyu-emu%2Fsuyu/", { - headers: { - Authorization: `Bearer ${GITLAB_API_TOKEN}`, - }, - }) - : Promise.resolve({ json: () => ({ star_count: 0 }) }), // Default to 0 if no token is provided - ]; +async function fetchServerSideData() { + console.log("Fetching member count"); - const [res, roles, gitlabRes] = await Promise.all(promises); - const jsonPromises = [res.json(), roles.json(), gitlabRes.json()]; - const [resJson, rolesJson, gitlabResJson] = await Promise.all(jsonPromises); + const promises = [ + fetch("https://discord.com/api/v9/invites/suyu?with_counts=true&with_expiration=true"), + DISCORD_USER_TOKEN + ? fetch("https://discord.com/api/v9/guilds/1214371687114477618/roles/member-counts", { + headers: { + Authorization: DISCORD_USER_TOKEN, + }, + }) + : Promise.resolve({ json: () => roleMembers }), + GITLAB_API_TOKEN + ? fetch("https://gitlab.com/api/v4/projects/suyu-emu%2Fsuyu/", { + headers: { + Authorization: `Bearer ${GITLAB_API_TOKEN}`, + }, + }) + : Promise.resolve({ json: () => ({ star_count: 0 }) }), // Default to 0 if no token is provided + ]; - memberCount = resJson.approximate_member_count; - starCount = gitlabResJson.star_count; - if (DISCORD_USER_TOKEN) roleMembers = rolesJson; + const [res, roles, gitlabRes] = await Promise.all(promises); + const jsonPromises = [res.json(), roles.json(), gitlabRes.json()]; + const [resJson, rolesJson, gitlabResJson] = await Promise.all(jsonPromises); - console.log("Member count:", memberCount); - console.log("Stars count:", starCount); + memberCount = resJson.approximate_member_count; + starCount = gitlabResJson.star_count; + if (DISCORD_USER_TOKEN) roleMembers = rolesJson; + + console.log("Member count:", memberCount); + console.log("Stars count:", starCount); } if (!building) { - await setMemberCount(); - setInterval(setMemberCount, 1000 * 60 * 10); + await fetchServerSideData(); + setInterval(fetchServerSideData, 1000 * 60 * 10); } export async function load(opts) { - return { - memberCount, - starCount, - roleMembers, - }; + return { + memberCount, + starCount, + roleMembers, + }; } diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte index a7d0c5b..3b92465 100644 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -8,7 +8,7 @@ export let data: PageData; $: memberCount = parseFloat(data.memberCount.toPrecision(2)); $: contributors = parseFloat(data.roleMembers["1214817156420862012"].toPrecision(2)); - $: starCount = data.starCount; + $: starCount = parseFloat(data.starCount.toPrecision(2)); let metadata = { url: "https://suyu.dev", title: "suyu - Open-source, non-profit Switch emulator", @@ -162,7 +162,7 @@

{starCount}+

-
Gitlab stars
+
GitLab stars

@@ -239,8 +239,8 @@ >

Discord

- Join our Discord server to chat with {memberCount}+ suyu users and developers. Get the latest - updates and help with any issues you have. + Join our Discord server to chat with {memberCount}+ suyu users and developers. Get the + latest updates and help with any issues you have.