remove records out of top 10

This commit is contained in:
Adrien MALINGREY 2019-11-01 13:17:01 +01:00
parent 5de5f03b3c
commit f12ff135fa
8 changed files with 67 additions and 35 deletions

View File

@ -53,7 +53,8 @@ a:active {
border-style: inset; border-style: inset;
} }
#actions { #settings {
/*visibility: hidden;*/
display: grid; display: grid;
grid-template-columns: repeat(4, 1fr); grid-template-columns: repeat(4, 1fr);
grid-gap: 2vmin; grid-gap: 2vmin;
@ -62,13 +63,21 @@ a:active {
justify-items: left; justify-items: left;
} }
.flex-container {
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: center;
margin: 2vmin;
}
#button-link { #button-link {
text-align: center; text-align: center;
font-weight: bold; font-weight: bold;
margin: 3vmin; margin: 3vmin;
} }
#container { #grid-container {
position: relative; position: relative;
display: grid; display: grid;
width: 72vmin; width: 72vmin;

View File

@ -9,7 +9,7 @@
</head> </head>
<body> <body>
<h1>WEBTRIS</h1> <h1>WEBTRIS</h1>
<div id="container"> <div id="grid-container">
<?php <?php
function echoTable($id, $rows, $columns) { function echoTable($id, $rows, $columns) {
echo " <table id='$id'>\n"; echo " <table id='$id'>\n";
@ -36,11 +36,17 @@
</table> </table>
<div id="message"></div> <div id="message"></div>
</div> </div>
<div id="button-link"> <!- settings ->
<a href="options.php" target="_blank">OPTIONS</a> <div class="flex-container">
</div> <div id="button-link">
<div id="button-link"> <a href="options.php" target="_blank">OPTIONS</a>
<a href="index.php">REJOUER</a> </div>
<div id="button-link">
<a href="leaderboard.php" target="_blank">TABLEAU DE SCORES</a>
</div>
<div id="button-link">
<a href="index.php">REJOUER</a>
</div>
</div> </div>
</body> </body>
</html> </html>

View File

@ -6,14 +6,14 @@
} catch (Exception $e) { } catch (Exception $e) {
die('Erreur : ' . $e->getMessage()); die('Erreur : ' . $e->getMessage());
} }
$query = $db->prepare('SELECT player, score FROM `leaderboard` ORDER BY score DESC LIMIT 10;'); $entryScore = (int) $db->query('SELECT score FROM `leaderboard` ORDER BY score DESC LIMIT 9, 1;')->fetch()['score'];
$entryScore = $query->fetch();
$query->closeCursor();
$score = (int) $_POST['score']; $score = (int) $_POST['score'];
if ($score > $entryScore) if ($score > $entryScore)
echo "true"; echo "true";
else else
echo "false"; echo "false";
$query->closeCursor();
$db->close();
} else { } else {
header($_SERVER["SERVER_PROTOCOL"] . " 405 Method Not Allowed", true, 405); header($_SERVER["SERVER_PROTOCOL"] . " 405 Method Not Allowed", true, 405);
} }

View File

