From 5226124e477aa1c706960b7c56fafd001ce7e007 Mon Sep 17 00:00:00 2001 From: mat Date: Sat, 5 Mar 2022 16:58:43 -0600 Subject: donators --- .gitignore | 1 + package.json | 6 +++--- scripts/updateDonators.js | 49 ++++++++++++++++++++++++++++++++++++++++++++++ src/_donators.json | 1 - src/routes/election.svelte | 10 ++++++++-- svelte.config.js | 49 ---------------------------------------------- 6 files changed, 61 insertions(+), 55 deletions(-) create mode 100644 scripts/updateDonators.js delete mode 100644 src/_donators.json diff --git a/.gitignore b/.gitignore index 91361e6..ff6e124 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,4 @@ node_modules .yarn /.vercel_build_output +/src/_donators.json \ No newline at end of file diff --git a/package.json b/package.json index 9d43bae..378d7fc 100644 --- a/package.json +++ b/package.json @@ -3,8 +3,8 @@ "version": "0.0.1", "license": "MIT", "scripts": { - "dev": "svelte-kit dev", - "build": "svelte-kit build", + "dev": "node ./scripts/updateDonators.js && svelte-kit dev", + "build": "node ./scripts/updateDonators.js && svelte-kit build", "package": "svelte-kit package", "preview": "svelte-kit preview", "check": "svelte-check --tsconfig ./tsconfig.json", @@ -41,4 +41,4 @@ "skyblock-assets": "^2.0.7" }, "packageManager": "yarn@3.1.1" -} +} \ No newline at end of file diff --git a/scripts/updateDonators.js b/scripts/updateDonators.js new file mode 100644 index 0000000..f350484 --- /dev/null +++ b/scripts/updateDonators.js @@ -0,0 +1,49 @@ +import fs from 'fs' +import https from 'https' + +const API_URL = 'https://skyblock-api.matdoes.dev/' + +function fetch(url) { + return new Promise((resolve, reject) => { + let data = '' + + const req = https.request(new URL(url), res => { + res.on('data', d => { + data += d + }) + + res.on('end', () => { + resolve(data) + }) + }) + + req.on('error', error => { + reject(error) + }) + + req.end() + }) +} +function shuffle(a) { + for (let i = a.length - 1; i > 0; i--) { + const j = Math.floor(Math.random() * (i + 1)) + ;[a[i], a[j]] = [a[j], a[i]] + } + return a +} + +// create a donators.json from the donators.txt +const donatorUuidsText = await fs.promises.readFile('src/donators.txt', { + encoding: 'utf8' +}) +const donatorUuids = donatorUuidsText.split('\n').map(u => u.split(' ')[0]).filter(u => u) +const donators = await Promise.all( + donatorUuids.map(u => fetch(`${API_URL}player/${u}`) + .then(r => JSON.parse(r).player) + ) +) +await fs.promises.writeFile( + 'src/_donators.json', + JSON.stringify(shuffle(donators)), + { encoding: 'utf8' } +) diff --git a/src/_donators.json b/src/_donators.json deleted file mode 100644 index 647c633..0000000 --- a/src/_donators.json +++ /dev/null @@ -1 +0,0 @@ -[{"uuid":"951801c9765944bbbd1adf22cc90294e","username":"oxnan","rank":{"name":"MVP+","color":"#3ffefe","colored":"§b[MVP§0+§b]"},"socials":{"discord":"oxnan#1337","forums":"https://hypixel.net/members/oxnan.2000303/"}},{"uuid":"c558970dca7343a79083825bacfa65c7","username":"FingaMan","rank":{"name":"MVP+","color":"#3ffefe","colored":"§b[MVP§4+§b]"},"socials":{"discord":"Fing#8864","forums":"https://hypixel.net/members/fingalickindood.141993/"}},{"uuid":"1915444928b64d8b8973df8044f8cdb7","username":"LeaPhant","rank":{"name":"MVP+","color":"#3ffefe","colored":"§b[MVP§d+§b]"},"socials":{"discord":"LeaPhant#2456","forums":"https://hypixel.net/members/leaphant.687175/"}},{"uuid":"585b739ac76d40a8a95604ea052930c7","username":"Canadas","rank":{"name":"MVP+","color":"#3ffefe","colored":"§b[MVP§0+§b]"},"socials":{"discord":"Canadas#6600","forums":"https://hypixel.net/members/canadas.675999/"}},{"uuid":"7e534bf90cc24b4689e9ab380aab9877","username":"CalebDavisPvP","rank":{"name":"MVP+","color":"#3ffefe","colored":"§b[MVP§c+§b]"},"socials":{"discord":"CalebDavisPvP#1337","forums":null}},{"uuid":"64e5da31f6ae40caa6e77cebd7694df8","username":"ImEvoke","rank":{"name":"MVP+","color":"#3ffefe","colored":"§b[MVP§6+§b]"},"socials":{"discord":"floog 1#5533","forums":null}},{"uuid":"26398ec782e5440cbcbb94c58b8b60a2","username":"jasperazzi","rank":{"name":"MVP+","color":"#3ffefe","colored":"§b[MVP§0+§b]"},"socials":{"discord":"jasperazzi#6422","forums":"https://hypixel.net/members/ceejcake.2183776/"}},{"uuid":"a7b317669c3945a380cd96d1f516b858","username":"nullDorito","rank":{"name":"MVP+","color":"#3ffefe","colored":"§b[MVP§9+§b]"},"socials":{"discord":"rito#3599","forums":"https://hypixel.net/members/nulldorito.3985801/"}},{"uuid":"25e670e88a104f96bbe6f5545170682f","username":"RunicYoungDragon","rank":{"name":"MVP+","color":"#3ffefe","colored":"§b[MVP§8+§b]"},"socials":{"discord":"RunicYoungDragon#8156","forums":"https://hypixel.net/members/minihouse.1063296/"}},{"uuid":"4c6beeff830e4bc1b9bc876e9ee8f357","username":"ItzMonday","rank":{"name":"MVP+","color":"#3ffefe","colored":"§b[MVP§5+§b]"},"socials":{"discord":"ItzMonday#8949","forums":"https://hypixel.net/members/itzmonday.2651009/"}}] \ No newline at end of file diff --git a/src/routes/election.svelte b/src/routes/election.svelte index 7b156b6..15134b1 100644 --- a/src/routes/election.svelte +++ b/src/routes/election.svelte @@ -43,14 +43,19 @@ $: nextSpecialMayorYear = Math.ceil(((data.current?.year || data.previous.year) + 1) / 8) * 8 const specialMayors = ['Scorpius', 'Derpy', 'Jerry'] + let autoInvalidateTimeout: null | NodeJS.Timeout = null + // invalidate at the end of every minute async function autoInvalidate(first: boolean) { - if (browser && !destroyed) { + if (browser) { // don't invalidate the first time the function is called if (!first) await invalidate('') const lastUpdatedAgo = Date.now() - data.last_updated * 1000 - setTimeout(() => autoInvalidate(false), lastUpdatedAgo + 10 * 60 * 1000) + autoInvalidateTimeout = setTimeout( + () => autoInvalidate(false), + lastUpdatedAgo + 10 * 60 * 1000 + ) } } @@ -63,6 +68,7 @@ onDestroy(() => { destroyed = true + if (autoInvalidateTimeout) clearTimeout(autoInvalidateTimeout) }) diff --git a/svelte.config.js b/svelte.config.js index 52db23e..9201300 100644 --- a/svelte.config.js +++ b/svelte.config.js @@ -1,57 +1,8 @@ // import adapter from '@sveltejs/adapter-cloudflare' import adapter from '@sveltejs/adapter-auto' import preprocess from 'svelte-preprocess' -import fs from 'fs' -import https from 'https' -function fetch(url) { - return new Promise((resolve, reject) => { - let data = '' - const req = https.request(new URL(url), res => { - res.on('data', d => { - data += d - }) - - res.on('end', () => { - resolve(data) - }) - }) - - req.on('error', error => { - reject(error) - }) - - req.end() - }) -} -function shuffle(a) { - for (let i = a.length - 1; i > 0; i--) { - const j = Math.floor(Math.random() * (i + 1)) - ;[a[i], a[j]] = [a[j], a[i]] - } - return a -} - -(async () => { - const API_URL = 'https://skyblock-api.matdoes.dev/' - - // create a donators.json from the donators.txt - const donatorUuidsText = await fs.promises.readFile('src/donators.txt', { - encoding: 'utf8' - }) - const donatorUuids = donatorUuidsText.split('\n').map(u => u.split(' ')[0]).filter(u => u) - const donators = await Promise.all( - donatorUuids.map(u => fetch(`${API_URL}player/${u}`) - .then(r => JSON.parse(r).player) - ) - ) - await fs.promises.writeFile( - 'src/_donators.json', - JSON.stringify(donators), - { encoding: 'utf8' } - ) -})() /** @type {import('@sveltejs/kit').Config} */ const config = { -- cgit