From 995e8c4dab68722a85685b6bc9942fc4e22bf0d9 Mon Sep 17 00:00:00 2001 From: dhain Date: Tue, 31 Jan 2023 10:30:01 +0100 Subject: [PATCH] Game only playing when logged in --- frontend/public/game.js | 21 +++++++++++++++------ frontend/src/components/Login.vue | 1 + frontend/src/model/Rest.ts | 2 +- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/frontend/public/game.js b/frontend/public/game.js index 3b51b91..eaecfb2 100644 --- a/frontend/public/game.js +++ b/frontend/public/game.js @@ -43,7 +43,7 @@ var score = 0; var paused; var hasAlreadyScored = false; var hasDied = true; -var ready = true; +var ready = false; function preload(){ font = loadFont(FONT_PATH); @@ -67,6 +67,12 @@ function setup(){ window.dispatchEvent(event); originalSetItem.apply(this, arguments); }; + window.addEventListener("itemInserted", e => { + if(e.key === "frontend-ready"){ + ready = e.value === "true"; + score = 0; + } + }) } function setupObstacleConsts(){ @@ -155,13 +161,15 @@ function collisionCheck(o){ function die(){ if(localStorage.getItem("frontend-ready") == "false") return; - + ready = false; hasDied = true; playTime = Date.now() - startTime; exportToLocalStorage(); setTimeout(function(){ - return ready = true; + if(localStorage.getItem("frontend-ready") == "true"){ + return ready = true; + } }, 1000); } @@ -208,19 +216,20 @@ function checkRaspberryScore(){ } function resetScore(){ - if(!hasDied || localStorage.getItem("frontend-ready") == "false") + if(!hasDied) return; hasDied = false; score = 0; hasAlreadyScored = false; startTime = Date.now(); - exportToLocalStorage(); } function keyPressed(){ - if(!ready) + if(!ready){ return; + } + if(BOOST_KEYS.includes(key.toLowerCase())){ resetScore(); raspberry.boost(); diff --git a/frontend/src/components/Login.vue b/frontend/src/components/Login.vue index 3be8534..ec3a1ce 100644 --- a/frontend/src/components/Login.vue +++ b/frontend/src/components/Login.vue @@ -39,6 +39,7 @@ export default { if (user) { this.$emit('userChange', user); + localStorage.setItem("frontend-ready", "true"); } }, } diff --git a/frontend/src/model/Rest.ts b/frontend/src/model/Rest.ts index 53b902e..04d93cc 100644 --- a/frontend/src/model/Rest.ts +++ b/frontend/src/model/Rest.ts @@ -1,3 +1,3 @@ export class Rest { - static readonly URL = 'http://localhost:3000'; + static readonly URL = 'http://139.144.178.216:3000'; } \ No newline at end of file