From d31cb638ea625ae4c9ce13b11df3556e79caa302 Mon Sep 17 00:00:00 2001 From: Owen Date: Mon, 15 Jul 2024 00:47:58 +0100 Subject: [PATCH] buncha player type fixes --- src/api/dynmap/Players.ts | 11 ++++++----- src/api/squaremap/Players.ts | 28 ++++++++++++++++------------ src/api/squaremap/Towns.ts | 11 +++++++---- src/api/squaremap/parser.ts | 7 +++---- src/types/gps.ts | 10 ++++++---- src/types/player.ts | 15 +++++++++------ src/utils/functions.ts | 14 ++++++++------ tests/squaremap/players.test.ts | 25 ++++++++++++++++--------- 8 files changed, 71 insertions(+), 50 deletions(-) diff --git a/src/api/dynmap/Players.ts b/src/api/dynmap/Players.ts index 2724519..0c31a83 100644 --- a/src/api/dynmap/Players.ts +++ b/src/api/dynmap/Players.ts @@ -3,7 +3,8 @@ import type Dynmap from './Dynmap.js' import type { MapResponse, - OnlinePlayer, Player, + OnlinePlayer, + Player, StrictPoint2D } from '../../types/index.js' @@ -37,12 +38,12 @@ class Players implements EntityApi { 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() => { @@ -94,14 +95,14 @@ class Players implements EntityApi { 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(location, radius, players, this.all, true) + getNearest>(location, radius, players, this.online, true) } export { diff --git a/src/api/squaremap/Players.ts b/src/api/squaremap/Players.ts index ba4a56c..13e12a3 100644 --- a/src/api/squaremap/Players.ts +++ b/src/api/squaremap/Players.ts @@ -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 { +class Players implements EntityApi { #map: Squaremap get map() { return this.#map } @@ -34,9 +38,9 @@ class Players implements EntityApi { 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 @@ -44,20 +48,20 @@ class Players implements EntityApi { 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 @@ -91,8 +95,8 @@ class Players implements EntityApi { }) } - readonly nearby = async(location: StrictPoint2D, radius: StrictPoint2D, players?: OnlinePlayer[]) => - getNearest(location, radius, players, this.all, true) + readonly nearby = async(location: StrictPoint2D, radius: StrictPoint2D, players?: SquaremapOnlinePlayer[]) => + getNearest>(location, radius, players, this.online, true) } export { diff --git a/src/api/squaremap/Towns.ts b/src/api/squaremap/Towns.ts index a5e1230..52659bb 100644 --- a/src/api/squaremap/Towns.ts +++ b/src/api/squaremap/Towns.ts @@ -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" @@ -23,7 +26,7 @@ class Towns implements EntityApi { 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) @@ -57,14 +60,14 @@ class Towns implements EntityApi { // 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() => { diff --git a/src/api/squaremap/parser.ts b/src/api/squaremap/parser.ts index 36d8389..38e24f4 100644 --- a/src/api/squaremap/parser.ts +++ b/src/api/squaremap/parser.ts @@ -7,7 +7,7 @@ import type { Resident, SquaremapMarkerset, SquaremapNation, - SquaremapPlayer, + SquaremapOnlinePlayer, SquaremapRawPlayer, SquaremapTown, StrictPoint2D @@ -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[]) => { diff --git a/src/types/gps.ts b/src/types/gps.ts index a68c715..923579e 100644 --- a/src/types/gps.ts +++ b/src/types/gps.ts @@ -1,3 +1,5 @@ +import type { Prettify } from "./util.js" + const createRoute = ( avoidPvp: boolean, avoidPublic: boolean @@ -15,13 +17,13 @@ export type RouteKey = keyof RouteType export type Route = RouteType[RouteKey] -export type Location = Point2D & { +export type Location = Prettify -export type SquaremapLocation = Point2D & { +export type SquaremapLocation = Prettify export type Point2D = { x: number | string diff --git a/src/types/player.ts b/src/types/player.ts index ecdc5ff..376ccf8 100644 --- a/src/types/player.ts +++ b/src/types/player.ts @@ -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 +export type Player = Prettify & { + online: boolean +}> -export type SquaremapOnlinePlayer = ParsedPlayer & SquaremapLocation -export type SquaremapPlayer = (Resident & SquaremapOnlinePlayer) | SquaremapOnlinePlayer \ No newline at end of file +export type SquaremapOnlinePlayer = Prettify +export type SquaremapPlayer = Prettify & { + online: boolean +}> \ No newline at end of file diff --git a/src/utils/functions.ts b/src/utils/functions.ts index 76b517c..4800f7b 100644 --- a/src/utils/functions.ts +++ b/src/utils/functions.ts @@ -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' @@ -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 diff --git a/tests/squaremap/players.test.ts b/tests/squaremap/players.test.ts index 06e0e73..e2a3c48 100644 --- a/tests/squaremap/players.test.ts +++ b/tests/squaremap/players.test.ts @@ -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(all) + assertType(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(ops) + assertType(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(op) + assertType(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(townless) + assertType(townless) //console.log(townless) })