Skip to content

Commit

Permalink
continue implementing OfficialAPI.V3
Browse files Browse the repository at this point in the history
- move interfaces from src/OAPI into proper types file.
- implement new `quarters`, `quarterList` and `location` methods in OAPIV3.
  • Loading branch information
Owen3H committed Jul 31, 2024
1 parent 7a7cf0e commit b0a2f94
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 31 deletions.
45 changes: 17 additions & 28 deletions src/OAPI.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import type {
RawTown,
RawNation,
RawResident,
OAPITown,
OAPIResident,
OAPINation,
RawServerInfoV2,
RawServerInfoV3
RawTown, RawNation, RawResident,
OAPITown, OAPIResident, OAPINation,
RawServerInfoV2, RawServerInfoV3,
RawQuarterResponseV3, RawEntityV3,
DiscordReqObject, DiscordResObject,
RawLocationResponseV3
} from './types/index.js'

import { townyData } from './utils/endpoint.js'
Expand Down Expand Up @@ -92,33 +90,24 @@ const parseNation = (nation: RawNation) => {
const ParamErr = () => new SyntaxError(`Parameter 'name' is invalid. Must be of type string!`)
const FetchErr = (type: string, name: string) => new FetchError(`Could not fetch ${type} '${name}'. Invalid response received!`)

interface DiscordReqObject {
type: 'minecraft' | 'discord'
target: string
}

interface DiscordResObject {
ID: string
UUID: string
}

export class OAPIV3 {
static serverInfo = async (): Promise<RawServerInfoV3> => await townyData('', 'v3')
static serverInfo = (): Promise<RawServerInfoV3> => townyData('', 'v3')

static location = (...objs: [number, number][]): Promise<RawLocationResponseV3> =>
townyData('location', 'v3', { query: objs })

static discord = async (...objs: DiscordReqObject[]): Promise<DiscordResObject[]> =>
await townyData('/discord', 'v3', { query: objs })
static discord = (...objs: DiscordReqObject[]): Promise<DiscordResObject[]> =>
townyData('/discord', 'v3', { query: objs })

static playerList = async (): Promise<{ name: string, uuid: string }[]> =>
await townyData('/players', 'v3')
static quarters = (...ids: string[]): Promise<RawQuarterResponseV3> => townyData('/quarters', 'v3', { query: ids })
static quarterList = (): Promise<RawEntityV3[]> => townyData('/quarters', 'v3')

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

export class OAPIV2 {
static serverInfo = async (): Promise<RawServerInfoV2> => {
return townyData('', 'v2')
}
static serverInfo = (): Promise<RawServerInfoV2> => townyData('', 'v2')

static resident = async (name: string) => {
if (!name) throw ParamErr()
Expand Down
51 changes: 48 additions & 3 deletions src/types/oapi.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type {
Location,
NestedOmit,
Prettify
Prettify,
StrictPoint2D
} from '../types/index.js'

//#region V2
Expand Down Expand Up @@ -36,8 +37,6 @@ export type OAPIResident = NestedOmit<RawResident,
"perms" |
"stats"
> & {
name: string
uuid: string
title?: string
surname?: string
town?: string
Expand Down Expand Up @@ -199,6 +198,11 @@ export interface RawServerInfoV2 {
//#endregion

//#region V3
export interface RawEntityV3 {
name: string
uuid: string
}

export interface RawServerInfoV3 {
version: string
moonPhase: string
Expand Down Expand Up @@ -228,4 +232,45 @@ export interface RawServerInfoV3 {
numRemaining: number
}
}

export interface RawQuarterResponseV3 {
uuid: string
type: "APARTMENT" | "COMMONS" | "PUBLIC" | "SHOP" | "STATION" | "WORKSITE"
owner: Partial<RawEntityV3>
town: RawEntityV3
timestamps: {
registered: number
claimedAt?: number
}
status: {
isEmbassy: boolean
}
stats: {
price?: number
volume: number
numCuboids: number
}
color: [number, number, number]
trusted: RawEntityV3[]
cuboids: {
[key: string]: [number, number, number]
}[]
}

export interface DiscordReqObject {
type: 'minecraft' | 'discord'
target: string
}

export interface DiscordResObject {
ID: string
UUID: string
}

export interface RawLocationResponseV3 {
isWilderness: boolean
location: Partial<StrictPoint2D>
town?: RawEntityV3
nation?: RawEntityV3
}
//#endregion

0 comments on commit b0a2f94

Please sign in to comment.