ChasseTresorPange/answer.php

89 lines
3.3 KiB
PHP
Raw Normal View History

<?php
require_once "require/base.php";
$team_id = -1;
$art_id = -1;
$article = array();
$database = new Database();
if (isset($_GET["code"]) && isset($_GET["team"]) && isset($_GET["id"])) {
$team_id = htmlspecialchars($_GET["team"]);
// Recherche du groupe :
$stmt = $database->pdo_teams->prepare("SELECT * FROM teams WHERE id == :id");
$stmt->bindValue(":id", $team_id);
$stmt->execute();
// Vérification de l'existence du groupe :
if (!empty($stmt->fetchAll())) {
$art_id = htmlspecialchars($_GET["id"]);
// Recherche de l'énigme avec son code :
$stmt = $database->pdo_article->prepare("SELECT * FROM puzzles WHERE (id == :id AND code == :code)");
$stmt->bindValue(":id", $art_id);
$stmt->bindValue(":code", htmlspecialchars($_GET["code"]));
$stmt->execute();
$article = $stmt->fetch();
// Données renvoyées :
$data = [
"valid_qr" => false,
"hint" => ""
];
// Vérification de la combinaison ID énigme + code :
if (empty($article)) {
// Si la combinaison ne fonctionne pas, affichage de l'indice :
$stmt = $database->pdo_article->prepare("SELECT * FROM puzzles WHERE id == :id");
$stmt->bindValue(":id", $art_id);
$stmt->execute();
$article = $stmt->fetch();
$data["hint"] = $article["hint"];
}
else {
// On enregistre la réussite dans la table :
// Vérification que ce n'est pas déjà enregistré :
$stmt = $database->pdo_teams->prepare("SELECT * FROM solved WHERE (puzzle_id == :puzzle_id AND team_id == :team_id)");
$stmt->bindValue(":puzzle_id", $art_id);
$stmt->bindValue(":team_id", $team_id);
$stmt->execute();
// Si ce n'est pas le cas, on enregistre :
if (empty($stmt->fetch())) {
$stmt = $database->pdo_teams->prepare("INSERT INTO solved VALUES (:puzzle_id, :team_id)");
$stmt->bindValue(":puzzle_id", $art_id);
$stmt->bindValue(":team_id", $team_id);
$stmt->execute();
// On ajoute l'énigme bonus si toutes les autres sont résolues :
$stmt = $database->pdo_teams->prepare("SELECT COUNT(*) FROM solved WHERE team_id == :team_id");
$stmt->bindValue(":team_id", $team_id);
$stmt->execute();
if ($stmt->fetchAll()[0]["COUNT(*)"] == "12") { // Il y a 12 énigmes sans compter la bonus
$stmt = $database->pdo_teams->prepare("UPDATE teams SET bonus = 1 WHERE id = :team_id");
$stmt->bindValue(":team_id", $team_id);
$stmt->execute();
}
}
$data["valid_qr"] = true;
}
header("Content-Type: application/json; charset=utf-8");
echo json_encode($data);
}
else {
header("Location: index.php");
die();
}
}
else {
header("Location: index.php");
die();
}
?>