141 lines
2.8 KiB
PHP
141 lines
2.8 KiB
PHP
<?php
|
|
|
|
session_start();
|
|
require_once '../clases/claseJuego.php';
|
|
header('Content-Type: application/json; charset=utf-8');
|
|
|
|
// Comprobamos que haya un usuario conectado
|
|
|
|
if (!isset($_SESSION['idUsuario'])) {
|
|
echo json_encode([
|
|
'error' => 'Debes iniciar sesión para jugar.'
|
|
]);
|
|
|
|
exit;
|
|
}
|
|
|
|
$idUsuario = (int) $_SESSION['idUsuario'];
|
|
|
|
// Comprobamos que se haya indicado la partida
|
|
|
|
if (!isset($_POST['idPartida'])) {
|
|
echo json_encode([
|
|
'error' => 'No se ha indicado la partida.'
|
|
]);
|
|
|
|
exit;
|
|
}
|
|
|
|
$idPartida = intval($_POST['idPartida']);
|
|
|
|
if ($idPartida <= 0) {
|
|
echo json_encode([
|
|
'error' => 'La partida indicada no es válida.'
|
|
]);
|
|
|
|
exit;
|
|
}
|
|
|
|
// Comprobamos que el usuario pertenezca a 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;
|
|
}
|
|
|
|
// Comprobamos la orden
|
|
|
|
$orden = $_POST['orden'] ?? '';
|
|
|
|
switch ($orden) {
|
|
|
|
// CARGAR LAS PREGUNTAS DE LA PARTIDA
|
|
|
|
case 'cargarPreguntas':
|
|
|
|
$resultado = Juego::cargarPreguntas($idPartida, $idUsuario);
|
|
echo json_encode($resultado);
|
|
|
|
break;
|
|
|
|
// REGISTRAR RESPUESTA
|
|
|
|
case 'responder':
|
|
|
|
if (!isset($_POST['idPartidaxPregunta']) || !isset($_POST['idRespuesta'])){
|
|
echo json_encode([
|
|
'error' => 'Faltan datos para registrar la respuesta.'
|
|
]);
|
|
|
|
break;
|
|
}
|
|
|
|
$idPartidaxPregunta = intval($_POST['idPartidaxPregunta']);
|
|
|
|
$idRespuesta = intval($_POST['idRespuesta']);
|
|
|
|
if ($idPartidaxPregunta <= 0 || $idRespuesta <= 0){
|
|
|
|
echo json_encode([
|
|
'error' => 'Los datos de la respuesta no son válidos.'
|
|
]);
|
|
|
|
break;
|
|
}
|
|
|
|
// Registramos la respuesta del usuario
|
|
|
|
$resultado = Juego::registrarRespuesta($idPartida, $idUsuario, $idPartidaxPregunta, $idRespuesta);
|
|
|
|
echo json_encode($resultado);
|
|
|
|
break;
|
|
|
|
// Finalizar la partida y registrar los aciertos del usuario
|
|
|
|
case 'finalizarPartida':
|
|
|
|
$aciertos = intval($_POST['aciertos'] ?? 0);
|
|
|
|
// Guardamos los aciertos en la sesión
|
|
|
|
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;
|
|
}
|
|
|
|
?>
|