Skip to content

Commit

Permalink
make maps const
Browse files Browse the repository at this point in the history
  • Loading branch information
Owen3H committed Jun 22, 2024
1 parent a2ae279 commit 4605178
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 39 deletions.
7 changes: 4 additions & 3 deletions src/api/dynmap/Dynmap.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type {
MapResponse, PlayersResponse,
Point2D, TownBounds, ValidMapName
Point2D, TownBounds,
DynmapMap
} from 'types'

import * as fn from 'utils/functions.js'
Expand All @@ -22,11 +23,11 @@ class Dynmap extends DataHandler {
//#endregion

//#region Map-specific properties
readonly name: ValidMapName
readonly name: DynmapMap
readonly inviteRange: number
//#endregion

constructor(mapName: ValidMapName) {
constructor(mapName: DynmapMap) {
super(mapName)

this.name = mapName
Expand Down
20 changes: 15 additions & 5 deletions src/api/squaremap/Squaremap.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type {
Point2D,
SquaremapMap,
SquaremapMapResponse,
ValidMapName
SquaremapPlayersResponse
} from "types"

import DataHandler from "helpers/DataHandler.js"
Expand All @@ -11,6 +12,8 @@ import Nations from './Nations.js'
import Players from "./Players.js"
import Residents from "./Residents.js"

import { parsePlayers } from "./parser.js"

class Squaremap extends DataHandler {
//#region Data classes
readonly Towns: Towns
Expand All @@ -21,11 +24,13 @@ class Squaremap extends DataHandler {
//#endregion

//#region Map-specific properties
readonly name: ValidMapName
readonly name: SquaremapMap
readonly inviteRange: number = 3500
//#endregion

constructor(mapName: ValidMapName) {
cacheTTL = 60

constructor(mapName: SquaremapMap) {
super(mapName)

this.name = mapName
Expand All @@ -38,12 +43,17 @@ class Squaremap extends DataHandler {
// this.GPS = new GPS(this)
}

markerset = async () => {
readonly onlinePlayerData = async() => {
const res = await this.playerData<SquaremapPlayersResponse>()
return parsePlayers(res.players)
}

readonly markerset = async() => {
const res = await this.mapData<SquaremapMapResponse>()
return res.find(x => x.id == "towny")
}

buildMapLink = (zoom?: number, location?: Point2D): URL => {
readonly buildMapLink = (zoom?: number, location?: Point2D): URL => {
const url = new URL(`https://map.earthmc.net/?mapname=flat`)
if (zoom) url.searchParams.append("zoom", zoom.toString())

Expand Down
4 changes: 2 additions & 2 deletions src/api/squaremap/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type {
Point2D,
Resident,
SquaremapMarkerset,
SquaremapPlayersResponse,
SquaremapRawPlayer,
SquaremapTown
} from 'types'

Expand Down Expand Up @@ -124,7 +124,7 @@ const parseResidents = (towns: SquaremapTown[]) => {
return residentsArray
}

const parsePlayers = async(res: SquaremapPlayersResponse) => {
const parsePlayers = async(res: SquaremapRawPlayer[]) => {

}

Expand Down
20 changes: 11 additions & 9 deletions src/helpers/DataHandler.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,33 @@
import * as endpoint from 'utils/endpoint.js'

import { Mutex } from 'async-mutex'
import type { ValidMapName } from 'types'
import type { AnyMap } from 'types'

class DataHandler {
#isNode = true
#cache: any

#map: ValidMapName
#map: AnyMap
get map() { return this.#map }

#cache: any
#cacheTTL: number
#cacheLock: Mutex

constructor(mapName: ValidMapName) {
#isNode = true

constructor(mapName: AnyMap, cacheTTL = 60) {
this.#map = mapName
this.#isNode = globalThis.process?.release?.name == 'node'

this.#cacheLock = new Mutex()
this.#cacheTTL = cacheTTL
}

private createCache = async (ttl = 120*1000) => {
private createCache = async () => {
const release = await this.#cacheLock.acquire()
let cacheInstance = null

try {
//@ts-expect-error
cacheInstance = import('timed-cache').then(tc => new tc.default({ defaultTtl: ttl }))
//@ts-expect-error
cacheInstance = import('timed-cache').then(tc => new tc.default({ defaultTtl: this.#cacheTTL }))
} catch (e) {
console.error(e)
} finally {
Expand Down
19 changes: 5 additions & 14 deletions src/types/dynmap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export type UpdatedTile = {
export type ConfigResponse = {
updaterate: number
components: Array<ConfigComponent>
worlds: ConfigWorld[]
worlds: WorldConfig[]
confighash: number
defaultmap: MapTypeName
title: string
Expand Down Expand Up @@ -141,16 +141,16 @@ type BaseComponent = {
type: string
}

type ConfigWorld = {
type WorldConfig = {
sealevel: boolean
protected: boolean
maps: ConfigMap[]
maps: MapConfig[]
center: Location
}

type MapTypeName = "flat" | "surface" | "Flat" | "Surface"

type ConfigMap = {
type MapConfig = {
name: MapTypeName
scale: number
icon?: string
Expand All @@ -176,13 +176,4 @@ type ConfigMap = {
mapzoomin: number
mapzoomout: number
boostzoom: number
}

const Maps = {
aurora: "aurora",
nova: "nova",
Aurora: "Aurora",
Nova: "Nova"
} as const

export type ValidMapName = typeof Maps[keyof typeof Maps] | `${string}aurora`
}
17 changes: 16 additions & 1 deletion src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { Prettify, ValuesOf } from './util.js'

export * from './gps.js'
export * from './town.js'
export * from './nation.js'
Expand All @@ -8,4 +10,17 @@ export * from './dynmap.js'
export * from './squaremap.js'

export * from './oapi.js'
export * from './util.js'
export * from './util.js'

export const Maps = {
Squaremap: {
AURORA: 'aurora'
},
Dynmap: {
NOVA: 'nova'
}
} as const

export type SquaremapMap = ValuesOf<typeof Maps.Squaremap>
export type DynmapMap = ValuesOf<typeof Maps.Dynmap>
export type AnyMap = Prettify<SquaremapMap | DynmapMap>
2 changes: 1 addition & 1 deletion src/types/squaremap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,4 @@ type SquaremapPoints = {
points?: Point2D[][][] | Point2D[]
}

export type SquaremapArea = Prettify<SquaremapMarker & CommonFields & SquaremapPoints>
export type SquaremapArea = Prettify<SquaremapMarker & CommonFields & SquaremapPoints>
2 changes: 2 additions & 0 deletions src/types/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export type Prettify<T> = {
[K in keyof T]: T[K]
} & unknown

export type ValuesOf<T> = T[keyof T]

export type AssertPositive<N extends number> = number extends N ? N : `${N}` extends `-${string}` ? never : N;

export type StringStartsWith<T extends string> = `${T}${string}`
Expand Down
8 changes: 4 additions & 4 deletions src/utils/endpoint.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import endpoints from '../endpoints.json'

import { request, type Dispatcher } from "undici"
import type { ValidMapName } from "types"
import type { AnyMap } from "types"

import { genRandomString } from './functions.js'

Expand Down Expand Up @@ -51,10 +51,10 @@ const getArchive = async (url: string, unixTs = Date.now()) => {
return await asJSON(`https://web.archive.org/web/${formattedTs}id_/${decodeURIComponent(url)}`)
}

const configData = async <T>(mapName: ValidMapName): Promise<T> => asJSON(get("config", mapName)) as T
const playerData = async <T>(mapName: ValidMapName): Promise<T> => asJSON(get("players", mapName))
const configData = async <T>(mapName: AnyMap): Promise<T> => asJSON(get("config", mapName)) as T
const playerData = async <T>(mapName: AnyMap): Promise<T> => asJSON(get("players", mapName))

const mapData = async <T>(mapName: ValidMapName): Promise<T> => {
const mapData = async <T>(mapName: AnyMap): Promise<T> => {
const url = mapName.toLowerCase() == 'aurora' ? get('squaremap', 'map') : get("map", mapName)
return !archiveTs ? asJSON(url) : getArchive(url, archiveTs)
}
Expand Down

0 comments on commit 4605178

Please sign in to comment.