From 4ecc3bf10df5c8fc590e92c1fe1f4263fde113bc Mon Sep 17 00:00:00 2001 From: Owen Date: Mon, 16 Oct 2023 17:30:23 +0100 Subject: [PATCH] constant resident lookup in townless filter. O(1) < O(n) --- src/classes/OAPI.ts | 2 +- src/classes/Players.ts | 17 ++++++++--------- src/types/town.ts | 24 ++++++++++++------------ 3 files changed, 21 insertions(+), 22 deletions(-) diff --git a/src/classes/OAPI.ts b/src/classes/OAPI.ts index 392d0a0..7593c13 100644 --- a/src/classes/OAPI.ts +++ b/src/classes/OAPI.ts @@ -90,7 +90,7 @@ const parseNation = (nation: RawNation) => { } const ParamErr = () => new SyntaxError(`Parameter 'name' is invalid. Must be of type string!`) -const FetchErr = (type, name) => new FetchError(`Could not fetch ${type} '${name}'. Invalid response received!`) +const FetchErr = (type: string, name: string) => new FetchError(`Could not fetch ${type} '${name}'. Invalid response received!`) class OfficialAPI { static serverInfo = async () => await townyData() as RawServerInfo diff --git a/src/classes/Players.ts b/src/classes/Players.ts index 096e425..1363d54 100644 --- a/src/classes/Players.ts +++ b/src/classes/Players.ts @@ -42,18 +42,18 @@ class Players implements Base { readonly townless = async () => { const mapData = await endpoint.mapData("aurora") - if (!mapData) return new FetchError('Error fetching townless! Please try again.') + if (!mapData) throw new FetchError('Error fetching townless! Please try again.') const onlinePlayers = await this.online() if (!onlinePlayers) return null const allResidents: string[] = [], markerset = mapData.sets["townyPlugin.markerset"], - townData = Object.keys(markerset.areas).map(key => markerset.areas[key]) + areas = Object.values(markerset.areas) - const len = townData.length + const len = areas.length for (let i = 0; i < len; i++) { - const town = townData[i], + const town = areas[i], rawinfo = town.desc.split("
"), info = rawinfo.map(x => striptags(x)) @@ -67,11 +67,10 @@ class Players implements Base { } // Filter out residents & sort alphabetically - return onlinePlayers.filter(op => !allResidents.find(resident => resident == op.name)).sort((a, b) => { - if (b.name.toLowerCase() < a.name.toLowerCase()) return 1 - if (b.name.toLowerCase() > a.name.toLowerCase()) return -1 - - return 0 + const residentSet = new Set(allResidents) + return onlinePlayers.filter(op => !residentSet.has(op.name)).sort((a, b) => { + const [aName, bName] = [a.name.toLowerCase(), b.name.toLowerCase()] + return bName < aName ? 1 : bName > aName ? -1 : 0 }) } diff --git a/src/types/town.ts b/src/types/town.ts index ab9d28f..905fbca 100644 --- a/src/types/town.ts +++ b/src/types/town.ts @@ -24,11 +24,9 @@ export type Town = { wiki?: string } -export type TownStatus = { - open: boolean - neutral: boolean - overclaimed: boolean - ruined: boolean +export type TownBounds = { + x: number[] + z: number[] } export type TownFlags = { @@ -40,11 +38,13 @@ export type TownFlags = { capital: boolean } -export type TownBounds = { - x: number[] - z: number[] -} +// export type TownStatus = { +// open: boolean +// neutral: boolean +// overclaimed: boolean +// ruined: boolean +// } -export type TownRanks = { - [key: string]: string -} \ No newline at end of file +// export type TownRanks = { +// [key: string]: string +// } \ No newline at end of file