From 85b1c46364e21c348ddcec6fe2ac61bc05dd3f25 Mon Sep 17 00:00:00 2001 From: Plague Date: Fri, 28 Aug 2020 18:13:47 -0700 Subject: [PATCH 1/2] Add buffer and increment functions to timer --- app.js | 26 +++++++++++++++++++++++++- index.html | 11 +++++++++-- index.js | 21 ++++++++++++++++++--- 3 files changed, 52 insertions(+), 6 deletions(-) diff --git a/app.js b/app.js index 769bb92..0a5db0b 100644 --- a/app.js +++ b/app.js @@ -35,7 +35,16 @@ app.post('/create',async function(req,res) { while(rooms[code]) { code = Math.floor(100000 + Math.random() * 900000).toString(); } - rooms[code] = {t1: data.time, t2: data.time, m1: true, p: true, t: 0}; + let buffer = parseInt(data.buffer) || 0 + rooms[code] = {t1: data.time, + t2: data.time, + buffer: buffer, + activeBuffer: buffer, + increment: parseInt(data.increment) || 0, + m1: true, + p: true, + t: 0 + }; res.send(code); } catch(e) { @@ -66,8 +75,16 @@ app.post('/switch',async function(req, res) { } var room = rooms[data.code]; if(data.s == 0) { + //apply increment + if (room.m1) { + room.t1 += room.increment; + } else { + room.t2 += room.increment; + } //switch room.m1 = !room.m1; + //reset buffer TODO: (Should this also happen on unpause?) + room.activeBuffer = room.buffer; } else if(data.s == 1) { //pause room.p = false; @@ -81,6 +98,13 @@ app.post('/switch',async function(req, res) { //reduce room timer value function roomTick(delta, room) { if(room.p) return; + if (room.activeBuffer > 0) { + room.activeBuffer -= delta; + delta = -1* room.activeBuffer; + if (delta < 0) { + return; + } + } if(room.m1 && room.t1 > 0) { room.t1 -= delta; } else if(room.t2 > 0) { diff --git a/index.html b/index.html index 8ab5e63..20f6b4a 100644 --- a/index.html +++ b/index.html @@ -18,12 +18,19 @@

Chess Timer Online

- +

- + +
+ Advanced Time Settings +
+ + +
+