diff --git a/index.js b/index.js index fcae679..8776356 100644 --- a/index.js +++ b/index.js @@ -6,6 +6,15 @@ var express = require('express'); const POLLING_TIMEOUT = 60000/*ms*/; +let nextMonotonicTime = 1; + +function monotonicTime() { + const now = +new Date(); + const mono = (now >= nextMonotonicTime) ? now : nextMonotonicTime; + nextMonotonicTime = mono + 1; + return mono; +} + var appendJournal = (function() { let lastJournalText = null; return function appendJournal(text) { @@ -53,6 +62,15 @@ const dbInit = (async function dbInit() { CREATE INDEX IF NOT EXISTS games_modified ON games(modified) `); + const maxModified = await db.getAsync(` + SELECT MAX(modified) as result FROM games + `).catch(logDbError('maxModified')); + + /* Just in case the system clock moved backward since the last record was written. */ + if (maxModified && maxModified.result) { + nextMonotonicTime = maxModified.result + 1; + } + console.log('Connected to the SQLite database.'); return db; @@ -286,7 +304,6 @@ function validateUpdate(body) { async function postGameHandler(req, res, next) { res.set('Cache-Control', 'no-store'); - const time = +new Date(); const gameId = req.params.gameId; const body = validateUpdate(req.body); @@ -300,6 +317,8 @@ async function postGameHandler(req, res, next) { return; } + const time = monotonicTime(); + const params = { $gameId: gameId, $lightName: '',