Add cooldown for updates
This commit is contained in:
parent
b4310842c0
commit
7eeaaa58c7
@ -1 +1 @@
|
||||
{"mates":87}
|
||||
{"mates":0,"lastUpdate":0}
|
||||
20
server.js
20
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)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user