Skip to content

Commit

Permalink
migrate to oapi v2
Browse files Browse the repository at this point in the history
  • Loading branch information
Owen3H committed Oct 12, 2023
1 parent 0403e07 commit d822017
Show file tree
Hide file tree
Showing 9 changed files with 241 additions and 52 deletions.
23 changes: 8 additions & 15 deletions src/classes/OAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,22 @@ import { FetchError } from '../utils/errors.js'
const parseResident = (res: RawResident) => {
const obj: any = {}

obj.online = res.status.isOnline
if (res.status)
obj.status = res.status

if (res.stats?.balance)
obj.balance = res.stats.balance

if (res.timestamps)
obj.timestamps = res.timestamps

if (res.strings?.username) obj.name = res.strings.username
if (res.strings?.title) obj.title = res.strings.title
if (res.strings?.surname) obj.surname = res.strings.surname
if (res.name) obj.name = res.name
if (res.uuid) obj.uuid = res.uuid
if (res.title) obj.title = res.title
if (res.surname) obj.surname = res.surname

const affiliation = res.affiliation
if (affiliation?.town) obj.town = affiliation.town
if (affiliation?.nation) obj.nation = affiliation.nation
if (res?.town) obj.town = res.town
if (res?.nation) obj.nation = res.nation

if (res.ranks?.townRanks) obj.townRanks = res.ranks.townRanks
if (res.ranks?.nationRanks) obj.nationRanks = res.ranks.nationRanks
Expand Down Expand Up @@ -58,9 +59,6 @@ const parseTown = (town: RawTown) => {

const obj: any = {
...town,
name: town.strings.town,
nation: town.affiliation.nation,
founder: town.strings.founder,
created: town.timestamps?.registered,
joinedNation: town.timestamps?.joinedNationAt,
perms: {
Expand All @@ -72,12 +70,8 @@ const parseTown = (town: RawTown) => {
}
}

delete obj.affiliation.nation
delete obj.timestamps

delete obj.strings.town
delete obj.strings.founder

delete obj.perms.rnaoPerms
delete obj.perms.flagPerms

Expand Down Expand Up @@ -113,7 +107,6 @@ class OfficialAPI {
if (!nation) return // TODO: Implement a proper error

return {
name: nation.strings.nation,
created: nation.timestamps.registered,
...nation
} as OAPINation
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from './types/oapi_v1.js'
export * from './types/oapi_v2.js'
export * from './types/dynmap.js'
export * from './types/gps.js'

Expand Down
17 changes: 3 additions & 14 deletions src/types/oapi_v1.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import { Location } from '../types.js'

type NestedOmit<T, K extends PropertyKey> = {
[P in keyof T as P extends K ? never : P]:
NestedOmit<T[P], K extends `${Exclude<P, symbol>}.${infer R}` ? R : never>
} extends infer O ? { [P in keyof O]: O[P] } : never;

export type ValidateShape<T, Shape> = T extends Shape ? Exclude<keyof T, keyof Shape> extends never ? T : never : never;
import { NestedOmit } from './util.js'

//#region Parsed
export type OAPITown = NestedOmit<RawTown,
Expand Down Expand Up @@ -133,10 +127,7 @@ export type RawTown = RawEntity & {
affiliation?: {
nation?: string
}
timestamps?: {
registered?: number
joinedNationAt?: number
}
timestamps?: Timestamps
home: Location
residents: string[]
perms: RawEntityPerms<RawTownPerms>
Expand All @@ -150,9 +141,7 @@ export type RawNation = RawEntity & {
capital: string
mapColorHexCode: string
}
timestamps?: {
registered?: number
}
timestamps?: Timestamps
towns: string[]
residents: string[]
allies: string[]
Expand Down
200 changes: 200 additions & 0 deletions src/types/oapi_v2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
import { Location } from '../types.js'
import { NestedOmit } from './util.js'

//#region Parsed
export type OAPITown = NestedOmit<RawTown,
"strings.town" |
"strings.founder" |
"timestamps" |
"perms.rnaoPerms" |
"perms.flagPerms"
> & {
name: string
nation: string
founder: string
created: number
joinedNation: number
perms: {
build: RawTownPerms
destroy: RawTownPerms
switch: RawTownPerms
itemUse: RawTownPerms
flags: RawFlagPerms
}
}

export type OAPINation = NestedOmit<RawNation,
"strings.nation" |
"timestamps"
> & {
name: string
created: number
}

export type OAPIResident = NestedOmit<RawResident,
"strings" |
"affiliation" |
"ranks" |
"perms" |
"stats"
> & {
name: string
uuid: string
title?: string
surname?: string
town?: string
nation?: string
balance: number
timestamps: Timestamps
townRanks?: string[]
nationRanks?: string[]
perms?: {
build: RawResidentPerms
destroy: RawResidentPerms
switch: RawResidentPerms
itemUse: RawResidentPerms
flags: RawFlagPerms
}
}
//#endregion

//#region Raw, unparsed types
export type RawEntity = {
uuid: string
status: RawEntityStatus
stats: RawEntityStats
ranks?: { [key: string]: string[] }
}

export type RawEntityStatus = Partial<{
isPublic: boolean
isOpen: boolean
isNeutral: boolean
isCapital: boolean
isOverClaimed: boolean
isRuined: boolean
isOnline: boolean
isNPC: boolean
}>

export type RawEntityStats = {
maxTownBlocks?: number
numTownBlocks?: number
numResidents?: number
numTowns?: number
balance: number
}

export type RawResidentPerms = {
friend: boolean
town: boolean
ally: boolean
outsider: boolean
}

export type RawTownPerms = {
resident: boolean
nation: boolean
ally: boolean
outsider: boolean
}

export type RawFlagPerms = {
pvp: boolean
explosion: boolean
fire: boolean
mobs: boolean
}

export type RawEntityPerms<PermsType> = {
flagPerms: RawFlagPerms
rnaoPerms: {
buildPerms: PermsType
destroyPerms: PermsType
switchPerms: PermsType
itemUsePerms: PermsType
}
}

export type TownSpawn = Location & {
world: string
pitch?: number
yaw?: number
}

export type TownCoordinates = {
spawn: TownSpawn
home: number[]
townBlocks: {
x: number[]
z: number[]
}
}

export type RawTown = RawEntity & {
name: string
board: string
mayor: string
founder: string
mapColorHexCode: string
nation?: string
timestamps?: Timestamps
perms: RawEntityPerms<RawTownPerms>
coordinates: TownCoordinates
residents: string[]
trusted?: string[]
outlaws?: string[]
}

export type RawNation = RawEntity & {
name: string
board: string
king: string
capital: string
mapColorHexCode: string
timestamps?: Timestamps
towns: string[]
residents: string[]
allies: string[]
enemies: string[]
}

export type Timestamps = {
joinedNationAt?: number
joinedTownAt?: number
registered: number
lastOnline?: number
}

export type RawResident = RawEntity & {
name: string
title: string
surname: string
town?: string
nation?: string
timestamps?: Timestamps
perms: RawEntityPerms<RawResidentPerms>
friends?: string[]
}

export type RawServerInfo = {
world: {
hasStorm: boolean
isThundering: boolean
time: number
fullTime: number
}
players: {
maxPlayers: number
numOnlineTownless: number
numOnlinePlayers: number
}
stats: {
numResidents: number
numTownless: number
numTowns: number
numNations: number
numTownBlocks: number
}
}
//#endregion
6 changes: 6 additions & 0 deletions src/types/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export type NestedOmit<T, K extends PropertyKey> = {
[P in keyof T as P extends K ? never : P]:
NestedOmit<T[P], K extends `${Exclude<P, symbol>}.${infer R}` ? R : never>
} extends infer O ? { [P in keyof O]: O[P] } : never;

export type ValidateShape<T, Shape> = T extends Shape ? Exclude<keyof T, keyof Shape> extends never ? T : never : never;
16 changes: 8 additions & 8 deletions src/utils/endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,20 @@ const configData = (mapName: ValidMapName) =>
const playerData = (mapName: ValidMapName) =>
asJSON(get("players", mapName)) as unknown as PlayersResponse

const townyData = async (endpoint = '', version = 'v1') => {
if (endpoint.startsWith("/"))
endpoint.replace("/", "")

const url = get("towny", `${version}/aurora`)
return await asJSON(`${url}${endpoint}?${genRandomString()}`) as unknown
}

const mapData = async (mapName: ValidMapName) => {
const url = await get("map", mapName)
const res: unknown = await !archiveTs ? asJSON(url) : getArchive(url, archiveTs)

return res as MapResponse
}

const townyData = async (endpoint = '', version = 'v2') => {
if (endpoint.startsWith("/"))
endpoint.replace("/", "")

const url = get("towny", `${version}/aurora`)
return await asJSON(`${url}${endpoint}?${genRandomString()}`) as unknown
}

export {
get, asJSON,
Expand Down
11 changes: 6 additions & 5 deletions tests/gps.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ describe('GPS', () => {
expect(route.distance).toBeGreaterThanOrEqual(0)
})

it('can check player is online when emitting', async () => {
const player = await globalThis.Aurora.GPS.getPlayer('Owen3H')
expect(player).not.toBeInstanceOf(NotFoundError)
it('can check player is online when emitting', async () => {
const ops = await globalThis.Aurora.Players.online()
const op = await globalThis.Aurora.GPS.getPlayer(ops[0]['name'])
expect(op).not.toBeInstanceOf(NotFoundError)

const online = await globalThis.Aurora.GPS.playerIsOnline(player)
expect(online).toEqual(false)
const online = await globalThis.Aurora.GPS.playerIsOnline(op)
expect(online).toEqual(true)
})

it('can throw not found error', async () => {
Expand Down
10 changes: 5 additions & 5 deletions tests/oapi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ describe('OfficialAPI', async () => {
expect(info.stats.numNations).toBeGreaterThanOrEqual(300)
})

it('can get valid resident (v1)', async () => {
it('can get valid resident', async () => {
const res = await OfficialAPI.resident('owen3h')

expect(res).toBeDefined()
assertType<OAPIResident>(res)

expect(res.name).toBe("Owen3H")
//console.log(res)
console.log(res)
})

it('can get valid nation (v1)', async () => {
it('can get valid nation', async () => {
const nation = await OfficialAPI.nation('venice')

expect(nation).toBeDefined()
Expand All @@ -42,13 +42,13 @@ describe('OfficialAPI', async () => {
//console.log(nation)
})

it('can get valid nation (v1)', async () => {
it('can get valid nation', async () => {
const town = await OfficialAPI.town('venice')

expect(town).toBeDefined()
assertType<OAPITown>(town)

expect(town.name).toBe("Venice")
console.log(town)
//console.log(town)
})
})
Loading

0 comments on commit d822017

Please sign in to comment.