Skip to content

Commit

Permalink
constant resident lookup in townless filter. O(1) < O(n)
Browse files Browse the repository at this point in the history
  • Loading branch information
Owen3H committed Oct 16, 2023
1 parent 000ebc0 commit 4ecc3bf
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/classes/OAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 8 additions & 9 deletions src/classes/Players.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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("<br />"),
info = rawinfo.map(x => striptags(x))

Expand All @@ -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
})
}

Expand Down
24 changes: 12 additions & 12 deletions src/types/town.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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
}
// export type TownRanks = {
// [key: string]: string
// }

0 comments on commit 4ecc3bf

Please sign in to comment.