Added online status to sidebar
This commit is contained in:
31
src/lib/hypixel/api/online.ts
Normal file
31
src/lib/hypixel/api/online.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import { statusSchema } from "@/lib/schema/status"
|
||||
import { cacheLife } from "next/dist/server/use-cache/cache-life"
|
||||
import { env } from "../../env/server"
|
||||
|
||||
const playerApi = "https://api.hypixel.net/v2/status"
|
||||
|
||||
export async function getSession(uuid: string) {
|
||||
"use cache"
|
||||
|
||||
if (process.env.NODE_ENV === "production") {
|
||||
cacheLife({
|
||||
stale: 1000 * 60,
|
||||
revalidate: 1000 * 60 * 5,
|
||||
expire: 1000 * 60 * 5
|
||||
})
|
||||
}
|
||||
|
||||
const res = await fetch(`${playerApi}?uuid=${uuid}`, {
|
||||
headers: {
|
||||
"API-Key": env.HYPIXEL_API_KEY
|
||||
}
|
||||
})
|
||||
|
||||
if (!res.ok) return null
|
||||
|
||||
const { success, data } = statusSchema.safeParse(await res.json())
|
||||
|
||||
if (!success) return null
|
||||
|
||||
return data.session
|
||||
}
|
||||
20
src/lib/hypixel/general/status.ts
Normal file
20
src/lib/hypixel/general/status.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import { GAMES, MODES } from "@/data/hypixel/hypixel"
|
||||
|
||||
export function getGame(val: string) {
|
||||
const game = GAMES.find(g => g.id === val)
|
||||
return game || null
|
||||
}
|
||||
|
||||
export function getGameMode(gameType?: string, mode?: string) {
|
||||
if (!gameType || !mode) return null
|
||||
const game = getGame(gameType)
|
||||
if (!game) return null
|
||||
|
||||
const modes = MODES as Record<string, undefined | Record<string, never> | Record<string, string | undefined>>
|
||||
|
||||
const gameModes = modes[gameType]
|
||||
if (!gameModes) return null
|
||||
|
||||
const modeName = gameModes[mode]
|
||||
return modeName || null
|
||||
}
|
||||
17
src/lib/schema/status.ts
Normal file
17
src/lib/schema/status.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import z from "zod"
|
||||
|
||||
export const statusSchema = z.object({
|
||||
session: z.discriminatedUnion("online", [
|
||||
z.object({
|
||||
online: z.literal(false)
|
||||
}),
|
||||
z.object({
|
||||
online: z.literal(true),
|
||||
gameType: z.string().optional(),
|
||||
mode: z.string().optional(),
|
||||
map: z.string().optional()
|
||||
})
|
||||
])
|
||||
})
|
||||
|
||||
export type Session = z.infer<typeof statusSchema>
|
||||
Reference in New Issue
Block a user