Skip to content

Commit

Permalink
buh
Browse files Browse the repository at this point in the history
  • Loading branch information
Owen3H committed Oct 26, 2023
1 parent 2d46453 commit bce0cfe
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 23 deletions.
60 changes: 41 additions & 19 deletions src/api/GPS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,30 @@ const NativeMap = globalThis.Map

class GPS extends Mitt {
static readonly Routes = Routes
private emittedUnderground = false
private lastLoc: undefined | {

#emittedUnderground = false

get emittedUnderground() {
return this.#emittedUnderground
}

protected set emittedUnderground(val: boolean) {
this.#emittedUnderground = val
}

#lastLoc: undefined | {
x: number
z: number
}

get lastLoc() {
return this.#lastLoc
}

protected set lastLoc(val: { x: number, z: number }) {
this.#lastLoc = val
}

#map: Map

get map() { return this.#map }
Expand All @@ -30,27 +48,31 @@ class GPS extends Mitt {
this.#map = map
}

readonly getPlayer = async (name: string) => {
#getPlayer = async (name: string) => {
const player = await this.map.Players.get(name)
return player
}

readonly playerIsOnline = (player: Player) => {
if (!player.online)
this.emit('error', { err: "INVALID_PLAYER", msg: "Player is offline or does not exist!" })
#playerIsOnline = (player: Player) => {
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 = await this.getPlayer(playerName).catch(e => {
const player = await this.#getPlayer(playerName).catch(e => {
this.emit('error', { err: "FETCH_ERROR", msg: e.message })
return null
}) as Player

if (!player) return
if (!this.playerIsOnline(player)) return
if (!this.#playerIsOnline(player)) return

if (player.underground) {
if (!this.emittedUnderground) {
Expand Down Expand Up @@ -149,24 +171,24 @@ class GPS extends Mitt {
return { nation, distance, direction } as RouteInfo
}

static cardinalDirection(loc1: Location, loc2: Location) {
static cardinalDirection(origin: Location, destination: Location) {
// Calculate the differences in x and z coordinates
const deltaX = fn.safeParseInt(loc2.x) - fn.safeParseInt(loc1.x)
const deltaZ = fn.safeParseInt(loc2.z) - fn.safeParseInt(loc1.z)

const angleRad = Math.atan2(deltaZ, deltaX) // Calculate the angle in radians
const angleDeg = (angleRad * 180) / Math.PI // Convert the angle from radians to degrees
const deltaX = fn.safeParseInt(origin.x) - fn.safeParseInt(destination.x)
const deltaZ = fn.safeParseInt(origin.z) - fn.safeParseInt(destination.z)

// Calculates radians with atan2, then converted to degrees.
const angle = Math.atan2(deltaZ, deltaX) * 180 / Math.PI

// Determine the cardinal direction
if (angleDeg >= -45 && angleDeg < 45)
if (angle >= -45 && angle < 45)
return "east"
if (angleDeg >= 45 && angleDeg < 135)

if (angle >= 45 && angle < 135)
return "north"

if (angleDeg >= 135 || angleDeg < -135)
if (angle >= 135 || angle < -135)
return "west"

return "south"
}
}
Expand Down
15 changes: 15 additions & 0 deletions src/api/TownFlow.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//import * as fn from '../utils/functions.js'
import { Map } from '../Map.js'

import Mitt from '../helpers/EventEmitter.js'

export class TownFlow extends Mitt {
#map: Map

get map() { return this.#map }

constructor(map: Map) {
super()
this.#map = map
}
}
6 changes: 3 additions & 3 deletions src/utils/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ const sqr = (a: Point2D, b: Point2D, range: number) => Math.hypot(
const average = (nums: Point2D[], key: keyof Point2D) => {
const sum = nums.map(obj => obj[key]).reduce((a, b) => safeParseInt(a) + safeParseInt(b))
return safeParseInt(sum) / nums.length
}
}

// TODO: Ensure this is returning T[] and not a string of names.
const getExisting = <T>(a1: T[], a2: string[], key: keyof T) => {
Expand Down Expand Up @@ -99,8 +99,8 @@ function genRandomString(maxAmount = 20) {
const len = validChars.length

for (let i = 0; i < maxAmount; i++) {
const randomIndex = Math.floor(Math.random() * len)
token += validChars.charAt(randomIndex)
const randomIndex = Math.floor(Math.random() * len)
token += validChars.charAt(randomIndex)
}

return token
Expand Down
2 changes: 1 addition & 1 deletion tests/gps.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { describe, it, expect } from 'vitest'
import { NotFoundError } from '../src/utils/errors'

describe('GPS', () => {
const sampleLoc = { x: -5100, z: 240 }
const sampleLoc = { x: -8000, z: 100 }

it('can find the safest route', async () => {
const route = await globalThis.Aurora.GPS.safestRoute(sampleLoc)
Expand Down

0 comments on commit bce0cfe

Please sign in to comment.