@ -550,9 +550,9 @@ function gameOver() {
if (stats.score == stats.highScore) { if (stats.score == stats.highScore) {
localStorage.setItem('highScore', stats.highScore) localStorage.setItem('highScore', stats.highScore)
info += "\nBravo ! Vous avez battu votre précédent record." info += "\nBravo ! Vous avez battu votre précédent record."
} else }
var info = `GAME OVER\nScore : ${stats.score}`
var retry = 0
var XHR = new XMLHttpRequest() var XHR = new XMLHttpRequest()
var FD = new FormData() var FD = new FormData()
FD.append("score", stats.score) FD.append("score", stats.score)
@ -561,32 +561,32 @@ function gameOver() {
var player = prompt(info + "\nBravo ! Vous êtes dans le Top 10.\nEntrez votre nom pour publier votre score :" , localStorage.getItem("name") || "") var player = prompt(info + "\nBravo ! Vous êtes dans le Top 10.\nEntrez votre nom pour publier votre score :" , localStorage.getItem("name") || "")
if (player.length) { if (player.length) {
localStorage.setItem("player", player) localStorage.setItem("player", player)
postScore(player, stats.score) XHR = new XMLHttpRequest()
FD = new FormData()
FD.append("player", player)
FD.append("score", stats.score)
XHR.addEventListener('load', function(event) {
open("leaderboard.php")
})
XHR.addEventListener('error', function(event) {
if (confirm('Erreur de connexion.\nRéessayer ?'))
XHR.send(FD)
})
XHR.open('POST', 'publish.php')
XHR.send(FD)
} }
} else { } else {
alert(info) retry++
if (retry < RETRIES)
XHR.send(FD)
else
alert(info)
} }
}) })
XHR.addEventListener('error', function(event) { XHR.addEventListener('error', function(event) {
alert(info) alert(info)
}) })
XHR.open('POST', 'intop10.php') XHR.open('POST', 'inleaderboard.php')
XHR.send(FD)
}
function postScore(player, score) {
var XHR = new XMLHttpRequest()
var FD = new FormData()
FD.append("player", player)
FD.append("score", stats.score)
XHR.addEventListener('load', function(event) {
open("leaderboard.php")
})
XHR.addEventListener('error', function(event) {
if (confirm('Erreur de connexion.\nRéessayer ?'))
postScore(player, score)
})
XHR.open('POST', 'publish.php')
XHR.send(FD) XHR.send(FD)
} }

View File

@ -2,7 +2,7 @@
<html> <html>
<head> <head>
<meta charset="utf-8" /> <meta charset="utf-8" />
<title>Webtris</title> <title>Meilleurs scores - Webtris</title>
<link rel="icon" type="image/png" href="favicon.png"> <link rel="icon" type="image/png" href="favicon.png">
<link rel="stylesheet" type="text/css" href="css/style.css" /> <link rel="stylesheet" type="text/css" href="css/style.css" />
</head> </head>
@ -22,7 +22,16 @@
echo ' <tr><th class="name">' . $i . '<td class="player">' . $row['player'] . '</td><td class="value">' . $row['score'] . "</td></tr>\n"; echo ' <tr><th class="name">' . $i . '<td class="player">' . $row['player'] . '</td><td class="value">' . $row['score'] . "</td></tr>\n";
} }
$top10->closeCursor(); $top10->closeCursor();
$db->close();
?> ?>
</table> </table>
<div class="flex-container">
<div id="button-link">
<a href="options.php" target="_blank">OPTIONS</a>
</div>
<div id="button-link">
<a href="index.php">REJOUER</a>
</div>
</div>
</body> </body>
</html> </html>

View File

@ -8,6 +8,14 @@
} }
$query = $db->prepare('INSERT INTO `leaderboard` (`player`, `score`) VALUES (:player, :score);'); $query = $db->prepare('INSERT INTO `leaderboard` (`player`, `score`) VALUES (:player, :score);');
$query->execute(array("player" => strip_tags($_POST['player']), "score" => (int) $_POST['score'])); $query->execute(array("player" => strip_tags($_POST['player']), "score" => (int) $_POST['score']));
$RowsToDelete = $db->query('SELECT id FROM `leaderboard` ORDER BY score DESC LIMIT 10, 10;');
while($row = $RowsToDelete->fetch()) {
$id = $row['id'];
$db->query("DELETE FROM `leaderboard` WHERE id=" . $row['id'] . ";");
}
$row->closeCursor();
$db->close();
} else { } else {
header($_SERVER["SERVER_PROTOCOL"] . " 405 Method Not Allowed", true, 405); header($_SERVER["SERVER_PROTOCOL"] . " 405 Method Not Allowed", true, 405);
} }

View File

@ -5,11 +5,11 @@
<title>Webtris</title> <title>Webtris</title>
<link rel="icon" type="image/png" href="favicon.png"> <link rel="icon" type="image/png" href="favicon.png">
<link rel="stylesheet" type="text/css" href="css/style.css" /> <link rel="stylesheet" type="text/css" href="css/style.css" />
<script type="text/javascript" src="js/index.js"></script> <script type="text/javascript" src="js/options.js"></script>
</head> </head>
<body> <body>
<h1>WEBTRIS</h1> <h1>WEBTRIS</h1>
<div id="actions"> <div id="settings">
<?php <?php
function addButton($action, $label) { function addButton($action, $label) {
?> ?>