a0a64bc70a
- lib/account.ts: getAccountDashboard(session) lee puntos (home_api_points), créditos battlepay (battlenet_accounts), estado del token de seguridad (home_securitytoken) y personajes (acore_characters). Resiliente si las BD de AzerothCore no están pobladas (devuelve vacíos/0). - app/[locale]/account: página protegida enriquecida (tarjetas de cuenta + puntos + rejilla de personajes con oro), i18n. Namespace Account ampliado. Verificado: build OK, guarda sigue redirigiendo a login sin sesión. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
91 lines
2.2 KiB
TypeScript
91 lines
2.2 KiB
TypeScript
import type { RowDataPacket } from 'mysql2'
|
|
import { db, DB } from './db'
|
|
import type { SessionData } from './session'
|
|
|
|
export interface Character {
|
|
name: string
|
|
level: number
|
|
gold: number
|
|
silver: number
|
|
copper: number
|
|
}
|
|
|
|
export interface AccountDashboard {
|
|
bnetEmail: string
|
|
gameAccount: string
|
|
dp: number
|
|
vp: number
|
|
battlepayCredits: number
|
|
securityTokenDate: string | null
|
|
characters: Character[]
|
|
}
|
|
|
|
/** Datos del panel de cuenta. Resiliente si las BD de AzerothCore no están pobladas. */
|
|
export async function getAccountDashboard(session: SessionData): Promise<AccountDashboard> {
|
|
const accountId = session.accountId ?? 0
|
|
|
|
let dp = 0
|
|
let vp = 0
|
|
try {
|
|
const [p] = await db(DB.default).query<RowDataPacket[]>(
|
|
'SELECT dp, vp FROM home_api_points WHERE accountID = ?',
|
|
[accountId],
|
|
)
|
|
if (p[0]) {
|
|
dp = p[0].dp
|
|
vp = p[0].vp
|
|
}
|
|
} catch {
|
|
/* ignore */
|
|
}
|
|
|
|
let battlepayCredits = 0
|
|
try {
|
|
const [c] = await db(DB.auth).query<RowDataPacket[]>(
|
|
'SELECT battlePayCredits FROM battlenet_accounts WHERE id = ?',
|
|
[session.bnetId ?? 0],
|
|
)
|
|
if (c[0]?.battlePayCredits != null) battlepayCredits = c[0].battlePayCredits
|
|
} catch {
|
|
/* ignore */
|
|
}
|
|
|
|
let securityTokenDate: string | null = null
|
|
try {
|
|
const [s] = await db(DB.default).query<RowDataPacket[]>(
|
|
'SELECT created_at FROM home_securitytoken WHERE user_id = ? ORDER BY created_at DESC LIMIT 1',
|
|
[accountId],
|
|
)
|
|
if (s[0]) securityTokenDate = new Date(s[0].created_at).toISOString()
|
|
} catch {
|
|
/* ignore */
|
|
}
|
|
|
|
let characters: Character[] = []
|
|
try {
|
|
const [ch] = await db(DB.characters).query<RowDataPacket[]>(
|
|
'SELECT name, level, money FROM characters WHERE account = ?',
|
|
[accountId],
|
|
)
|
|
characters = ch.map((r) => ({
|
|
name: r.name,
|
|
level: r.level,
|
|
gold: Math.floor(r.money / 10000),
|
|
silver: Math.floor((r.money % 10000) / 100),
|
|
copper: r.money % 100,
|
|
}))
|
|
} catch {
|
|
/* la BD de personajes puede no estar poblada */
|
|
}
|
|
|
|
return {
|
|
bnetEmail: session.bnetEmail ?? '',
|
|
gameAccount: session.username ?? '',
|
|
dp,
|
|
vp,
|
|
battlepayCredits,
|
|
securityTokenDate,
|
|
characters,
|
|
}
|
|
}
|