Add sincronizacion de jugadores en partida
This commit is contained in:
+35
-19
@@ -132,27 +132,43 @@ class Partida
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function iniciarPartida(){
|
public function iniciarPartida()
|
||||||
$conexion = conexionBD();
|
{
|
||||||
if (is_array($conexion)){
|
$conexion = conexionBD();
|
||||||
return $conexion;
|
|
||||||
}
|
if (is_array($conexion)) {
|
||||||
try {
|
return $conexion;
|
||||||
$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()
|
|
||||||
];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
$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" => "ERROR REAL: " . $error->getMessage()
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public function finalizar(){
|
public function finalizar(){
|
||||||
$conexion = conexionBD();
|
$conexion = conexionBD();
|
||||||
|
|||||||
@@ -80,7 +80,7 @@ switch ($orden) {
|
|||||||
!isset($_POST['tiempoRespuesta'])
|
!isset($_POST['tiempoRespuesta'])
|
||||||
) {
|
) {
|
||||||
echo json_encode([
|
echo json_encode([
|
||||||
'error' => 'Faltan datos para registrar la respuesta.'
|
'error' => 'Faltan datos para registrar la respuestfdfa.'
|
||||||
]);
|
]);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -164,7 +164,7 @@ switch ($_POST['orden']) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* ==========================================
|
* ==========================================
|
||||||
* 3. INSERTAMOS LOS EQUIPOS CON SU PARTIDA EN EQUIPOXPARTIDA
|
* 3. INSERTAMOS LOS EQUIPOS CON SU PARTIDA EN EQUIPOXPARTIDA
|
||||||
* ==========================================
|
* ==========================================
|
||||||
@@ -246,7 +246,7 @@ switch ($_POST['orden']) {
|
|||||||
// Asignamos el ID real de la pregunta a la partida
|
// Asignamos el ID real de la pregunta a la partida
|
||||||
$partida->asignarPregunta($idPreguntaAleatoria);
|
$partida->asignarPregunta($idPreguntaAleatoria);
|
||||||
}
|
}
|
||||||
}//fin shile
|
} //fin shile
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -795,13 +795,47 @@ switch ($_POST['orden']) {
|
|||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
$resultado = $partida->iniciar();
|
$resultado = $partida->iniciarPartida();
|
||||||
|
|
||||||
|
|
||||||
echo json_encode($resultado);
|
echo json_encode($resultado);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case 'comprobarInicio':
|
||||||
|
|
||||||
|
if (!isset($_POST['idPartida'])) {
|
||||||
|
|
||||||
|
echo json_encode([
|
||||||
|
'error' => 'No se ha indicado la partida.'
|
||||||
|
]);
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
$partida = new Partida();
|
||||||
|
|
||||||
|
$partida->idPartida =
|
||||||
|
intval($_POST['idPartida']);
|
||||||
|
|
||||||
|
$datosPartida = $partida->buscar();
|
||||||
|
|
||||||
|
if (!$datosPartida || isset($datosPartida['error'])) {
|
||||||
|
|
||||||
|
echo json_encode([
|
||||||
|
'error' => 'La partida no existe.'
|
||||||
|
]);
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
echo json_encode([
|
||||||
|
'iniciada' => $datosPartida['fechaInicio'] !== null
|
||||||
|
]);
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// OBTENER LA PREGUNTA ACTUAL
|
// OBTENER LA PREGUNTA ACTUAL
|
||||||
@@ -933,7 +967,7 @@ switch ($_POST['orden']) {
|
|||||||
) {
|
) {
|
||||||
|
|
||||||
echo json_encode([
|
echo json_encode([
|
||||||
'error' => 'Faltan datos para registrar la respuesta.'
|
'error' => 'Faltan datos para registrar la respuestadd.'
|
||||||
]);
|
]);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|||||||
+88
-11
@@ -118,7 +118,12 @@ function mostrarPregunta() {
|
|||||||
// 3. El usuario pulsa una respuesta: se guarda y se enseña si ha acertado
|
// 3. El usuario pulsa una respuesta: se guarda y se enseña si ha acertado
|
||||||
function responder($botonPulsado) {
|
function responder($botonPulsado) {
|
||||||
clearInterval(intervaloTiempo);
|
clearInterval(intervaloTiempo);
|
||||||
console.log("Tiempo de respuesta: " + tiempoRespuesta / 10 + " segundos");
|
|
||||||
|
console.log(
|
||||||
|
"Tiempo de respuesta: " +
|
||||||
|
tiempoRespuesta / 10 +
|
||||||
|
" segundos"
|
||||||
|
);
|
||||||
|
|
||||||
if (bloqueado) {
|
if (bloqueado) {
|
||||||
return;
|
return;
|
||||||
@@ -127,7 +132,29 @@ function responder($botonPulsado) {
|
|||||||
bloqueado = true;
|
bloqueado = true;
|
||||||
|
|
||||||
const pregunta = preguntas[indice];
|
const pregunta = preguntas[indice];
|
||||||
const idRespuesta = parseInt($botonPulsado.attr("data-id-respuesta"), 10);
|
|
||||||
|
const idRespuesta = parseInt(
|
||||||
|
$botonPulsado.attr("data-id-respuesta"),
|
||||||
|
10
|
||||||
|
);
|
||||||
|
|
||||||
|
console.log("PREGUNTA ACTUAL:", pregunta);
|
||||||
|
console.log(
|
||||||
|
"idPartidaxPregunta:",
|
||||||
|
pregunta.idPartidaxPregunta
|
||||||
|
);
|
||||||
|
console.log(
|
||||||
|
"idRespuesta:",
|
||||||
|
idRespuesta
|
||||||
|
);
|
||||||
|
console.log(
|
||||||
|
"tiempoRespuesta:",
|
||||||
|
tiempoRespuesta
|
||||||
|
);
|
||||||
|
console.log(
|
||||||
|
"idPartida:",
|
||||||
|
idPartida
|
||||||
|
);
|
||||||
|
|
||||||
$("#respuestas").addClass("bloqueado");
|
$("#respuestas").addClass("bloqueado");
|
||||||
|
|
||||||
@@ -135,46 +162,96 @@ function responder($botonPulsado) {
|
|||||||
url: URL_CONTROLADOR,
|
url: URL_CONTROLADOR,
|
||||||
type: "POST",
|
type: "POST",
|
||||||
dataType: "json",
|
dataType: "json",
|
||||||
|
|
||||||
data: {
|
data: {
|
||||||
orden: "responder",
|
orden: "responder",
|
||||||
idPartida: idPartida,
|
idPartida: idPartida,
|
||||||
idPartidaxPregunta: pregunta.idPartidaxPregunta,
|
idPartidaxPregunta: pregunta.idPartidaxPregunta,
|
||||||
idRespuesta: idRespuesta,
|
idRespuesta: idRespuesta,
|
||||||
tiempoRespuesta: tiempoRespuesta,
|
tiempoRespuesta: tiempoRespuesta
|
||||||
},
|
},
|
||||||
|
|
||||||
success: function (datos) {
|
success: function (datos) {
|
||||||
|
|
||||||
|
console.log("RESPUESTA DEL SERVIDOR:", datos);
|
||||||
|
|
||||||
if (datos.error) {
|
if (datos.error) {
|
||||||
// No se guardó: dejamos que vuelva a intentarlo
|
|
||||||
$("#feedback").text(datos.error).removeClass("ok").addClass("ko");
|
$("#feedback")
|
||||||
|
.text(datos.error)
|
||||||
|
.removeClass("ok")
|
||||||
|
.addClass("ko");
|
||||||
|
|
||||||
$("#respuestas").removeClass("bloqueado");
|
$("#respuestas").removeClass("bloqueado");
|
||||||
|
|
||||||
bloqueado = false;
|
bloqueado = false;
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
pregunta.respondida = true;
|
pregunta.respondida = true;
|
||||||
mostrarFeedback($botonPulsado, datos);
|
|
||||||
|
mostrarFeedback(
|
||||||
|
$botonPulsado,
|
||||||
|
datos
|
||||||
|
);
|
||||||
|
|
||||||
setTimeout(function () {
|
setTimeout(function () {
|
||||||
if (datos.terminada || indice >= preguntas.length - 1) {
|
|
||||||
|
if (
|
||||||
|
datos.terminada ||
|
||||||
|
indice >= preguntas.length - 1
|
||||||
|
) {
|
||||||
terminar();
|
terminar();
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
indice++;
|
indice++;
|
||||||
$("#respuestas").removeClass("bloqueado");
|
|
||||||
|
$("#respuestas")
|
||||||
|
.removeClass("bloqueado");
|
||||||
|
|
||||||
mostrarPregunta();
|
mostrarPregunta();
|
||||||
}
|
}
|
||||||
|
|
||||||
}, PAUSA_FEEDBACK);
|
}, PAUSA_FEEDBACK);
|
||||||
},
|
},
|
||||||
error: function () {
|
|
||||||
|
error: function (xhr, status, error) {
|
||||||
|
|
||||||
|
console.error(
|
||||||
|
"ERROR AJAX RESPONDER"
|
||||||
|
);
|
||||||
|
|
||||||
|
console.error(
|
||||||
|
"HTTP:",
|
||||||
|
xhr.status
|
||||||
|
);
|
||||||
|
|
||||||
|
console.error(
|
||||||
|
"ERROR:",
|
||||||
|
error
|
||||||
|
);
|
||||||
|
|
||||||
|
console.error(
|
||||||
|
"RESPUESTA:",
|
||||||
|
xhr.responseText
|
||||||
|
);
|
||||||
|
|
||||||
$("#feedback")
|
$("#feedback")
|
||||||
.text("No se pudo guardar la respuesta. Inténtalo de nuevo.")
|
.text(
|
||||||
|
"No se pudo guardar la respuesta. Inténtalo de nuevo."
|
||||||
|
)
|
||||||
.removeClass("ok")
|
.removeClass("ok")
|
||||||
.addClass("ko");
|
.addClass("ko");
|
||||||
|
|
||||||
$("#respuestas").removeClass("bloqueado");
|
$("#respuestas").removeClass("bloqueado");
|
||||||
|
|
||||||
bloqueado = false;
|
bloqueado = false;
|
||||||
},
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Marca la respuesta correcta en verde y atenúa el resto
|
// Marca la respuesta correcta en verde y atenúa el resto
|
||||||
function mostrarFeedback($botonPulsado, datos) {
|
function mostrarFeedback($botonPulsado, datos) {
|
||||||
$("#respuestas .btn-answer").each(function () {
|
$("#respuestas .btn-answer").each(function () {
|
||||||
|
|||||||
+112
-43
@@ -263,15 +263,16 @@ function mostrarSala(partida, equipos, jugadores) {
|
|||||||
|
|
||||||
if (esHost) {
|
if (esHost) {
|
||||||
botonIniciarHTML = `
|
botonIniciarHTML = `
|
||||||
<a
|
<button
|
||||||
href="juego.php?idPartida=${partida.idPartida}"
|
type="button"
|
||||||
class="btn btn-success btn-lg px-4">
|
class="btn btn-success btn-lg px-4 btn-iniciar-partida"
|
||||||
|
data-id-partida="${partida.idPartida}">
|
||||||
|
|
||||||
<i class="bi bi-controller"></i>
|
<i class="bi bi-controller"></i>
|
||||||
¡Iniciar Partida!
|
¡Iniciar Partida!
|
||||||
|
|
||||||
</a>
|
</button>
|
||||||
`;
|
`;
|
||||||
}
|
}
|
||||||
|
|
||||||
const salaHTML = `
|
const salaHTML = `
|
||||||
@@ -313,8 +314,98 @@ function mostrarSala(partida, equipos, jugadores) {
|
|||||||
`;
|
`;
|
||||||
|
|
||||||
contenedor.html(salaHTML);
|
contenedor.html(salaHTML);
|
||||||
|
if (!partida.fechaInicio) {
|
||||||
|
iniciarComprobacionPartida(partida.idPartida);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let intervaloComprobarPartida = null;
|
||||||
|
|
||||||
|
function iniciarComprobacionPartida(idPartida) {
|
||||||
|
if (intervaloComprobarPartida) {
|
||||||
|
clearInterval(intervaloComprobarPartida);
|
||||||
|
}
|
||||||
|
|
||||||
|
intervaloComprobarPartida = setInterval(function () {
|
||||||
|
$.ajax({
|
||||||
|
url: "controladores/controladorPartida.php",
|
||||||
|
|
||||||
|
type: "POST",
|
||||||
|
|
||||||
|
dataType: "json",
|
||||||
|
|
||||||
|
data: {
|
||||||
|
orden: "comprobarInicio",
|
||||||
|
idPartida: idPartida,
|
||||||
|
},
|
||||||
|
|
||||||
|
success: function (respuesta) {
|
||||||
|
if (respuesta.iniciada) {
|
||||||
|
clearInterval(intervaloComprobarPartida);
|
||||||
|
|
||||||
|
window.location.href = "juego.php?idPartida=" + idPartida;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}, 1000);
|
||||||
|
}
|
||||||
|
|
||||||
|
$(document).on("click", ".btn-iniciar-partida", function () {
|
||||||
|
const boton = $(this);
|
||||||
|
|
||||||
|
const idPartida = boton.data("id-partida");
|
||||||
|
|
||||||
|
boton.prop("disabled", true);
|
||||||
|
|
||||||
|
boton.html(`
|
||||||
|
<span class="spinner-border spinner-border-sm"></span>
|
||||||
|
Iniciando...
|
||||||
|
`);
|
||||||
|
|
||||||
|
$.ajax({
|
||||||
|
url: "controladores/controladorPartida.php",
|
||||||
|
|
||||||
|
type: "POST",
|
||||||
|
|
||||||
|
dataType: "json",
|
||||||
|
|
||||||
|
data: {
|
||||||
|
orden: "iniciar",
|
||||||
|
|
||||||
|
idPartida: idPartida,
|
||||||
|
},
|
||||||
|
|
||||||
|
success: function (respuesta) {
|
||||||
|
if (respuesta.error) {
|
||||||
|
alert(respuesta.error);
|
||||||
|
|
||||||
|
boton.prop("disabled", false);
|
||||||
|
|
||||||
|
boton.html(`
|
||||||
|
<i class="bi bi-controller"></i>
|
||||||
|
¡Iniciar Partida!
|
||||||
|
`);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// El host entra directamente
|
||||||
|
window.location.href = "juego.php?idPartida=" + idPartida;
|
||||||
|
},
|
||||||
|
|
||||||
|
error: function () {
|
||||||
|
alert("Ocurrió un error al iniciar la partida.");
|
||||||
|
|
||||||
|
boton.prop("disabled", false);
|
||||||
|
|
||||||
|
boton.html(`
|
||||||
|
<i class="bi bi-controller"></i>
|
||||||
|
¡Iniciar Partida!
|
||||||
|
`);
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
$(document).on("click", ".btn-unirse-equipo", function () {
|
$(document).on("click", ".btn-unirse-equipo", function () {
|
||||||
const boton = $(this);
|
const boton = $(this);
|
||||||
|
|
||||||
@@ -364,8 +455,7 @@ $(document).on("click", ".btn-abandonar-sala", function () {
|
|||||||
botonAbandonarReferencia = $(this);
|
botonAbandonarReferencia = $(this);
|
||||||
|
|
||||||
const esHost =
|
const esHost =
|
||||||
$(this).data("es-host") === true ||
|
$(this).data("es-host") === true || $(this).data("es-host") === "true";
|
||||||
$(this).data("es-host") === "true";
|
|
||||||
|
|
||||||
const titulo = $("#modalAbandonarSalaLabel");
|
const titulo = $("#modalAbandonarSalaLabel");
|
||||||
|
|
||||||
@@ -384,9 +474,7 @@ $(document).on("click", ".btn-abandonar-sala", function () {
|
|||||||
} else {
|
} else {
|
||||||
titulo.text("¿Abandonar la sala?");
|
titulo.text("¿Abandonar la sala?");
|
||||||
|
|
||||||
mensaje.text(
|
mensaje.text("Si sales de la sala, perderás tu lugar en el equipo.");
|
||||||
"Si sales de la sala, perderás tu lugar en el equipo.",
|
|
||||||
);
|
|
||||||
|
|
||||||
botonConfirmar.text("Sí, salir");
|
botonConfirmar.text("Sí, salir");
|
||||||
}
|
}
|
||||||
@@ -399,17 +487,13 @@ $(document).on("click", ".btn-abandonar-sala", function () {
|
|||||||
});
|
});
|
||||||
|
|
||||||
$(document).on("click", "#btnConfirmarAbandonar", function () {
|
$(document).on("click", "#btnConfirmarAbandonar", function () {
|
||||||
const modalEl =
|
const modalEl = document.getElementById("modalAbandonarSala");
|
||||||
document.getElementById("modalAbandonarSala");
|
|
||||||
|
|
||||||
const modalInstance =
|
const modalInstance = bootstrap.Modal.getInstance(modalEl);
|
||||||
bootstrap.Modal.getInstance(modalEl);
|
|
||||||
|
|
||||||
const btnConfirmar = $(this);
|
const btnConfirmar = $(this);
|
||||||
|
|
||||||
btnConfirmar
|
btnConfirmar.prop("disabled", true).text("Saliendo...");
|
||||||
.prop("disabled", true)
|
|
||||||
.text("Saliendo...");
|
|
||||||
|
|
||||||
$.ajax({
|
$.ajax({
|
||||||
url: "controladores/controladorEquipoxUsuario.php",
|
url: "controladores/controladorEquipoxUsuario.php",
|
||||||
@@ -421,9 +505,7 @@ $(document).on("click", "#btnConfirmarAbandonar", function () {
|
|||||||
},
|
},
|
||||||
|
|
||||||
success: function (respuesta) {
|
success: function (respuesta) {
|
||||||
btnConfirmar
|
btnConfirmar.prop("disabled", false).text("Sí, salir");
|
||||||
.prop("disabled", false)
|
|
||||||
.text("Sí, salir");
|
|
||||||
|
|
||||||
if (modalInstance) {
|
if (modalInstance) {
|
||||||
modalInstance.hide();
|
modalInstance.hide();
|
||||||
@@ -449,16 +531,11 @@ $(document).on("click", "#btnConfirmarAbandonar", function () {
|
|||||||
},
|
},
|
||||||
|
|
||||||
error: function (xhr, status, error) {
|
error: function (xhr, status, error) {
|
||||||
console.error(
|
console.error("Error al salir de la sala:", error);
|
||||||
"Error al salir de la sala:",
|
|
||||||
error
|
|
||||||
);
|
|
||||||
|
|
||||||
alert("No se pudo salir de la sala.");
|
alert("No se pudo salir de la sala.");
|
||||||
|
|
||||||
btnConfirmar
|
btnConfirmar.prop("disabled", false).text("Sí, salir");
|
||||||
.prop("disabled", false)
|
|
||||||
.text("Sí, salir");
|
|
||||||
|
|
||||||
if (modalInstance) {
|
if (modalInstance) {
|
||||||
modalInstance.hide();
|
modalInstance.hide();
|
||||||
@@ -473,25 +550,19 @@ function escapeHTML(texto) {
|
|||||||
.html();
|
.html();
|
||||||
}
|
}
|
||||||
|
|
||||||
function mostrarEquipos(
|
function mostrarEquipos(partida, equipos, jugadores) {
|
||||||
partida,
|
|
||||||
equipos,
|
|
||||||
jugadores
|
|
||||||
) {
|
|
||||||
let equiposHTML = "";
|
let equiposHTML = "";
|
||||||
|
|
||||||
equipos.forEach(function (equipo) {
|
equipos.forEach(function (equipo) {
|
||||||
const jugadoresEquipo =
|
const jugadoresEquipo = jugadores.filter(function (jugador) {
|
||||||
jugadores.filter(function (jugador) {
|
return jugador.idEquipo == equipo.idEquipo;
|
||||||
return jugador.idEquipo == equipo.idEquipo;
|
});
|
||||||
});
|
|
||||||
|
|
||||||
let jugadoresHTML = "";
|
let jugadoresHTML = "";
|
||||||
|
|
||||||
if (jugadoresEquipo.length > 0) {
|
if (jugadoresEquipo.length > 0) {
|
||||||
jugadoresEquipo.forEach(function (jugador) {
|
jugadoresEquipo.forEach(function (jugador) {
|
||||||
const esHost =
|
const esHost = jugador.idUsuario == partida.idUsuario;
|
||||||
jugador.idUsuario == partida.idUsuario;
|
|
||||||
|
|
||||||
jugadoresHTML += `
|
jugadoresHTML += `
|
||||||
<div class="jugador border border-2 d-flex align-items-center justify-content-between ${
|
<div class="jugador border border-2 d-flex align-items-center justify-content-between ${
|
||||||
@@ -507,9 +578,7 @@ function mostrarEquipos(
|
|||||||
}
|
}
|
||||||
</span>
|
</span>
|
||||||
|
|
||||||
${escapeHTML(
|
${escapeHTML(jugador.nombreUsuario)}
|
||||||
jugador.nombreUsuario
|
|
||||||
)}
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
${
|
${
|
||||||
|
|||||||
Reference in New Issue
Block a user