1263 lines
30 KiB
PHP
1263 lines
30 KiB
PHP
<?php
|
|
|
|
require_once __DIR__ . '/../includes/conexionBD.php';
|
|
|
|
class Partida
|
|
{
|
|
public $idPartida;
|
|
public $nombrePartida;
|
|
public $idUsuario;
|
|
public $estado;
|
|
public $preguntaActual;
|
|
|
|
|
|
|
|
|
|
public function __construct(
|
|
$idPartida = null,
|
|
$nombrePartida = null,
|
|
$idUsuario = null,
|
|
$estado = 'esperando',
|
|
$preguntaActual = 0
|
|
) {
|
|
$this->idPartida = $idPartida;
|
|
$this->nombrePartida = $nombrePartida;
|
|
$this->idUsuario = $idUsuario;
|
|
$this->estado = $estado;
|
|
$this->preguntaActual = $preguntaActual;
|
|
}
|
|
|
|
|
|
|
|
|
|
public function insertar()
|
|
{
|
|
$conexion = conexionBD();
|
|
|
|
if (is_array($conexion)) {
|
|
return $conexion;
|
|
}
|
|
|
|
try {
|
|
|
|
$consulta = $conexion->prepare("
|
|
INSERT INTO partida
|
|
(
|
|
nombrePartida,
|
|
idUsuario,
|
|
estado,
|
|
preguntaActual
|
|
)
|
|
VALUES
|
|
(
|
|
:nombrePartida,
|
|
:idUsuario,
|
|
:estado,
|
|
:preguntaActual
|
|
)
|
|
");
|
|
|
|
$consulta->bindParam(
|
|
":nombrePartida",
|
|
$this->nombrePartida
|
|
);
|
|
|
|
$consulta->bindParam(
|
|
":idUsuario",
|
|
$this->idUsuario
|
|
);
|
|
|
|
$consulta->bindParam(
|
|
":estado",
|
|
$this->estado
|
|
);
|
|
|
|
$consulta->bindParam(
|
|
":preguntaActual",
|
|
$this->preguntaActual,
|
|
PDO::PARAM_INT
|
|
);
|
|
|
|
$consulta->execute();
|
|
|
|
return [
|
|
"exito" => "La partida se creó correctamente.",
|
|
"exito_tecnico" => $conexion->lastInsertId()
|
|
];
|
|
|
|
} catch (PDOException $error) {
|
|
|
|
return [
|
|
"error" => "Ocurrió un error al guardar la partida.",
|
|
"error_tecnico" => $error->getMessage()
|
|
];
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
public function listar()
|
|
{
|
|
$conexion = conexionBD();
|
|
|
|
if (is_array($conexion)) {
|
|
return $conexion;
|
|
}
|
|
|
|
try {
|
|
|
|
$consulta = $conexion->prepare("
|
|
SELECT *
|
|
FROM partida
|
|
");
|
|
|
|
$consulta->execute();
|
|
|
|
return $consulta->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
} catch (PDOException $error) {
|
|
|
|
return [
|
|
"error" => "Ocurrió un error al obtener el listado.",
|
|
"error_tecnico" => $error->getMessage()
|
|
];
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
public function buscar()
|
|
{
|
|
$conexion = conexionBD();
|
|
|
|
if (is_array($conexion)) {
|
|
return $conexion;
|
|
}
|
|
|
|
try {
|
|
|
|
$consulta = $conexion->prepare("
|
|
SELECT *
|
|
FROM partida
|
|
WHERE idPartida = :idPartida
|
|
");
|
|
|
|
$consulta->bindParam(
|
|
":idPartida",
|
|
$this->idPartida
|
|
);
|
|
|
|
$consulta->execute();
|
|
|
|
return $consulta->fetch(PDO::FETCH_ASSOC);
|
|
|
|
} catch (PDOException $error) {
|
|
|
|
return [
|
|
"error" => "Ocurrió un error al buscar la partida.",
|
|
"error_tecnico" => $error->getMessage()
|
|
];
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
public function editar()
|
|
{
|
|
$conexion = conexionBD();
|
|
|
|
if (is_array($conexion)) {
|
|
return $conexion;
|
|
}
|
|
|
|
try {
|
|
|
|
$consulta = $conexion->prepare("
|
|
UPDATE partida
|
|
SET
|
|
nombrePartida = :nombrePartida,
|
|
idUsuario = :idUsuario,
|
|
estado = :estado,
|
|
preguntaActual = :preguntaActual
|
|
WHERE idPartida = :idPartida
|
|
");
|
|
|
|
$consulta->bindParam(
|
|
":idPartida",
|
|
$this->idPartida
|
|
);
|
|
|
|
$consulta->bindParam(
|
|
":nombrePartida",
|
|
$this->nombrePartida
|
|
);
|
|
|
|
$consulta->bindParam(
|
|
":idUsuario",
|
|
$this->idUsuario
|
|
);
|
|
|
|
$consulta->bindParam(
|
|
":estado",
|
|
$this->estado
|
|
);
|
|
|
|
$consulta->bindParam(
|
|
":preguntaActual",
|
|
$this->preguntaActual,
|
|
PDO::PARAM_INT
|
|
);
|
|
|
|
$consulta->execute();
|
|
|
|
return [
|
|
"exito" => "La partida se editó correctamente.",
|
|
"exito_tecnico" => $this->idPartida
|
|
];
|
|
|
|
} catch (PDOException $error) {
|
|
|
|
return [
|
|
"error" => "Ocurrió un error al editar la partida.",
|
|
"error_tecnico" => $error->getMessage()
|
|
];
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
public function eliminar()
|
|
{
|
|
$conexion = conexionBD();
|
|
|
|
if (is_array($conexion)) {
|
|
return $conexion;
|
|
}
|
|
|
|
try {
|
|
|
|
$consulta = $conexion->prepare("
|
|
DELETE FROM partida
|
|
WHERE idPartida = :idPartida
|
|
");
|
|
|
|
$consulta->bindParam(
|
|
":idPartida",
|
|
$this->idPartida
|
|
);
|
|
|
|
$consulta->execute();
|
|
|
|
return [
|
|
"exito" => "La partida se eliminó correctamente.",
|
|
"exito_tecnico" => $this->idPartida
|
|
];
|
|
|
|
} catch (PDOException $error) {
|
|
|
|
return [
|
|
"error" => "No se pudo eliminar la partida.",
|
|
"error_tecnico" => $error->getMessage()
|
|
];
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
} catch (PDOException $error) {
|
|
|
|
return [
|
|
"error" =>
|
|
"Ocurrió un error al recuperar las preguntas de la partida.",
|
|
"error_tecnico" => $error->getMessage()
|
|
];
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
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->bindParam(
|
|
":idPartida",
|
|
$this->idPartida
|
|
);
|
|
|
|
$consulta->bindParam(
|
|
":idPregunta",
|
|
$idPregunta
|
|
);
|
|
|
|
$consulta->execute();
|
|
|
|
return [
|
|
"exito" =>
|
|
"La pregunta se asignó correctamente.",
|
|
"exito_tecnico" =>
|
|
$conexion->lastInsertId()
|
|
];
|
|
|
|
} catch (PDOException $error) {
|
|
|
|
return [
|
|
"error" =>
|
|
"Ocurrió un error al asignar la pregunta.",
|
|
"error_tecnico" =>
|
|
$error->getMessage()
|
|
];
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
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->bindParam(
|
|
":idPartida",
|
|
$this->idPartida
|
|
);
|
|
|
|
$consulta->bindParam(
|
|
":idPregunta",
|
|
$idPregunta
|
|
);
|
|
|
|
$consulta->execute();
|
|
|
|
$fila = $consulta->fetch(PDO::FETCH_ASSOC);
|
|
|
|
return (int)$fila["total"] > 0;
|
|
|
|
} catch (PDOException $error) {
|
|
|
|
return [
|
|
"error" =>
|
|
"Ocurrió un error al comprobar la pregunta.",
|
|
"error_tecnico" =>
|
|
$error->getMessage()
|
|
];
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
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->bindParam(
|
|
":idPartida",
|
|
$this->idPartida
|
|
);
|
|
|
|
$consulta->execute();
|
|
|
|
$fila = $consulta->fetch(PDO::FETCH_ASSOC);
|
|
|
|
return (int)$fila["total"];
|
|
|
|
} catch (PDOException $error) {
|
|
|
|
return [
|
|
"error" =>
|
|
"Ocurrió un error al contar las preguntas.",
|
|
"error_tecnico" =>
|
|
$error->getMessage()
|
|
];
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
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->bindParam(
|
|
":idUsuario",
|
|
$this->idUsuario
|
|
);
|
|
|
|
$consulta->execute();
|
|
|
|
return $consulta->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
} catch (PDOException $error) {
|
|
|
|
return [
|
|
"error" =>
|
|
"Ocurrió un error al recuperar las partidas del usuario.",
|
|
"error_tecnico" =>
|
|
$error->getMessage()
|
|
];
|
|
}
|
|
}
|
|
|
|
|
|
|
|
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->bindParam(
|
|
":idPartida",
|
|
$this->idPartida
|
|
);
|
|
|
|
$consulta->bindParam(
|
|
":idUsuario",
|
|
$idUsuario
|
|
);
|
|
|
|
$consulta->execute();
|
|
|
|
$fila = $consulta->fetch(PDO::FETCH_ASSOC);
|
|
|
|
return (int)$fila["total"];
|
|
|
|
} catch (PDOException $error) {
|
|
|
|
return [
|
|
"error" =>
|
|
"Ocurrió un error al contar las respuestas del usuario.",
|
|
"error_tecnico" =>
|
|
$error->getMessage()
|
|
];
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
public function estaFinalizadaParaUsuario($idUsuario)
|
|
{
|
|
$totalPreguntas =
|
|
$this->contarPreguntasAsignadas();
|
|
|
|
if (is_array($totalPreguntas)) {
|
|
return $totalPreguntas;
|
|
}
|
|
|
|
$totalRespondidas =
|
|
$this->contarRespuestasUsuario($idUsuario);
|
|
|
|
if (is_array($totalRespondidas)) {
|
|
return $totalRespondidas;
|
|
}
|
|
|
|
return
|
|
$totalPreguntas > 0 &&
|
|
$totalPreguntas === $totalRespondidas;
|
|
}
|
|
|
|
|
|
|
|
|
|
public function registrarEquipoEnPartida($idEquipo)
|
|
{
|
|
$conexion = conexionBD();
|
|
|
|
if (is_array($conexion)) {
|
|
return $conexion;
|
|
}
|
|
|
|
try {
|
|
|
|
$consulta = $conexion->prepare("
|
|
INSERT INTO equipoxpartida
|
|
(idPartida, idEquipo)
|
|
VALUES
|
|
(:idPartida, :idEquipo)
|
|
");
|
|
|
|
$consulta->bindParam(
|
|
":idPartida",
|
|
$this->idPartida
|
|
);
|
|
|
|
$consulta->bindParam(
|
|
":idEquipo",
|
|
$idEquipo
|
|
);
|
|
|
|
$consulta->execute();
|
|
|
|
return [
|
|
"exito" =>
|
|
"El equipo se vinculó a la partida correctamente.",
|
|
"exito_tecnico" =>
|
|
$conexion->lastInsertId()
|
|
];
|
|
|
|
} catch (PDOException $error) {
|
|
|
|
return [
|
|
"error" =>
|
|
"Ocurrió un error al asignar el equipo a la partida.",
|
|
"error_tecnico" =>
|
|
$error->getMessage()
|
|
];
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getEquiposParticipantes()
|
|
{
|
|
$conexion = conexionBD();
|
|
|
|
if (is_array($conexion)) {
|
|
return $conexion;
|
|
}
|
|
|
|
try {
|
|
|
|
$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();
|
|
|
|
return $consulta->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
} catch (PDOException $error) {
|
|
|
|
return [
|
|
"error" =>
|
|
"Ocurrió un error al recuperar los equipos de la partida.",
|
|
"error_tecnico" =>
|
|
$error->getMessage()
|
|
];
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getJugadoresDeLaPartida()
|
|
{
|
|
$conexion = conexionBD();
|
|
|
|
if (is_array($conexion)) {
|
|
return $conexion;
|
|
}
|
|
|
|
try {
|
|
|
|
$consulta = $conexion->prepare("
|
|
SELECT DISTINCT
|
|
u.idUsuario,
|
|
u.nombreUsuario,
|
|
u.email,
|
|
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
|
|
");
|
|
|
|
$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()
|
|
];
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
public function iniciar()
|
|
{
|
|
$conexion = conexionBD();
|
|
|
|
if (is_array($conexion)) {
|
|
return $conexion;
|
|
}
|
|
|
|
try {
|
|
|
|
// Comprobamos el estado actual.
|
|
|
|
$consulta = $conexion->prepare("
|
|
SELECT estado
|
|
FROM partida
|
|
WHERE idPartida = :idPartida
|
|
");
|
|
|
|
$consulta->bindParam(
|
|
":idPartida",
|
|
$this->idPartida
|
|
);
|
|
|
|
$consulta->execute();
|
|
|
|
$partida = $consulta->fetch(PDO::FETCH_ASSOC);
|
|
|
|
if (!$partida) {
|
|
return [
|
|
"error" => "La partida no existe."
|
|
];
|
|
}
|
|
|
|
|
|
// Solo podemos iniciar una partida
|
|
// que esté esperando.
|
|
|
|
if ($partida['estado'] !== 'esperando') {
|
|
return [
|
|
"error" =>
|
|
"La partida ya ha sido iniciada o finalizada."
|
|
];
|
|
}
|
|
|
|
|
|
// Comprobamos que tenga preguntas.
|
|
|
|
$numeroPreguntas =
|
|
$this->contarPreguntasAsignadas();
|
|
|
|
if (is_array($numeroPreguntas)) {
|
|
return $numeroPreguntas;
|
|
}
|
|
|
|
if ($numeroPreguntas <= 0) {
|
|
return [
|
|
"error" =>
|
|
"No se puede iniciar una partida sin preguntas."
|
|
];
|
|
}
|
|
|
|
|
|
// Ponemos la partida en estado "jugando"
|
|
// y empezamos por la primera pregunta.
|
|
|
|
$consulta = $conexion->prepare("
|
|
UPDATE partida
|
|
SET
|
|
estado = 'jugando',
|
|
preguntaActual = 1
|
|
WHERE idPartida = :idPartida
|
|
");
|
|
|
|
$consulta->bindParam(
|
|
":idPartida",
|
|
$this->idPartida
|
|
);
|
|
|
|
$consulta->execute();
|
|
|
|
return [
|
|
"exito" =>
|
|
"La partida se ha iniciado correctamente.",
|
|
"idPartida" =>
|
|
$this->idPartida,
|
|
"preguntaActual" =>
|
|
1
|
|
];
|
|
|
|
} catch (PDOException $error) {
|
|
|
|
return [
|
|
"error" =>
|
|
"Ocurrió un error al iniciar la partida.",
|
|
"error_tecnico" =>
|
|
$error->getMessage()
|
|
];
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getPreguntaActual()
|
|
{
|
|
$conexion = conexionBD();
|
|
|
|
if (is_array($conexion)) {
|
|
return $conexion;
|
|
}
|
|
|
|
try {
|
|
|
|
$consulta = $conexion->prepare("
|
|
SELECT
|
|
preguntaActual,
|
|
estado
|
|
FROM partida
|
|
WHERE idPartida = :idPartida
|
|
");
|
|
|
|
$consulta->bindParam(
|
|
":idPartida",
|
|
$this->idPartida
|
|
);
|
|
|
|
$consulta->execute();
|
|
|
|
$datosPartida =
|
|
$consulta->fetch(PDO::FETCH_ASSOC);
|
|
|
|
if (!$datosPartida) {
|
|
return [
|
|
"error" => "La partida no existe."
|
|
];
|
|
}
|
|
|
|
|
|
if ($datosPartida['estado'] === 'esperando') {
|
|
return [
|
|
"estado" => "esperando",
|
|
"pregunta" => null
|
|
];
|
|
}
|
|
|
|
|
|
if ($datosPartida['estado'] === 'finalizada') {
|
|
return [
|
|
"estado" => "finalizada",
|
|
"pregunta" => null
|
|
];
|
|
}
|
|
|
|
|
|
$numeroPregunta =
|
|
(int)$datosPartida['preguntaActual'];
|
|
|
|
|
|
$consulta = $conexion->prepare("
|
|
SELECT
|
|
pxp.idPartidaxPregunta,
|
|
pxp.idPregunta,
|
|
p.*
|
|
FROM partidaxpregunta pxp
|
|
INNER JOIN pregunta p
|
|
ON pxp.idPregunta = p.idPregunta
|
|
WHERE pxp.idPartida = :idPartida
|
|
ORDER BY pxp.idPartidaxPregunta
|
|
LIMIT 1 OFFSET :offset
|
|
");
|
|
|
|
$consulta->bindParam(
|
|
":idPartida",
|
|
$this->idPartida
|
|
);
|
|
|
|
// SQL empieza a contar desde 0,
|
|
// mientras que preguntaActual empieza desde 1.
|
|
|
|
$offset = $numeroPregunta - 1;
|
|
|
|
$consulta->bindValue(
|
|
":offset",
|
|
$offset,
|
|
PDO::PARAM_INT
|
|
);
|
|
|
|
$consulta->execute();
|
|
|
|
$pregunta =
|
|
$consulta->fetch(PDO::FETCH_ASSOC);
|
|
|
|
if (!$pregunta) {
|
|
return [
|
|
"error" =>
|
|
"No se ha encontrado la pregunta actual."
|
|
];
|
|
}
|
|
|
|
return [
|
|
"estado" => "jugando",
|
|
"numeroPregunta" => $numeroPregunta,
|
|
"pregunta" => $pregunta
|
|
];
|
|
|
|
} catch (PDOException $error) {
|
|
|
|
return [
|
|
"error" =>
|
|
"Ocurrió un error al obtener la pregunta actual.",
|
|
"error_tecnico" =>
|
|
$error->getMessage()
|
|
];
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
public function siguientePregunta()
|
|
{
|
|
$conexion = conexionBD();
|
|
|
|
if (is_array($conexion)) {
|
|
return $conexion;
|
|
}
|
|
|
|
try {
|
|
|
|
$consulta = $conexion->prepare("
|
|
SELECT
|
|
estado,
|
|
preguntaActual
|
|
FROM partida
|
|
WHERE idPartida = :idPartida
|
|
");
|
|
|
|
$consulta->bindParam(
|
|
":idPartida",
|
|
$this->idPartida
|
|
);
|
|
|
|
$consulta->execute();
|
|
|
|
$datos =
|
|
$consulta->fetch(PDO::FETCH_ASSOC);
|
|
|
|
if (!$datos) {
|
|
return [
|
|
"error" => "La partida no existe."
|
|
];
|
|
}
|
|
|
|
if ($datos['estado'] !== 'jugando') {
|
|
return [
|
|
"error" =>
|
|
"La partida no está actualmente en juego."
|
|
];
|
|
}
|
|
|
|
|
|
$totalPreguntas =
|
|
$this->contarPreguntasAsignadas();
|
|
|
|
if (is_array($totalPreguntas)) {
|
|
return $totalPreguntas;
|
|
}
|
|
|
|
|
|
$siguiente =
|
|
(int)$datos['preguntaActual'] + 1;
|
|
|
|
|
|
if ($siguiente > $totalPreguntas) {
|
|
|
|
$consulta = $conexion->prepare("
|
|
UPDATE partida
|
|
SET
|
|
estado = 'finalizada'
|
|
WHERE idPartida = :idPartida
|
|
");
|
|
|
|
$consulta->bindParam(
|
|
":idPartida",
|
|
$this->idPartida
|
|
);
|
|
|
|
$consulta->execute();
|
|
|
|
return [
|
|
"exito" =>
|
|
"La partida ha finalizado.",
|
|
"estado" =>
|
|
"finalizada"
|
|
];
|
|
}
|
|
|
|
|
|
$consulta = $conexion->prepare("
|
|
UPDATE partida
|
|
SET
|
|
preguntaActual = :preguntaActual
|
|
WHERE idPartida = :idPartida
|
|
");
|
|
|
|
$consulta->bindParam(
|
|
":preguntaActual",
|
|
$siguiente,
|
|
PDO::PARAM_INT
|
|
);
|
|
|
|
$consulta->bindParam(
|
|
":idPartida",
|
|
$this->idPartida
|
|
);
|
|
|
|
$consulta->execute();
|
|
|
|
return [
|
|
"exito" =>
|
|
"Se ha avanzado a la siguiente pregunta.",
|
|
"preguntaActual" =>
|
|
$siguiente
|
|
];
|
|
|
|
} catch (PDOException $error) {
|
|
|
|
return [
|
|
"error" =>
|
|
"Ocurrió un error al pasar a la siguiente pregunta.",
|
|
"error_tecnico" =>
|
|
$error->getMessage()
|
|
];
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
public function registrarRespuesta(
|
|
$idUsuario,
|
|
$idPregunta,
|
|
$idRespuesta
|
|
) {
|
|
$conexion = conexionBD();
|
|
|
|
if (is_array($conexion)) {
|
|
return $conexion;
|
|
}
|
|
|
|
try {
|
|
|
|
// Comprobamos que la pregunta pertenezca
|
|
// a esta partida.
|
|
|
|
$consulta = $conexion->prepare("
|
|
SELECT idPartidaxPregunta
|
|
FROM partidaxpregunta
|
|
WHERE idPartida = :idPartida
|
|
AND idPregunta = :idPregunta
|
|
");
|
|
|
|
$consulta->bindParam(
|
|
":idPartida",
|
|
$this->idPartida
|
|
);
|
|
|
|
$consulta->bindParam(
|
|
":idPregunta",
|
|
$idPregunta
|
|
);
|
|
|
|
$consulta->execute();
|
|
|
|
$pregunta =
|
|
$consulta->fetch(PDO::FETCH_ASSOC);
|
|
|
|
if (!$pregunta) {
|
|
return [
|
|
"error" =>
|
|
"La pregunta no pertenece a esta partida."
|
|
];
|
|
}
|
|
|
|
$idPartidaxPregunta =
|
|
$pregunta['idPartidaxPregunta'];
|
|
|
|
|
|
// Comprobamos si el usuario ya ha respondido.
|
|
|
|
$consulta = $conexion->prepare("
|
|
SELECT idRespuestaAlmacenada
|
|
FROM respuestaalmacenada
|
|
WHERE idPartidaxPregunta =
|
|
:idPartidaxPregunta
|
|
AND idUsuario = :idUsuario
|
|
");
|
|
|
|
$consulta->bindParam(
|
|
":idPartidaxPregunta",
|
|
$idPartidaxPregunta
|
|
);
|
|
|
|
$consulta->bindParam(
|
|
":idUsuario",
|
|
$idUsuario
|
|
);
|
|
|
|
$consulta->execute();
|
|
|
|
$respuestaExistente =
|
|
$consulta->fetch(PDO::FETCH_ASSOC);
|
|
|
|
if ($respuestaExistente) {
|
|
return [
|
|
"error" =>
|
|
"Ya has respondido esta pregunta."
|
|
];
|
|
}
|
|
|
|
|
|
// Comprobamos si existe ya alguien que haya
|
|
// respondido primero.
|
|
|
|
$consulta = $conexion->prepare("
|
|
SELECT COUNT(*) AS total
|
|
FROM respuestaalmacenada
|
|
WHERE idPartidaxPregunta =
|
|
:idPartidaxPregunta
|
|
AND primero = 1
|
|
");
|
|
|
|
$consulta->bindParam(
|
|
":idPartidaxPregunta",
|
|
$idPartidaxPregunta
|
|
);
|
|
|
|
$consulta->execute();
|
|
|
|
$fila =
|
|
$consulta->fetch(PDO::FETCH_ASSOC);
|
|
|
|
$primero =
|
|
((int)$fila['total'] === 0) ? 1 : 0;
|
|
|
|
|
|
// Guardamos la respuesta.
|
|
|
|
$consulta = $conexion->prepare("
|
|
INSERT INTO respuestaalmacenada
|
|
(
|
|
idPartidaxPregunta,
|
|
idUsuario,
|
|
idRespuesta,
|
|
primero
|
|
)
|
|
VALUES
|
|
(
|
|
:idPartidaxPregunta,
|
|
:idUsuario,
|
|
:idRespuesta,
|
|
:primero
|
|
)
|
|
");
|
|
|
|
$consulta->bindParam(
|
|
":idPartidaxPregunta",
|
|
$idPartidaxPregunta
|
|
);
|
|
|
|
$consulta->bindParam(
|
|
":idUsuario",
|
|
$idUsuario
|
|
);
|
|
|
|
$consulta->bindParam(
|
|
":idRespuesta",
|
|
$idRespuesta
|
|
);
|
|
|
|
$consulta->bindParam(
|
|
":primero",
|
|
$primero
|
|
);
|
|
|
|
$consulta->execute();
|
|
|
|
return [
|
|
"exito" =>
|
|
"La respuesta se ha registrado correctamente.",
|
|
"primero" =>
|
|
$primero == 1
|
|
];
|
|
|
|
} catch (PDOException $error) {
|
|
|
|
return [
|
|
"error" =>
|
|
"Ocurrió un error al registrar la respuesta.",
|
|
"error_tecnico" =>
|
|
$error->getMessage()
|
|
];
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
public function finalizar()
|
|
{
|
|
$conexion = conexionBD();
|
|
|
|
if (is_array($conexion)) {
|
|
return $conexion;
|
|
}
|
|
|
|
try {
|
|
|
|
$consulta = $conexion->prepare("
|
|
UPDATE partida
|
|
SET
|
|
estado = 'finalizada'
|
|
WHERE idPartida = :idPartida
|
|
");
|
|
|
|
$consulta->bindParam(
|
|
":idPartida",
|
|
$this->idPartida
|
|
);
|
|
|
|
$consulta->execute();
|
|
|
|
return [
|
|
"exito" =>
|
|
"La partida ha finalizado correctamente.",
|
|
"estado" =>
|
|
"finalizada"
|
|
];
|
|
|
|
} catch (PDOException $error) {
|
|
|
|
return [
|
|
"error" =>
|
|
"Ocurrió un error al finalizar la partida.",
|
|
"error_tecnico" =>
|
|
$error->getMessage()
|
|
];
|
|
}
|
|
}
|
|
}
|
|
|
|
?>
|
|
|