Skip to content

Commit

Permalink
types + gps stuff (read desc)
Browse files Browse the repository at this point in the history
- EventEmitter now has proper inferencing with a type parameter that takes in a record of events.
- Fixed a problem with the silly `mitt` library not exporting the default function correctly, causing props to be `any`.
- Fixed internal on and off props not being readonly.
- Updated Squaremap GPS to use its own types instead of Dynmap ones.
  • Loading branch information
Owen3H committed Jul 14, 2024
1 parent 831e050 commit 87f165e
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 38 deletions.
28 changes: 21 additions & 7 deletions src/api/dynmap/GPS.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
import type Dynmap from './Dynmap.js'

import {
type Route, Routes, type RouteInfo,
type Location, type Nation, type Player
import { Routes } from '../../types/index.js'
import type {
Route, RouteInfo,
Location, Nation, Player,
StrictPoint2D
} from '../../types/index.js'

import Mitt from '../../helpers/EventEmitter.js'
import Emitter from '../../helpers/EventEmitter.js'
import { manhattan, safeParseInt, strictFalsy } from '../../utils/functions.js'

class GPS extends Mitt {
import type Dynmap from './Dynmap.js'

type GPSEvents = {
error: {
err: string
msg: string
}
underground: string | {
lastLocation: StrictPoint2D,
routeInfo: RouteInfo
}
locationUpdate: RouteInfo
}

class GPS extends Emitter<GPSEvents> {
#map: Dynmap
#emittedUnderground = false
#lastLoc: undefined | {
Expand Down
48 changes: 30 additions & 18 deletions src/api/squaremap/GPS.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
import {
type Route, Routes, type RouteInfo,
type Location, type Nation, type Player
import { Routes } from '../../types/index.js'
import type {
Route, RouteInfo,
Point2D, SquaremapNation, SquaremapPlayer,
StrictPoint2D
} from '../../types/index.js'

import Mitt from '../../helpers/EventEmitter.js'
import Emitter from '../../helpers/EventEmitter.js'
import { manhattan, safeParseInt, strictFalsy } from '../../utils/functions.js'

import type Squaremap from './Squaremap.js'

class GPS extends Mitt {
type GPSEvents = {
error: {
err: string
msg: string
}
underground: string | {
lastLocation: StrictPoint2D,
routeInfo: RouteInfo
}
locationUpdate: RouteInfo
}

class GPS extends Emitter<GPSEvents> {
#map: Squaremap
#emittedUnderground = false
#lastLoc: undefined | {
Expand All @@ -35,20 +49,18 @@ class GPS extends Mitt {
this.#map = map
}

playerIsOnline = (player: Player) => {
if (!player.online) {
this.emit('error', {
err: "INVALID_PLAYER",
msg: "Player is offline or does not exist!"
})
}
playerIsOnline = (player: SquaremapPlayer) => {
if (!player.online) this.emit('error', {
err: "INVALID_PLAYER",
msg: "Player is offline or does not exist!"
})

return player.online
}

readonly track = async(playerName: string, interval = 3000, route = Routes.FASTEST) => {
setInterval(async () => {
const player: Player = await this.map.Players.get(playerName).catch(e => {
const player: SquaremapPlayer = await this.map.Players.get(playerName).catch(e => {
this.emit('error', { err: "FETCH_ERROR", msg: e.message })
return null
})
Expand Down Expand Up @@ -98,10 +110,10 @@ class GPS extends Mitt {
return this
}

readonly safestRoute = (loc: Location) => this.findRoute(loc, Routes.SAFEST)
readonly fastestRoute = (loc: Location) => this.findRoute(loc, Routes.FASTEST)
readonly safestRoute = (loc: Point2D) => this.findRoute(loc, Routes.SAFEST)
readonly fastestRoute = (loc: Point2D) => this.findRoute(loc, Routes.FASTEST)

readonly findRoute = async(loc: Location, options: Route = Routes.SAFEST) => {
readonly findRoute = async(loc: Point2D, options: Route = Routes.SAFEST) => {
if (strictFalsy(loc.x) || strictFalsy(loc.z)) {
const obj = JSON.stringify(loc)
throw new Error(`Cannot calculate route! One or more inputs are invalid:\n${obj}`)
Expand Down Expand Up @@ -134,7 +146,7 @@ class GPS extends Mitt {
}

// Use reduce to find the minimum distance and corresponding nation
const { distance, nation } = filtered.reduce((acc: RouteInfo, nation: Nation) => {
const { distance, nation } = filtered.reduce((acc: RouteInfo, nation: SquaremapNation) => {
const dist = manhattan(
safeParseInt(nation.capital.x), safeParseInt(nation.capital.z),
safeParseInt(loc.x), safeParseInt(loc.z)
Expand Down Expand Up @@ -162,7 +174,7 @@ class GPS extends Mitt {
* @param origin The location where something is currently at.
* @param destination The location we wish to arrive at.
*/
static cardinalDirection(origin: Location, destination: Location) {
static cardinalDirection(origin: Point2D, destination: Point2D) {
// Calculate the differences in x and z coordinates
const deltaX = safeParseInt(origin.x) - safeParseInt(destination.x)
const deltaZ = safeParseInt(origin.z) - safeParseInt(destination.z)
Expand Down
23 changes: 10 additions & 13 deletions src/helpers/EventEmitter.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
import mitt from 'mitt'
import type { EventType } from 'mitt'

export default class Mitt {
_on: any
export default class Emitter<Events extends Record<EventType, unknown>> {
private _on
get on() {
return this._on
}

_off: any

private _off
get off() {
return this._off
}

protected emit: any
protected emit

constructor() {
//@ts-ignore
const emitter = mitt()

Object.keys(emitter).forEach(() => {
this._on = emitter['on']
this._off = emitter['off']
this.emit = emitter['emit']
})
const emitter = mitt.default<Events>()

this._on = emitter.on
this._off = emitter.off
this.emit = emitter.emit
}
}

0 comments on commit 87f165e

Please sign in to comment.