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

View File

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

View File

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

View File

@ -550,9 +550,9 @@ function gameOver() {
if (stats.score == stats.highScore) {
localStorage.setItem('highScore', stats.highScore)
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 FD = new FormData()
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") || "")
if (player.length) {
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 {
alert(info)
retry++
if (retry < RETRIES)
XHR.send(FD)
else
alert(info)
}
})
XHR.addEventListener('error', function(event) {
alert(info)
})
XHR.open('POST', 'intop10.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.open('POST', 'inleaderboard.php')
XHR.send(FD)
}

View File

@ -2,7 +2,7 @@
<html>
<head>
<meta charset="utf-8" />
<title>Webtris</title>
<title>Meilleurs scores - Webtris</title>
<link rel="icon" type="image/png" href="favicon.png">
<link rel="stylesheet" type="text/css" href="css/style.css" />
</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";
}
$top10->closeCursor();
$db->close();
?>
</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>
</html>

View File

@ -8,6 +8,14 @@
}
$query = $db->prepare('INSERT INTO `leaderboard` (`player`, `score`) VALUES (:player, :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 {
header($_SERVER["SERVER_PROTOCOL"] . " 405 Method Not Allowed", true, 405);
}

View File

@ -5,11 +5,11 @@
<title>Webtris</title>
<link rel="icon" type="image/png" href="favicon.png">
<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>
<body>
<h1>WEBTRIS</h1>
<div id="actions">
<div id="settings">
<?php
function addButton($action, $label) {
?>