5 Commits

Author SHA1 Message Date
jorge c8199aa261 Añadida BD definitiva 2.0
Añadido a main la base de datos (modificada por Víctor Díaz)
2026-09-29 11:25:26 +02:00
profesor 7661343572 Merge pull request 'Se asignan 10 preguntas a la partida al crearse' (#3) from asignar_preguntas into main
Reviewed-on: #3
2026-09-29 09:56:06 +02:00
profesor e97586b033 Se asignan 10 preguntas a la partida al crearse
La clase Pregunta tiene un nuevo método getTodosIds() que devuelve un array con todos los id de las preguntas
2026-09-29 09:42:57 +02:00
Isaac 42cc1489e9 Arreglado CSS (Alguien™ estaba usando modo oscuro) 2026-09-29 09:10:30 +02:00
Isaac a694f19667 Funcionalidades del host 2026-09-28 14:17:54 +02:00
9 changed files with 154 additions and 29 deletions
+2 -2
View File
@@ -82,8 +82,8 @@
<div class="modal-content modal-kahoot text-center p-3"> <div class="modal-content modal-kahoot text-center p-3">
<div class="modal-body"> <div class="modal-body">
<div class="mb-3 fs-1">⚠️</div> <div class="mb-3 fs-1">⚠️</div>
<h4 class="modal-title mb-2 fw-bold text-white" id="modalAbandonarSalaLabel">¿Abandonar la sala?</h4> <h4 class="modal-title mb-2 fw-bold" id="modalAbandonarSalaLabel">¿Abandonar la sala?</h4>
<p class="text-white-50">Si sales de la sala, perderás tu lugar en el equipo.</p> <p class="text-dark-50">Si sales de la sala, perderás tu lugar en el equipo.</p>
</div> </div>
<div class="modal-footer justify-content-center border-0 gap-2"> <div class="modal-footer justify-content-center border-0 gap-2">
<button type="button" class="btn btn-secondary px-4 py-2" data-bs-dismiss="modal"> <button type="button" class="btn btn-secondary px-4 py-2" data-bs-dismiss="modal">
+22
View File
@@ -1,3 +1,4 @@
<?php <?php
require_once __DIR__ . '/../includes/conexionBD.php'; require_once __DIR__ . '/../includes/conexionBD.php';
@@ -466,4 +467,25 @@ class Partida
]; ];
} }
} }
public function contar()
{
$conexion = conexionBD();
if (is_array($conexion)) {
return $conexion;
}
try {
$sql="SELECT COUNT(*) AS total FROM partida";
$consulta = $conexion->prepare($sql);
$consulta->execute();
return (int) $consulta->fetchColumn();
} catch (PDOException $error) {
return [
"error" => "Ocurrió un error al obtener el listado.",
'error_tecnico' => $error->getMessage()
];
}
}
} }
+19
View File
@@ -1,3 +1,4 @@
<?php <?php
require_once __DIR__ . '/../includes/conexionBD.php'; require_once __DIR__ . '/../includes/conexionBD.php';
@@ -110,4 +111,22 @@ class Pregunta {
]; ];
} }
} }
public static function getTodosIds()//devuelve solamente los id de todas las preguntas
{
$conexion = conexionBD();
if (is_array($conexion)) {
return $conexion;
}
try {
$consulta = $conexion->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()
];
}
}
} }
+39 -1
View File
@@ -6,6 +6,7 @@ require_once '../clases/ClasePartida.php';
require_once '../clases/ClaseEquipo.php'; require_once '../clases/ClaseEquipo.php';
require_once '../clases/ClaseEquipoxUsuario.php'; require_once '../clases/ClaseEquipoxUsuario.php';
require_once '../clases/ClaseEquipoxPartida.php'; require_once '../clases/ClaseEquipoxPartida.php';
require_once '../clases/ClasePregunta.php';
switch ($_POST['orden']) { switch ($_POST['orden']) {
@@ -119,6 +120,7 @@ switch ($_POST['orden']) {
} }
$idPartida = (int)$resultadoPartida['exito_tecnico']; $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 * TODO CORRECTO
@@ -218,7 +256,7 @@ switch ($_POST['orden']) {
*/ */
echo json_encode([ echo json_encode([
'exito' => 'Sala creada correctamente.', 'exito' => 'Sala creada correctamente. Preguntas preparadas.',
'idPartida' => $resultadoPartida['exito_tecnico'], 'idPartida' => $resultadoPartida['exito_tecnico'],
'idEquipo1' => $equipo1->idEquipo, 'idEquipo1' => $equipo1->idEquipo,
'idEquipo2' => $equipo2->idEquipo 'idEquipo2' => $equipo2->idEquipo
+10 -2
View File
@@ -68,8 +68,8 @@
.btn-unirse { .btn-unirse {
border: none; border: none;
border-bottom: 3px solid #0c4891; border-bottom: 3px solid #1f6f09;
background-color: var(--kahoot-blue); background-color: #26890c;
color: white; color: white;
font-weight: 600; font-weight: 600;
border-radius: 8px; border-radius: 8px;
@@ -105,3 +105,11 @@
border-color: var(--kahoot-purple); border-color: var(--kahoot-purple);
box-shadow: 0 0 0 3px rgba(70, 23, 143, 0.15); box-shadow: 0 0 0 3px rgba(70, 23, 143, 0.15);
} }
.jugador-host {
background-color: #fff3cd;
border: 1px solid #ffc107;
border-radius: 8px;
padding: 6px 10px;
font-weight: 600;
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 6.9 KiB

+44 -13
View File
@@ -123,11 +123,23 @@ function mostrarSala(partida, equipos, jugadores) {
let jugadoresHTML = ""; let jugadoresHTML = "";
if (jugadoresEquipo.length > 0) { if (jugadoresEquipo.length > 0) {
jugadoresEquipo.forEach(function (jugador) { jugadoresEquipo.forEach(function (jugador) {
const esHost = jugador.idUsuario == partida.idUsuario;
jugadoresHTML += ` jugadoresHTML += `
<div class="jugador"> <div class="jugador border border-2 d-flex align-items-center justify-content-between ${
<span class="avatar">👤</span> esHost ? "jugador-host" : ""
}">
<div>
<span class="avatar">${esHost ? "👑" : "👤"}</span>
${escapeHTML(jugador.nombreUsuario)} ${escapeHTML(jugador.nombreUsuario)}
</div> </div>
${
esHost
? `<span class="badge bg-warning text-dark ms-2">HOST</span>`
: ""
}
</div>
`; `;
}); });
} else { } else {
@@ -138,7 +150,7 @@ function mostrarSala(partida, equipos, jugadores) {
let botonUnirseHTML = ""; let botonUnirseHTML = "";
if (!usuarioYaEstaEnEquipo) { if (!usuarioYaEstaEnEquipo) {
botonUnirseHTML = ` botonUnirseHTML = `
<button type="button" class="btn btn-unirse w-100 btn-unirse-equipo mt-2" <button type="button" class="btn-unirse w-100 btn-unirse-equipo mt-2"
data-id-partida="${partida.idPartida}" data-id-partida="${partida.idPartida}"
data-id-equipo="${equipo.idEquipo}"> data-id-equipo="${equipo.idEquipo}">
Unirse Unirse
@@ -157,17 +169,34 @@ function mostrarSala(partida, equipos, jugadores) {
`; `;
}); });
// Acciones disponibles si el usuario YA ESTÁ dentro de esta sala
// Acciones disponibles si el usuario YA ESTÁ dentro de esta sala // Acciones disponibles si el usuario YA ESTÁ dentro de esta sala
let accionesSalaHTML = ""; let accionesSalaHTML = "";
if (usuarioYaEstaEnEquipo) { if (usuarioYaEstaEnEquipo) {
accionesSalaHTML = ` let botonIniciarHTML = "";
<div class="sala-acciones text-center mt-4 pt-3 border-top d-flex justify-content-center gap-3">
<button type="button" class="btn btn-outline-danger btn-abandonar-sala" data-id-partida="${partida.idPartida}"> // Solo el host puede iniciar la partida
Abandonar sala if (partida.idUsuario == idUsuarioSesion) {
</button> botonIniciarHTML = `
<a href="juego.php?idPartida=${partida.idPartida}" class="btn btn-success btn-lg px-4"> <a href="juego.php?idPartida=${partida.idPartida}"
class="btn btn-success btn-lg px-4">
🎮 ¡Iniciar Partida! 🎮 ¡Iniciar Partida!
</a> </a>
`;
}
accionesSalaHTML = `
<div class="sala-acciones text-center mt-4 pt-3 border-top d-flex justify-content-center gap-3">
<button type="button"
class="btn btn-outline-danger btn-abandonar-sala"
data-id-partida="${partida.idPartida}">
Abandonar sala
</button>
${botonIniciarHTML}
</div> </div>
`; `;
} }
@@ -175,7 +204,7 @@ function mostrarSala(partida, equipos, jugadores) {
const salaHTML = ` const salaHTML = `
<div class="col-12 col-md-10 col-lg-8"> <div class="col-12 col-md-10 col-lg-8">
<div class="sala card p-3 shadow-sm mb-4"> <div class="sala card p-3 shadow-sm mb-4">
<div class="sala-header h4 text-center mb-3"> <div class="sala-header h4 text-center mb-3 text-white">
${escapeHTML(partida.nombrePartida)} ${escapeHTML(partida.nombrePartida)}
</div> </div>
<div class="sala-body"> <div class="sala-body">
@@ -234,13 +263,15 @@ let botonAbandonarReferencia = null;
// A. Al hacer clic en el botón "Abandonar sala" de la tarjeta, abrimos el modal // A. Al hacer clic en el botón "Abandonar sala" de la tarjeta, abrimos el modal
$(document).on("click", ".btn-abandonar-sala", function () { $(document).on("click", ".btn-abandonar-sala", function () {
botonAbandonarReferencia = $(this); botonAbandonarReferencia = $(this);
const modal = new bootstrap.Modal(document.getElementById('modalAbandonarSala')); const modal = new bootstrap.Modal(
document.getElementById("modalAbandonarSala"),
);
modal.show(); modal.show();
}); });
// B. Al hacer clic en "Sí, salir" dentro del modal Kahoot // B. Al hacer clic en "Sí, salir" dentro del modal Kahoot
$(document).on("click", "#btnConfirmarAbandonar", function () { $(document).on("click", "#btnConfirmarAbandonar", function () {
const modalEl = document.getElementById('modalAbandonarSala'); const modalEl = document.getElementById("modalAbandonarSala");
const modalInstance = bootstrap.Modal.getInstance(modalEl); const modalInstance = bootstrap.Modal.getInstance(modalEl);
const btnConfirmar = $(this); const btnConfirmar = $(this);
@@ -275,7 +306,7 @@ $(document).on("click", "#btnConfirmarAbandonar", function () {
if (modalInstance) { if (modalInstance) {
modalInstance.hide(); modalInstance.hide();
} }
} },
}); });
}); });
// 8. Evento: Crear Sala // 8. Evento: Crear Sala
+3
View File
@@ -1,3 +1,5 @@
let idUsuarioSesion = null;
$(document).ready(function () { $(document).ready(function () {
// Comprobar la sesión activa al cargar la página // Comprobar la sesión activa al cargar la página
comprobarEstadoSesion(); comprobarEstadoSesion();
@@ -36,6 +38,7 @@ function comprobarEstadoSesion() {
data: { orden: 'comprobarSesion' }, data: { orden: 'comprobarSesion' },
success: function (respuesta) { success: function (respuesta) {
if (respuesta.activa) { if (respuesta.activa) {
idUsuarioSesion = respuesta.usuario.idUsuario;
renderizarCabeceraConSesion(respuesta.usuario); renderizarCabeceraConSesion(respuesta.usuario);
$('#colBotonJugar').removeClass('d-none'); $('#colBotonJugar').removeClass('d-none');
} else { } else {
+8 -4
View File
@@ -166,6 +166,9 @@ CREATE TABLE `partida` (
`idPartida` int(11) NOT NULL AUTO_INCREMENT, `idPartida` int(11) NOT NULL AUTO_INCREMENT,
`nombrePartida` varchar(100) NOT NULL, `nombrePartida` varchar(100) NOT NULL,
`idUsuario` int(11) NOT NULL, `idUsuario` int(11) NOT NULL,
`fechaCreacion` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
`fechaInicio` DATETIME NULL,
`fechaFin` DATETIME NULL,
PRIMARY KEY (`idPartida`), PRIMARY KEY (`idPartida`),
KEY `FK idUsuario tabla usuario` (`idUsuario`), KEY `FK idUsuario tabla usuario` (`idUsuario`),
CONSTRAINT `FK idUsuario tabla usuario` FOREIGN KEY (`idUsuario`) REFERENCES `usuario` (`idUsuario`) CONSTRAINT `FK idUsuario tabla usuario` FOREIGN KEY (`idUsuario`) REFERENCES `usuario` (`idUsuario`)
@@ -178,10 +181,10 @@ CREATE TABLE `partida` (
LOCK TABLES `partida` WRITE; LOCK TABLES `partida` WRITE;
/*!40000 ALTER TABLE `partida` DISABLE KEYS */; /*!40000 ALTER TABLE `partida` DISABLE KEYS */;
INSERT INTO `partida` VALUES INSERT INTO `partida` (idPartida, nombrePartida, idUsuario, fechaInicio, fechaFin) VALUES
(1,'Partida de prueba 1',1), (1,'Partida de prueba 1',1, NULL, NULL),
(2,'Partida de prueba 2',4), (2,'Partida de prueba 2',4, NULL, NULL),
(3,'Partida de prueba 3',9); (3,'Partida de prueba 3',9, NULL, NULL);
/*!40000 ALTER TABLE `partida` ENABLE KEYS */; /*!40000 ALTER TABLE `partida` ENABLE KEYS */;
UNLOCK TABLES; UNLOCK TABLES;
@@ -2362,3 +2365,4 @@ UNLOCK TABLES;
/*M!100616 SET NOTE_VERBOSITY=@OLD_NOTE_VERBOSITY */; /*M!100616 SET NOTE_VERBOSITY=@OLD_NOTE_VERBOSITY */;
-- Dump completed on 2026-09-25 10:00:41 -- Dump completed on 2026-09-25 10:00:41