From e97586b03390988d1ae12a19393fdcd7fbe25b78 Mon Sep 17 00:00:00 2001 From: profesor Date: Tue, 29 Sep 2026 09:42:57 +0200 Subject: [PATCH] =?UTF-8?q?Se=20asignan=2010=20preguntas=20a=20la=20partid?= =?UTF-8?q?a=20al=20crearse=20La=20clase=20Pregunta=20tiene=20un=20nuevo?= =?UTF-8?q?=20m=C3=A9todo=20getTodosIds()=20que=20devuelve=20un=20array=20?= =?UTF-8?q?con=20todos=20los=20id=20de=20las=20preguntas?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- clases/ClasePartida.php | 22 +++++++++++++++ clases/ClasePregunta.php | 19 +++++++++++++ controladores/ControladorPartida.php | 40 +++++++++++++++++++++++++++- 3 files changed, 80 insertions(+), 1 deletion(-) diff --git a/clases/ClasePartida.php b/clases/ClasePartida.php index b765afe..3c38d62 100644 --- a/clases/ClasePartida.php +++ b/clases/ClasePartida.php @@ -1,3 +1,4 @@ + prepare($sql); + $consulta->execute(); + return (int) $consulta->fetchColumn(); + } catch (PDOException $error) { + return [ + "error" => "Ocurrió un error al obtener el listado.", + 'error_tecnico' => $error->getMessage() + ]; + } + } + + } diff --git a/clases/ClasePregunta.php b/clases/ClasePregunta.php index b9de2ac..6fca2d6 100644 --- a/clases/ClasePregunta.php +++ b/clases/ClasePregunta.php @@ -1,3 +1,4 @@ + prepare("SELECT idPregunta FROM pregunta"); + $consulta->execute(); + return $consulta->fetchAll(PDO::FETCH_COLUMN); + } catch (PDOException $error) { + return [ + "error" => "Ocurrió un error al obtener el listado.", + 'error_tecnico' => $error->getMessage() + ]; + } + } } \ No newline at end of file diff --git a/controladores/ControladorPartida.php b/controladores/ControladorPartida.php index 4f49203..9fe9603 100644 --- a/controladores/ControladorPartida.php +++ b/controladores/ControladorPartida.php @@ -6,6 +6,7 @@ require_once '../clases/ClasePartida.php'; require_once '../clases/ClaseEquipo.php'; require_once '../clases/ClaseEquipoxUsuario.php'; require_once '../clases/ClaseEquipoxPartida.php'; +require_once '../clases/ClasePregunta.php'; switch ($_POST['orden']) { @@ -119,6 +120,7 @@ switch ($_POST['orden']) { } $idPartida = (int)$resultadoPartida['exito_tecnico']; + $partida->idPartida = $idPartida; /* @@ -211,6 +213,42 @@ switch ($_POST['orden']) { } + + + /* + * ========================================== + * 5. ASIGNAMOS 10 PREGUNTAS ALEATORIAS A LA PARTIDA + * ========================================== + */ + + // Obtenemos todos los IDs de las preguntas + + $listaPreguntas = Pregunta::getTodosIds(); + + $totalPreguntas = count($listaPreguntas); + + // Guardamos las preguntas que ya hemos asignado + $preguntasAsignadas = []; + + // Generamos 10 posiciones aleatorias sin repetir + while (count($preguntasAsignadas) < 10) { + + // Generamos una posición aleatoria del array + $posicionAleatoria = rand(0, $totalPreguntas - 1); + + // Obtenemos el ID real de la pregunta + $idPreguntaAleatoria = $listaPreguntas[$posicionAleatoria]; + + // Comprobamos que esa pregunta no haya sido asignada ya + if (!in_array($idPreguntaAleatoria, $preguntasAsignadas)) { + $preguntasAsignadas[] = $idPreguntaAleatoria; + + // Asignamos el ID real de la pregunta a la partida + $partida->asignarPregunta($idPreguntaAleatoria); + } + }//fin shile + + /* * ========================================== * TODO CORRECTO @@ -218,7 +256,7 @@ switch ($_POST['orden']) { */ echo json_encode([ - 'exito' => 'Sala creada correctamente.', + 'exito' => 'Sala creada correctamente. Preguntas preparadas.', 'idPartida' => $resultadoPartida['exito_tecnico'], 'idEquipo1' => $equipo1->idEquipo, 'idEquipo2' => $equipo2->idEquipo -- 2.47.3