Skip to content

Commit

Permalink
begin working on parsed types
Browse files Browse the repository at this point in the history
  • Loading branch information
Owen3H committed Sep 26, 2023
1 parent 607bf0e commit c4b11ac
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 48 deletions.
38 changes: 17 additions & 21 deletions src/classes/OAPI.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import {
ApiTownRaw,
ApiNationRaw,
ApiResidentRaw,
RawTown,
RawNation,
RawResident,
ApiResident,
ServerInfoRaw
RawServerInfo,
OAPITown
} from '../types.js'

import { townyData } from '../utils/endpoint.js'
import { FetchError } from '../utils/errors.js'

class OfficialAPI {
static serverInfo = async () => await townyData('', 'v2') as ServerInfoRaw
static serverInfo = async () => await townyData('', 'v2') as RawServerInfo

static resident = async (name: string) => {
// TODO: Properly handle this case and implement an error.
if (!name) return

const res = await townyData(`/residents/${name}`) as ApiResidentRaw
const res = await townyData(`/residents/${name}`) as RawResident
if (!res) throw new FetchError(`Could not fetch resident '${name}'. Received invalid response.`)

const obj: any = {
Expand Down Expand Up @@ -56,28 +57,23 @@ class OfficialAPI {
static town = async (name: string) => {
if (!name) return

const town = await townyData(`/towns/${name}`) as ApiTownRaw
const obj: any = {}
const town = await townyData(`/towns/${name}`) as RawTown

if (town) {
if (town.strings.founder) obj.founder = town.strings.founder
if (town.stats) obj.stats = town.stats
if (town.ranks) obj.ranks = town.ranks

if (town.timestamps?.registered)
obj.created = town.timestamps.registered

if (town.timestamps?.joinedNationAt)
obj.joinedNation = town.timestamps.joinedNationAt
}
// TODO: Implement a proper error
if (!town) return

return obj
return {
name: town.strings.town,
founder: town.strings.founder,
created: town.timestamps?.registered,
joinedNation: town.timestamps?.joinedNationAt
} as OAPITown
}

static nation = async (name: string) => {
if (!name) return

const nation = await townyData(`/nations/${name}`) as ApiNationRaw
const nation = await townyData(`/nations/${name}`) as RawNation
const obj: any = {}

if (nation) {
Expand Down
71 changes: 50 additions & 21 deletions src/types/oapi_v1.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import { Location } from '../types.js'

export type ApiEntityRaw = {
status: EntityStatusRaw
stats: EntityStatsRaw
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;

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

export type EntityStatusRaw = {
export type RawEntityStatus = {
isPublic: boolean
isOpen: boolean
isNeutral: boolean
Expand All @@ -17,37 +23,37 @@ export type EntityStatusRaw = {
isOnline?: boolean
}

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

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

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

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

export type EntityPermsRaw<PermsType> = {
flagPerms: FlagPermsRaw
export type RawEntityPerms<PermsType> = {
flagPerms: RawFlagPerms
rnaoPerms: {
buildPerms: PermsType
destroyPerms: PermsType
Expand All @@ -56,7 +62,7 @@ export type EntityPermsRaw<PermsType> = {
}
}

export type ApiTownRaw = ApiEntityRaw & {
export type RawTown = RawEntity & {
strings: {
town: string
board: string
Expand All @@ -73,10 +79,10 @@ export type ApiTownRaw = ApiEntityRaw & {
}
home: Location
residents: string[]
perms: EntityPermsRaw<TownPermsRaw>
perms: RawEntityPerms<RawTownPerms>
}

export type ApiNationRaw = ApiEntityRaw & {
export type RawNation = RawEntity & {
strings: {
nation: string
board: string
Expand All @@ -93,26 +99,26 @@ export type ApiNationRaw = ApiEntityRaw & {
enemies: string[]
}

export type ApiResidentRaw = ApiEntityRaw & {
export type RawResident = RawEntity & {
strings: {
title: string
username: string
surname: string
}
affiliation?: {
town?: string
nation?: string
}
affiliation?: Partial<{
town: string
nation: string
}>
timestamps?: {
joinedTownAt?: number
registered: number
lastOnline: number
}
perms: EntityPermsRaw<ResidentPermsRaw>
perms: RawEntityPerms<RawResidentPerms>
friends: string[]
}

export type ServerInfoRaw = {
export type RawServerInfo = {
world: {
hasStorm: boolean
isThundering: boolean
Expand All @@ -131,4 +137,27 @@ export type ServerInfoRaw = {
numNations: number
numTownBlocks: number
}
}
}
//#endregion

//#region Parsed
export type OAPITown = NestedOmit<RawTown,
"strings.town" |
"strings.founder" |
"timestamps.registered" |
"timestamps.joinedNationAt"
> & {
name: string
founder: string
created: number
joinedNation: number
}

export type OAPINation = Partial<RawEntity> & {
s
}

export type OAPIResident = Partial<RawEntity> & {
s
}
//#endregion
12 changes: 6 additions & 6 deletions src/types/resident.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FlagPermsRaw, ResidentPermsRaw } from '../types.js'
import { RawFlagPerms, RawResidentPerms } from '../types.js'

export type Resident = {
name: string
Expand All @@ -23,11 +23,11 @@ export type ApiResident = {
townRanks: ResidentRanks
nationRanks: ResidentRanks
perms?: {
build: ResidentPermsRaw
destroy: ResidentPermsRaw
switch: ResidentPermsRaw
itemUse: ResidentPermsRaw
flags: FlagPermsRaw
build: RawResidentPerms
destroy: RawResidentPerms
switch: RawResidentPerms
itemUse: RawResidentPerms
flags: RawFlagPerms
}
friends?: string[]
}
Expand Down

0 comments on commit c4b11ac

Please sign in to comment.