From 014798836822d8e83df39d69ed7f511dc201a3fb Mon Sep 17 00:00:00 2001 From: Rushil Umaretiya Date: Tue, 13 Sep 2022 02:19:22 -0400 Subject: [PATCH] feat: added data update and optimized reset --- index.js | 5 +++++ public/js/admin.js | 24 ++++++++++++++++++++++-- public/js/script.js | 8 ++++++++ 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 512583e..d29f07a 100644 --- a/index.js +++ b/index.js @@ -36,6 +36,11 @@ io.on('connection', (socket) => { console.log("reset") io.emit("reset") }) + + socket.on("do-update", (data) => { + console.log("update"); + io.emit("update", data); + }); }); diff --git a/public/js/admin.js b/public/js/admin.js index bb65ae9..ef9521b 100644 --- a/public/js/admin.js +++ b/public/js/admin.js @@ -1,5 +1,25 @@ let socket = io(); +const _default = { + yearA: 2022, + yearB: 2022, + scoreA: 0, + scoreB: 0, + current: 0, + question: "Waiting to continue...", + answers: [], +}; + + +if (sessionStorage.getItem("data") === null) sessionStorage.setItem("data", JSON.stringify(data)) +else data = sessionStorage.getItem("data") + document.getElementById("reset").addEventListener("click", () => { - socket.emit("do-reset") -}) \ No newline at end of file + sessionStorage.setItem("data", JSON.stringify(_default)) + update() +}); + + +var update = () => { + socket.emit("do-update", JSON.parse(sessionStorage.getItem("data"))) +} \ No newline at end of file diff --git a/public/js/script.js b/public/js/script.js index c93a584..16dc188 100644 --- a/public/js/script.js +++ b/public/js/script.js @@ -45,4 +45,12 @@ var reset = () => { socket.on("reset", () => { reset() +}) + +socket.on("update", (data) => { + setYear(data["yearA"], data["yearB"]) + setScore(data["scoreA"], data["scoreB"]) + setCurrent(data["current"]) + setQuestion(data["question"]) + data["answers"] }) \ No newline at end of file