Add cooldown for updates

This commit is contained in:
Thomas Wade 2018-10-06 17:18:35 +09:30
parent b4310842c0
commit 7eeaaa58c7
2 changed files with 21 additions and 1 deletions

View File

@ -1 +1 @@
{"mates":87}
{"mates":0,"lastUpdate":0}

View File

@ -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)