// score: string (several options; '*' == running),
// }
+import { ajax } from "@/utils/ajax";
+
function dbOperation(callback)
{
let db = null;
});
},
- // Retrieve any live game from its identifiers (locally, running or not)
- // NOTE: need callback because result is obtained asynchronously
- get: function(gameId, callback)
+ // Retrieve all local games (running, completed, imported...)
+ getAll: function(callback)
{
dbOperation((db) => {
let objectStore = db.transaction('games').objectStore('games');
- if (!gameId) //retrieve all
- {
- let games = [];
- objectStore.openCursor().onsuccess = function(event) {
- let cursor = event.target.result;
- // if there is still another cursor to go, keep running this code
- if (cursor)
- {
- games.push(cursor.value);
- cursor.continue();
- }
- else
- callback(games);
+ let games = [];
+ objectStore.openCursor().onsuccess = function(event) {
+ let cursor = event.target.result;
+ // if there is still another cursor to go, keep running this code
+ if (cursor)
+ {
+ games.push(cursor.value);
+ cursor.continue();
}
+ else
+ callback(games);
}
- else //just one game
- {
+ });
+ },
+
+ // 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);
}
- }
- });
+ });
+ }
},
getCurrent: function(callback)
}
}, 1000);
},
+ // In case variants array was't loaded when game was retrieved
+ "st.variants": function(variantArray) {
+ if (!!this.game.vname && this.game.vname == "")
+ this.game.vname = variantArray.filter(v => v.id == this.game.vid)[0].name;
+ },
},
created: function() {
if (!!this.$route.params["id"])
// - from remote peer (one live game I don't play, finished or not)
loadGame: function(game) {
const afterRetrieval = async (game) => {
- const vname = this.st.variants.filter(v => v.id == game.vid)[0].name;
+ // NOTE: variants array might not be available yet, thus the two next lines
+ const variantCell = this.st.variants.filter(v => v.id == game.vid);
+ const vname = (variantCell.length > 0 ? variantCell[0].name : "");
+ if (!game.fen)
+ game.fen = game.fenStart; //game wasn't started
+
+
+ // TODO: process rtime, clocks............ game.clocks doesn't exist anymore
+console.log(game);
+
const tc = extractTime(game.timeControl);
if (game.clocks[0] < 0) //game unstarted
{
},
};
},
+ watch: {
+ // st.variants changes only once, at loading from [] to [...]
+ "st.variants": function(variantArray) {
+ // Set potential challenges and games variant names:
+ this.challenges.forEach(c => {
+ if (c.vname == "")
+ c.vname = this.getVname(c.vid);
+ });
+ this.games.forEach(g => {
+ if (g.vname == "")
+ g.vname = this.getVname(g.vid)
+ });
+ },
+ },
computed: {
uniquePlayers: function() {
// Show e.g. "@nonymous (5)", and do nothing on click on anonymous
);
}
);
- // TODO: I don't like this code below; improvement?
- let retryForVnames = setInterval(() => {
- if (this.st.variants.length > 0) //variants array is loaded
- {
- if (this.games.length > 0 && this.games[0].vname == "")
- {
- // Fix games' vnames:
- this.games.forEach(g => { g.vname = this.getVname(g.vid); });
- }
- if (this.challenges.length > 0 && this.challenges[0].vname == "")
- {
- // Fix challenges' vnames:
- this.challenges.forEach(c => { c.vname = this.getVname(c.vid); });
- }
- clearInterval(retryForVnames);
- }
- }, 50);
}
// 0.1] Ask server for room composition:
const funcPollClients = () => {
// si dernier lastMove sur serveur n'est pas le mien et nextColor == moi, alors background orange
// ==> background orange si à moi de jouer par corr (sur main index)
// (helper: static fonction "GetNextCol()" dans base_rules.js)
-
+//use GameStorage.getAll()
{
db.serialize(function() {
let query =
- "INSERT INTO Games (vid, fen, timeControl) " +
- "VALUES (" + vid + ",'" + fen + "','" + timeControl + "')";
+ "INSERT INTO Games (vid, fenStart, score, timeControl) " +
+ "VALUES (" + vid + ",'" + fen + "','*','" + timeControl + "')";
db.run(query, function(err) {
if (!!err)
return cb(err);
{
db.serialize(function() {
let query =
- "SELECT v.name AS vname, g.fen, g.fenStart, g.score " +
- "FROM Games g " +
- "JOIN Variants v " +
- " ON g.vid = v.id "
+ "SELECT * " +
+ "FROM Games " +
"WHERE id = " + id;
db.get(query, (err,gameInfo) => {
if (!!err)
return cb(err);
query =
- "SELECT p.uid AS id, p.color, p.rtime, u.name " +
- "FROM Players p " +
- "JOIN Users u " +
- " ON p.uid = u.id " +
- "WHERE p.gid = " + id;
- db.run(query, (err2,players) => {
+ "SELECT uid, color, rtime " +
+ "FROM Players " +
+ "WHERE gid = " + id;
+ db.all(query, (err2,players) => {
if (!!err2)
return cb(err2);
query =
- "SELECT move AS desc, message, played, idx, color " +
+ "SELECT move, message, played, idx, color " +
"FROM Moves " +
"WHERE gid = " + id;
- db.run(query, (err3,moves) => {
+ db.all(query, (err3,moves) => {
if (!!err3)
return cb(err3);
- const game = {
- id: id,
- vname: gameInfo.vname,
- fenStart: gameInfo.fenStart,
- fen: gameInfo.fen,
- score: gameInfo.score,
- players: players,
- moves: moves,
- };
+ const game = Object.assign({},
+ gameInfo,
+ {
+ players: players,
+ moves: moves
+ }
+ );
return cb(null, game);
});
});