38428-vm/api/redeem.php
2026-02-19 20:04:20 +00:00

74 lines
2.9 KiB
PHP

<?php
require_once __DIR__ . '/../db/config.php';
header('Content-Type: application/json');
$data = json_decode(file_get_contents('php://input'), true);
$username = $data['username'] ?? '';
$item = $data['item'] ?? '';
$color = $data['color'] ?? null;
if (empty($username) || empty($item)) {
echo json_encode(['success' => false, 'error' => 'Datos incompletos']);
exit;
}
try {
$pdo = db();
$stmt = $pdo->prepare("SELECT loyalty_points, chat_color FROM fans WHERE name = ?");
$stmt->execute([$username]);
$fan = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$fan) {
echo json_encode(['success' => false, 'error' => 'Usuario no encontrado']);
exit;
}
$points = $fan['loyalty_points'];
$current_color = $fan['chat_color'];
if ($item === 'god_mode') {
$cost = 500;
if ($points < $cost) {
echo json_encode(['success' => false, 'error' => "Puntos insuficientes ($points/$cost)"]);
exit;
}
$new_color = $color ?: '#ffd700'; // Gold default
$stmt = $pdo->prepare("UPDATE fans SET loyalty_points = loyalty_points - ?, chat_color = ? WHERE name = ?");
$stmt->execute([$cost, $new_color, $username]);
echo json_encode(['success' => true, 'message' => '¡Modo Dios activado! Ahora tienes un color exclusivo.']);
} elseif ($item === 'dj_day') {
$cost = 2000;
if ($points < $cost) {
echo json_encode(['success' => false, 'error' => "Puntos insuficientes ($points/$cost)"]);
exit;
}
$stmt = $pdo->prepare("UPDATE fans SET loyalty_points = loyalty_points - ?, dj_day_until = DATE_ADD(NOW(), INTERVAL 1 DAY) WHERE name = ?");
$stmt->execute([$cost, $username]);
echo json_encode(['success' => true, 'message' => '¡Eres DJ por un día! Tu nombre destacará en toda la sala.']);
} elseif ($item === 'change_color') {
// If they already have god mode, changing color might be cheaper or free?
// Let's say 100 points to change color if already bought, or free if we want.
if (!$current_color) {
echo json_encode(['success' => false, 'error' => 'Primero debes desbloquear el Modo Dios']);
exit;
}
$cost = 50;
if ($points < $cost) {
echo json_encode(['success' => false, 'error' => "Puntos insuficientes ($points/$cost)"]);
exit;
}
$stmt = $pdo->prepare("UPDATE fans SET loyalty_points = loyalty_points - ?, chat_color = ? WHERE name = ?");
$stmt->execute([$cost, $color, $username]);
echo json_encode(['success' => true, 'message' => '¡Color actualizado!']);
} else {
echo json_encode(['success' => false, 'error' => 'Ítem no reconocido']);
}
} catch (Exception $e) {
echo json_encode(['success' => false, 'error' => $e->getMessage()]);
}