Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| c8199aa261 | |||
| 7661343572 | |||
| e97586b033 |
+1
-2
@@ -7,7 +7,6 @@
|
||||
<title>Trivial - Lobby</title>
|
||||
<link rel="icon" type="image/svg+xml" href="img/favicon/favicon.svg">
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
|
||||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.13.1/font/bootstrap-icons.min.css">
|
||||
<link rel="stylesheet" href="css/index.css">
|
||||
<link rel="stylesheet" href="css/buscadorSala.css">
|
||||
</head>
|
||||
@@ -84,7 +83,7 @@
|
||||
<div class="modal-body">
|
||||
<div class="mb-3 fs-1">⚠️</div>
|
||||
<h4 class="modal-title mb-2 fw-bold" id="modalAbandonarSalaLabel">¿Abandonar la sala?</h4>
|
||||
<p class="text-dark-50" id="modalAbandonarSalaMensaje">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 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">
|
||||
|
||||
@@ -60,7 +60,6 @@ class EquipoxUsuario
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
public function obtenerSalaUsuario($idUsuario)
|
||||
{
|
||||
$conexion = conexionBD();
|
||||
@@ -109,6 +108,7 @@ class EquipoxUsuario
|
||||
'idEquipo' => $resultado['idEquipo'],
|
||||
'idPartida' => $resultado['idPartida']
|
||||
];
|
||||
|
||||
} catch (PDOException $error) {
|
||||
|
||||
return [
|
||||
@@ -127,143 +127,21 @@ class EquipoxUsuario
|
||||
}
|
||||
|
||||
try {
|
||||
|
||||
// 1. Averiguamos el equipo y la partida del usuario
|
||||
$sql = "SELECT exu.idEquipo, exp.idPartida, p.idUsuario AS idHost FROM equipoxusuario exu INNER JOIN equipoxpartida exp ON exu.idEquipo = exp.idEquipo INNER JOIN partida p ON exp.idPartida = p.idPartida WHERE exu.idUsuario = :idUsuario LIMIT 1";
|
||||
|
||||
$sql = "DELETE FROM equipoxusuario WHERE idUsuario = :idUsuario";
|
||||
$consulta = $conexion->prepare($sql);
|
||||
$consulta->bindParam(':idUsuario', $idUsuario, PDO::PARAM_INT);
|
||||
$consulta->execute();
|
||||
|
||||
$datosSala = $consulta->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
// El usuario no pertenece a ninguna sala
|
||||
if (!$datosSala) {
|
||||
return [
|
||||
'error' => 'No perteneces a ninguna sala.'
|
||||
];
|
||||
}
|
||||
|
||||
$idEquipo = (int)$datosSala['idEquipo'];
|
||||
$idPartida = (int)$datosSala['idPartida'];
|
||||
$idHost = (int)$datosSala['idHost'];
|
||||
|
||||
// Comenzamos una transacción
|
||||
$conexion->beginTransaction();
|
||||
|
||||
/*
|
||||
* 2. Comprobamos si quien abandona es el HOST
|
||||
*/
|
||||
if ($idUsuario === $idHost) {
|
||||
|
||||
/*
|
||||
* Obtenemos todos los equipos pertenecientes
|
||||
* a esta partida.
|
||||
*/
|
||||
$sqlEquipos = "SELECT idEquipo FROM equipoxpartida WHERE idPartida = :idPartida";
|
||||
|
||||
$consultaEquipos = $conexion->prepare($sqlEquipos);
|
||||
$consultaEquipos->bindParam(':idPartida', $idPartida, PDO::PARAM_INT);
|
||||
$consultaEquipos->execute();
|
||||
|
||||
$equipos = $consultaEquipos->fetchAll(PDO::FETCH_COLUMN);
|
||||
|
||||
/*
|
||||
* 3. Eliminamos los jugadores de esos equipos
|
||||
* de la tabla equipoxusuario.
|
||||
*/
|
||||
if (!empty($equipos)) {
|
||||
|
||||
$placeholders = implode(',', array_fill(0, count($equipos), '?'));
|
||||
|
||||
$sqlUsuarios = "DELETE FROM equipoxusuario WHERE idEquipo IN ($placeholders)";
|
||||
|
||||
$consultaUsuarios = $conexion->prepare($sqlUsuarios);
|
||||
$consultaUsuarios->execute($equipos);
|
||||
}
|
||||
|
||||
/*
|
||||
* 4. Eliminamos las relaciones
|
||||
* entre equipos y partida.
|
||||
*/
|
||||
$sqlEquipoxPartida = "DELETE FROM equipoxpartida WHERE idPartida = :idPartida";
|
||||
|
||||
$consultaEquipoxPartida = $conexion->prepare($sqlEquipoxPartida);
|
||||
$consultaEquipoxPartida->bindParam(':idPartida', $idPartida,PDO::PARAM_INT);
|
||||
$consultaEquipoxPartida->execute();
|
||||
|
||||
/*
|
||||
* 5. Eliminamos los equipos.
|
||||
*
|
||||
* Los equipos ya no tienen relaciones con
|
||||
* usuarios ni con la partida.
|
||||
*/
|
||||
if (!empty($equipos)) {
|
||||
|
||||
$placeholders = implode(',', array_fill(0, count($equipos), '?'));
|
||||
|
||||
$sqlEliminarEquipos = "DELETE FROM equipo WHERE idEquipo IN ($placeholders)";
|
||||
$consultaEliminarEquipos = $conexion->prepare($sqlEliminarEquipos);
|
||||
$consultaEliminarEquipos->execute($equipos);
|
||||
}
|
||||
|
||||
/*
|
||||
* 6. Eliminamos las relaciones
|
||||
* entre preguntas y partida.
|
||||
*/
|
||||
$sqlPartidaxPregunta = "DELETE FROM partidaxpregunta WHERE idPartida = :idPartida";
|
||||
|
||||
$consultaPartidaxPregunta = $conexion->prepare($sqlPartidaxPregunta);
|
||||
$consultaPartidaxPregunta->bindParam(':idPartida', $idPartida, PDO::PARAM_INT);
|
||||
$consultaPartidaxPregunta->execute();
|
||||
|
||||
/*
|
||||
* 7. Finalmente eliminamos la partida.
|
||||
*/
|
||||
$sqlPartida = "DELETE FROM partida WHERE idPartida = :idPartida";
|
||||
|
||||
$consultaPartida = $conexion->prepare($sqlPartida);
|
||||
$consultaPartida->bindParam(':idPartida', $idPartida, PDO::PARAM_INT);
|
||||
$consultaPartida->execute();
|
||||
|
||||
// Todo ha ido correctamente
|
||||
$conexion->commit();
|
||||
|
||||
return [
|
||||
'exito' => true,
|
||||
'salaEliminada' => true,
|
||||
'mensaje' => 'La sala ha sido eliminada correctamente.'
|
||||
];
|
||||
}
|
||||
|
||||
/*
|
||||
* 7. Si NO es el host, simplemente abandona
|
||||
* su relación con el equipo.
|
||||
*/
|
||||
$sqlSalir = "DELETE FROM equipoxusuario WHERE idUsuario = :idUsuario";
|
||||
|
||||
$consultaSalir = $conexion->prepare($sqlSalir);
|
||||
$consultaSalir->bindParam(':idUsuario', $idUsuario, PDO::PARAM_INT);
|
||||
$consultaSalir->execute();
|
||||
|
||||
$conexion->commit();
|
||||
|
||||
return [
|
||||
'exito' => true,
|
||||
'salaEliminada' => false,
|
||||
'mensaje' => 'Has abandonado la sala correctamente.'
|
||||
'mensaje' => 'Usuario eliminado de la sala correctamente.'
|
||||
];
|
||||
} catch (PDOException $error) {
|
||||
|
||||
// Si había una transacción activa, la deshacemos
|
||||
if ($conexion->inTransaction()) {
|
||||
$conexion->rollBack();
|
||||
}
|
||||
|
||||
return [
|
||||
'error' => 'Ocurrió un error al abandonar la sala.',
|
||||
'error' => 'Ocurrió un error al salir de la sala.',
|
||||
'error_tecnico' => $error->getMessage()
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
|
||||
<?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()
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
|
||||
<?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()
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
+4
-28
@@ -130,7 +130,7 @@ function mostrarSala(partida, equipos, jugadores) {
|
||||
esHost ? "jugador-host" : ""
|
||||
}">
|
||||
<div>
|
||||
<span class="avatar">${esHost ? '<i class="bi bi-award-fill"></i>' : '<i class="bi bi-person-fill"></i>'}</span>
|
||||
<span class="avatar">${esHost ? "👑" : "👤"}</span>
|
||||
${escapeHTML(jugador.nombreUsuario)}
|
||||
</div>
|
||||
|
||||
@@ -174,15 +174,14 @@ function mostrarSala(partida, equipos, jugadores) {
|
||||
let accionesSalaHTML = "";
|
||||
|
||||
if (usuarioYaEstaEnEquipo) {
|
||||
const esHost = partida.idUsuario == idUsuarioSesion;
|
||||
let botonIniciarHTML = "";
|
||||
|
||||
// Solo el host puede iniciar la partida
|
||||
if (esHost) {
|
||||
if (partida.idUsuario == idUsuarioSesion) {
|
||||
botonIniciarHTML = `
|
||||
<a href="juego.php?idPartida=${partida.idPartida}"
|
||||
class="btn btn-success btn-lg px-4">
|
||||
<i class="bi bi-controller"></i> ¡Iniciar Partida!
|
||||
🎮 ¡Iniciar Partida!
|
||||
</a>
|
||||
`;
|
||||
}
|
||||
@@ -192,8 +191,7 @@ function mostrarSala(partida, equipos, jugadores) {
|
||||
|
||||
<button type="button"
|
||||
class="btn btn-outline-danger btn-abandonar-sala"
|
||||
data-id-partida="${partida.idPartida}"
|
||||
data-es-host="${esHost}">
|
||||
data-id-partida="${partida.idPartida}">
|
||||
Abandonar sala
|
||||
</button>
|
||||
|
||||
@@ -265,34 +263,12 @@ let botonAbandonarReferencia = null;
|
||||
// A. Al hacer clic en el botón "Abandonar sala" de la tarjeta, abrimos el modal
|
||||
$(document).on("click", ".btn-abandonar-sala", function () {
|
||||
botonAbandonarReferencia = $(this);
|
||||
|
||||
const esHost = $(this).data("es-host") === true || $(this).data("es-host") === "true";
|
||||
const titulo = $("#modalAbandonarSalaLabel");
|
||||
const mensaje = $("#modalAbandonarSalaMensaje");
|
||||
const botonConfirmar = $("#btnConfirmarAbandonar");
|
||||
|
||||
if (esHost) {
|
||||
titulo.text("¿Abandonar la sala?");
|
||||
mensaje.text(
|
||||
"Si abandonas la sala como anfitrión, la partida terminará para todos los jugadores.",
|
||||
);
|
||||
botonConfirmar.text("Sí, abandonar");
|
||||
} else {
|
||||
titulo.text("¿Abandonar la sala?");
|
||||
mensaje.text(
|
||||
"Si sales de la sala, perderás tu lugar en el equipo.",
|
||||
);
|
||||
botonConfirmar.text("Sí, salir");
|
||||
}
|
||||
|
||||
const modal = new bootstrap.Modal(
|
||||
document.getElementById("modalAbandonarSala"),
|
||||
);
|
||||
|
||||
modal.show();
|
||||
});
|
||||
|
||||
|
||||
// B. Al hacer clic en "Sí, salir" dentro del modal Kahoot
|
||||
$(document).on("click", "#btnConfirmarAbandonar", function () {
|
||||
const modalEl = document.getElementById("modalAbandonarSala");
|
||||
|
||||
@@ -166,6 +166,9 @@ CREATE TABLE `partida` (
|
||||
`idPartida` int(11) NOT NULL AUTO_INCREMENT,
|
||||
`nombrePartida` varchar(100) NOT NULL,
|
||||
`idUsuario` int(11) NOT NULL,
|
||||
`fechaCreacion` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
`fechaInicio` DATETIME NULL,
|
||||
`fechaFin` DATETIME NULL,
|
||||
PRIMARY KEY (`idPartida`),
|
||||
KEY `FK idUsuario tabla usuario` (`idUsuario`),
|
||||
CONSTRAINT `FK idUsuario tabla usuario` FOREIGN KEY (`idUsuario`) REFERENCES `usuario` (`idUsuario`)
|
||||
@@ -178,10 +181,10 @@ CREATE TABLE `partida` (
|
||||
|
||||
LOCK TABLES `partida` WRITE;
|
||||
/*!40000 ALTER TABLE `partida` DISABLE KEYS */;
|
||||
INSERT INTO `partida` VALUES
|
||||
(1,'Partida de prueba 1',1),
|
||||
(2,'Partida de prueba 2',4),
|
||||
(3,'Partida de prueba 3',9);
|
||||
INSERT INTO `partida` (idPartida, nombrePartida, idUsuario, fechaInicio, fechaFin) VALUES
|
||||
(1,'Partida de prueba 1',1, NULL, NULL),
|
||||
(2,'Partida de prueba 2',4, NULL, NULL),
|
||||
(3,'Partida de prueba 3',9, NULL, NULL);
|
||||
/*!40000 ALTER TABLE `partida` ENABLE KEYS */;
|
||||
UNLOCK TABLES;
|
||||
|
||||
@@ -2362,3 +2365,4 @@ UNLOCK TABLES;
|
||||
/*M!100616 SET NOTE_VERBOSITY=@OLD_NOTE_VERBOSITY */;
|
||||
|
||||
-- Dump completed on 2026-09-25 10:00:41
|
||||
|
||||
|
||||
Reference in New Issue
Block a user