From 7eeaaa58c7c643a42c069000193643291b1e6b10 Mon Sep 17 00:00:00 2001 From: Thomas Wade Date: Sat, 6 Oct 2018 17:18:35 +0930 Subject: [PATCH] Add cooldown for updates --- mates.json | 2 +- server.js | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/mates.json b/mates.json index b163b82..330d608 100644 --- a/mates.json +++ b/mates.json @@ -1 +1 @@ -{"mates":87} \ No newline at end of file +{"mates":0,"lastUpdate":0} \ No newline at end of file diff --git a/server.js b/server.js index 2e0651e..a732d0b 100644 --- a/server.js +++ b/server.js @@ -38,7 +38,27 @@ app.post('/mates', function(rq, rs) { var data = fs.readFileSync('./mates.json'); var json = JSON.parse(data); + + var cooldownSecs = 60; + var currentTime = new Date().getTime(); + var timeSinceLast = (currentTime - json.lastUpdate) / 1000; + + // Check if update was sent too quickly + if (timeSinceLast < cooldownSecs) + { + rs + .status(403) + .json({ + error:"Update sent too quickly", + cooldown: cooldownSecs, + remaining: Math.ceil(cooldownSecs - timeSinceLast) + }); + + return; + } + json.mates += 1; + json.lastUpdate = new Date().getTime(); fs.writeFile('./mates.json', JSON.stringify(json), (err) => { if (err)