Compare commits

..

No commits in common. "random-puzzles" and "master" have entirely different histories.

13 changed files with 144 additions and 170 deletions

2
.gitignore vendored
View File

@ -1,4 +1,2 @@
localhost-key.pem
localhost.pem
data/teams.db
data/article.db

View File

@ -2,8 +2,6 @@
**Auteur :** Antoine WAEHREN
Sur cette version, la liste des énigmes n'affiche pas immédiatement toutes les énigmes, uniquement celles qui ont été résolues. L'énigme suivante est choisie aléatoirement.
## Crédits
Ce projet utilise le module QR Scanner de NIMIQ : https://github.com/nimiq/qr-scanner

View File

@ -1,17 +1,30 @@
<?php
require_once "require/base.php";
$team_id = -1;
$art_id = -1;
$article = array();
$database = new Database();
$bonus = $database->getArticleNb(); // L'énigme bonus est la dernière
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 ($database->checkTeamExists($team_id)) {
if (!empty($stmt->fetch())) {
$art_id = htmlspecialchars($_GET["id"]);
$code = htmlspecialchars($_GET["code"]);
$article = $database->getArticle($art_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 = [
@ -20,28 +33,41 @@
];
// Vérification de la combinaison ID énigme + code :
if (!$database->checkPuzzle($art_id, $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é
// 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 (!$database->isPuzzleSolved($art_id, $team_id)) {
$database->solvePuzzle($art_id, $team_id);
}
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(*)"] == $bonus - 1) {
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;
}

View File

@ -1,43 +1,50 @@
<?php
require_once "require/base.php";
$article = array();
$solved = false;
$team_id = -1;
$art_id = -1;
$database = new Database();
$bonus = $database->getArticleNb(); // L'énigme bonus est la dernière
$max_art = $database->getArticleNb();
if (isset($_GET["id"]) && isset($_GET["team"])) {
$art_id = htmlspecialchars($_GET["id"]);
$team_id = htmlspecialchars($_GET["team"]);
// Recherche de l'énigme indiquée dans la base :
$stmt = $database->pdo_article->prepare("SELECT * FROM puzzles WHERE id == :id");
$stmt->bindValue(":id", $art_id);
$stmt->execute();
$article = $stmt->fetch();
// Vérification de l'existence du groupe et de l'énigme :
if (!$database->checkArticleExists($art_id)) {
if (!$database->checkTeamExists($team_id)) {
if (empty($article) || !$database->checkTeamExists($team_id)) {
header("Location: index.php");
die();
}
else if ($art_id == -1) {
header("Location: puzzles.php?team=" . $team_id);
die();
}
else {
header("Location: index.php");
die();
}
}
else {
// On vérifie si l'énigme bonus n'est pas débloquée :
if (!$database->checkTeamBonus($team_id)) {
// Si c'est celle qui est sélectionnée, on choisit une autre énigme :
if ($art_id == $bonus) {
$next_art = $database->getNextPuzzle($team_id);
header("Location: article.php?team=" . $team_id . "&id=" . $next_art);
// Si c'est celle qui est sélectionnée, on retourne à la liste des énigmes :
if ($art_id == $max_art) {
header("Location: puzzles.php?team=" . $team_id);
die();
}
// On retire l'énigme bonus du total :
$max_art--;
}
$article = $database->getArticle($art_id);
$next_art = $database->getNextPuzzle($team_id);
$solved = $database->isPuzzleSolved($art_id, $team_id);
// On indique si l'énigme est résolue :
$stmt = $database->pdo_teams->prepare("SELECT * FROM solved WHERE (team_id == :team_id AND puzzle_id == :puzzle_id)");
$stmt->bindValue(":team_id", $team_id);
$stmt->bindValue(":puzzle_id", $art_id);
$stmt->execute();
$solved = !empty($stmt->fetch());
}
}
@ -66,10 +73,15 @@
<header>
<h1><?= $tr["page_title"]["article"] . $art_id . " : " . $article["title"] ?></h1>
<h2><?= $article["text"] ?></h2>
<?php if ($solved) : ?>
<a href="article.php?team=<?= $team_id ?>&id=<?= $next_art ?>">
<button><?= $tr["article"]["next_but"] ?></button>
<section>
<a href="puzzles.php?team=<?= $team_id ?>">
<button><?= $tr["article"]["back_but"] ?></button>
</a>
<!-- <a href="article.php?team=<?= $team_id ?>&id=<?= ($art_id % $max_art) + 1 ?>">
<button><?= $tr["article"]["next_but"] ?></button>
</a> -->
</section>
<?php if ($solved) : ?>
<p><?= $tr["article"]["success"] ?></p>
<?php else : ?>
<p><?= $tr["article"]["message"] ?></p>

Binary file not shown.

Binary file not shown.

View File

@ -2,9 +2,8 @@
require_once "require/base.php";
$database = new Database();
$teams = $database->getTeams();
// Si l'appareil a déjà choisi une équipe, on le redirige vers une énigme :
// Si l'appareil a déjà choisi une équipe, on le redirige vers la bonne page :
if (isset($_COOKIE["team"])) {
$team_id = htmlspecialchars($_COOKIE["team"]);
@ -12,6 +11,19 @@
header("Location: puzzles.php?team=" . $team_id);
die();
}
else {
$stmt = $database->pdo_teams->prepare("SELECT * FROM teams");
$stmt->execute();
$teams = $stmt->fetchAll();
}
}
// Sinon, on affiche la liste des équipes :
else {
$stmt = $database->pdo_teams->prepare("SELECT * FROM teams");
$stmt->execute();
$teams = $stmt->fetchAll();
}
?>

View File

@ -1,18 +1,25 @@
<?php
require_once "require/base.php";
$puzzles = array();
$team_id = -1;
$database = new Database();
if (isset($_GET["team"])) {
$team_id = htmlspecialchars($_GET["team"]);
if (!$database->checkTeamExists($team_id)) {
// Vérification de l'existence du groupe :
$stmt = $database->pdo_teams->prepare("SELECT * FROM teams WHERE id == :id");
$stmt->bindValue(":id", $team_id);
$stmt->execute();
if (empty($stmt->fetch())) {
header("Location: index.php");
die();
}
else {
if (!isset($_COOKIE["team"])) {
// On crée un cookie pour enregistrer l'équipe sélectionnée :
setcookie(
"team",
@ -23,14 +30,24 @@
false,
false
);
// On vérifie si l'équipe a débloqué l'énigme bonus :
$stmt = $database->pdo_teams->prepare("SELECT * FROM teams WHERE id = :id");
$stmt->bindValue(":id", $team_id);
$stmt->execute();
// Si c'est le cas, on affiche toutes les énigmes :
if ($stmt->fetchAll()[0][1] == 1) {
$stmt = $database->pdo_article->prepare("SELECT * FROM puzzles");
}
$solved = $database->getSolvedPuzzles($team_id);
$puzzles = [];
foreach ($solved as $p) {
array_push($puzzles, $database->getArticle($p));
// Sinon, on cache la bonus :
else {
$stmt = $database->pdo_article->prepare("SELECT * FROM puzzles WHERE id <> 13");
}
$next_art = $database->getNextPuzzle($team_id);
$stmt->execute();
$puzzles = $stmt->fetchAll();
}
}
@ -59,23 +76,12 @@
<h1><?= $tr["page_title"]["puzzles"] . $team_id ?></h1>
</header>
<article>
<?php if ($next_art != -1) : ?>
<a href="article.php?team=<?= $team_id ?>&id=<?= $next_art ?>">
<button><?= $tr["puzzles"]["pz_but"] ?></button>
</a>
<?php if (!empty($puzzles)) : ?>
<p><?= $tr["puzzles"]["message"]?></p>
<?php endif; ?>
<?php else : ?>
<p><?= $tr["puzzles"]["success"]?></p>
<?php endif; ?>
<?php if (!empty($puzzles)) : ?>
<ul>
<?php foreach ($puzzles as $puzzle) : ?>
<li><a href="article.php?team=<?= $team_id ?>&id=<?= $puzzle["id"] ?>"><?= $tr["page_title"]["article"] . $puzzle["id"] . " : " . $puzzle["title"] ?></a></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</article>
</main>
<footer>

View File

@ -45,18 +45,6 @@
}
}
public function getArticle(int $id) {
$stmt = $this->pdo_article->prepare("SELECT * FROM puzzles WHERE id == :id");
$stmt->bindValue(":id", $id);
$stmt->execute();
return $stmt->fetch();
}
public function getArticleNb() {
$query = $this->pdo_article->query("SELECT COUNT(*) FROM puzzles");
return $query->fetch()["COUNT(*)"];
}
public function checkTeamExists(int $id) {
$stmt = $this->pdo_teams->prepare("SELECT * FROM teams WHERE id == :id");
$stmt->bindValue(":id", $id);
@ -64,78 +52,15 @@
return !empty($stmt->fetch());
}
public function checkArticleExists(int $id) {
return !empty($this->getArticle($id));
public function getArticleNb() {
$query = $this->pdo_article->query("SELECT COUNT(*) FROM puzzles");
return $query->fetch()["COUNT(*)"];
}
public function checkPuzzle(int $id, string $code) {
$stmt = $this->pdo_article->prepare("SELECT * FROM puzzles WHERE (id == :id AND code == :code)");
$stmt->bindValue(":id", $id);
$stmt->bindValue(":code", $code);
$stmt->execute();
return !empty($stmt->fetch());
}
public function isPuzzleSolved(int $id, int $team) {
$stmt = $this->pdo_teams->prepare("SELECT * FROM solved WHERE (team_id == :team_id AND puzzle_id == :puzzle_id)");
$stmt->bindValue(":team_id", $team);
$stmt->bindValue(":puzzle_id", $id);
$stmt->execute();
return !empty($stmt->fetch());
}
public function solvePuzzle(int $id, int $team) {
$stmt = $this->pdo_teams->prepare("INSERT INTO solved VALUES (:puzzle_id, :team_id)");
$stmt->bindValue(":puzzle_id", $id);
$stmt->bindValue(":team_id", $team);
$stmt->execute();
}
public function getSolvedPuzzles(int $team) {
function checkTeamBonus(int $team) {
// $team doit être une équipe existante :
$stmt = $this->pdo_teams->prepare("SELECT * FROM solved WHERE team_id == :id");
$stmt->bindValue(":id", $team);
$stmt->execute();
$res = $stmt->fetchAll();
$solved = [];
foreach ($res as $r) {
array_push($solved, $r["puzzle_id"]);
}
return $solved;
}
function getUnsolvedPuzzles(int $team) {
$solved = $this->getSolvedPuzzles($team);
$unsolved = range(1, $this->getArticleNb());
// On supprime les énigmes résolues :
foreach ($solved as $p) {
$key = array_search($p, $unsolved);
array_splice($unsolved, $key, 1);
}
return $unsolved;
}
public function getNextPuzzle(int $team) {
$unsolved = $this->getUnsolvedPuzzles($team);
$next = -1; // Quand il n'y a plus d'autre énigme à résoudre
if (!empty($unsolved)) {
$next = $unsolved[array_rand($unsolved)];
}
return $next;
}
public function checkTeamBonus(int $team) {
// $team doit être une équipe existante :
$stmt = $this->pdo_teams->prepare("SELECT bonus FROM teams WHERE id == :id");
$stmt->bindValue(":id", $team);
$stmt->execute();
return $stmt->fetch()[0] == 1;
}
public function getTeams() {
$stmt = $this->pdo_teams->prepare("SELECT * FROM teams");
$stmt->execute();
return $stmt->fetchAll();
$query = $this->pdo_teams->query("SELECT bonus FROM teams WHERE id = " . $team);
return $query->fetch()[0] == 1;
}
// public function getTeamsNb() {

View File

@ -35,8 +35,6 @@
],
"puzzles" => [
"message" => "Select a puzzle in the list below :",
"success" => "Congratulations, you solved all the puzzles ! You can now select a specific puzzle in the list below :",
"pz_but" => "Solve a puzzle",
"map_desc" => "Here is a map showing the location of the puzzles you already solved :"
],
"nav" => [

View File

@ -35,8 +35,6 @@
],
"puzzles" => [
"message" => "Choisissez une énigme dans la liste ci-dessous :",
"success" => "Félicitations, vous avez résolu toutes les énigmes ! Vous pouvez maintenant sélectionner une énigme en particulier dans la liste ci-dessous :",
"pz_but" => "Résoudre une énigme",
"map_desc" => "Voici la carte des emplacements des énigmes que vous avez résolues :"
],
"nav" => [

View File

@ -149,7 +149,6 @@ main header section {
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
gap: 15px;
}

View File

@ -1,6 +1,8 @@
<?php
require_once "require/base.php";
$team_id = -1;
$database = new Database();
if (isset($_GET["team"])) {