summaryrefslogtreecommitdiff
path: root/src/match_up/static/custom.js
diff options
context:
space:
mode:
authorKaran Jayachandra <mail@karanjayachandra.com>2024-11-15 17:26:47 +0000
committerKaran Jayachandra <mail@karanjayachandra.com>2024-11-15 17:26:47 +0000
commit7b7f8cdef5f035ccdab714059c1a6c54b80c4ba3 (patch)
tree2d7ea5de37f2bda57be419b4aa37971afa09eab5 /src/match_up/static/custom.js
parentf68c54ae8f921c23851680b2501fac8bda1b4010 (diff)
parent7b8bbe6db3680af65c3701afbcd31ac1f61773af (diff)
Merge branch 'develop' into 'main'2.0
Database and javascript timer implemented See merge request KaranJayachandra/match_up!11
Diffstat (limited to 'src/match_up/static/custom.js')
-rw-r--r--src/match_up/static/custom.js87
1 files changed, 87 insertions, 0 deletions
diff --git a/src/match_up/static/custom.js b/src/match_up/static/custom.js
new file mode 100644
index 0000000..2e4b049
--- /dev/null
+++ b/src/match_up/static/custom.js
@@ -0,0 +1,87 @@
+let minsPerRound = Number(sessionStorage.getItem("minsPerRound")) || 12;
+let timerRunningFlag = Number(sessionStorage.getItem("timerRunningFlag")) || 0;
+let deadline = Number(sessionStorage.getItem("deadline")) || new Date().getTime();
+let clock = 0;
+
+function setTimer(m, s) {
+ let timer = document.getElementById("timer-display");
+ m = m < 10 ? ("0" + m) : m;
+ s = s < 10 ? ("0" + s) : s;
+ timer.value = `${m}:${s}`;
+}
+
+function setSessionData() {
+ sessionStorage.setItem("deadline", deadline);
+ sessionStorage.setItem("minsPerRound", minsPerRound);
+ sessionStorage.setItem("timerRunningFlag", timerRunningFlag);
+}
+
+function resetTimer() {
+ clearInterval(clock);
+ setTimer(minsPerRound, 0);
+ timerRunningFlag = 0;
+ setSessionData();
+}
+
+function updateTimer(milliseconds) {
+ const m = Math.floor((milliseconds % (1000 * 60 * 60)) / (1000 * 60));
+ const s = Math.floor((milliseconds % (1000 * 60)) / 1000);
+ setTimer(m, s);
+}
+
+function refreshTimer() {
+ const now = new Date().getTime();
+ const millisecondsToDeadline = deadline - now;
+ if (millisecondsToDeadline > 0) {
+ updateTimer(millisecondsToDeadline);
+ return;
+ }
+ resetTimer();
+ const notification = new Notyf({
+ duration: 30 * 1000,
+ position: {x: 'center', y: 'top'},
+ types: [{
+ type: 'success',
+ background: 'indianred',
+ duration: 1000 * 1000,
+ dismissible: true
+ }]
+ });
+ notification.success("Round completed");
+}
+
+function startTimer(fresh) {
+ if (fresh) {
+ now = new Date().getTime();
+ const millisecondsToDeadline = minsPerRound * 60 * 1000;
+ deadline = now + millisecondsToDeadline;
+ }
+ timerRunningFlag = 1;
+ setSessionData();
+ clock = setInterval(refreshTimer, 1000);
+}
+
+if (timerRunningFlag) startTimer(0); else resetTimer();
+
+document.getElementById("timer-start").addEventListener('click', ()=> {
+ if (timerRunningFlag != 0) return;
+ startTimer(1);
+});
+
+document.getElementById("timer-stop").addEventListener('click', ()=>{
+ resetTimer();
+});
+
+document.getElementById("timer-inc").addEventListener('click', ()=> {
+ if (timerRunningFlag == 0) {
+ minsPerRound += 1;
+ resetTimer();
+ }
+});
+
+document.getElementById("timer-dec").addEventListener('click', ()=> {
+ if (timerRunningFlag == 0 && minsPerRound != 0) {
+ minsPerRound -= 1;
+ resetTimer();
+ }
+}); \ No newline at end of file