From 0403e07daf749bd3cb0b5fd4b2b0b4e71636988b Mon Sep 17 00:00:00 2001 From: Owen Date: Tue, 10 Oct 2023 15:53:44 +0100 Subject: [PATCH] guh --- src/classes/Nations.ts | 6 ++++-- src/classes/Towns.ts | 5 +++-- src/utils/endpoint.ts | 14 +++++++------- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/classes/Nations.ts b/src/classes/Nations.ts index bad47b9..1d91021 100644 --- a/src/classes/Nations.ts +++ b/src/classes/Nations.ts @@ -50,12 +50,14 @@ class Nations implements Base { const town = towns[i], nationName = town.nation - if (nationName == "No Nation") continue + if (nationName == "No Nation") { + continue + } // Doesn't already exist, create new. if (!raw[nationName]) { raw[nationName] = { - name: town.nation, + name: nationName, residents: town.residents, towns: [], area: 0, diff --git a/src/classes/Towns.ts b/src/classes/Towns.ts index 6eb1e1d..c9a8444 100644 --- a/src/classes/Towns.ts +++ b/src/classes/Towns.ts @@ -62,12 +62,13 @@ class Towns implements Base { rawinfo = town.desc.split("
"), info = rawinfo.map(i => striptags(i, ['a'])) - if (info[0].includes("(Shop)")) continue + const firstEl = info[0] + if (firstEl.includes("(Shop)")) continue const mayor = info[1].slice(7) if (mayor == "") continue - let split: string | string[] = info[0].split(" (") + let split: string | string[] = firstEl.split(" (") split = (split[2] ?? split[1]).slice(0, -1) const residents = info[2].slice(9).split(", "), diff --git a/src/utils/endpoint.ts b/src/utils/endpoint.ts index 2489779..fb2ca15 100644 --- a/src/utils/endpoint.ts +++ b/src/utils/endpoint.ts @@ -20,24 +20,24 @@ const get = (dataType: keyof typeof endpoints, map: ValidMapName) => { * and retrieve the response as a **JSON** object. * * This method does the following: - * - Uses a proxy in browser runtimed to bypass CORS. - * - + * - Uses a proxy when in browser environment to bypass CORS. + * - Retries a failed request up to 3 times by default. * * @param url - The full URL to send the request to. - * @param n - The number of retries to attempt. + * @param retries - The amount of retries to attempt before erroring. */ -const asJSON = async (url: string, /** @defaultValue `3` */ n = 3) => { +const asJSON = async (url: string, retries = 3) => { const isBrowser = typeof window === "object" if (isBrowser) url = `https://corsproxy.io/?${encodeURIComponent(url)}` const res = await request(url) .then((res: any) => res.body?.json() || res.json()) - .catch(async err => await retry(err, url, n)) + .catch(async err => await retry(err, url, retries)) - return res ?? await retry(null, url, n) + return res ?? await retry(null, url, retries) } -const retry = (val: any, url: string, n: number): any => n === 1 ? val : asJSON(url, n - 1) +const retry = (val: any, url: string, amt: number): any => amt === 1 ? val : asJSON(url, amt - 1) let archiveTs = 0 const useArchive = (ts: number) => archiveTs = ts