Saper dodanie rekordów w cookie
This commit is contained in:
1 parent
9790eb5b02
commit
3d59850313
1 file changed
+193
-4
+193
-4
@@ -17,14 +17,27 @@
|
|||||||
background-color: darkslategray;
|
background-color: darkslategray;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.container {
|
||||||
|
display: flex;
|
||||||
|
justify-content: center;
|
||||||
|
}
|
||||||
|
|
||||||
main {
|
main {
|
||||||
margin: 1em auto;
|
margin: 1em;
|
||||||
width: fit-content;
|
width: fit-content;
|
||||||
padding: 1em;
|
padding: 1em;
|
||||||
background-color: rgb(40, 40, 45);
|
background-color: rgb(40, 40, 45);
|
||||||
color: white;
|
color: white;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
aside {
|
||||||
|
margin: 1em;
|
||||||
|
width: 20rem;
|
||||||
|
padding: 1em;
|
||||||
|
background-color: rgb(40, 40, 45);
|
||||||
|
color: white;
|
||||||
|
}
|
||||||
|
|
||||||
hr {
|
hr {
|
||||||
margin: .5em 0;
|
margin: .5em 0;
|
||||||
}
|
}
|
||||||
@@ -90,6 +103,7 @@
|
|||||||
.timer {
|
.timer {
|
||||||
display: flex;
|
display: flex;
|
||||||
justify-content: space-between;
|
justify-content: space-between;
|
||||||
|
margin-bottom: 1rem;
|
||||||
}
|
}
|
||||||
|
|
||||||
.timer>p {
|
.timer>p {
|
||||||
@@ -99,10 +113,31 @@
|
|||||||
.timer>p:last-child {
|
.timer>p:last-child {
|
||||||
font-weight: bold;
|
font-weight: bold;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ol {
|
||||||
|
margin-top: .5rem;
|
||||||
|
margin-left: 1.5rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
ol>li:nth-child(1) {
|
||||||
|
color: gold;
|
||||||
|
font-size: 1.5rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
ol>li:nth-child(2) {
|
||||||
|
color: silver;
|
||||||
|
font-size: 1.25rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
ol>li:nth-child(3) {
|
||||||
|
color: burlywood;
|
||||||
|
font-size: 1.125rem;
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
<body>
|
<body>
|
||||||
|
<div class="container">
|
||||||
<main>
|
<main>
|
||||||
<h1>Saperrro</h1>
|
<h1>Saperrro</h1>
|
||||||
<div class="settings">
|
<div class="settings">
|
||||||
@@ -123,12 +158,16 @@
|
|||||||
<div class="setting">
|
<div class="setting">
|
||||||
<button id="start">Graj</button>
|
<button id="start">Graj</button>
|
||||||
</div>
|
</div>
|
||||||
<hr>
|
<hr style="margin-bottom: 1.5rem;">
|
||||||
</div>
|
</div>
|
||||||
<div class="timer" id="timer-wrapper">
|
<div class="timer" id="timer-wrapper">
|
||||||
<p>Twój czas:</p>
|
<p>Twój czas:</p>
|
||||||
<p id="timer-time">0:00</p>
|
<p id="timer-time">0:00</p>
|
||||||
</div>
|
</div>
|
||||||
|
<div class="timer" id="nickname-wrapper">
|
||||||
|
<p>Twój nick:</p>
|
||||||
|
<input type="text" name="nickname" id="nickname-box" style="text-align: right;">
|
||||||
|
</div>
|
||||||
<div class="game">
|
<div class="game">
|
||||||
<table id="plansza">
|
<table id="plansza">
|
||||||
</table>
|
</table>
|
||||||
@@ -139,6 +178,132 @@
|
|||||||
</div>
|
</div>
|
||||||
</main>
|
</main>
|
||||||
|
|
||||||
|
<aside>
|
||||||
|
<h2>Rekordy dla <span id="mode-name"></span></h2>
|
||||||
|
<ol id="leaderboard"></ol>
|
||||||
|
</aside>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
function get_cookie(cname) {
|
||||||
|
let name = cname + "=";
|
||||||
|
let ca = document.cookie.split(";");
|
||||||
|
for (let i = 0; i < ca.length; i++) {
|
||||||
|
let c = ca[i];
|
||||||
|
while (c.charAt(0) == " ") {
|
||||||
|
c = c.substring(1);
|
||||||
|
}
|
||||||
|
if (c.indexOf(name) == 0) {
|
||||||
|
let cookie_string = c.substring(name.length, c.length);
|
||||||
|
let cookie_array = cookie_string.split(" ");
|
||||||
|
if (cookie_array.length < 2) { return [] }
|
||||||
|
if (cookie_array.length % 2 != 0) { return [] }
|
||||||
|
|
||||||
|
let arr = [];
|
||||||
|
let obj = {};
|
||||||
|
|
||||||
|
for (let i = 0; i < cookie_array.length; i += 2) {
|
||||||
|
obj = {
|
||||||
|
nickname: decodeURIComponent(cookie_array[i]),
|
||||||
|
score: decodeURIComponent(cookie_array[i + 1])
|
||||||
|
};
|
||||||
|
arr.push(obj);
|
||||||
|
}
|
||||||
|
|
||||||
|
return arr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
function set_cookie(name, nickname_array, time_array) {
|
||||||
|
let cookie_string = '';
|
||||||
|
let len = Math.min(nickname_array.length, time_array.length);
|
||||||
|
|
||||||
|
for (let i = 0; i < len; i++) {
|
||||||
|
cookie_string += encodeURIComponent(nickname_array[i]) + ' ' + encodeURIComponent(time_array[i]) + ' ';
|
||||||
|
}
|
||||||
|
|
||||||
|
cookie_string = cookie_string.trim();
|
||||||
|
document.cookie = name + "=" + cookie_string + ";";
|
||||||
|
}
|
||||||
|
|
||||||
|
function read_settings() {
|
||||||
|
// później: funkcja posłuży do zapamiętania ustawień rozgrywki (wysokość, szerokość, ilość min, nick)
|
||||||
|
}
|
||||||
|
|
||||||
|
function update_leaderboard() {
|
||||||
|
let w = document.getElementById('width').value;
|
||||||
|
let h = document.getElementById('height').value;
|
||||||
|
let m = document.getElementById('mines').value;
|
||||||
|
|
||||||
|
document.getElementById('mode-name').innerText = `${w}x${h}:${m}`;
|
||||||
|
|
||||||
|
let lb = get_cookie(`R-${w}-${h}-${m}`);
|
||||||
|
let html = '';
|
||||||
|
|
||||||
|
if (lb.length < 1) {
|
||||||
|
html = 'Nikt jeszcze w to nie grał!';
|
||||||
|
document.getElementById('leaderboard').innerHTML = html;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (let i = 0; i < lb.length; i++) {
|
||||||
|
html += `<li>${lb[i].score}: ${lb[i].nickname}</li>`;
|
||||||
|
}
|
||||||
|
|
||||||
|
document.getElementById('leaderboard').innerHTML = html;
|
||||||
|
}
|
||||||
|
update_leaderboard();
|
||||||
|
|
||||||
|
document.getElementById('width').onchange = update_leaderboard;
|
||||||
|
document.getElementById('height').onchange = update_leaderboard;
|
||||||
|
document.getElementById('mines').onchange = update_leaderboard;
|
||||||
|
|
||||||
|
function leaderboard_candidate(w, h, m, n, t) { // width, height, mines, nick, time
|
||||||
|
let lb = get_cookie(`R-${w}-${h}-${m}`);
|
||||||
|
|
||||||
|
lb.push({ nickname: n, score: t });
|
||||||
|
|
||||||
|
if (lb.length > 1) {
|
||||||
|
lb = lb.sort(function (a, b) {
|
||||||
|
let a_split = a.score.split(":");
|
||||||
|
let b_split = b.score.split(":");
|
||||||
|
a_split[0] = parseInt(a_split[0]);
|
||||||
|
a_split[1] = parseInt(a_split[1]);
|
||||||
|
b_split[0] = parseInt(b_split[0]);
|
||||||
|
b_split[1] = parseInt(b_split[1]);
|
||||||
|
|
||||||
|
if (a_split[0] > b_split[0]) {
|
||||||
|
return 1;
|
||||||
|
} else if (a_split[0] < b_split[0]) {
|
||||||
|
return -1;
|
||||||
|
} else if (a_split[0] == b_split[0]) {
|
||||||
|
if (a_split[1] > b_split[1]) {
|
||||||
|
return 1;
|
||||||
|
} else {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
let nickname_array = [];
|
||||||
|
let time_array = [];
|
||||||
|
|
||||||
|
let best_of_me = Math.min(lb.length, 10);
|
||||||
|
|
||||||
|
for (let i = 0; i < best_of_me; i++) {
|
||||||
|
nickname_array.push(lb[i].nickname);
|
||||||
|
time_array.push(lb[i].score);
|
||||||
|
}
|
||||||
|
|
||||||
|
set_cookie(`R-${w}-${h}-${m}`, nickname_array, time_array);
|
||||||
|
|
||||||
|
update_leaderboard();
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
// wymiary gry
|
// wymiary gry
|
||||||
let w = 0
|
let w = 0
|
||||||
@@ -202,10 +367,18 @@
|
|||||||
if (document.getElementById(`p${x}-${y}`).classList.contains("odkryte-pole")) { return }
|
if (document.getElementById(`p${x}-${y}`).classList.contains("odkryte-pole")) { return }
|
||||||
|
|
||||||
if (plansza[y][x].mina) {
|
if (plansza[y][x].mina) {
|
||||||
alert("Koniec gry, przegrana!")
|
document.getElementById('message').innerText = "Koniec gry, przegrana!";
|
||||||
document.getElementById(`p${x}-${y}`).style.backgroundColor = "red";
|
document.getElementById(`p${x}-${y}`).style.backgroundColor = "red";
|
||||||
gra = false
|
gra = false
|
||||||
clearInterval(interwal);
|
clearInterval(interwal);
|
||||||
|
|
||||||
|
let pola = plansza.flat();
|
||||||
|
|
||||||
|
pola.forEach(pole => {
|
||||||
|
if (pole.mina) {
|
||||||
|
document.getElementById(`p${pole.x}-${pole.y}`).style.backgroundColor = 'red';
|
||||||
|
}
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
btn.classList.remove("nieodkryte-pole")
|
btn.classList.remove("nieodkryte-pole")
|
||||||
btn.classList.add("odkryte-pole")
|
btn.classList.add("odkryte-pole")
|
||||||
@@ -215,9 +388,18 @@
|
|||||||
}
|
}
|
||||||
odkryte_pola++
|
odkryte_pola++
|
||||||
if (odkryte_pola == (w * h) - m) {
|
if (odkryte_pola == (w * h) - m) {
|
||||||
alert("Koniec gry, wygrana!")
|
document.getElementById('message').innerText = "Koniec gry, wygrana!";
|
||||||
|
leaderboard_candidate(w, h, m, document.getElementById("nickname-box").value, document.getElementById("timer-time").innerText);
|
||||||
gra = false
|
gra = false
|
||||||
clearInterval(interwal);
|
clearInterval(interwal);
|
||||||
|
|
||||||
|
let pola = plansza.flat();
|
||||||
|
|
||||||
|
pola.forEach(pole => {
|
||||||
|
if (pole.mina) {
|
||||||
|
document.getElementById(`p${pole.x}-${pole.y}`).style.backgroundColor = 'red';
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -319,6 +501,11 @@
|
|||||||
|
|
||||||
// funkcja startu gry
|
// funkcja startu gry
|
||||||
document.getElementById("start").onclick = function () {
|
document.getElementById("start").onclick = function () {
|
||||||
|
if (document.getElementById("nickname-box").value == '') {
|
||||||
|
document.getElementById('message').innerText = 'Wprowadź nick!';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
let w_t = parseInt(document.getElementById("width").value);
|
let w_t = parseInt(document.getElementById("width").value);
|
||||||
let h_t = parseInt(document.getElementById("height").value);
|
let h_t = parseInt(document.getElementById("height").value);
|
||||||
let m_t = parseInt(document.getElementById("mines").value);
|
let m_t = parseInt(document.getElementById("mines").value);
|
||||||
@@ -340,6 +527,8 @@
|
|||||||
h = h_t
|
h = h_t
|
||||||
m = m_t
|
m = m_t
|
||||||
|
|
||||||
|
document.getElementById('message').innerText = 'Powodzenia!';
|
||||||
|
|
||||||
przygotuj()
|
przygotuj()
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
Reference in new issue
Block a user