created daily leaderboard clear function

This commit is contained in:
lukew3 2021-05-26 16:09:52 -04:00
parent 9a3e1749b8
commit ab13cd439d
2 changed files with 22 additions and 5 deletions

View file

@ -24,10 +24,8 @@
### leaderboard
- Add boardcleartime
- How will boards be cleared?
- Can there be a function that runs outside of requests
- Wait until desired time with setTimeout and then set next timeout
- Does clearDailyLeaderboards cause a memory leak?
- Try commenting it out and seeing if it makes a difference
- Identify bugs
- Username not highlighted and added to the bottom if current user made the leaderboard

View file

@ -24,7 +24,24 @@ const mtRootDir = __dirname.substring(0, __dirname.length - 8); //will this work
app.use(express.static(mtRootDir + "/dist"));
app.use(bodyParser.json());
// Initialize database leaderboards if no leaderboards exist
// Daily leaderboard clear function
function clearDailyLeaderboards() {
var nextClear = new Date();
nextClear.setHours(24, 0, 0, 0); //next occurrence of 12am
let currentTime = new Date();
setTimeout(() => {
Leaderboard.find({ type: "daily" }, (err, lbs) => {
lbs.forEach((lb) => {
lb.board = [];
lb.resetTime = nextClear;
lb.save();
});
});
clearDailyLeaderboards();
}, nextClear.getTime() - currentTime.getTime());
}
// Initialize database leaderboards if no leaderboards exist and start clearDailyLeaderboards
Leaderboard.findOne((err, lb) => {
if (lb === null) {
let lb = {
@ -43,6 +60,8 @@ Leaderboard.findOne((err, lb) => {
lb.mode2 = 15;
Leaderboard.create(lb);
}
}).then(() => {
clearDailyLeaderboards();
});
function authenticateToken(req, res, next) {