From cab3bad62ea06a1290e4c91ddae6b017f45b8018 Mon Sep 17 00:00:00 2001 From: ElGurkus Date: Fri, 6 Nov 2020 10:42:32 +0100 Subject: [PATCH] changed code -> Highscore with cookies implemented, am done changed index -> minor navbar tweaks changed style -> minor tweaks --- code.js | 38 ++++++++++++++++++++++++++++++++++++++ index.html | 4 +--- style.css | 4 ++-- 3 files changed, 41 insertions(+), 5 deletions(-) diff --git a/code.js b/code.js index 828529d..651b6dd 100644 --- a/code.js +++ b/code.js @@ -25,11 +25,27 @@ let scoreO = 0; let scoreCounterO = 100; let gameOver = false; let winner; +let highScore; +let allowHSCookies; +let saveChoiceCookie; let somethingIsSelected = false; document.getElementById("scoreCounter").innerText = "Score counter: 100"; document.getElementById("scoreX").innerText = "Score X: 0"; document.getElementById("scoreO").innerText = "Score O: 0"; +if(getCookie("saveChoiceAllowed") !== "true") { + allowHSCookies = confirm("Do you allow the use of Cookies to store the local Highscore? \nNote, that existing Cookies will not be deleted"); + saveChoiceCookie = confirm("Do you allow a Cookie to save that answer?"); + if (saveChoiceCookie) { + document.cookie = "cookiesAllowed=" + allowHSCookies + "; expires=Thu, 5 Dec 2030 12:00:00 UTC"; + document.cookie = "saveChoiceAllowed=" + saveChoiceCookie + "; expires=Thu, 5 Dec 2030 12:00:00 UTC"; + } +} +allowHSCookies = getCookie("cookiesAllowed"); +if(allowHSCookies){ + highScore = getCookie("highScore"); +} +document.getElementById("highScore").innerText = "Highscore: " + highScore; for(let i = 0; i < 64; i++){ fieldId = i.toString(); document.getElementById("field"+fieldId).selected = 0; //Sorgt dafür dass alle Felder die selected variable haben und diese 0 ist @@ -429,6 +445,13 @@ function checkWinCondition(){ document.getElementById("field"+fieldId).style.backgroundColor = "rgb(33,157,35, 0.3)" } } + if(allowHSCookies && parseInt(highScore) < scoreX){ + document.cookie = "highScore=" + scoreX + "; expires=Thu, 5 Dec 2030 12:00:00 UTC"; + } else if(allowHSCookies && parseInt(highScore) < scoreO){ + document.cookie = "highScore=" + scoreO + "; expires=Thu, 5 Dec 2030 12:00:00 UTC"; + } + highScore = getCookie("highScore"); + document.getElementById("highScore").innerText = "Highscore: " + highScore; } } function score (field){ @@ -456,4 +479,19 @@ function score (field){ } document.getElementById("scoreO").innerText = "Score O: " + scoreO; document.getElementById("scoreX").innerText = "Score X: " + scoreX; +} +function getCookie(cname) { + let name = cname + "="; + let decodedCookie = decodeURIComponent(document.cookie); + let ca = decodedCookie.split(';'); + for(let i = 0; i diff --git a/style.css b/style.css index 46e4312..ff8a36a 100644 --- a/style.css +++ b/style.css @@ -5,7 +5,7 @@ ul { overflow: hidden; background-color: #333; height: auto; - width: 1000px; + min-width: 600px; position: absolute; top: 5%; } @@ -19,7 +19,7 @@ li:last-child { border-right: none; } -li a, #turnTotal, #turnPlayer, #scoreCounter, #scoreO, #scoreX { +li a, #turnTotal, #turnPlayer, #scoreCounter, #scoreO, #scoreX, #highScore { display: block; color: white; text-align: center;