Saper dodanie rekordów w cookie
This commit is contained in:
1 parent
9790eb5b02
commit
3d59850313
1 file changed
+223
-34
+223
-34
@@ -17,14 +17,27 @@
|
||||
background-color: darkslategray;
|
||||
}
|
||||
|
||||
.container {
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
main {
|
||||
margin: 1em auto;
|
||||
margin: 1em;
|
||||
width: fit-content;
|
||||
padding: 1em;
|
||||
background-color: rgb(40, 40, 45);
|
||||
color: white;
|
||||
}
|
||||
|
||||
aside {
|
||||
margin: 1em;
|
||||
width: 20rem;
|
||||
padding: 1em;
|
||||
background-color: rgb(40, 40, 45);
|
||||
color: white;
|
||||
}
|
||||
|
||||
hr {
|
||||
margin: .5em 0;
|
||||
}
|
||||
@@ -90,6 +103,7 @@
|
||||
.timer {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
.timer>p {
|
||||
@@ -99,45 +113,196 @@
|
||||
.timer>p:last-child {
|
||||
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>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<main>
|
||||
<h1>Saperrro</h1>
|
||||
<div class="settings">
|
||||
<hr>
|
||||
<p>Ustawienia</p>
|
||||
<div class="setting">
|
||||
<label for="width">Szerokość planszy</label>
|
||||
<input type="number" name="width" id="width" size="2" value="10">
|
||||
<div class="container">
|
||||
<main>
|
||||
<h1>Saperrro</h1>
|
||||
<div class="settings">
|
||||
<hr>
|
||||
<p>Ustawienia</p>
|
||||
<div class="setting">
|
||||
<label for="width">Szerokość planszy</label>
|
||||
<input type="number" name="width" id="width" size="2" value="10">
|
||||
</div>
|
||||
<div class="setting">
|
||||
<label for="height">Wysokość planszy</label>
|
||||
<input type="number" name="height" id="height" size="2" value="10">
|
||||
</div>
|
||||
<div class="setting">
|
||||
<label for="mines">Ilość min</label>
|
||||
<input type="number" name="mines" id="mines" size="2" value="10">
|
||||
</div>
|
||||
<div class="setting">
|
||||
<button id="start">Graj</button>
|
||||
</div>
|
||||
<hr style="margin-bottom: 1.5rem;">
|
||||
</div>
|
||||
<div class="setting">
|
||||
<label for="height">Wysokość planszy</label>
|
||||
<input type="number" name="height" id="height" size="2" value="10">
|
||||
<div class="timer" id="timer-wrapper">
|
||||
<p>Twój czas:</p>
|
||||
<p id="timer-time">0:00</p>
|
||||
</div>
|
||||
<div class="setting">
|
||||
<label for="mines">Ilość min</label>
|
||||
<input type="number" name="mines" id="mines" size="2" value="10">
|
||||
<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="setting">
|
||||
<button id="start">Graj</button>
|
||||
<div class="game">
|
||||
<table id="plansza">
|
||||
</table>
|
||||
</div>
|
||||
<hr>
|
||||
</div>
|
||||
<div class="timer" id="timer-wrapper">
|
||||
<p>Twój czas:</p>
|
||||
<p id="timer-time">0:00</p>
|
||||
</div>
|
||||
<div class="game">
|
||||
<table id="plansza">
|
||||
</table>
|
||||
</div>
|
||||
<div class="messages">
|
||||
<hr>
|
||||
<p id="message">Podaj wymiary planszy i rozpocznij grę.</p>
|
||||
</div>
|
||||
</main>
|
||||
<div class="messages">
|
||||
<hr>
|
||||
<p id="message">Podaj wymiary planszy i rozpocznij grę.</p>
|
||||
</div>
|
||||
</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>
|
||||
// wymiary gry
|
||||
@@ -202,10 +367,18 @@
|
||||
if (document.getElementById(`p${x}-${y}`).classList.contains("odkryte-pole")) { return }
|
||||
|
||||
if (plansza[y][x].mina) {
|
||||
alert("Koniec gry, przegrana!")
|
||||
document.getElementById('message').innerText = "Koniec gry, przegrana!";
|
||||
document.getElementById(`p${x}-${y}`).style.backgroundColor = "red";
|
||||
gra = false
|
||||
clearInterval(interwal);
|
||||
|
||||
let pola = plansza.flat();
|
||||
|
||||
pola.forEach(pole => {
|
||||
if (pole.mina) {
|
||||
document.getElementById(`p${pole.x}-${pole.y}`).style.backgroundColor = 'red';
|
||||
}
|
||||
});
|
||||
} else {
|
||||
btn.classList.remove("nieodkryte-pole")
|
||||
btn.classList.add("odkryte-pole")
|
||||
@@ -215,9 +388,18 @@
|
||||
}
|
||||
odkryte_pola++
|
||||
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
|
||||
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
|
||||
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 h_t = parseInt(document.getElementById("height").value);
|
||||
let m_t = parseInt(document.getElementById("mines").value);
|
||||
@@ -340,6 +527,8 @@
|
||||
h = h_t
|
||||
m = m_t
|
||||
|
||||
document.getElementById('message').innerText = 'Powodzenia!';
|
||||
|
||||
przygotuj()
|
||||
}
|
||||
</script>
|
||||
|
||||
Reference in new issue
Block a user