Files
NightSpire/web-next/lib/vote.ts
T
Inna b9803adeb9 web-next: rediseño de security-token, recover, vote-points + rename-guild
- /security-token: diseño completo del original (info + advertencia + NOTA 7 días),
  fecha en formato HH:MM:SS DD-MM-YYYY, botón "Token enviado", enlace a /recover.
- /recover: 4 opciones (contraseña por usuario, nombre de cuenta, token de seguridad
  y enlace de activación por correo); recuperación de token nueva; Turnstile por envío.
- /vote-points: caja de info con las 7 secciones Q&A + panel "Sitios de votación" con
  tarjetas inline-div (imagen, PV, último voto), botón refrescar; abre el sitio y acredita
  PV al volver. lib/vote.ts + getVoteSitesForAccount.
- /rename-guild: renombra una hermandad de la que la cuenta es Maestro por 1000 PD +
  token de seguridad; SOAP .guild rename con comillas; reembolsa los PD si SOAP falla.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-14 10:06:49 +00:00

92 lines
3.1 KiB
TypeScript

import type { RowDataPacket } from 'mysql2'
import { db, DB } from './db'
const COOLDOWN_MS = (12 * 60 + 30) * 60 * 1000 // 12h 30m
export interface VoteSite {
id: number
name: string
url: string
image_url: string
points: number
}
export interface VoteResult {
success: boolean
error?: string
points?: number
siteName?: string
hours?: number
minutes?: number
}
export async function getVoteSites(): Promise<VoteSite[]> {
try {
const [rows] = await db(DB.default).query<RowDataPacket[]>(
'SELECT id, name, url, image_url, points FROM home_votesite ORDER BY id',
)
return rows.map((r) => ({ id: r.id, name: r.name, url: r.url, image_url: r.image_url, points: r.points }))
} catch {
return []
}
}
export interface VoteSiteStatus extends VoteSite {
lastVoteAt: string | null // ISO del último voto de la cuenta en este sitio, o null
}
/** Sitios de votación con la fecha del último voto de la cuenta (para el recuadro). */
export async function getVoteSitesForAccount(accountId: number): Promise<VoteSiteStatus[]> {
const sites = await getVoteSites()
if (!accountId) return sites.map((s) => ({ ...s, lastVoteAt: null }))
try {
const [rows] = await db(DB.default).query<RowDataPacket[]>(
'SELECT vote_site_id, MAX(created_at) AS last FROM home_votelog WHERE account_id = ? GROUP BY vote_site_id',
[accountId],
)
const map = new Map(rows.map((r) => [Number(r.vote_site_id), r.last as Date]))
return sites.map((s) => {
const last = map.get(s.id)
return { ...s, lastVoteAt: last ? new Date(last).toISOString() : null }
})
} catch {
return sites.map((s) => ({ ...s, lastVoteAt: null }))
}
}
export async function registerVote(accountId: number, url: string): Promise<VoteResult> {
const [siteRows] = await db(DB.default).query<RowDataPacket[]>(
'SELECT id, name, points FROM home_votesite WHERE url = ?',
[url],
)
const site = siteRows[0]
if (!site) return { success: false, error: 'siteNotFound' }
const [last] = await db(DB.default).query<RowDataPacket[]>(
'SELECT created_at FROM home_votelog WHERE account_id = ? AND vote_site_id = ? ORDER BY created_at DESC LIMIT 1',
[accountId, site.id],
)
if (last[0]) {
const diff = Date.now() - new Date(last[0].created_at).getTime()
if (diff < COOLDOWN_MS) {
const rem = COOLDOWN_MS - diff
return { success: false, error: 'cooldown', hours: Math.floor(rem / 3600000), minutes: Math.floor((rem % 3600000) / 60000) }
}
}
// Acreditar PV
const [pts] = await db(DB.default).query<RowDataPacket[]>('SELECT id FROM home_api_points WHERE accountID = ?', [accountId])
if (pts[0]) {
await db(DB.default).query('UPDATE home_api_points SET vp = vp + ? WHERE accountID = ?', [site.points, accountId])
} else {
await db(DB.default).query('INSERT INTO home_api_points (accountID, vp, dp) VALUES (?, ?, 0)', [accountId, site.points])
}
await db(DB.default).query(
'INSERT INTO home_votelog (account_id, vote_site_id, created_at, last_vote_time, processed) VALUES (?, ?, NOW(), NOW(), 0)',
[accountId, site.id],
)
return { success: true, points: site.points, siteName: site.name }
}