- // Retrieve any game from its identifiers (locally or on server)
- // NOTE: need callback because result is obtained asynchronously
- get: function(gameId, callback)
- {
- // corr games identifiers are integers
- if (Number.isInteger(gameId) || !isNaN(parseInt(gameId)))
- {
- ajax("/games", "GET", {gid:gameId}, res => {
- callback(res.game);
- });
- }
- else //local game
- {
- dbOperation((db) => {
- let objectStore = db.transaction('games').objectStore('games');
- objectStore.get(gameId).onsuccess = function(event) {
- callback(event.target.result);
+ // Retrieve completed local games
+ getNext: function(upperDt, callback) {
+ dbOperation((err, db) => {
+ let objectStore = db
+ .transaction("games", "readonly")
+ .objectStore("games");
+ let index = objectStore.index("created");
+ const range = IDBKeyRange.upperBound(upperDt);
+ let games = [];
+ index.openCursor(range).onsuccess = function(event) {
+ let cursor = event.target.result;
+ if (!cursor) {
+ // Most recent games first:
+ games = games.sort((g1, g2) => g2.created - g1.created);
+ // TODO: 20 games showed per request is arbitrary
+ callback(games.slice(0, 20));