Merge pull request 'Add funcionamiento del juego' (#12) from juegophp into main
Reviewed-on: #12 Reviewed-by: jorge <jdavidnunez@gmail.com>
This commit was merged in pull request #12.
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
<?php
|
||||
|
||||
session_start();
|
||||
|
||||
require_once '../clases/ClaseJuego.php';
|
||||
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
|
||||
// Hay que estar logueado y decir de qué partida se trata
|
||||
if (!isset($_SESSION['idUsuario'])) {
|
||||
echo json_encode(['error' => 'Debes iniciar sesión para jugar.']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$idUsuario = (int)$_SESSION['idUsuario'];
|
||||
$idPartida = intval($_POST['idPartida'] ?? 0);
|
||||
|
||||
if ($idPartida <= 0) {
|
||||
echo json_encode(['error' => 'No se ha indicado la partida.']);
|
||||
exit;
|
||||
}
|
||||
|
||||
// Solo pueden jugar los usuarios que están en uno de los equipos de la partida
|
||||
$perteneceAPartida = Juego::usuarioEnPartida($idPartida, $idUsuario);
|
||||
|
||||
if (is_array($perteneceAPartida)) {
|
||||
echo json_encode($perteneceAPartida);
|
||||
exit;
|
||||
}
|
||||
|
||||
if (!$perteneceAPartida) {
|
||||
echo json_encode(['error' => 'No formas parte de esta partida.']);
|
||||
exit;
|
||||
}
|
||||
|
||||
|
||||
switch ($_POST['orden'] ?? '') {
|
||||
|
||||
// Devuelve las 10 preguntas con sus respuestas (sin marcar la correcta)
|
||||
case 'cargarPreguntas':
|
||||
|
||||
echo json_encode(Juego::cargarPreguntas($idPartida, $idUsuario));
|
||||
|
||||
break;
|
||||
|
||||
|
||||
// Guarda la respuesta del usuario y dice si ha acertado
|
||||
case 'responder':
|
||||
|
||||
$idPartidaxPregunta = intval($_POST['idPartidaxPregunta'] ?? 0);
|
||||
$idRespuesta = intval($_POST['idRespuesta'] ?? 0);
|
||||
|
||||
if ($idPartidaxPregunta <= 0 || $idRespuesta <= 0) {
|
||||
echo json_encode(['error' => 'Faltan datos para registrar la respuesta.']);
|
||||
break;
|
||||
}
|
||||
|
||||
echo json_encode(
|
||||
Juego::registrarRespuesta(
|
||||
$idPartida,
|
||||
$idUsuario,
|
||||
$idPartidaxPregunta,
|
||||
$idRespuesta
|
||||
)
|
||||
);
|
||||
|
||||
break;
|
||||
|
||||
|
||||
// Recibe los aciertos del jugador al terminar sus 10 preguntas,
|
||||
// para que resultados.php los pueda usar al montar la hoja de resultados
|
||||
case 'finalizarPartida':
|
||||
|
||||
$aciertos = intval($_POST['aciertos'] ?? 0);
|
||||
|
||||
if (!isset($_SESSION['aciertosPorPartida'])) {
|
||||
$_SESSION['aciertosPorPartida'] = [];
|
||||
}
|
||||
|
||||
$_SESSION['aciertosPorPartida'][$idPartida] = $aciertos;
|
||||
|
||||
echo json_encode([
|
||||
'exito' => 'Aciertos registrados correctamente.',
|
||||
'idPartida' => $idPartida,
|
||||
'aciertos' => $aciertos
|
||||
]);
|
||||
|
||||
break;
|
||||
|
||||
|
||||
default:
|
||||
|
||||
echo json_encode(['error' => 'La orden indicada no existe.']);
|
||||
|
||||
break;
|
||||
}
|
||||
Reference in New Issue
Block a user