Add get profile endpoint
This commit is contained in:
@@ -9,6 +9,7 @@ import { handler as launcherLatestHandler } from './routes/launcher/latest'
|
||||
import { handler as launcherLoaderLatestHandler } from './routes/launcher/loader/latest'
|
||||
import { handler as launcherLoaderUpdateDataHandler } from './routes/launcher/loader/update-data'
|
||||
import { handler as berrydashLeaderboardsHandler } from './routes/berrydash/leaderboards'
|
||||
import { handler as berrydashProfileGetHandler } from './routes/berrydash/profile/get'
|
||||
import { handler as berrydashProfilePostsGetHandler } from './routes/berrydash/profile/posts/get'
|
||||
import { handler as berrydashProfilePostsDeleteHandler } from './routes/berrydash/profile/posts/delete'
|
||||
|
||||
@@ -43,6 +44,7 @@ app.get('/berrydash/leaderboards/legacy', context =>
|
||||
app.get('/berrydash/leaderboards/total', context =>
|
||||
berrydashLeaderboardsHandler(context, 4)
|
||||
)
|
||||
app.get('/berrydash/profile', context => berrydashProfileGetHandler(context))
|
||||
app.get('/berrydash/profile/posts', context =>
|
||||
berrydashProfilePostsGetHandler(context)
|
||||
)
|
||||
|
||||
@@ -63,9 +63,9 @@ export const checkClientDatabaseVersion = (request: Request) => {
|
||||
if (!allowedDatabaseVersions.includes(clientVersion)) return '-998'
|
||||
}
|
||||
|
||||
export const genTimestamp = (time: number): string => {
|
||||
time = Math.floor(Date.now() / 1000) - time
|
||||
time = time < 1 ? 1 : time
|
||||
export const genTimestamp = (time: number, extra = 0): string => {
|
||||
let remaining = Math.floor(Date.now() / 1000) - time
|
||||
remaining = remaining < 1 ? 1 : remaining
|
||||
|
||||
const tokens: [number, string][] = [
|
||||
[31536000, 'year'],
|
||||
@@ -77,12 +77,17 @@ export const genTimestamp = (time: number): string => {
|
||||
[1, 'second']
|
||||
]
|
||||
|
||||
for (const [unit, text] of tokens) {
|
||||
if (time < unit) continue
|
||||
const parts: string[] = []
|
||||
|
||||
const numberOfUnits = Math.floor(time / unit)
|
||||
return numberOfUnits + ' ' + text + (numberOfUnits > 1 ? 's' : '')
|
||||
for (const [unit, text] of tokens) {
|
||||
if (remaining < unit) continue
|
||||
if (parts.length > extra) break
|
||||
|
||||
const value = Math.floor(remaining / unit)
|
||||
remaining -= value * unit
|
||||
|
||||
parts.push(value + ' ' + text + (value > 1 ? 's' : ''))
|
||||
}
|
||||
|
||||
return '1 second'
|
||||
return parts.length ? parts.join(' ') : '1 second'
|
||||
}
|
||||
|
||||
132
src/routes/berrydash/profile/get.ts
Normal file
132
src/routes/berrydash/profile/get.ts
Normal file
@@ -0,0 +1,132 @@
|
||||
import { Context } from 'elysia'
|
||||
import {
|
||||
genTimestamp,
|
||||
getDatabaseConnection,
|
||||
jsonResponse
|
||||
} from '../../../lib/util'
|
||||
import {
|
||||
berryDashUserData,
|
||||
berryDashUserPosts,
|
||||
users
|
||||
} from '../../../lib/tables'
|
||||
import { and, desc, eq } from 'drizzle-orm'
|
||||
|
||||
export async function handler (context: Context) {
|
||||
const dbInfo0 = getDatabaseConnection(0)
|
||||
const dbInfo1 = getDatabaseConnection(1)
|
||||
|
||||
if (!dbInfo0 || !dbInfo1)
|
||||
return jsonResponse(
|
||||
{ success: false, message: 'Failed to connect to database', data: null },
|
||||
500
|
||||
)
|
||||
const { connection: connection0, db: db0 } = dbInfo0
|
||||
const { connection: connection1, db: db1 } = dbInfo1
|
||||
|
||||
let userIdQuery = context.query.userId
|
||||
? parseInt(context.query.userId, 10)
|
||||
: 0
|
||||
if (!userIdQuery || userIdQuery < 1) {
|
||||
connection0.end()
|
||||
connection1.end()
|
||||
return jsonResponse(
|
||||
{ success: false, message: 'No valid user ID provided', data: null },
|
||||
400
|
||||
)
|
||||
}
|
||||
|
||||
const user = await db0
|
||||
.select({
|
||||
id: users.id,
|
||||
username: users.username,
|
||||
registerTime: users.registerTime
|
||||
})
|
||||
.from(users)
|
||||
.where(eq(users.id, userIdQuery))
|
||||
.execute()
|
||||
|
||||
if (!user[0]) {
|
||||
connection0.end()
|
||||
connection1.end()
|
||||
return jsonResponse(
|
||||
{ success: false, message: 'User does not exist', data: null },
|
||||
404
|
||||
)
|
||||
}
|
||||
|
||||
const userData = await db1
|
||||
.select({
|
||||
id: berryDashUserData.id,
|
||||
saveData: berryDashUserData.saveData
|
||||
})
|
||||
.from(berryDashUserData)
|
||||
.where(and(eq(berryDashUserData.id, user[0].id)))
|
||||
.execute()
|
||||
const savedata = userData[0].saveData
|
||||
? JSON.parse(userData[0].saveData)
|
||||
: null
|
||||
if (!savedata)
|
||||
return jsonResponse(
|
||||
{ success: false, message: 'User save does not exist', data: null },
|
||||
404
|
||||
)
|
||||
|
||||
connection0.end()
|
||||
connection1.end()
|
||||
|
||||
let custom = null
|
||||
if (savedata['bird']['customIcon']['selected']) {
|
||||
const selected = savedata['bird']['customIcon']['selected']
|
||||
for (const entry of savedata['bird']['customIcon']['data']) {
|
||||
if (entry['uuid'] && entry['uuid'] === selected) {
|
||||
custom = entry['data']
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return jsonResponse({
|
||||
success: true,
|
||||
message: null,
|
||||
data: {
|
||||
username: user[0].username,
|
||||
memberFor: genTimestamp(user[0].registerTime, 2),
|
||||
icon: custom ? null : savedata['bird']['icon'] ?? 1,
|
||||
overlay: custom ? null : savedata['bird']['overlay'] ?? 0,
|
||||
iconColor: custom
|
||||
? null
|
||||
: savedata['settings']['colors']['icon'] ?? [255, 255, 255],
|
||||
overlayColor: custom
|
||||
? null
|
||||
: savedata['settings']['colors']['overlay'] ?? [255, 255, 255],
|
||||
customIcon: custom,
|
||||
stats: {
|
||||
totalNormalBerries: parseInt(
|
||||
savedata['gameStore']['totalNormalBerries'] ?? 0
|
||||
),
|
||||
totalPoisonBerries: parseInt(
|
||||
savedata['gameStore']['totalPoisonBerries'] ?? 0
|
||||
),
|
||||
totalSlowBerries: parseInt(
|
||||
savedata['gameStore']['totalSlowBerries'] ?? 0
|
||||
),
|
||||
totalUltraBerries: parseInt(
|
||||
savedata['gameStore']['totalUltraBerries'] ?? 0
|
||||
),
|
||||
totalSpeedyBerries: parseInt(
|
||||
savedata['gameStore']['totalSpeedyBerries'] ?? 0
|
||||
),
|
||||
totalCoinBerries: parseInt(
|
||||
savedata['gameStore']['totalCoinBerries'] ?? 0
|
||||
),
|
||||
totalRandomBerries: parseInt(
|
||||
savedata['gameStore']['totalRandomBerries'] ?? 0
|
||||
),
|
||||
totalAntiBerries: parseInt(
|
||||
savedata['gameStore']['totalAntiBerries'] ?? 0
|
||||
),
|
||||
coins: parseInt(savedata['bird']['customIcon']['balance'] ?? 0)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user