Initial commit: Proyecto Portal ePromos completo con documentación unificada y seguridad base
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
// memoria/registrar_jugada.php
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
|
||||
session_start();
|
||||
require_once __DIR__ . '/../admin/db.php';
|
||||
|
||||
// Leer payload
|
||||
$payload = json_decode(file_get_contents('php://input'), true) ?: [];
|
||||
$juego_id = (int)($payload['juego_id'] ?? 1);
|
||||
$elapsed = (int)($payload['elapsed_ms'] ?? 0);
|
||||
$aciertos = (int)($payload['aciertos'] ?? 0);
|
||||
|
||||
$user_agent = $_SERVER['HTTP_USER_AGENT'] ?? '';
|
||||
$ip_address = $_SERVER['REMOTE_ADDR'] ?? '';
|
||||
|
||||
// 1) Traer la ruleta asociada a este juego
|
||||
$stmt = $conn->prepare("SELECT ruleta_id FROM juegos_config WHERE juego_id=? LIMIT 1");
|
||||
$stmt->bind_param("i", $juego_id);
|
||||
$stmt->execute();
|
||||
$ruleta_id = (int)($stmt->get_result()->fetch_assoc()['ruleta_id'] ?? 0);
|
||||
$stmt->close();
|
||||
|
||||
// 2) Registrar jugada propia del juego de memoria
|
||||
$stmt = $conn->prepare("INSERT INTO jugadas_memoria (juego_id, ip_address, user_agent, tiempo_ms, aciertos) VALUES (?,?,?,?,?)");
|
||||
$stmt->bind_param("issii", $juego_id, $ip_address, $user_agent, $elapsed, $aciertos);
|
||||
$stmt->execute();
|
||||
$stmt->close();
|
||||
|
||||
// 3) Seleccionar premio usando la misma lógica que la ruleta
|
||||
$premio_nombre = null;
|
||||
if ($ruleta_id > 0) {
|
||||
// Leer premios con inventario y probabilidad
|
||||
$stmt = $conn->prepare("SELECT id, nombre, cantidad, probabilidad FROM premios WHERE ruleta_id=? AND cantidad > 0");
|
||||
$stmt->bind_param("i", $ruleta_id);
|
||||
$stmt->execute();
|
||||
$res = $stmt->get_result();
|
||||
$premios = [];
|
||||
$totalProb = 0.0;
|
||||
while ($row = $res->fetch_assoc()) {
|
||||
$row['probabilidad'] = (float)$row['probabilidad'];
|
||||
$premios[] = $row;
|
||||
$totalProb += $row['probabilidad'];
|
||||
}
|
||||
$stmt->close();
|
||||
|
||||
if ($premios && $totalProb > 0) {
|
||||
// sorteo ponderado
|
||||
$rnd = mt_rand() / mt_getrandmax() * $totalProb;
|
||||
$acc = 0.0; $ganador = null;
|
||||
foreach ($premios as $p) {
|
||||
$acc += $p['probabilidad'];
|
||||
if ($rnd <= $acc) { $ganador = $p; break; }
|
||||
}
|
||||
if (!$ganador) $ganador = end($premios);
|
||||
|
||||
// Guardar jugada general de la ruleta (para tus reportes unificados)
|
||||
$stmt = $conn->prepare("INSERT INTO jugadas (ruleta_id, premio_id, ip_address, user_agent) VALUES (?,?,?,?)");
|
||||
$stmt->bind_param("iiss", $ruleta_id, $ganador['id'], $ip_address, $user_agent);
|
||||
$stmt->execute();
|
||||
$stmt->close();
|
||||
|
||||
// Descontar inventario
|
||||
$stmt = $conn->prepare("UPDATE premios SET cantidad = cantidad - 1 WHERE id=? AND cantidad > 0");
|
||||
$stmt->bind_param("i", $ganador['id']);
|
||||
$stmt->execute();
|
||||
$stmt->close();
|
||||
|
||||
$premio_nombre = $ganador['nombre'];
|
||||
}
|
||||
}
|
||||
|
||||
echo json_encode([
|
||||
'ok' => true,
|
||||
'premio' => $premio_nombre
|
||||
]);
|
||||
Reference in New Issue
Block a user