Skip to content
This repository has been archived by the owner on Oct 4, 2024. It is now read-only.

Commit

Permalink
Add PlayerDB API.
Browse files Browse the repository at this point in the history
  • Loading branch information
miosenpai committed Apr 24, 2024
1 parent cccbdf4 commit 25c317a
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 33 deletions.
16 changes: 9 additions & 7 deletions server/api/stats/[username].ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ export default defineEventHandler(async (event) => {

const username = getRouterParam(event, 'username')!

const uuidRes = await useAshconApi().usernameToUuid(username)
let uuidRes = null

if (uuidRes.status !== 200 || uuidRes._data!.demo) {
try {
uuidRes = await usePlayerDb().usernameToUuid(username)
} catch (err: any) {
throw createError({
statusCode: 404,
message: ScrapeError.PROFILE_NOT_FOUND,
Expand All @@ -24,7 +26,7 @@ export default defineEventHandler(async (event) => {

const cacheStore = useStorage('cache')

const currKeys = await cacheStore.getKeys(`nitro:functions:stats:${uuidRes._data!.uuid}`)
const currKeys = await cacheStore.getKeys(`nitro:functions:stats:${uuidRes.id}`)

// console.log(currKeys)

Expand All @@ -43,7 +45,7 @@ export default defineEventHandler(async (event) => {

const runtimeCfg = useRuntimeConfig()

const sseToken = await new jose.SignJWT({ uuid: uuidRes._data!.uuid, username: uuidRes._data!.username })
const sseToken = await new jose.SignJWT({ uuid: uuidRes.id, username: uuidRes.name })
.setProtectedHeader({ alg: 'HS256' })
.sign(Buffer.from(runtimeCfg.sseSecret))

Expand All @@ -54,10 +56,10 @@ export default defineEventHandler(async (event) => {

switch (query.category) {
case 'battle-royale':
statsRes = await getBattleRoyaleStats(uuidRes._data!.uuid, uuidRes._data!.username)
statsRes = await getBattleRoyaleStats(uuidRes.id, uuidRes.name)
break
case 'duels':
statsRes = await getDuelsStats(uuidRes._data!.uuid, uuidRes._data!.username)
statsRes = await getDuelsStats(uuidRes.id, uuidRes.name)
break
}

Expand All @@ -68,7 +70,7 @@ export default defineEventHandler(async (event) => {
})
} else {
return {
username: uuidRes._data!.username,
username: uuidRes.name,
stats: statsRes,
}
}
Expand Down
22 changes: 0 additions & 22 deletions server/utils/ashcon.ts

This file was deleted.

6 changes: 2 additions & 4 deletions server/utils/mojang.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
type UsernameToUuidRes = {
export type UsernameToUuidRes = {
id: string
name: string
legacy?: true
demo?: true
}

function usernameToUuid(username: string) {
return $fetch.raw<UsernameToUuidRes>(`https://api.mojang.com/users/profiles/minecraft/${username}`, {
ignoreResponseError: true,
})
return $fetch<UsernameToUuidRes>(`https://api.mojang.com/users/profiles/minecraft/${username}`)
}

export function useMojangApi() {
Expand Down
27 changes: 27 additions & 0 deletions server/utils/playerdb.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
async function usernameToUuid(username: string): Promise<UsernameToUuidRes> {
const playerDbRes = await $fetch<{
data: {
player: {
username: string
raw_id: string
}
}
}>(`https://playerdb.co/api/player/minecraft/${username}`, {
onResponseError: ({ error, response }) => {
if (response.status >= 500 || response.status < 400)
console.log(error)
},
})

// compatible interface with mojang API (if we return to using it in the future)
return {
name: playerDbRes.data.player.username,
id: playerDbRes.data.player.raw_id,
}
}

export function usePlayerDb() {
return {
usernameToUuid,
}
}

0 comments on commit 25c317a

Please sign in to comment.