Skip to content

Commit

Permalink
buncha player type fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Owen3H committed Jul 14, 2024
1 parent cc7dd47 commit d31cb63
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 50 deletions.
11 changes: 6 additions & 5 deletions src/api/dynmap/Players.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import type Dynmap from './Dynmap.js'

import type {
MapResponse,
OnlinePlayer, Player,
OnlinePlayer,
Player,
StrictPoint2D
} from '../../types/index.js'

Expand Down Expand Up @@ -37,12 +38,12 @@ class Players implements EntityApi<Player | NotFoundError> {
if (!residents) return

// Loop over residents and merge data for any online players
const merged = residents.map(res => {
const merged: Player[] = residents.map(res => {
const op = onlinePlayers.find(op => op.name === res.name)
return !op ? { ...res, online: false } : { ...res, ...op, online: true }
})

return merged as Player[]
return merged
}

readonly townless = async() => {
Expand Down Expand Up @@ -94,14 +95,14 @@ class Players implements EntityApi<Player | NotFoundError> {
const curOp = onlinePlayers[i]
const foundRes = residents.find(res => res.name === curOp.name)

merged.push({ ...curOp, ...foundRes })
merged.push({ online: true, ...curOp, ...foundRes })
}

return merged
}

readonly nearby = async(location: StrictPoint2D, radius: StrictPoint2D, players?: OnlinePlayer[]) =>
getNearest<OnlinePlayer>(location, radius, players, this.all, true)
getNearest<Partial<Player>>(location, radius, players, this.online, true)
}

export {
Expand Down
28 changes: 16 additions & 12 deletions src/api/squaremap/Players.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@ import type {
EntityApi
} from "../../helpers/EntityApi.js"

import type { OnlinePlayer, Player, StrictPoint2D } from "../../types/index.js"
import type {
SquaremapOnlinePlayer, SquaremapPlayer,
StrictPoint2D
} from "../../types/index.js"

import { FetchError, type NotFoundError } from "../../utils/errors.js"
import { getExisting } from "../../utils/functions.js"
import { parsePopup } from "./parser.js"
import { getNearest } from "../common.js"

class Players implements EntityApi<Player | NotFoundError> {
class Players implements EntityApi<SquaremapPlayer | NotFoundError> {
#map: Squaremap
get map() { return this.#map }

Expand All @@ -34,30 +38,30 @@ class Players implements EntityApi<Player | NotFoundError> {
if (!residents) throw new Error('Error getting all players: Something went wrong getting residents?')

// Loop over residents and merge data for any online players
const merged = residents.map(res => {
const merged: SquaremapPlayer[] = residents.map(res => {
const op = onlinePlayers.find(op => op.name === res.name)
return (!op ? { ...res, online: false } : { ...res, ...op, online: true }) as Player
return (!op ? { ...res, online: false } : { ...res, ...op, online: true })
})

return merged
}

readonly online = async(includeResidentInfo = false) => {
const onlinePlayers = await this.map.onlinePlayerData()
if (!onlinePlayers) return null
if (!onlinePlayers) return null // TODO: Should probably throw a proper err
if (!includeResidentInfo) return onlinePlayers

const residents = await this.map.Residents.all()
if (!residents) return null
if (!residents) return onlinePlayers

const merged: Player[] = []
const len = onlinePlayers.length
const merged: SquaremapPlayer[] = []
const opsLen = onlinePlayers.length

for (let i = 0; i < len; i++) {
for (let i = 0; i < opsLen; i++) {
const curOp = onlinePlayers[i]
const foundRes = residents.find(res => res.name === curOp.name)

merged.push({ ...curOp, ...foundRes })
merged.push({ online: true, ...curOp, ...foundRes })
}

return merged
Expand Down Expand Up @@ -91,8 +95,8 @@ class Players implements EntityApi<Player | NotFoundError> {
})
}

readonly nearby = async(location: StrictPoint2D, radius: StrictPoint2D, players?: OnlinePlayer[]) =>
getNearest<OnlinePlayer>(location, radius, players, this.all, true)
readonly nearby = async(location: StrictPoint2D, radius: StrictPoint2D, players?: SquaremapOnlinePlayer[]) =>
getNearest<Partial<SquaremapPlayer>>(location, radius, players, this.online, true)
}

export {
Expand Down
11 changes: 7 additions & 4 deletions src/api/squaremap/Towns.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import type Squaremap from "./Squaremap.js"
import type { Nation, SquaremapTown, StrictPoint2D } from "../../types/index.js"
import type {
SquaremapNation, SquaremapTown,
StrictPoint2D
} from "../../types/index.js"

import type { EntityApi } from "../../helpers/EntityApi.js"
import { parseTowns } from "./parser.js"
Expand All @@ -23,7 +26,7 @@ class Towns implements EntityApi<SquaremapTown | NotFoundError> {
readonly fromNation = async(nationName: string) => {
if (!nationName) throw new InvalidError(`Parameter 'nation' is ${nationName}`)

const nation = await this.map.Nations.get(nationName) as Nation
const nation = await this.map.Nations.get(nationName) as SquaremapNation
if (nation instanceof Error) throw nation

return await this.get(...nation.towns)
Expand Down Expand Up @@ -57,14 +60,14 @@ class Towns implements EntityApi<SquaremapTown | NotFoundError> {

// TODO: Maybe put this into common.ts ?
readonly invitable = async(nationName: string, includeBelonging = false) => {
const nation = await this.map.Nations.get(nationName)
const nation = await this.map.Nations.get(nationName) as SquaremapNation
if (nation instanceof NotFoundError) throw new Error("Error checking invitable: Nation does not exist!")
if (!nation) throw new Error("Error checking invitable: Could not fetch the nation!")

const towns = await this.all()
if (!towns) throw new FetchError('An error occurred fetching towns!')

return towns.filter(t => isInvitable(t, nation as Nation, this.map.inviteRange, includeBelonging))
return towns.filter(t => isInvitable(t, nation, this.map.inviteRange, includeBelonging))
}

readonly totalWealth = async() => {
Expand Down
7 changes: 3 additions & 4 deletions src/api/squaremap/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type {
Resident,
SquaremapMarkerset,
SquaremapNation,
SquaremapPlayer,
SquaremapOnlinePlayer,
SquaremapRawPlayer,
SquaremapTown,
StrictPoint2D
Expand Down Expand Up @@ -261,15 +261,14 @@ export const parseResidents = (towns: SquaremapTown[]) => towns.reduce((acc: Res
return acc
}, [])

const editPlayerProps = (player: SquaremapRawPlayer): SquaremapPlayer => ({
const editPlayerProps = (player: SquaremapRawPlayer): SquaremapOnlinePlayer => ({
name: player.name,
nickname: striptags(formatString(player.display_name)),
x: player.x,
z: player.z,
yaw: player.yaw,
underground: player.world != 'earth',
world: player.world,
online: true
world: player.world
})

export const parsePlayers = (players: SquaremapRawPlayer[]) => {
Expand Down
10 changes: 6 additions & 4 deletions src/types/gps.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { Prettify } from "./util.js"

const createRoute = (
avoidPvp: boolean,
avoidPublic: boolean
Expand All @@ -15,13 +17,13 @@ export type RouteKey = keyof RouteType

export type Route = RouteType[RouteKey]

export type Location = Point2D & {
export type Location = Prettify<Point2D & {
y?: number | string
}
}>

export type SquaremapLocation = Point2D & {
export type SquaremapLocation = Prettify<Point2D & {
yaw?: number | string
}
}>

export type Point2D = {
x: number | string
Expand Down
15 changes: 9 additions & 6 deletions src/types/player.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import type { Location, Resident, SquaremapLocation } from '../types/index.js'
import type { Location, Prettify, Resident, SquaremapLocation } from '../types/index.js'

export type ParsedPlayer = {
name: string
nickname?: string
underground?: boolean
world?: string
online: boolean
}

export type OnlinePlayer = ParsedPlayer & Location
export type Player = (Resident & OnlinePlayer) | OnlinePlayer
export type OnlinePlayer = Prettify<ParsedPlayer & Location>
export type Player = Prettify<Resident & Partial<OnlinePlayer> & {
online: boolean
}>

export type SquaremapOnlinePlayer = ParsedPlayer & SquaremapLocation
export type SquaremapPlayer = (Resident & SquaremapOnlinePlayer) | SquaremapOnlinePlayer
export type SquaremapOnlinePlayer = Prettify<ParsedPlayer & SquaremapLocation>
export type SquaremapPlayer = Prettify<Resident & Partial<SquaremapOnlinePlayer> & {
online: boolean
}>
14 changes: 8 additions & 6 deletions src/utils/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import { removeDiacritics } from "modern-diacritics"

import type {
Point2D,
RawPlayer, Player, Town,
RawPlayer, Town,
BaseTown, BaseNation,
StrictPoint2D
StrictPoint2D,
OnlinePlayer
} from '../types/index.js'

import { NotFound } from './errors.js'
Expand Down Expand Up @@ -39,13 +40,14 @@ export function editPlayerProps(props: RawPlayer[]) {
throw new TypeError("Can't edit player props! Type isn't of object or array.")
}

export const editPlayerProp = (player: RawPlayer): Player => ({
export const editPlayerProp = (player: RawPlayer): OnlinePlayer => ({
name: player.account,
nickname: striptags(player.name),
x: player.x, y: player.y, z: player.z,
x: player.x,
y: player.y,
z: player.z,
underground: player.world != 'earth',
world: player.world,
online: true
world: player.world
})

export const roundToNearest16 = (num: number) => Math.round(num / 16) * 16
Expand Down
25 changes: 16 additions & 9 deletions tests/squaremap/players.test.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,42 @@
import { describe, it, expect, assertType } from 'vitest'
import { Player } from '../../src/types'
import { SquaremapOnlinePlayer, SquaremapPlayer } from '../../src/types'

import { Aurora } from '../../src/main'

describe('[Squaremap/Aurora] Players', () => {
it('can get all players (online + residents)', async () => {
const all = await Aurora.Players.all()
const all = await Aurora.Players.all() as SquaremapPlayer[]

expect(all).toBeTruthy()
assertType<Player[]>(all)
assertType<SquaremapPlayer[]>(all)
})

it('can get online players', async () => {
const ops = await Aurora.Players.online()
const ops = await Aurora.Players.online() as SquaremapPlayer[]

expect(ops).toBeTruthy()
assertType<Player[]>(ops)
assertType<SquaremapPlayer[]>(ops)
})

it('can get online players (with resident info)', async () => {
const ops = await Aurora.Players.online(true) as SquaremapPlayer[]

expect(ops).toBeTruthy()
expect(ops.every(x => x.online == true)).toBeTruthy()
})

it('can get single online player', async () => {
const op = await Aurora.Players.get('Alan_yy') as Player
const op = await Aurora.Players.get('Alan_yy') as SquaremapOnlinePlayer

expect(op).toBeTruthy()
assertType<Player | Player[]>(op)
assertType<SquaremapOnlinePlayer | SquaremapOnlinePlayer[]>(op)
})

it('can get townless players', async () => {
const townless = await Aurora.Players.townless()
const townless = await Aurora.Players.townless() as SquaremapPlayer[]

expect(townless).toBeTruthy()
assertType<Player[]>(townless)
assertType<SquaremapPlayer[]>(townless)

//console.log(townless)
})
Expand Down

0 comments on commit d31cb63

Please sign in to comment.