- Footer: add site-footer with one-liner + Cloud Host branding linking to cloudhost.es (mobile only, hidden on desktop) - Auto-pause: game pauses automatically after AUTOPAUSE_DELAY_MS (default 60s) with no viewers connected; auto-resumes when first viewer connects; shows "Esperando espectadores…" in the UI - Redsys: new /pregunta page lets viewers pay 1€ via Redsys to submit a fill-in question; submitted questions are used as prompts in the next round instead of AI generation; new redsys.ts implements HMAC_SHA256_V1 signing (3DES key derivation + HMAC-SHA256); notification endpoint at /api/redsys/notificacion handles server-to-server confirmation - db.ts: questions table with createPendingQuestion / markQuestionPaid / getNextPendingQuestion / markQuestionUsed - .env.sample: added AUTOPAUSE_DELAY_MS, PUBLIC_URL, REDSYS_* vars Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
77 lines
2.8 KiB
TypeScript
77 lines
2.8 KiB
TypeScript
import { Database } from "bun:sqlite";
|
|
import type { RoundState } from "./game.ts";
|
|
|
|
const dbPath = process.env.DATABASE_PATH ?? "argumentes.sqlite";
|
|
export const db = new Database(dbPath, { create: true });
|
|
|
|
db.exec(`
|
|
CREATE TABLE IF NOT EXISTS rounds (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
num INTEGER,
|
|
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,
|
|
data TEXT
|
|
);
|
|
`);
|
|
|
|
export function saveRound(round: RoundState) {
|
|
const insert = db.prepare("INSERT INTO rounds (num, data) VALUES ($num, $data)");
|
|
insert.run({ $num: round.num, $data: JSON.stringify(round) });
|
|
}
|
|
|
|
export function getRounds(page: number = 1, limit: number = 10) {
|
|
const offset = (page - 1) * limit;
|
|
const countQuery = db.query("SELECT COUNT(*) as count FROM rounds").get() as { count: number };
|
|
const rows = db.query("SELECT data FROM rounds ORDER BY num DESC, id DESC LIMIT $limit OFFSET $offset")
|
|
.all({ $limit: limit, $offset: offset }) as { data: string }[];
|
|
return {
|
|
rounds: rows.map(r => JSON.parse(r.data) as RoundState),
|
|
total: countQuery.count,
|
|
page,
|
|
limit,
|
|
totalPages: Math.ceil(countQuery.count / limit)
|
|
};
|
|
}
|
|
|
|
export function getAllRounds() {
|
|
const rows = db.query("SELECT data FROM rounds ORDER BY num ASC, id ASC").all() as { data: string }[];
|
|
return rows.map(r => JSON.parse(r.data) as RoundState);
|
|
}
|
|
|
|
export function clearAllRounds() {
|
|
db.exec("DELETE FROM rounds;");
|
|
db.exec("DELETE FROM sqlite_sequence WHERE name = 'rounds';");
|
|
}
|
|
|
|
// ── Questions (user-submitted via Redsys) ───────────────────────────────────
|
|
|
|
db.exec(`
|
|
CREATE TABLE IF NOT EXISTS questions (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
text TEXT NOT NULL,
|
|
order_id TEXT NOT NULL UNIQUE,
|
|
status TEXT NOT NULL DEFAULT 'pending',
|
|
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
|
);
|
|
`);
|
|
|
|
export function createPendingQuestion(text: string, orderId: string): number {
|
|
const stmt = db.prepare("INSERT INTO questions (text, order_id) VALUES ($text, $orderId)");
|
|
const result = stmt.run({ $text: text, $orderId: orderId });
|
|
return result.lastInsertRowid as number;
|
|
}
|
|
|
|
export function markQuestionPaid(orderId: string): boolean {
|
|
const stmt = db.prepare("UPDATE questions SET status = 'paid' WHERE order_id = $orderId AND status = 'pending'");
|
|
const result = stmt.run({ $orderId: orderId });
|
|
return result.changes > 0;
|
|
}
|
|
|
|
export function getNextPendingQuestion(): { id: number; text: string; order_id: string } | null {
|
|
return db.query("SELECT id, text, order_id FROM questions WHERE status = 'paid' ORDER BY id ASC LIMIT 1")
|
|
.get() as { id: number; text: string; order_id: string } | null;
|
|
}
|
|
|
|
export function markQuestionUsed(id: number): void {
|
|
db.prepare("UPDATE questions SET status = 'used' WHERE id = $id").run({ $id: id });
|
|
}
|