Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 5420c83b30 | |||
| d28fbc7160 | |||
| 4efb831b7a | |||
| 22f38f924f | |||
| 01aa2643f6 |
+48
-154
@@ -4,24 +4,19 @@
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
|
||||
<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 href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
|
||||
<link rel="stylesheet" href="css/index.css">
|
||||
<link rel="stylesheet" href="css/buscadorSala.css">
|
||||
</head>
|
||||
|
||||
<body>
|
||||
|
||||
<?php
|
||||
include_once("cabecera.php");
|
||||
?>
|
||||
<?php include_once("cabecera.php"); ?>
|
||||
|
||||
<main class="container py-4">
|
||||
|
||||
<main class="container py-5">
|
||||
<!-- Botón Volver -->
|
||||
<div class="mb-4">
|
||||
<a href="index.php" class="btn btn-outline-secondary d-inline-flex align-items-center gap-2">
|
||||
@@ -31,181 +26,80 @@
|
||||
Volver al Inicio
|
||||
</a>
|
||||
</div>
|
||||
|
||||
<!-- Botón Crear Sala -->
|
||||
<div id="contenedorCrearSala" class="text-center mb-5">
|
||||
|
||||
|
||||
<button
|
||||
type="button"
|
||||
class="btn btn-play-kahoot btn-crear-sala"
|
||||
data-bs-toggle="modal"
|
||||
data-bs-target="#modalCrearSala">
|
||||
|
||||
<span>+</span>
|
||||
Crear sala
|
||||
|
||||
<button type="button" class="btn btn-play-kahoot btn-crear-sala" data-bs-toggle="modal" data-bs-target="#modalCrearSala">
|
||||
<span>+</span> Crear sala
|
||||
</button>
|
||||
|
||||
</div>
|
||||
|
||||
<div
|
||||
id="contenedorSalas"
|
||||
class="row justify-content-center g-4">
|
||||
|
||||
<div
|
||||
id="cargandoSalas"
|
||||
class="col-12 text-center">
|
||||
|
||||
<div
|
||||
class="spinner-border text-primary"
|
||||
role="status">
|
||||
|
||||
<span class="visually-hidden">
|
||||
Cargando...
|
||||
</span>
|
||||
|
||||
<!-- Contenedor Principal de Salas -->
|
||||
<div id="contenedorSalas" class="row justify-content-center g-4">
|
||||
<div id="cargandoSalas" class="col-12 text-center">
|
||||
<div class="spinner-border text-primary" role="status">
|
||||
<span class="visually-hidden">Cargando...</span>
|
||||
</div>
|
||||
|
||||
<p class="mt-3 text-muted">
|
||||
Cargando salas...
|
||||
</p>
|
||||
|
||||
<p class="mt-3 text-muted">Cargando salas...</p>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
</main>
|
||||
|
||||
<!-- Modal Crear Sala -->
|
||||
<div
|
||||
class="modal fade"
|
||||
id="modalCrearSala"
|
||||
tabindex="-1"
|
||||
aria-labelledby="modalCrearSalaLabel"
|
||||
aria-hidden="true">
|
||||
|
||||
<div class="modal fade" id="modalCrearSala" tabindex="-1" aria-labelledby="modalCrearSalaLabel" aria-hidden="true">
|
||||
<div class="modal-dialog modal-dialog-centered">
|
||||
|
||||
<div class="modal-content modal-kahoot">
|
||||
|
||||
<div class="modal-header modal-kahoot-header">
|
||||
|
||||
<h5
|
||||
class="modal-title"
|
||||
id="modalCrearSalaLabel">
|
||||
|
||||
Crear sala
|
||||
|
||||
</h5>
|
||||
|
||||
<button
|
||||
type="button"
|
||||
class="btn-close btn-close-white"
|
||||
data-bs-dismiss="modal"
|
||||
aria-label="Cerrar">
|
||||
</button>
|
||||
|
||||
<h5 class="modal-title" id="modalCrearSalaLabel">Crear sala</h5>
|
||||
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Cerrar"></button>
|
||||
</div>
|
||||
|
||||
<div class="modal-body p-4">
|
||||
|
||||
<div class="mb-3">
|
||||
|
||||
<label
|
||||
for="nombreSala"
|
||||
class="form-label">
|
||||
|
||||
Nombre de la sala
|
||||
|
||||
</label>
|
||||
|
||||
<input
|
||||
type="text"
|
||||
id="nombreSala"
|
||||
class="form-control input-kahoot"
|
||||
name="nombreSala"
|
||||
placeholder="Ej. Noche de Trivial"
|
||||
maxlength="100">
|
||||
|
||||
<label for="nombreSala" class="form-label">Nombre de la sala</label>
|
||||
<input type="text" id="nombreSala" class="form-control input-kahoot" name="nombreSala" placeholder="Ej. Noche de Trivial" maxlength="100">
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
|
||||
<label
|
||||
for="nombreEquipo1"
|
||||
class="form-label">
|
||||
|
||||
Nombre del equipo 1
|
||||
|
||||
</label>
|
||||
|
||||
<input
|
||||
type="text"
|
||||
id="nombreEquipo1"
|
||||
name="nombreEquipo1"
|
||||
class="form-control input-kahoot"
|
||||
placeholder="Ej. Los cerebritos"
|
||||
maxlength="50">
|
||||
|
||||
<label for="nombreEquipo1" class="form-label">Nombre del equipo 1</label>
|
||||
<input type="text" id="nombreEquipo1" name="nombreEquipo1" class="form-control input-kahoot" placeholder="Ej. Los cerebritos" maxlength="50">
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
|
||||
<label
|
||||
for="nombreEquipo2"
|
||||
class="form-label">
|
||||
|
||||
Nombre del equipo 2
|
||||
|
||||
</label>
|
||||
|
||||
<input
|
||||
type="text"
|
||||
id="nombreEquipo2"
|
||||
name="nombreEquipo2"
|
||||
class="form-control input-kahoot"
|
||||
placeholder="Ej. Los campeones"
|
||||
maxlength="50">
|
||||
|
||||
<label for="nombreEquipo2" class="form-label">Nombre del equipo 2</label>
|
||||
<input type="text" id="nombreEquipo2" name="nombreEquipo2" class="form-control input-kahoot" placeholder="Ej. Los campeones" maxlength="50">
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
<div class="modal-footer">
|
||||
|
||||
<button
|
||||
type="button"
|
||||
class="btn btn-secondary"
|
||||
data-bs-dismiss="modal">
|
||||
|
||||
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancelar</button>
|
||||
<button type="button" class="btn btn-kahoot-login" id="btnCrearSala">Crear sala</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<!--MODAL ABANDONAR SALA -->
|
||||
<!-- Modal Confirmar Abandonar Sala -->
|
||||
<div class="modal fade" id="modalAbandonarSala" tabindex="-1" aria-labelledby="modalAbandonarSalaLabel" aria-hidden="true">
|
||||
<div class="modal-dialog modal-dialog-centered">
|
||||
<div class="modal-content modal-kahoot text-center p-3">
|
||||
<div class="modal-body">
|
||||
<div class="mb-3 fs-1">⚠️</div>
|
||||
<h4 class="modal-title mb-2 fw-bold text-white" id="modalAbandonarSalaLabel">¿Abandonar la sala?</h4>
|
||||
<p class="text-white-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">
|
||||
Cancelar
|
||||
|
||||
</button>
|
||||
|
||||
<button
|
||||
type="button"
|
||||
class="btn btn-kahoot-login"
|
||||
id="btnCrearSala">
|
||||
|
||||
Crear sala
|
||||
|
||||
<button type="button" id="btnConfirmarAbandonar" class="btn btn-danger px-4 py-2">
|
||||
Sí, salir
|
||||
</button>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
<script
|
||||
src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js">
|
||||
</script>
|
||||
|
||||
<script src="js/jquery.min.js"></script>
|
||||
|
||||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
|
||||
<script src="js/jquery-4.0.0.min.js"></script>
|
||||
<script src="js/buscadorSala.js"></script>
|
||||
|
||||
</body>
|
||||
|
||||
</html>
|
||||
+9
-8
@@ -21,9 +21,8 @@
|
||||
<div class="collapse navbar-collapse" id="navbarContent">
|
||||
<ul class="navbar-nav me-auto mb-2 mb-lg-0"></ul>
|
||||
|
||||
<!-- Adtoy ti pagianan ti botones / dropdown a patulaon ti JS -->
|
||||
<!-- Contenedor dinámico cargado por js/cabecera.js -->
|
||||
<div class="d-flex align-items-center gap-3" id="contenedorUsuarioCabecera">
|
||||
<!-- Ikapit ti JS ti HTML dita -->
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -49,11 +48,6 @@
|
||||
<input type="password" class="form-control border-dark" id="loginPassword" name="contrasena" placeholder="Contraseña" required>
|
||||
<label for="loginPassword">Contraseña</label>
|
||||
</div>
|
||||
<div class="form-check">
|
||||
<input class="form-check-input" id="recordar" name="recordar" type="checkbox" />
|
||||
<label for="recordar" class="small">Recuérdeme durante 10 días</label>
|
||||
</div>
|
||||
<input type="hidden" name="orden" value="validar">
|
||||
</form>
|
||||
<div id="errorLogin" class="text-danger fw-bold mt-2"></div>
|
||||
</div>
|
||||
@@ -93,7 +87,14 @@
|
||||
<input type="password" class="form-control border-dark" id="regPasswordConfirm" name="contrasenaConfirm" placeholder="Confirmar contraseña" required>
|
||||
<label for="regPasswordConfirm">Confirmar contraseña</label>
|
||||
</div>
|
||||
<input type="hidden" name="orden" value="registrar">
|
||||
|
||||
<!-- Checkbox Recuérdame -->
|
||||
<div class="form-check mb-3">
|
||||
<input class="form-check-input border-dark" type="checkbox" value="" id="recuerdameCheck">
|
||||
<label class="form-check-label fw-semibold" for="recuerdameCheck">
|
||||
Recuérdame en 10 días
|
||||
</label>
|
||||
</div>
|
||||
</form>
|
||||
<div id="errorRegistro" class="text-danger fw-bold mt-2"></div>
|
||||
</div>
|
||||
|
||||
@@ -118,8 +118,6 @@ class EquipoxUsuario
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static function salirDeSala($idUsuario)
|
||||
{
|
||||
$conexion = conexionBD();
|
||||
@@ -147,6 +145,3 @@ public static function salirDeSala($idUsuario)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
+158
-164
@@ -7,17 +7,27 @@ class Partida
|
||||
public $idPartida;
|
||||
public $nombrePartida;
|
||||
public $idUsuario;
|
||||
public $fechaCreacion;
|
||||
public $fechaInicio;
|
||||
public $fechaFin;
|
||||
|
||||
public function __construct(
|
||||
$idPartida = null,
|
||||
$nombrePartida = null,
|
||||
$idUsuario = null
|
||||
$idUsuario = null,
|
||||
$fechaCreacion = null,
|
||||
$fechaInicio = null,
|
||||
$fechaFin = null
|
||||
) {
|
||||
$this->idPartida = $idPartida;
|
||||
$this->nombrePartida = $nombrePartida;
|
||||
$this->idUsuario = $idUsuario;
|
||||
$this->fechaCreacion = $fechaCreacion;
|
||||
$this->fechaInicio = $fechaInicio;
|
||||
$this->fechaFin = $fechaFin;
|
||||
}
|
||||
|
||||
|
||||
public function insertar()
|
||||
{
|
||||
$conexion = conexionBD();
|
||||
@@ -25,12 +35,7 @@ class Partida
|
||||
return $conexion;
|
||||
}
|
||||
try {
|
||||
$consulta = $conexion->prepare("
|
||||
INSERT INTO partida
|
||||
(nombrePartida,idUsuario)
|
||||
VALUES
|
||||
(:nombrePartida,:idUsuario)
|
||||
");
|
||||
$consulta = $conexion->prepare("INSERT INTO partida (nombrePartida,idUsuario) VALUES (:nombrePartida,:idUsuario)");
|
||||
$consulta->bindParam(":nombrePartida", $this->nombrePartida);
|
||||
$consulta->bindParam(":idUsuario", $this->idUsuario);
|
||||
$consulta->execute();
|
||||
@@ -46,8 +51,7 @@ class Partida
|
||||
}
|
||||
}
|
||||
|
||||
public function listar()
|
||||
{
|
||||
public function listar(){
|
||||
$conexion = conexionBD();
|
||||
if (is_array($conexion)) {
|
||||
return $conexion;
|
||||
@@ -64,18 +68,13 @@ class Partida
|
||||
}
|
||||
}
|
||||
|
||||
public function buscar()
|
||||
{
|
||||
public function buscar(){
|
||||
$conexion = conexionBD();
|
||||
if (is_array($conexion)) {
|
||||
return $conexion;
|
||||
}
|
||||
try {
|
||||
$consulta = $conexion->prepare("
|
||||
SELECT *
|
||||
FROM partida
|
||||
WHERE idPartida=:idPartida
|
||||
");
|
||||
$consulta = $conexion->prepare("SELECT * FROM partida WHERE idPartida=:idPartida");
|
||||
$consulta->bindParam(":idPartida", $this->idPartida);
|
||||
$consulta->execute();
|
||||
return $consulta->fetch(PDO::FETCH_ASSOC);
|
||||
@@ -87,20 +86,14 @@ class Partida
|
||||
}
|
||||
}
|
||||
|
||||
public function editar()
|
||||
{
|
||||
|
||||
public function editar(){
|
||||
$conexion = conexionBD();
|
||||
if (is_array($conexion)) {
|
||||
return $conexion;
|
||||
}
|
||||
try {
|
||||
$consulta = $conexion->prepare("
|
||||
UPDATE partida
|
||||
SET
|
||||
nombrePartida=:nombrePartida,
|
||||
idUsuario=:idUsuario
|
||||
WHERE idPartida=:idPartida
|
||||
");
|
||||
$consulta = $conexion->prepare("UPDATE partida SET nombrePartida=:nombrePartida, idUsuario=:idUsuario WHERE idPartida=:idPartida");
|
||||
$consulta->bindParam(":idPartida", $this->idPartida);
|
||||
$consulta->bindParam(":nombrePartida", $this->nombrePartida);
|
||||
$consulta->bindParam(":idUsuario", $this->idUsuario);
|
||||
@@ -117,17 +110,13 @@ class Partida
|
||||
}
|
||||
}
|
||||
|
||||
public function eliminar()
|
||||
{
|
||||
public function eliminar(){
|
||||
$conexion = conexionBD();
|
||||
if (is_array($conexion)) {
|
||||
return $conexion;
|
||||
}
|
||||
try {
|
||||
$consulta = $conexion->prepare("
|
||||
DELETE FROM partida
|
||||
WHERE idPartida=:idPartida
|
||||
");
|
||||
$consulta = $conexion->prepare("DELETE FROM partida WHERE idPartida=:idPartida");
|
||||
$consulta->bindParam(":idPartida", $this->idPartida);
|
||||
$consulta->execute();
|
||||
return [
|
||||
@@ -142,27 +131,121 @@ class Partida
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Devuelve las preguntas asignadas a la partida, junto con el
|
||||
* idPartidaxPregunta (necesario para guardar respuestas después).
|
||||
*/
|
||||
public function getPreguntas()
|
||||
{
|
||||
|
||||
public function iniciarPartida(){
|
||||
$conexion = conexionBD();
|
||||
if (is_array($conexion)){
|
||||
return $conexion;
|
||||
}
|
||||
try {
|
||||
$consulta = $conexion->prepare("
|
||||
SELECT
|
||||
pxp.idPartidaxPregunta,
|
||||
p.*
|
||||
FROM partidaxpregunta pxp
|
||||
INNER JOIN pregunta p
|
||||
ON pxp.idPregunta=p.idPregunta
|
||||
WHERE pxp.idPartida=:idPartida
|
||||
ORDER BY pxp.idPartidaxPregunta
|
||||
");
|
||||
$consulta = $conexion->prepare("UPDATE partida SET fechaInicio=NOW() WHERE idPartida=:idPartida AND fechaInicio IS NULL");
|
||||
$consulta->bindParam(":idPartida", $this->idPartida);
|
||||
$consulta->execute();
|
||||
return [
|
||||
"exito" => "La partida ha comenzado.",
|
||||
"exito_tecnico" => $this->idPartida
|
||||
];
|
||||
} catch (PDOException $error) {
|
||||
return [
|
||||
"error" => "Ocurrió un error al iniciar la partida.",
|
||||
'error_tecnico' => $error->getMessage()
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function finalizar(){
|
||||
$conexion = conexionBD();
|
||||
if (is_array($conexion)) {
|
||||
return $conexion;
|
||||
}
|
||||
try {
|
||||
$consulta = $conexion->prepare("UPDATE partida SET fechaFin=NOW() WHERE idPartida=:idPartida AND fechaFin IS NULL");
|
||||
$consulta->bindParam(":idPartida", $this->idPartida);
|
||||
$consulta->execute();
|
||||
return [
|
||||
"exito" => "La partida se finalizó correctamente.",
|
||||
"exito_tecnico" => $this->idPartida
|
||||
];
|
||||
} catch (PDOException $error) {
|
||||
return [
|
||||
"error" => "Ocurrió un error al finalizar la partida.",
|
||||
'error_tecnico' => $error->getMessage()
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//true si la partida ya tiene fecha de fin (fechaFin no es NULL)
|
||||
//Devuelve false si la partida no existe
|
||||
|
||||
public function estaFinalizada() {
|
||||
$datos = $this->buscar();
|
||||
if (is_array($datos) && isset($datos["error"])) {
|
||||
return $datos;
|
||||
}
|
||||
if (!$datos) {
|
||||
return false;
|
||||
}
|
||||
return $datos["fechaFin"] !== null;
|
||||
}
|
||||
|
||||
|
||||
//Devuelve la partida más reciente del usuario que aún no ha
|
||||
//terminado (fechaFin NULL), o false si no tiene ninguna.
|
||||
//Incluye también las salas que todavía no han empezado.
|
||||
|
||||
public function partidaEnCursoDeUsuario(){
|
||||
$conexion = conexionBD();
|
||||
if (is_array($conexion)) {
|
||||
return $conexion;
|
||||
}
|
||||
try {
|
||||
$consulta = $conexion->prepare("SELECT * FROM partida WHERE idUsuario=:idUsuario AND fechaFin IS NULL ORDER BY idPartida DESC LIMIT 1");
|
||||
$consulta->bindParam(":idUsuario", $this->idUsuario);
|
||||
$consulta->execute();
|
||||
return $consulta->fetch(PDO::FETCH_ASSOC);
|
||||
} catch (PDOException $error) {
|
||||
return [
|
||||
"error" => "Ocurrió un error al buscar la partida en curso.",
|
||||
'error_tecnico' => $error->getMessage()
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function listarSalasAbiertas() {
|
||||
$conexion = conexionBD();
|
||||
if (is_array($conexion)) {
|
||||
return $conexion;
|
||||
}
|
||||
try {
|
||||
$consulta = $conexion->prepare("SELECT p.idPartida, p.nombrePartida, e.idEquipo, e.nombreEquipo, u.idUsuario, u.nombreUsuario
|
||||
FROM partida p INNER JOIN equipoxpartida exp ON exp.idPartida=p.idPartida INNER JOIN equipo e ON e.idEquipo=exp.idEquipo
|
||||
LEFT JOIN equipoxusuario exu ON exu.idEquipo=e.idEquipo LEFT JOIN usuario u ON u.idUsuario=exu.idUsuario WHERE p.fechaInicio IS NULL
|
||||
AND p.fechaFin IS NULL ORDER BY p.idPartida DESC, e.idEquipo ASC");
|
||||
$consulta->execute();
|
||||
return $consulta->fetchAll(PDO::FETCH_ASSOC);
|
||||
} catch (PDOException $error) {
|
||||
return [
|
||||
"error" => "Ocurrió un error al recuperar las salas abiertas.",
|
||||
'error_tecnico' => $error->getMessage()
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Devuelve las preguntas asignadas a la partida, junto con el
|
||||
* idPartidaxPregunta (necesario para guardar respuestas después).
|
||||
*/
|
||||
public function getPreguntas(){
|
||||
$conexion = conexionBD();
|
||||
if (is_array($conexion)) {
|
||||
return $conexion;
|
||||
}
|
||||
try {
|
||||
$consulta = $conexion->prepare("SELECT pxp.idPartidaxPregunta, p.* FROM partidaxpregunta pxp INNER JOIN pregunta p ON pxp.idPregunta=p.idPregunta
|
||||
WHERE pxp.idPartida=:idPartida ORDER BY pxp.idPartidaxPregunta");
|
||||
$consulta->bindParam(":idPartida", $this->idPartida);
|
||||
$consulta->execute();
|
||||
return $consulta->fetchAll(PDO::FETCH_ASSOC);
|
||||
@@ -174,22 +257,14 @@ class Partida
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Asocia una pregunta ya existente a esta partida.
|
||||
*/
|
||||
public function asignarPregunta($idPregunta)
|
||||
{
|
||||
|
||||
public function asignarPregunta($idPregunta) {
|
||||
$conexion = conexionBD();
|
||||
if (is_array($conexion)) {
|
||||
return $conexion;
|
||||
}
|
||||
try {
|
||||
$consulta = $conexion->prepare("
|
||||
INSERT INTO partidaxpregunta
|
||||
(idPartida,idPregunta)
|
||||
VALUES
|
||||
(:idPartida,:idPregunta)
|
||||
");
|
||||
$consulta = $conexion->prepare("INSERT INTO partidaxpregunta (idPartida,idPregunta) VALUES (:idPartida,:idPregunta)");
|
||||
$consulta->bindParam(":idPartida", $this->idPartida);
|
||||
$consulta->bindParam(":idPregunta", $idPregunta);
|
||||
$consulta->execute();
|
||||
@@ -205,18 +280,13 @@ class Partida
|
||||
}
|
||||
}
|
||||
//Cuenta cuántas preguntas tiene la partida en total. Es la primera mitad de la comprobación "¿ha terminado el usuario de jugar?".
|
||||
public function contarPreguntasAsignadas()
|
||||
{
|
||||
public function contarPreguntasAsignadas() {
|
||||
$conexion = conexionBD();
|
||||
if (is_array($conexion)) {
|
||||
return $conexion;
|
||||
}
|
||||
try {
|
||||
$consulta = $conexion->prepare("
|
||||
SELECT COUNT(*) AS total
|
||||
FROM partidaxpregunta
|
||||
WHERE idPartida=:idPartida
|
||||
");
|
||||
$consulta = $conexion->prepare("SELECT COUNT(*) AS total FROM partidaxpregunta WHERE idPartida=:idPartida");
|
||||
$consulta->bindParam(":idPartida", $this->idPartida);
|
||||
$consulta->execute();
|
||||
$fila = $consulta->fetch(PDO::FETCH_ASSOC);
|
||||
@@ -229,21 +299,14 @@ class Partida
|
||||
}
|
||||
}
|
||||
|
||||
public function contarRespuestasUsuario($idUsuario)
|
||||
{
|
||||
public function contarRespuestasUsuario($idUsuario){
|
||||
$conexion = conexionBD();
|
||||
if (is_array($conexion)) {
|
||||
return $conexion;
|
||||
}
|
||||
try {
|
||||
$consulta = $conexion->prepare("
|
||||
SELECT COUNT(*) AS total
|
||||
FROM respuestaalmacenada ra
|
||||
INNER JOIN partidaxpregunta pxp
|
||||
ON ra.idPartidaxPregunta=pxp.idPartidaxPregunta
|
||||
WHERE pxp.idPartida=:idPartida
|
||||
AND ra.idUsuario=:idUsuario
|
||||
");
|
||||
$consulta = $conexion->prepare("SELECT COUNT(*) AS total FROM respuestaalmacenada ra INNER JOIN partidaxpregunta pxp
|
||||
ON ra.idPartidaxPregunta=pxp.idPartidaxPregunta WHERE pxp.idPartida=:idPartida AND ra.idUsuario=:idUsuario");
|
||||
$consulta->bindParam(":idPartida", $this->idPartida);
|
||||
$consulta->bindParam(":idUsuario", $idUsuario);
|
||||
$consulta->execute();
|
||||
@@ -258,11 +321,11 @@ class Partida
|
||||
}
|
||||
|
||||
/**
|
||||
* Sustituye al antiguo campo `terminada`: compara cuántas preguntas
|
||||
* tiene la partida contra cuántas ha respondido este usuario.
|
||||
* Indica si ESTE usuario ya ha respondido todas las preguntas:
|
||||
* compara cuántas preguntas tiene la partida contra cuántas ha
|
||||
* respondido él. (La partida completa se marca con finalizar().)
|
||||
*/
|
||||
public function estaFinalizadaParaUsuario($idUsuario)
|
||||
{
|
||||
public function estaFinalizadaParaUsuario($idUsuario){
|
||||
$totalPreguntas = $this->contarPreguntasAsignadas();
|
||||
if (is_array($totalPreguntas)) {
|
||||
return $totalPreguntas;
|
||||
@@ -277,19 +340,13 @@ class Partida
|
||||
/**
|
||||
* Partidas creadas por un usuario, de más reciente a más antigua.
|
||||
*/
|
||||
public function listarDelUsuario()
|
||||
{
|
||||
public function listarDelUsuario(){
|
||||
$conexion = conexionBD();
|
||||
if (is_array($conexion)) {
|
||||
return $conexion;
|
||||
}
|
||||
try {
|
||||
$consulta = $conexion->prepare("
|
||||
SELECT *
|
||||
FROM partida
|
||||
WHERE idUsuario=:idUsuario
|
||||
ORDER BY idPartida DESC
|
||||
");
|
||||
$consulta = $conexion->prepare("SELECT * FROM partida WHERE idUsuario=:idUsuario ORDER BY idPartida DESC");
|
||||
$consulta->bindParam(":idUsuario", $this->idUsuario);
|
||||
$consulta->execute();
|
||||
return $consulta->fetchAll(PDO::FETCH_ASSOC);
|
||||
@@ -301,22 +358,13 @@ class Partida
|
||||
}
|
||||
}
|
||||
|
||||
public function preguntaAsignada($idPregunta)
|
||||
{
|
||||
public function preguntaAsignada($idPregunta) {
|
||||
$conexion = conexionBD();
|
||||
|
||||
if (is_array($conexion)) {
|
||||
return $conexion;
|
||||
}
|
||||
|
||||
try {
|
||||
$consulta = $conexion->prepare("
|
||||
SELECT COUNT(*) AS total
|
||||
FROM partidaxpregunta
|
||||
WHERE idPartida=:idPartida
|
||||
AND idPregunta=:idPregunta
|
||||
");
|
||||
|
||||
$consulta = $conexion->prepare("SELECT COUNT(*) AS total FROM partidaxpregunta WHERE idPartida=:idPartida AND idPregunta=:idPregunta");
|
||||
$consulta->bindParam(":idPartida", $this->idPartida);
|
||||
$consulta->bindParam(":idPregunta", $idPregunta);
|
||||
$consulta->execute();
|
||||
@@ -336,24 +384,13 @@ class Partida
|
||||
* Vincula un equipo a esta partida en la tabla intermedia 'equipoxpartida'.
|
||||
* Permite registrar qué grupos están participando en este torneo.
|
||||
*/
|
||||
public function registrarEquipoEnPartida($idEquipo)
|
||||
{
|
||||
$conexion = conexionBD(); // Obtenemos la conexión modular del proyecto
|
||||
|
||||
public function registrarEquipoEnPartida($idEquipo){
|
||||
$conexion = conexionBD();
|
||||
if (is_array($conexion)) {
|
||||
return $conexion; // Si hay error en la conexión, lo devuelve
|
||||
return $conexion;
|
||||
}
|
||||
|
||||
try {
|
||||
// Preparamos la consulta para insertar la relación en la tabla intermedia real
|
||||
$consulta = $conexion->prepare("
|
||||
INSERT INTO equipoxpartida
|
||||
(idPartida, idEquipo)
|
||||
VALUES
|
||||
(:idPartida, :idEquipo)
|
||||
");
|
||||
|
||||
// Vinculamos los parámetros con el objeto de la partida actual y el equipo recibido
|
||||
$consulta = $conexion->prepare("INSERT INTO equipoxpartida (idPartida, idEquipo) VALUES (:idPartida, :idEquipo)");
|
||||
$consulta->bindParam(":idPartida", $this->idPartida);
|
||||
$consulta->bindParam(":idEquipo", $idEquipo);
|
||||
$consulta->execute();
|
||||
@@ -363,7 +400,6 @@ class Partida
|
||||
"exito_tecnico" => $conexion->lastInsertId()
|
||||
];
|
||||
} catch (PDOException $error) {
|
||||
// Capturamos el error extrayendo solo el mensaje de texto limpio
|
||||
return [
|
||||
"error" => "Ocurrió un error al asignar el equipo a la partida.",
|
||||
"error_tecnico" => $error->getMessage()
|
||||
@@ -375,28 +411,14 @@ class Partida
|
||||
* Obtiene todos los equipos que están compitiendo en esta partida.
|
||||
* Es útil para mostrar la lista de rivales antes de empezar a responder.
|
||||
*/
|
||||
public function getEquiposParticipantes()
|
||||
{
|
||||
public function getEquiposParticipantes(){
|
||||
$conexion = conexionBD();
|
||||
|
||||
if (is_array($conexion)) {
|
||||
return $conexion;
|
||||
}
|
||||
|
||||
try {
|
||||
// Hacemos un INNER JOIN para traer los nombres de los equipos desde la tabla intermedia
|
||||
$consulta = $conexion->prepare("
|
||||
SELECT
|
||||
exp.idEquipoxPartida,
|
||||
e.idEquipo,
|
||||
e.nombreEquipo
|
||||
FROM equipoxpartida exp
|
||||
INNER JOIN equipo e
|
||||
ON exp.idEquipo = e.idEquipo
|
||||
WHERE exp.idPartida = :idPartida
|
||||
ORDER BY e.nombreEquipo ASC
|
||||
");
|
||||
|
||||
$consulta = $conexion->prepare("SELECT exp.idEquipoxPartida, e.idEquipo, e.nombreEquipo FROM equipoxpartida exp
|
||||
INNER JOIN equipo e ON exp.idEquipo = e.idEquipo WHERE exp.idPartida = :idPartida ORDER BY e.nombreEquipo ASC");
|
||||
$consulta->bindParam(":idPartida", $this->idPartida);
|
||||
$consulta->execute();
|
||||
|
||||
@@ -413,54 +435,26 @@ class Partida
|
||||
* Obtiene todos los jugadores reales (Usuarios) que están jugando en esta partida.
|
||||
* Utiliza la tabla 'equipoxusuario' en SQL para cruzar los datos.
|
||||
*/
|
||||
public function getJugadoresDeLaPartida()
|
||||
{
|
||||
public function getJugadoresDeLaPartida(){
|
||||
$conexion = conexionBD();
|
||||
|
||||
if (is_array($conexion)) {
|
||||
return $conexion;
|
||||
}
|
||||
|
||||
try {
|
||||
|
||||
$consulta = $conexion->prepare("
|
||||
SELECT DISTINCT
|
||||
u.idUsuario,
|
||||
u.nombreUsuario,
|
||||
u.email,
|
||||
e.idEquipo,
|
||||
e.nombreEquipo
|
||||
FROM equipoxpartida exp
|
||||
|
||||
INNER JOIN equipoxusuario exu
|
||||
ON exp.idEquipo = exu.idEquipo
|
||||
|
||||
INNER JOIN usuario u
|
||||
ON exu.idUsuario = u.idUsuario
|
||||
|
||||
INNER JOIN equipo e
|
||||
ON exu.idEquipo = e.idEquipo
|
||||
|
||||
WHERE exp.idPartida = :idPartida
|
||||
|
||||
ORDER BY e.nombreEquipo ASC,
|
||||
u.nombreUsuario ASC
|
||||
");
|
||||
|
||||
$consulta = $conexion->prepare("SELECT DISTINCT u.idUsuario, u.nombreUsuario, u.email, e.idEquipo, e.nombreEquipo
|
||||
FROM equipoxpartida exp INNER JOIN equipoxusuario exu ON exp.idEquipo = exu.idEquipo INNER JOIN usuario u
|
||||
ON exu.idUsuario = u.idUsuario INNER JOIN equipo e ON exu.idEquipo = e.idEquipo WHERE exp.idPartida = :idPartida
|
||||
ORDER BY e.nombreEquipo ASC, u.nombreUsuario ASC");
|
||||
$consulta->bindParam(
|
||||
":idPartida",
|
||||
$this->idPartida
|
||||
);
|
||||
|
||||
$consulta->execute();
|
||||
|
||||
return $consulta->fetchAll(PDO::FETCH_ASSOC);
|
||||
} catch (PDOException $error) {
|
||||
|
||||
return [
|
||||
"error" =>
|
||||
"Ocurrió un error al obtener la lista de jugadores.",
|
||||
|
||||
"error_tecnico" =>
|
||||
$error->getMessage()
|
||||
];
|
||||
|
||||
@@ -75,6 +75,7 @@ switch ($_POST['orden']) {
|
||||
|
||||
break;
|
||||
|
||||
|
||||
case 'salirDeSala':
|
||||
if (!isset($_SESSION['idUsuario'])) {
|
||||
echo json_encode(['error' => 'Usuario no autenticado']);
|
||||
@@ -86,5 +87,4 @@ switch ($_POST['orden']) {
|
||||
|
||||
echo json_encode($respuesta);
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 19 KiB |
@@ -1,7 +1,3 @@
|
||||
<?php
|
||||
include 'cabecera.php';
|
||||
?>
|
||||
|
||||
<!DOCTYPE html>
|
||||
<html lang="es-ES">
|
||||
|
||||
@@ -9,16 +5,17 @@ include 'cabecera.php';
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Triviagmático</title>
|
||||
<script src="js/jquery-4.0.0.min.js"></script>
|
||||
<link rel="icon" type="image/svg+xml" href="img/favicon/favicon.svg">
|
||||
</head>
|
||||
|
||||
<body>
|
||||
|
||||
<?php include 'cabecera.php'; ?>
|
||||
|
||||
<div class="container my-5">
|
||||
<div class="row justify-content-center align-items-center g-4">
|
||||
|
||||
<!-- Botón Jugar (oculto por defecto, JS lo muestra si hay sesión) -->
|
||||
<!-- Botón Jugar (oculto por defecto, JS lo muestra si hay sesión activa) -->
|
||||
<div class="col-12 col-lg-5 col-xl-4 d-none" id="colBotonJugar">
|
||||
<a href="buscadorSala.php" class="btn btn-play-kahoot" id="btnJugar">
|
||||
<i class="fa-solid fa-play"></i>
|
||||
|
||||
+113
-221
@@ -4,6 +4,7 @@ $(document).ready(function () {
|
||||
comprobarSalaUsuario();
|
||||
});
|
||||
|
||||
// 1. Comprobar si el usuario actual ya pertenece a una sala
|
||||
function comprobarSalaUsuario() {
|
||||
$("#cargandoSalas").remove();
|
||||
|
||||
@@ -11,101 +12,64 @@ function comprobarSalaUsuario() {
|
||||
url: "controladores/ControladorEquipoxUsuario.php",
|
||||
type: "POST",
|
||||
dataType: "json",
|
||||
|
||||
data: {
|
||||
orden: "obtenerSalaUsuario",
|
||||
},
|
||||
|
||||
data: { orden: "obtenerSalaUsuario" },
|
||||
success: function (respuesta) {
|
||||
console.log("Sala del usuario:", respuesta);
|
||||
|
||||
if (respuesta.error) {
|
||||
console.error(respuesta.error);
|
||||
|
||||
if (respuesta.error || !respuesta.asociado) {
|
||||
usuarioYaEstaEnEquipo = false;
|
||||
|
||||
$("#contenedorCrearSala").show();
|
||||
cargarSalas();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (respuesta.asociado) {
|
||||
usuarioYaEstaEnEquipo = true;
|
||||
|
||||
$("#contenedorCrearSala").hide();
|
||||
|
||||
cargarSalaUsuario(respuesta.idPartida);
|
||||
} else {
|
||||
usuarioYaEstaEnEquipo = false;
|
||||
|
||||
cargarSalas();
|
||||
usuarioYaEstaEnEquipo = true;
|
||||
$("#contenedorCrearSala").hide(); // Ocultar "Crear Sala" si ya pertenece a una
|
||||
cargarSalaUsuario(respuesta.idPartida);
|
||||
}
|
||||
},
|
||||
|
||||
error: function (xhr, status, error) {
|
||||
console.error("Error al comprobar la sala del usuario:", error);
|
||||
|
||||
usuarioYaEstaEnEquipo = false;
|
||||
|
||||
cargarSalas();
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// 2. Cargar únicamente la sala en la que está metido el usuario
|
||||
function cargarSalaUsuario(idPartida) {
|
||||
$.ajax({
|
||||
url: "controladores/controladorPartida.php",
|
||||
type: "POST",
|
||||
dataType: "json",
|
||||
|
||||
data: {
|
||||
orden: "buscar",
|
||||
idPartida: idPartida,
|
||||
},
|
||||
|
||||
data: { orden: "buscar", idPartida: idPartida },
|
||||
success: function (partida) {
|
||||
if (partida.error) {
|
||||
console.error(partida.error);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$("#contenedorSalas").empty();
|
||||
|
||||
cargarInformacionSala(partida);
|
||||
},
|
||||
|
||||
error: function (xhr, status, error) {
|
||||
console.error("Error al obtener la sala del usuario:", error);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// 3. Cargar la lista completa de todas las salas
|
||||
function cargarSalas() {
|
||||
$.ajax({
|
||||
url: "controladores/controladorPartida.php",
|
||||
type: "POST",
|
||||
dataType: "json",
|
||||
|
||||
data: {
|
||||
orden: "listar",
|
||||
},
|
||||
|
||||
data: { orden: "listar" },
|
||||
success: function (partidas) {
|
||||
const contenedor = $("#contenedorSalas");
|
||||
|
||||
contenedor.empty();
|
||||
contenedor.empty();
|
||||
|
||||
if (!Array.isArray(partidas) || partidas.length === 0) {
|
||||
contenedor.html(`
|
||||
<div class="col-12 text-center">
|
||||
<p class="text-muted">
|
||||
No hay salas disponibles.
|
||||
</p>
|
||||
<p class="text-muted">No hay salas disponibles actualmente.</p>
|
||||
</div>
|
||||
`);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -113,54 +77,42 @@ function cargarSalas() {
|
||||
cargarInformacionSala(partida);
|
||||
});
|
||||
},
|
||||
|
||||
error: function (xhr, status, error) {
|
||||
console.error("Error al obtener las partidas:", error);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// 4. Obtener equipos y jugadores asociados a una sala concreta
|
||||
function cargarInformacionSala(partida) {
|
||||
$.ajax({
|
||||
url: "controladores/controladorPartida.php",
|
||||
type: "POST",
|
||||
dataType: "json",
|
||||
|
||||
data: {
|
||||
orden: "getEquipos",
|
||||
idPartida: partida.idPartida,
|
||||
},
|
||||
|
||||
data: { orden: "getEquipos", idPartida: partida.idPartida },
|
||||
success: function (equipos) {
|
||||
$.ajax({
|
||||
url: "controladores/controladorPartida.php",
|
||||
type: "POST",
|
||||
dataType: "json",
|
||||
|
||||
data: {
|
||||
orden: "getJugadores",
|
||||
idPartida: partida.idPartida,
|
||||
},
|
||||
|
||||
data: { orden: "getJugadores", idPartida: partida.idPartida },
|
||||
success: function (jugadores) {
|
||||
mostrarSala(partida, equipos, jugadores);
|
||||
},
|
||||
|
||||
error: function (xhr, status, error) {
|
||||
console.error("Error al obtener jugadores:", error);
|
||||
},
|
||||
});
|
||||
},
|
||||
|
||||
error: function (xhr, status, error) {
|
||||
console.error("Error al obtener equipos:", error);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// 5. Renderizar el HTML de la sala y botones según estado
|
||||
function mostrarSala(partida, equipos, jugadores) {
|
||||
const contenedor = $("#contenedorSalas");
|
||||
|
||||
let equiposHTML = "";
|
||||
|
||||
equipos.forEach(function (equipo) {
|
||||
@@ -169,229 +121,194 @@ function mostrarSala(partida, equipos, jugadores) {
|
||||
});
|
||||
|
||||
let jugadoresHTML = "";
|
||||
|
||||
if (jugadoresEquipo.length > 0) {
|
||||
jugadoresEquipo.forEach(function (jugador) {
|
||||
jugadoresHTML += `
|
||||
<div class="jugador">
|
||||
|
||||
<span class="avatar">
|
||||
👤
|
||||
</span>
|
||||
|
||||
<span class="avatar">👤</span>
|
||||
${escapeHTML(jugador.nombreUsuario)}
|
||||
|
||||
</div>
|
||||
`;
|
||||
});
|
||||
} else {
|
||||
jugadoresHTML = `
|
||||
<div class="text-muted small">
|
||||
Sin jugadores
|
||||
</div>
|
||||
`;
|
||||
jugadoresHTML = `<div class="text-muted small">Sin jugadores</div>`;
|
||||
}
|
||||
|
||||
// Botón "Unirse" solo visible si NO pertenece a ningún equipo
|
||||
let botonUnirseHTML = "";
|
||||
|
||||
if (!usuarioYaEstaEnEquipo) {
|
||||
botonUnirseHTML = `
|
||||
<button
|
||||
type="button"
|
||||
class="btn btn-unirse w-100 btn-unirse-equipo"
|
||||
<button type="button" class="btn btn-unirse w-100 btn-unirse-equipo mt-2"
|
||||
data-id-partida="${partida.idPartida}"
|
||||
data-id-equipo="${equipo.idEquipo}">
|
||||
|
||||
Unirse
|
||||
|
||||
</button>
|
||||
`;
|
||||
}
|
||||
|
||||
equiposHTML += `
|
||||
<div class="equipo">
|
||||
|
||||
<h6>
|
||||
${escapeHTML(equipo.nombreEquipo)}
|
||||
</h6>
|
||||
|
||||
<div class="jugadores">
|
||||
|
||||
<div class="equipo me-2 ms-2 p-3 border rounded">
|
||||
<h6>${escapeHTML(equipo.nombreEquipo)}</h6>
|
||||
<div class="jugadores mb-2">
|
||||
${jugadoresHTML}
|
||||
|
||||
</div>
|
||||
|
||||
${botonUnirseHTML}
|
||||
|
||||
</div>
|
||||
`;
|
||||
});
|
||||
|
||||
// Acciones disponibles si el usuario YA ESTÁ dentro de esta sala
|
||||
let accionesSalaHTML = "";
|
||||
if (usuarioYaEstaEnEquipo) {
|
||||
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>
|
||||
<a href="juego.php?idPartida=${partida.idPartida}" class="btn btn-success btn-lg px-4">
|
||||
🎮 ¡Iniciar Partida!
|
||||
</a>
|
||||
</div>
|
||||
`;
|
||||
}
|
||||
|
||||
const salaHTML = `
|
||||
<div class="col-12 col-md-10 col-lg-8">
|
||||
|
||||
<div class="sala">
|
||||
|
||||
<div class="sala-header">
|
||||
|
||||
<div class="sala card p-3 shadow-sm mb-4">
|
||||
<div class="sala-header h4 text-center mb-3">
|
||||
${escapeHTML(partida.nombrePartida)}
|
||||
|
||||
</div>
|
||||
|
||||
<div class="sala-body">
|
||||
|
||||
<div class="equipos">
|
||||
|
||||
<div class="equipos d-flex justify-content-around">
|
||||
${equiposHTML}
|
||||
|
||||
</div>
|
||||
${accionesSalaHTML}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
</div>
|
||||
`;
|
||||
|
||||
contenedor.append(salaHTML);
|
||||
}
|
||||
|
||||
// ======================================================
|
||||
// UNIRSE A UN EQUIPO
|
||||
// ======================================================
|
||||
|
||||
// 6. Evento: Unirse a un Equipo
|
||||
$(document).on("click", ".btn-unirse-equipo", function () {
|
||||
const boton = $(this);
|
||||
|
||||
const idEquipo = boton.data("id-equipo");
|
||||
|
||||
const idPartida = boton.data("id-partida");
|
||||
|
||||
boton.prop("disabled", true);
|
||||
|
||||
boton.text("Uniéndose...");
|
||||
boton.prop("disabled", true).text("Uniéndose...");
|
||||
|
||||
$.ajax({
|
||||
url: "controladores/ControladorEquipoxUsuario.php",
|
||||
|
||||
type: "POST",
|
||||
|
||||
dataType: "json",
|
||||
|
||||
data: {
|
||||
orden: "registrarUsuarioEnEquipo",
|
||||
idEquipo: idEquipo,
|
||||
},
|
||||
|
||||
success: function (respuesta) {
|
||||
console.log("Respuesta al unirse:", respuesta);
|
||||
|
||||
if (respuesta.error) {
|
||||
alert(respuesta.error);
|
||||
|
||||
boton.prop("disabled", false);
|
||||
|
||||
boton.text("Unirse");
|
||||
|
||||
boton.prop("disabled", false).text("Unirse");
|
||||
return;
|
||||
}
|
||||
|
||||
// ==========================================
|
||||
// UNIÓN CORRECTA
|
||||
// ==========================================
|
||||
|
||||
usuarioYaEstaEnEquipo = true;
|
||||
|
||||
// Ocultamos el botón "Crear sala"
|
||||
|
||||
$("#contenedorCrearSala").hide();
|
||||
|
||||
// Eliminamos las salas actuales
|
||||
|
||||
$("#contenedorSalas").empty();
|
||||
|
||||
// Cargamos directamente la sala
|
||||
// donde acaba de entrar el usuario
|
||||
|
||||
cargarSalaUsuario(idPartida);
|
||||
},
|
||||
|
||||
error: function (xhr, status, error) {
|
||||
console.error("Error al unirse al equipo:", error);
|
||||
|
||||
console.error("Respuesta del servidor:", xhr.responseText);
|
||||
|
||||
alert("No se ha podido unir al equipo.");
|
||||
|
||||
boton.prop("disabled", false);
|
||||
|
||||
boton.text("Unirse");
|
||||
boton.prop("disabled", false).text("Unirse");
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
// ======================================================
|
||||
// ESCAPAR HTML
|
||||
// ======================================================
|
||||
// 7. Evento: Abandonar Sala
|
||||
// Guardamos temporalmente el botón presionado
|
||||
let botonAbandonarReferencia = null;
|
||||
|
||||
function escapeHTML(texto) {
|
||||
return $("<div>")
|
||||
.text(texto ?? "")
|
||||
.html();
|
||||
// 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 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');
|
||||
const modalInstance = bootstrap.Modal.getInstance(modalEl);
|
||||
|
||||
const btnConfirmar = $(this);
|
||||
btnConfirmar.prop("disabled", true).text("Saliendo...");
|
||||
|
||||
$.ajax({
|
||||
url: "controladores/ControladorEquipoxUsuario.php",
|
||||
type: "POST",
|
||||
dataType: "json",
|
||||
data: { orden: "salirDeSala" },
|
||||
success: function (respuesta) {
|
||||
btnConfirmar.prop("disabled", false).text("Sí, salir");
|
||||
|
||||
if (modalInstance) {
|
||||
modalInstance.hide();
|
||||
}
|
||||
|
||||
// ======================================================
|
||||
// CREAR SALA
|
||||
// ======================================================
|
||||
if (respuesta.error) {
|
||||
alert(respuesta.error);
|
||||
return;
|
||||
}
|
||||
|
||||
// Restablecemos la interfaz a la lista completa de salas
|
||||
usuarioYaEstaEnEquipo = false;
|
||||
$("#contenedorCrearSala").show();
|
||||
cargarSalas();
|
||||
},
|
||||
error: function (xhr, status, error) {
|
||||
console.error("Error al salir de la sala:", error);
|
||||
alert("No se pudo salir de la sala.");
|
||||
btnConfirmar.prop("disabled", false).text("Sí, salir");
|
||||
if (modalInstance) {
|
||||
modalInstance.hide();
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
// 8. Evento: Crear Sala
|
||||
$("#btnCrearSala").on("click", function () {
|
||||
const nombreSala = $("#nombreSala").val().trim();
|
||||
|
||||
const nombreEquipo1 = $("#nombreEquipo1").val().trim();
|
||||
|
||||
const nombreEquipo2 = $("#nombreEquipo2").val().trim();
|
||||
|
||||
let valido = true;
|
||||
|
||||
$(".mensaje-error-sala").remove();
|
||||
|
||||
$(".input-kahoot").removeClass("is-invalid");
|
||||
|
||||
if (nombreSala === "") {
|
||||
mostrarError("#nombreSala", "El nombre de la sala es obligatorio.");
|
||||
|
||||
valido = false;
|
||||
} else if (nombreSala.length < 3) {
|
||||
if (nombreSala === "" || nombreSala.length < 3) {
|
||||
mostrarError(
|
||||
"#nombreSala",
|
||||
"El nombre de la sala debe tener al menos 3 caracteres.",
|
||||
);
|
||||
|
||||
valido = false;
|
||||
}
|
||||
|
||||
if (nombreEquipo1 === "") {
|
||||
mostrarError("#nombreEquipo1", "El nombre del equipo 1 es obligatorio.");
|
||||
|
||||
valido = false;
|
||||
} else if (nombreEquipo1.length < 2) {
|
||||
if (nombreEquipo1 === "" || nombreEquipo1.length < 2) {
|
||||
mostrarError(
|
||||
"#nombreEquipo1",
|
||||
"El nombre del equipo debe tener al menos 2 caracteres.",
|
||||
);
|
||||
|
||||
valido = false;
|
||||
}
|
||||
|
||||
if (nombreEquipo2 === "") {
|
||||
mostrarError("#nombreEquipo2", "El nombre del equipo 2 es obligatorio.");
|
||||
|
||||
valido = false;
|
||||
} else if (nombreEquipo2.length < 2) {
|
||||
if (nombreEquipo2 === "" || nombreEquipo2.length < 2) {
|
||||
mostrarError(
|
||||
"#nombreEquipo2",
|
||||
"El nombre del equipo debe tener al menos 2 caracteres.",
|
||||
);
|
||||
|
||||
valido = false;
|
||||
}
|
||||
|
||||
@@ -404,22 +321,16 @@ $("#btnCrearSala").on("click", function () {
|
||||
"#nombreEquipo2",
|
||||
"Los dos equipos deben tener nombres diferentes.",
|
||||
);
|
||||
|
||||
valido = false;
|
||||
}
|
||||
|
||||
if (!valido) {
|
||||
return;
|
||||
}
|
||||
if (!valido) return;
|
||||
|
||||
// Deshabilitamos el botón mientras se crea la sala
|
||||
const boton = $("#btnCrearSala");
|
||||
|
||||
boton.prop("disabled", true);
|
||||
boton.text("Creando sala...");
|
||||
boton.prop("disabled", true).text("Creando sala...");
|
||||
|
||||
$.ajax({
|
||||
url: "controladores/controladorPartida.php", // <-- cambia esto por la ruta de tu controlador
|
||||
url: "controladores/controladorPartida.php",
|
||||
type: "POST",
|
||||
dataType: "json",
|
||||
data: {
|
||||
@@ -428,16 +339,10 @@ $("#btnCrearSala").on("click", function () {
|
||||
nombreEquipo1: nombreEquipo1,
|
||||
nombreEquipo2: nombreEquipo2,
|
||||
},
|
||||
|
||||
success: function (respuesta) {
|
||||
console.log("Respuesta del servidor:", respuesta);
|
||||
|
||||
if (respuesta.error) {
|
||||
alert(respuesta.error);
|
||||
|
||||
boton.prop("disabled", false);
|
||||
boton.text("Crear sala");
|
||||
|
||||
boton.prop("disabled", false).text("Crear sala");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -446,43 +351,30 @@ $("#btnCrearSala").on("click", function () {
|
||||
location.reload();
|
||||
}
|
||||
},
|
||||
|
||||
error: function (xhr, status, error) {
|
||||
console.error("Error al crear la sala:", error);
|
||||
console.error("Respuesta:", xhr.responseText);
|
||||
|
||||
alert("No se ha podido crear la sala.");
|
||||
|
||||
boton.prop("disabled", false);
|
||||
boton.text("Crear sala");
|
||||
boton.prop("disabled", false).text("Crear sala");
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
// ======================================================
|
||||
// MOSTRAR ERROR
|
||||
// ======================================================
|
||||
// Auxiliares
|
||||
function escapeHTML(texto) {
|
||||
return $("<div>")
|
||||
.text(texto ?? "")
|
||||
.html();
|
||||
}
|
||||
|
||||
function mostrarError(selector, mensaje) {
|
||||
const input = $(selector);
|
||||
|
||||
input.addClass("is-invalid");
|
||||
|
||||
input.after(`
|
||||
<div class="mensaje-error-sala text-danger mt-1 small">
|
||||
|
||||
${mensaje}
|
||||
|
||||
</div>
|
||||
`);
|
||||
input.after(
|
||||
`<div class="mensaje-error-sala text-danger mt-1 small">${mensaje}</div>`,
|
||||
);
|
||||
}
|
||||
|
||||
// ======================================================
|
||||
// QUITAR ERROR AL ESCRIBIR
|
||||
// ======================================================
|
||||
|
||||
$(".input-kahoot").on("input", function () {
|
||||
$(document).on("input", ".input-kahoot", function () {
|
||||
$(this).removeClass("is-invalid");
|
||||
|
||||
$(this).next(".mensaje-error-sala").remove();
|
||||
});
|
||||
|
||||
+189
-272
@@ -1,299 +1,216 @@
|
||||
// Función de escape colocada arriba para evitar el ReferenceError
|
||||
function escapeHTML(str) {
|
||||
return $("<div>").text(str ?? "").html();
|
||||
}
|
||||
|
||||
// Comprobar estado de sesión al cargar la página
|
||||
$(document).ready(function () {
|
||||
comprobarSesion();
|
||||
// Comprobar la sesión activa al cargar la página
|
||||
comprobarEstadoSesion();
|
||||
|
||||
// Evento Click: Login
|
||||
$('#botonAceptarLogin').on('click', function () {
|
||||
ejecutarLogin();
|
||||
});
|
||||
|
||||
// Permitir Submit con tecla Enter en el formulario de Login
|
||||
$('#formLogin').on('submit', function (e) {
|
||||
e.preventDefault();
|
||||
ejecutarLogin();
|
||||
});
|
||||
|
||||
// Comprobar estado de sesión al cargar la página
|
||||
function comprobarSesion() {
|
||||
// Evento Click: Registro
|
||||
$('#botonAceptarRegistro').on('click', function () {
|
||||
ejecutarRegistro();
|
||||
});
|
||||
|
||||
// Permitir Submit con tecla Enter en el formulario de Registro
|
||||
$('#formRegistro').on('submit', function (e) {
|
||||
e.preventDefault();
|
||||
ejecutarRegistro();
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
* Comprueba con el servidor si existe una sesión PHP activa.
|
||||
*/
|
||||
function comprobarEstadoSesion() {
|
||||
$.ajax({
|
||||
method: "POST",
|
||||
url: "controladores/controladorUsuario.php",
|
||||
data: { orden: "comprobarSesion" },
|
||||
}).done(function (respuesta) {
|
||||
try {
|
||||
if (typeof respuesta === "string") {
|
||||
respuesta = JSON.parse(respuesta);
|
||||
url: 'controladores/controladorUsuario.php',
|
||||
type: 'POST',
|
||||
dataType: 'json',
|
||||
data: { orden: 'comprobarSesion' },
|
||||
success: function (respuesta) {
|
||||
if (respuesta.activa) {
|
||||
renderizarCabeceraConSesion(respuesta.usuario);
|
||||
// Si existe el contenedor del botón Jugar en index, lo mostramos
|
||||
$('#colBotonJugar').removeClass('d-none');
|
||||
} else {
|
||||
renderizarCabeceraSinSesion();
|
||||
$('#colBotonJugar').addClass('d-none');
|
||||
}
|
||||
},
|
||||
error: function () {
|
||||
renderizarCabeceraSinSesion();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
const contenedor = $("#contenedorUsuarioCabecera");
|
||||
contenedor.empty();
|
||||
/**
|
||||
* Petición AJAX para iniciar sesión
|
||||
*/
|
||||
function ejecutarLogin() {
|
||||
let email = $('#loginEmail').val().trim();
|
||||
let contrasena = $('#loginPassword').val().trim();
|
||||
$('#errorLogin').text('');
|
||||
|
||||
if (respuesta.activa && respuesta.usuario) {
|
||||
// 1. Renderizar Menú de Usuario en la Cabecera
|
||||
const htmlUsuario = `
|
||||
if (email === '' || contrasena === '') {
|
||||
$('#errorLogin').text('Por favor, rellena todos los campos.');
|
||||
return;
|
||||
}
|
||||
|
||||
$.ajax({
|
||||
url: 'controladores/controladorUsuario.php',
|
||||
type: 'POST',
|
||||
dataType: 'json',
|
||||
data: {
|
||||
orden: 'comprobarUsuario',
|
||||
email: email,
|
||||
contrasena: contrasena
|
||||
},
|
||||
success: function (respuesta) {
|
||||
if (respuesta.error) {
|
||||
$('#errorLogin').text(respuesta.error);
|
||||
} else if (respuesta.exito) {
|
||||
$('#modalLogin').modal('hide');
|
||||
$('#formLogin')[0].reset();
|
||||
|
||||
renderizarCabeceraConSesion({
|
||||
idUsuario: respuesta.idUsuario,
|
||||
nombreUsuario: respuesta.nombreUsuario,
|
||||
avatar: respuesta.avatar
|
||||
});
|
||||
|
||||
$('#colBotonJugar').removeClass('d-none');
|
||||
}
|
||||
},
|
||||
error: function () {
|
||||
$('#errorLogin').text('Error al conectar con el servidor.');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Petición AJAX para registrar un usuario
|
||||
*/
|
||||
function ejecutarRegistro() {
|
||||
let nombre = $('#regNombre').val().trim();
|
||||
let email = $('#regEmail').val().trim();
|
||||
let pass = $('#regPassword').val().trim();
|
||||
let passConfirm = $('#regPasswordConfirm').val().trim();
|
||||
|
||||
$('#errorRegistro').text('');
|
||||
|
||||
if (nombre === '' || email === '' || pass === '') {
|
||||
$('#errorRegistro').text('Todos los campos son obligatorios.');
|
||||
return;
|
||||
}
|
||||
|
||||
if (pass !== passConfirm) {
|
||||
$('#errorRegistro').text('Las contraseñas no coinciden.');
|
||||
return;
|
||||
}
|
||||
|
||||
$.ajax({
|
||||
url: 'controladores/controladorUsuario.php',
|
||||
type: 'POST',
|
||||
dataType: 'json',
|
||||
data: {
|
||||
orden: 'registrarUsuario',
|
||||
nombreUsuario: nombre,
|
||||
email: email,
|
||||
contrasena: pass
|
||||
},
|
||||
success: function (respuesta) {
|
||||
if (respuesta.error) {
|
||||
$('#errorRegistro').text(respuesta.error);
|
||||
} else {
|
||||
$('#modalRegistro').modal('hide');
|
||||
$('#formRegistro')[0].reset();
|
||||
|
||||
// Autologin
|
||||
$('#loginEmail').val(email);
|
||||
$('#loginPassword').val(pass);
|
||||
ejecutarLogin();
|
||||
}
|
||||
},
|
||||
error: function () {
|
||||
$('#errorRegistro').text('Error al procesar el registro.');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Petición AJAX para cerrar la sesión
|
||||
*/
|
||||
function cerrarSesion() {
|
||||
$.ajax({
|
||||
url: 'controladores/controladorUsuario.php',
|
||||
type: 'POST',
|
||||
dataType: 'json',
|
||||
data: { orden: 'cerrarSesion' },
|
||||
success: function () {
|
||||
renderizarCabeceraSinSesion();
|
||||
$('#colBotonJugar').addClass('d-none');
|
||||
|
||||
if (window.location.pathname.includes('perfil.php') || window.location.pathname.includes('buscadorSala.php')) {
|
||||
window.location.href = 'index.php';
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Renderiza la cabecera cuando HAY sesión iniciada (Menú Perfil + Avatar)
|
||||
*/
|
||||
function renderizarCabeceraConSesion(usuario) {
|
||||
let avatarImg = usuario.avatar ? usuario.avatar : 'img/avatars/defaultProfile.jpeg';
|
||||
|
||||
let html = `
|
||||
<div class="dropdown">
|
||||
<button class="btn user-dropdown-btn dropdown-toggle d-flex align-items-center gap-2"
|
||||
type="button" id="userDropdown" data-bs-toggle="dropdown" aria-expanded="false">
|
||||
<i class="fa-solid fa-circle-user fa-lg text-warning"></i>
|
||||
<span>${escapeHTML(respuesta.usuario.nombreUsuario)}</span>
|
||||
<button class="btn btn-outline-light dropdown-toggle d-flex align-items-center gap-2 rounded-pill px-3 py-1 fw-bold" type="button" id="userMenu" data-bs-toggle="dropdown" aria-expanded="false">
|
||||
<img src="${avatarImg}" alt="Avatar" class="rounded-circle border" style="width: 32px; height: 32px; object-fit: cover;">
|
||||
<span>${escapeHtml(usuario.nombreUsuario)}</span>
|
||||
</button>
|
||||
<ul class="dropdown-menu dropdown-menu-end user-dropdown-menu" aria-labelledby="userDropdown">
|
||||
<ul class="dropdown-menu dropdown-menu-end shadow border-0 mt-2" aria-labelledby="userMenu">
|
||||
<li>
|
||||
<a class="dropdown-item d-flex align-items-center gap-2" href="perfil.php">
|
||||
<i class="fa-solid fa-user text-primary"></i> Perfil
|
||||
<a class="dropdown-item py-2 fw-semibold" href="perfil.php">
|
||||
<i class="fa-solid fa-user me-2 text-primary"></i>Mi Perfil
|
||||
</a>
|
||||
</li>
|
||||
<li><hr class="dropdown-divider"></li>
|
||||
<li>
|
||||
<a class="dropdown-item logout-item d-flex align-items-center gap-2" href="#" id="botonCerrarSesion">
|
||||
<i class="fa-solid fa-right-from-bracket"></i> Cerrar Sesión
|
||||
</a>
|
||||
<button class="dropdown-item py-2 fw-semibold text-danger" onclick="cerrarSesion()">
|
||||
<i class="fa-solid fa-right-from-bracket me-2"></i>Cerrar Sesión
|
||||
</button>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
`;
|
||||
contenedor.html(htmlUsuario);
|
||||
$('#contenedorUsuarioCabecera').html(html);
|
||||
}
|
||||
|
||||
// 2. Muestra el botón JUGAR y ajusta columnas
|
||||
$("#colBotonJugar").removeClass("d-none");
|
||||
$("#colRanking").removeClass("col-lg-8").addClass("col-lg-7 col-xl-8");
|
||||
|
||||
} else {
|
||||
// 1. Renderizar Botones de Invitado
|
||||
const htmlInvitado = `
|
||||
<button type="button" class="btn btn-kahoot-login rounded-pill px-4" data-bs-toggle="modal" data-bs-target="#modalLogin">
|
||||
<i class="fa-solid fa-right-to-bracket me-1"></i> Iniciar Sesión
|
||||
/**
|
||||
* Renderiza los botones originales:
|
||||
* - "Iniciar Sesión" (Rojo -> btn-danger)
|
||||
* - "Registrarse" (Verde -> btn-success)
|
||||
*/
|
||||
function renderizarCabeceraSinSesion() {
|
||||
let html = `
|
||||
<button class="btn btn-danger rounded-pill px-4 fw-bold shadow-sm" data-bs-toggle="modal" data-bs-target="#modalLogin">
|
||||
<i class="fa-solid fa-right-to-bracket me-1"></i> Iniciar sesión
|
||||
</button>
|
||||
<button type="button" class="btn btn-kahoot-register rounded-pill px-4" data-bs-toggle="modal" data-bs-target="#modalRegistro">
|
||||
<button class="btn btn-success rounded-pill px-4 fw-bold shadow-sm" data-bs-toggle="modal" data-bs-target="#modalRegistro">
|
||||
<i class="fa-solid fa-user-plus me-1"></i> Registrarse
|
||||
</button>
|
||||
`;
|
||||
contenedor.html(htmlInvitado);
|
||||
|
||||
// 2. Ocultar el botón JUGAR y expandir ranking
|
||||
$("#colBotonJugar").addClass("d-none");
|
||||
$("#colRanking").removeClass("col-lg-7 col-xl-8").addClass("col-lg-8");
|
||||
}
|
||||
} catch (e) {
|
||||
console.error("Error al procesar la respuesta de la sesión:", e);
|
||||
}
|
||||
});
|
||||
$('#contenedorUsuarioCabecera').html(html);
|
||||
}
|
||||
|
||||
// Registro
|
||||
$("#botonAceptarRegistro").click(function (event) {
|
||||
event.preventDefault();
|
||||
let nombreUsuario = $("#regNombre").val().trim();
|
||||
let email = $("#regEmail").val().trim();
|
||||
let contrasena = $("#regPassword").val();
|
||||
let contrasenaConfirmacion = $("#regPasswordConfirm").val();
|
||||
|
||||
$("#regNombre").removeClass("is-invalid");
|
||||
$("#regEmail").removeClass("is-invalid");
|
||||
$("#regPassword").removeClass("is-invalid");
|
||||
$("#regPasswordConfirm").removeClass("is-invalid");
|
||||
$("#errorRegistro").text("");
|
||||
|
||||
if (contrasena !== contrasenaConfirmacion) {
|
||||
$("#regPassword").addClass("is-invalid");
|
||||
$("#regPasswordConfirm").addClass("is-invalid");
|
||||
$("#errorRegistro").text("Las contraseñas no coinciden.");
|
||||
return;
|
||||
function escapeHtml(texto) {
|
||||
return $('<div>').text(texto).html();
|
||||
}
|
||||
|
||||
$.ajax({
|
||||
method: "POST",
|
||||
url: "controladores/controladorUsuario.php",
|
||||
data: {
|
||||
nombreUsuario: nombreUsuario,
|
||||
email: email,
|
||||
contrasena: contrasena,
|
||||
orden: "registrarUsuario",
|
||||
},
|
||||
}).done(function (respuesta) {
|
||||
if (typeof respuesta === "string") respuesta = JSON.parse(respuesta);
|
||||
|
||||
if (respuesta.error) {
|
||||
$("#errorRegistro").text(respuesta.error);
|
||||
$("#regNombre").addClass("is-invalid");
|
||||
$("#regEmail").addClass("is-invalid");
|
||||
} else {
|
||||
$("#errorRegistro")
|
||||
.removeClass("text-danger")
|
||||
.addClass("text-success")
|
||||
.text(respuesta.exito);
|
||||
|
||||
$("#formRegistro")[0].reset();
|
||||
|
||||
setTimeout(function () {
|
||||
location.reload();
|
||||
}, 1000);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// Login
|
||||
$("#botonAceptarLogin").click(function (event) {
|
||||
event.preventDefault();
|
||||
|
||||
let email = $("#loginEmail").val().trim();
|
||||
let contrasena = $("#loginPassword").val();
|
||||
|
||||
$("#loginEmail").removeClass("is-invalid");
|
||||
$("#loginPassword").removeClass("is-invalid");
|
||||
$("#errorLogin").text("");
|
||||
|
||||
if (email === "" || contrasena === "") {
|
||||
$("#errorLogin").text("Todos los campos son obligatorios.");
|
||||
if (email === "") $("#loginEmail").addClass("is-invalid");
|
||||
if (contrasena === "") $("#loginPassword").addClass("is-invalid");
|
||||
return;
|
||||
}
|
||||
|
||||
$.ajax({
|
||||
method: "POST",
|
||||
url: "controladores/controladorUsuario.php",
|
||||
data: {
|
||||
email: email,
|
||||
contrasena: contrasena,
|
||||
orden: "comprobarUsuario",
|
||||
},
|
||||
}).done(function (respuesta) {
|
||||
if (typeof respuesta === "string") respuesta = JSON.parse(respuesta);
|
||||
|
||||
if (respuesta.error) {
|
||||
$("#loginEmail").addClass("is-invalid");
|
||||
$("#loginPassword").addClass("is-invalid");
|
||||
$("#errorLogin").text(respuesta.error);
|
||||
} else {
|
||||
location.reload();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// Cerrar sesión
|
||||
$(document).on("click", "#botonCerrarSesion", function (event) {
|
||||
event.preventDefault();
|
||||
|
||||
$.ajax({
|
||||
method: "POST",
|
||||
url: "controladores/controladorUsuario.php",
|
||||
data: { orden: "cerrarSesion" },
|
||||
}).done(function (respuesta) {
|
||||
if (typeof respuesta === "string") respuesta = JSON.parse(respuesta);
|
||||
|
||||
if (respuesta.error) {
|
||||
console.log(respuesta.error);
|
||||
} else {
|
||||
window.location.href = "index.php";
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// Registro
|
||||
$("#botonAceptarRegistro").click(function (event) {
|
||||
event.preventDefault();
|
||||
let nombreUsuario = $("#regNombre").val().trim();
|
||||
let email = $("#regEmail").val().trim();
|
||||
let contrasena = $("#regPassword").val();
|
||||
let contrasenaConfirmacion = $("#regPasswordConfirm").val();
|
||||
|
||||
$("#regNombre").removeClass("is-invalid");
|
||||
$("#regEmail").removeClass("is-invalid");
|
||||
$("#regPassword").removeClass("is-invalid");
|
||||
$("#regPasswordConfirm").removeClass("is-invalid");
|
||||
$("#errorRegistro").text("");
|
||||
|
||||
if (contrasena !== contrasenaConfirmacion) {
|
||||
$("#regPassword").addClass("is-invalid");
|
||||
$("#regPasswordConfirm").addClass("is-invalid");
|
||||
$("#errorRegistro").text("Las contraseñas no coinciden.");
|
||||
return;
|
||||
}
|
||||
|
||||
$.ajax({
|
||||
method: "POST",
|
||||
url: "controladores/controladorUsuario.php",
|
||||
data: {
|
||||
nombreUsuario: nombreUsuario,
|
||||
email: email,
|
||||
contrasena: contrasena,
|
||||
orden: "registrarUsuario",
|
||||
},
|
||||
}).done(function (respuesta) {
|
||||
if (typeof respuesta === "string") respuesta = JSON.parse(respuesta);
|
||||
|
||||
if (respuesta.error) {
|
||||
$("#errorRegistro").text(respuesta.error);
|
||||
$("#regNombre").addClass("is-invalid");
|
||||
$("#regEmail").addClass("is-invalid");
|
||||
} else {
|
||||
$("#errorRegistro")
|
||||
.removeClass("text-danger")
|
||||
.addClass("text-success")
|
||||
.text(respuesta.exito);
|
||||
|
||||
$("#formRegistro")[0].reset();
|
||||
|
||||
setTimeout(function () {
|
||||
location.reload();
|
||||
}, 1000);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// Login
|
||||
$("#botonAceptarLogin").click(function (event) {
|
||||
event.preventDefault();
|
||||
|
||||
let email = $("#loginEmail").val().trim();
|
||||
let contrasena = $("#loginPassword").val();
|
||||
|
||||
$("#loginEmail").removeClass("is-invalid");
|
||||
$("#loginPassword").removeClass("is-invalid");
|
||||
$("#errorLogin").text("");
|
||||
|
||||
if (email === "" || contrasena === "") {
|
||||
$("#errorLogin").text("Todos los campos son obligatorios.");
|
||||
if (email === "") $("#loginEmail").addClass("is-invalid");
|
||||
if (contrasena === "") $("#loginPassword").addClass("is-invalid");
|
||||
return;
|
||||
}
|
||||
|
||||
$.ajax({
|
||||
method: "POST",
|
||||
url: "controladores/controladorUsuario.php",
|
||||
data: {
|
||||
email: email,
|
||||
contrasena: contrasena,
|
||||
orden: "comprobarUsuario",
|
||||
},
|
||||
}).done(function (respuesta) {
|
||||
if (typeof respuesta === "string") respuesta = JSON.parse(respuesta);
|
||||
|
||||
if (respuesta.error) {
|
||||
$("#loginEmail").addClass("is-invalid");
|
||||
$("#loginPassword").addClass("is-invalid");
|
||||
$("#errorLogin").text(respuesta.error);
|
||||
} else {
|
||||
location.reload();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// Cerrar sesión (Usa delegación de eventos por si el botón se crea dinámicamente)
|
||||
$(document).on("click", "#botonCerrarSesion", function (event) {
|
||||
event.preventDefault();
|
||||
|
||||
$.ajax({
|
||||
method: "POST",
|
||||
url: "controladores/controladorUsuario.php",
|
||||
data: { orden: "cerrarSesion" },
|
||||
}).done(function (respuesta) {
|
||||
if (typeof respuesta === "string") respuesta = JSON.parse(respuesta);
|
||||
|
||||
if (respuesta.error) {
|
||||
console.log(respuesta.error);
|
||||
} else {
|
||||
window.location.href = "index.php";
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
+48
-205
@@ -1,3 +1,17 @@
|
||||
<?php
|
||||
if (session_status() === PHP_SESSION_NONE) {
|
||||
session_start();
|
||||
}
|
||||
|
||||
// Redirección si el usuario no ha iniciado sesión
|
||||
if (!isset($_SESSION['idUsuario'])) {
|
||||
header('Location: login.php');
|
||||
exit();
|
||||
}
|
||||
|
||||
$nombreUsuario = htmlspecialchars($_SESSION['nombreUsuario'] ?? 'Usuario');
|
||||
$avatarUsuario = htmlspecialchars($_SESSION['avatar'] ?? 'img/avatars/defaultProfile.jpeg');
|
||||
?>
|
||||
<!DOCTYPE html>
|
||||
<html lang="es">
|
||||
|
||||
@@ -9,151 +23,99 @@
|
||||
<title>Perfil</title>
|
||||
|
||||
<!-- Bootstrap -->
|
||||
<link
|
||||
href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css"
|
||||
rel="stylesheet">
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
|
||||
|
||||
<!-- Bootstrap Icons -->
|
||||
<link
|
||||
rel="stylesheet"
|
||||
href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
|
||||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
|
||||
|
||||
<!-- Google Font: Fredoka -->
|
||||
<link rel="preconnect" href="https://fonts.googleapis.com">
|
||||
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
||||
|
||||
<link
|
||||
href="https://fonts.googleapis.com/css2?family=Fredoka:wght@400;500;600;700&display=swap"
|
||||
rel="stylesheet">
|
||||
<link href="https://fonts.googleapis.com/css2?family=Fredoka:wght@400;500;600;700&display=swap" rel="stylesheet">
|
||||
|
||||
<!-- Tu CSS -->
|
||||
<link rel="stylesheet" href="css/index.css">
|
||||
<link rel="stylesheet" href="css/perfil.css">
|
||||
<script src="js/jquery-4.0.0.min.js"></script>
|
||||
|
||||
</head>
|
||||
|
||||
|
||||
<body>
|
||||
|
||||
<?php
|
||||
include_once 'cabecera.php';
|
||||
?>
|
||||
|
||||
<?php include_once 'cabecera.php'; ?>
|
||||
|
||||
<div class="perfil-container">
|
||||
|
||||
<div class="perfil-card">
|
||||
|
||||
<!-- ========================= -->
|
||||
<!-- CABECERA -->
|
||||
<!-- ========================= -->
|
||||
|
||||
<div class="perfil-header">
|
||||
<i class="bi bi-person-circle"></i>
|
||||
Mi perfil
|
||||
</div>
|
||||
|
||||
|
||||
<div class="perfil-content">
|
||||
|
||||
<div class="row align-items-center g-5">
|
||||
|
||||
|
||||
<!-- ========================= -->
|
||||
<!-- PERFIL -->
|
||||
<!-- ========================= -->
|
||||
|
||||
<div class="col-md-6 text-center">
|
||||
|
||||
<!-- Nombre -->
|
||||
|
||||
<h1 id="nombre" class="nombre">
|
||||
<?php echo $_SESSION['nombreUsuario'] ?>
|
||||
<?= $nombreUsuario ?>
|
||||
</h1>
|
||||
|
||||
|
||||
<!-- ========================= -->
|
||||
<!-- FOTO DE PERFIL -->
|
||||
<!-- ========================= -->
|
||||
|
||||
<div class="foto-container">
|
||||
|
||||
<img
|
||||
id="fotoPerfil"
|
||||
src="<?= htmlspecialchars($_SESSION['avatar'] ?? '') ?>"
|
||||
<img id="fotoPerfil"
|
||||
src="<?= $avatarUsuario ?>"
|
||||
alt="Foto de perfil"
|
||||
class="avatar rounded-circle">
|
||||
|
||||
|
||||
<!-- Botón lápiz -->
|
||||
|
||||
<button
|
||||
type="button"
|
||||
<button type="button"
|
||||
class="btn-foto"
|
||||
onclick="seleccionarFoto()"
|
||||
title="Cambiar foto de perfil"
|
||||
aria-label="Cambiar foto de perfil">
|
||||
|
||||
<i class="bi bi-pencil-fill"></i>
|
||||
|
||||
</button>
|
||||
|
||||
|
||||
<!-- Input oculto -->
|
||||
|
||||
<input
|
||||
type="file"
|
||||
<input type="file"
|
||||
id="inputFoto"
|
||||
accept="image/*"
|
||||
hidden>
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
<!-- ========================= -->
|
||||
<!-- CAMBIAR NOMBRE -->
|
||||
<!-- ========================= -->
|
||||
|
||||
<div class="formulario-nombre">
|
||||
|
||||
<label
|
||||
for="nuevoNombre"
|
||||
class="form-label">
|
||||
|
||||
<label for="nuevoNombre" class="form-label">
|
||||
Cambia tu nombre
|
||||
|
||||
</label>
|
||||
|
||||
|
||||
<input
|
||||
type="text"
|
||||
<input type="text"
|
||||
id="nuevoNombre"
|
||||
class="form-control mb-3"
|
||||
placeholder="Escribe tu nuevo nombre">
|
||||
|
||||
|
||||
<div class="d-flex justify-content-center gap-2">
|
||||
|
||||
<button
|
||||
type="button"
|
||||
<button type="button"
|
||||
class="btn-perfil-cambiar"
|
||||
onclick="cambiarNombre()">
|
||||
|
||||
<i class="bi bi-check-lg"></i>
|
||||
Cambiar nombre
|
||||
|
||||
</button>
|
||||
|
||||
|
||||
<button
|
||||
type="button"
|
||||
<button type="button"
|
||||
class="btn-perfil-borrar"
|
||||
onclick="borrarNombre()">
|
||||
|
||||
<i class="bi bi-x-lg"></i>
|
||||
Borrar
|
||||
|
||||
</button>
|
||||
|
||||
</div>
|
||||
@@ -162,11 +124,7 @@
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
<!-- ========================= -->
|
||||
<!-- ESTADÍSTICAS -->
|
||||
<!-- ========================= -->
|
||||
|
||||
<div class="col-md-6">
|
||||
|
||||
<div class="estadisticas">
|
||||
@@ -176,60 +134,33 @@
|
||||
Estadísticas
|
||||
</h2>
|
||||
|
||||
|
||||
<div class="estadistica estadistica-victorias">
|
||||
|
||||
<i class="bi bi-trophy-fill"></i>
|
||||
Victorias:
|
||||
|
||||
<strong id="victorias">
|
||||
|
||||
</strong>
|
||||
|
||||
<strong id="victorias">0</strong>
|
||||
</div>
|
||||
|
||||
|
||||
<div class="estadistica estadistica-derrotas">
|
||||
|
||||
<i class="bi bi-x-circle-fill"></i>
|
||||
Derrotas:
|
||||
|
||||
<strong id="derrotas">
|
||||
|
||||
</strong>
|
||||
|
||||
<strong id="derrotas">0</strong>
|
||||
</div>
|
||||
|
||||
|
||||
<div class="estadistica estadistica-aciertos">
|
||||
|
||||
<i class="bi bi-bullseye"></i>
|
||||
Aciertos:
|
||||
|
||||
<strong id="aciertos">
|
||||
|
||||
</strong>
|
||||
|
||||
<strong id="aciertos">0</strong>
|
||||
</div>
|
||||
|
||||
|
||||
<!-- ========================= -->
|
||||
<!-- CREAR PREGUNTA -->
|
||||
<!-- ========================= -->
|
||||
|
||||
<div class="text-center mt-4">
|
||||
|
||||
<button
|
||||
type="button"
|
||||
<button type="button"
|
||||
class="btn btn-play-kahoot"
|
||||
data-bs-toggle="modal"
|
||||
data-bs-target="#modalCrearPregunta">
|
||||
|
||||
<i class="bi bi-plus-circle-fill"></i>
|
||||
Crear pregunta
|
||||
|
||||
</button>
|
||||
|
||||
</div>
|
||||
|
||||
</div>
|
||||
@@ -245,12 +176,8 @@
|
||||
</div>
|
||||
|
||||
|
||||
<!-- ================================================= -->
|
||||
<!-- MODAL CREAR PREGUNTA -->
|
||||
<!-- ================================================= -->
|
||||
|
||||
<div
|
||||
class="modal fade"
|
||||
<div class="modal fade"
|
||||
id="modalCrearPregunta"
|
||||
tabindex="-1"
|
||||
aria-labelledby="modalCrearPreguntaLabel"
|
||||
@@ -260,186 +187,107 @@
|
||||
|
||||
<div class="modal-content modal-kahoot">
|
||||
|
||||
|
||||
<!-- CABECERA DEL MODAL -->
|
||||
|
||||
<div class="modal-header modal-kahoot-header">
|
||||
|
||||
<h5
|
||||
class="modal-title"
|
||||
id="modalCrearPreguntaLabel">
|
||||
|
||||
<h5 class="modal-title" id="modalCrearPreguntaLabel">
|
||||
<i class="bi bi-question-circle-fill me-2"></i>
|
||||
Crear pregunta
|
||||
|
||||
</h5>
|
||||
|
||||
|
||||
<button
|
||||
type="button"
|
||||
<button type="button"
|
||||
class="btn-close btn-close-white"
|
||||
data-bs-dismiss="modal"
|
||||
aria-label="Cerrar">
|
||||
</button>
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
<!-- CUERPO DEL MODAL -->
|
||||
|
||||
<div class="modal-body p-4">
|
||||
|
||||
<form id="formCrearPregunta">
|
||||
|
||||
|
||||
<!-- PREGUNTA -->
|
||||
|
||||
<div class="mb-4">
|
||||
|
||||
<label
|
||||
for="pregunta"
|
||||
class="form-label fw-semibold">
|
||||
|
||||
<label for="pregunta" class="form-label fw-semibold">
|
||||
<i class="bi bi-question-lg me-1"></i>
|
||||
Pregunta
|
||||
|
||||
</label>
|
||||
|
||||
<textarea
|
||||
id="pregunta"
|
||||
<textarea id="pregunta"
|
||||
name="pregunta"
|
||||
class="form-control input-kahoot"
|
||||
rows="3"
|
||||
placeholder="Escribe aquí la pregunta..."
|
||||
required></textarea>
|
||||
|
||||
</div>
|
||||
|
||||
<!-- CATEGORÍA -->
|
||||
|
||||
<div class="mb-4">
|
||||
|
||||
<label
|
||||
for="idCategoria"
|
||||
class="form-label fw-semibold">
|
||||
|
||||
<label for="idCategoria" class="form-label fw-semibold">
|
||||
<i class="bi bi-folder-fill me-1"></i>
|
||||
Categoría
|
||||
|
||||
</label>
|
||||
|
||||
<select
|
||||
id="idCategoria"
|
||||
<select id="idCategoria"
|
||||
name="idCategoria"
|
||||
class="form-select input-kahoot"
|
||||
required>
|
||||
|
||||
<option value="">
|
||||
Selecciona una categoría
|
||||
</option>
|
||||
|
||||
<option value="">Selecciona una categoría</option>
|
||||
</select>
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
<!-- RESPUESTA CORRECTA -->
|
||||
|
||||
<div class="mb-3">
|
||||
|
||||
<label
|
||||
for="respuestaCorrecta"
|
||||
class="form-label fw-semibold text-success">
|
||||
|
||||
<label for="respuestaCorrecta" class="form-label fw-semibold text-success">
|
||||
<i class="bi bi-check-circle-fill me-1"></i>
|
||||
Respuesta correcta
|
||||
|
||||
</label>
|
||||
|
||||
<input
|
||||
type="text"
|
||||
<input type="text"
|
||||
id="respuestaCorrecta"
|
||||
name="respuestaCorrecta"
|
||||
class="form-control input-kahoot"
|
||||
placeholder="Escribe la respuesta correcta..."
|
||||
required>
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
<!-- RESPUESTAS INCORRECTAS -->
|
||||
|
||||
<div class="mb-3">
|
||||
|
||||
<label
|
||||
class="form-label fw-semibold text-danger">
|
||||
|
||||
<label class="form-label fw-semibold text-danger">
|
||||
<i class="bi bi-x-circle-fill me-1"></i>
|
||||
Respuestas incorrectas
|
||||
|
||||
</label>
|
||||
|
||||
|
||||
<input
|
||||
type="text"
|
||||
<input type="text"
|
||||
id="respuestaIncorrecta1"
|
||||
name="respuestaIncorrecta1"
|
||||
class="form-control input-kahoot mb-2"
|
||||
placeholder="Respuesta incorrecta 1"
|
||||
required>
|
||||
|
||||
|
||||
<input
|
||||
type="text"
|
||||
<input type="text"
|
||||
id="respuestaIncorrecta2"
|
||||
name="respuestaIncorrecta2"
|
||||
class="form-control input-kahoot mb-2"
|
||||
placeholder="Respuesta incorrecta 2"
|
||||
required>
|
||||
|
||||
|
||||
<input
|
||||
type="text"
|
||||
<input type="text"
|
||||
id="respuestaIncorrecta3"
|
||||
name="respuestaIncorrecta3"
|
||||
class="form-control input-kahoot"
|
||||
placeholder="Respuesta incorrecta 3"
|
||||
required>
|
||||
|
||||
</div>
|
||||
|
||||
</form>
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
<!-- PIE DEL MODAL -->
|
||||
|
||||
<div class="modal-footer">
|
||||
|
||||
<button
|
||||
type="button"
|
||||
class="btn btn-secondary"
|
||||
data-bs-dismiss="modal">
|
||||
|
||||
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">
|
||||
<i class="bi bi-x-lg me-1"></i>
|
||||
Cancelar
|
||||
|
||||
</button>
|
||||
|
||||
|
||||
<button
|
||||
type="submit"
|
||||
form="formCrearPregunta"
|
||||
class="btn btn-kahoot-login"
|
||||
id="btnCrearPregunta">
|
||||
|
||||
<button type="submit" form="formCrearPregunta" class="btn btn-kahoot-login" id="btnCrearPregunta">
|
||||
<i class="bi bi-check-lg me-1"></i>
|
||||
Crear pregunta
|
||||
|
||||
</button>
|
||||
|
||||
</div>
|
||||
|
||||
</div>
|
||||
@@ -449,14 +297,9 @@
|
||||
</div>
|
||||
|
||||
<!-- Bootstrap JS -->
|
||||
|
||||
<script
|
||||
src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js">
|
||||
</script>
|
||||
|
||||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
|
||||
|
||||
<!-- Tu JavaScript -->
|
||||
|
||||
<script src="js/perfil.js"></script>
|
||||
|
||||
</body>
|
||||
|
||||
Reference in New Issue
Block a user