Skip to content

Commit

Permalink
implement proper type for player from Official API
Browse files Browse the repository at this point in the history
  • Loading branch information
Owen3H committed Oct 13, 2024
1 parent 7b226c2 commit 836e1c7
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 5 deletions.
5 changes: 3 additions & 2 deletions src/OAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import type {
RawServerInfoV2, RawServerInfoV3,
RawQuarterResponseV3, RawEntityV3,
DiscordReqObject, DiscordResObject,
RawLocationResponseV3
RawLocationResponseV3,
RawPlayerV3
} from './types/index.js'

import { townyData } from './utils/endpoint.js'
Expand Down Expand Up @@ -102,7 +103,7 @@ export class OAPIV3 {
static quarters = (...ids: string[]): Promise<RawQuarterResponseV3> => townyData('/quarters', 'v3', { query: ids })
static quarterList = (): Promise<RawEntityV3[]> => townyData('/quarters', 'v3')

static players = (...ids: string[]): Promise<OAPIResident[]> => townyData('/players', 'v3', { query: ids })
static players = (...ids: string[]): Promise<RawPlayerV3[]> => townyData('/players', 'v3', { query: ids })
static playerList = (): Promise<RawEntityV3[]> => townyData('/players', 'v3')
}

Expand Down
43 changes: 43 additions & 0 deletions src/types/oapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,4 +273,47 @@ export interface RawLocationResponseV3 {
town?: RawEntityV3
nation?: RawEntityV3
}

export interface RawPlayerV3 extends RawEntityV3 {
title?: string
surname?: string
formattedName: string
about: string
town?: RawEntityV3
nation?: RawEntityV3
timestamps: {
registered: number
joinedTownAt?: number
lastOnline?: number
}
status: {
isOnline: boolean,
isNPC: boolean
isMayor: boolean
isKing: boolean
hasTown: boolean
hasNation: boolean
}
stats: {
balance: number
numFriends: number
}
perms: {
build: [boolean, boolean, boolean, boolean]
destroy: [boolean, boolean, boolean, boolean]
switch: [boolean, boolean, boolean, boolean]
itemUse: [boolean, boolean, boolean, boolean]
flags: {
pvp: boolean
explosion: boolean
fire: boolean
mobs: boolean
}
}
ranks?: {
townRanks: string[]
nationRanks: string[]
}
friends: RawEntityV3[]
}
//#endregion
4 changes: 2 additions & 2 deletions tests/oapi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
import { OfficialAPI } from '../src/main'
import {
OAPINation, OAPIResident, OAPITown,
RawServerInfoV2, RawServerInfoV3
RawPlayerV3, RawServerInfoV2, RawServerInfoV3
} from '../src/types'

describe('[v3] OfficialAPI', async () => {
Expand Down Expand Up @@ -36,7 +36,7 @@ describe('[v3] OfficialAPI', async () => {
const players = await OfficialAPI.V3.players("af77d9b5-ab5d-4714-b92e-3b191c895ee7")

expect(players).toBeDefined()
assertType<OAPIResident[]>(players)
assertType<RawPlayerV3>(players[0])
}, 10000)
})

Expand Down
2 changes: 1 addition & 1 deletion tests/squaremap/nations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('[Squaremap/Aurora] Nations', () => {
})

it('can get multiple nations', async () => {
const nations = await Aurora.Nations.get('SiBeRia', 'veNICE', 'verMOnt', 'fAroe_ISlanDs') as SquaremapNation[]
const nations = await Aurora.Nations.get('SiBeRia', 'veNICE', 'verMOnt') as SquaremapNation[]

expect(nations).toBeTruthy()
expect(nations.length).toBe(4)
Expand Down

0 comments on commit 836e1c7

Please sign in to comment.