- // 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 => {
- let game = res.game;
- game.moves.forEach(m => {
- m.squares = JSON.parse(m.squares);
- });
- callback(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));
+ }
+ else {
+ // If there is still another cursor to go, keep running this code
+ let g = cursor.value;
+ if (g.score != "*") {
+ // Do not retrieve moves or clocks (unused in list mode)
+ g.movesCount = g.moves.length;
+ delete g.moves;
+ delete g.clocks;
+ delete g.initime;
+ games.push(g);
+ }
+ cursor.continue();