From: Benjamin Auder Date: Sat, 30 Nov 2019 10:16:43 +0000 (+0100) Subject: GameStorage.getCorrGame ok + add watchers for st.variants X-Git-Url: https://git.auder.net/variants/Chakart/pieces/current/css/doc/R.css?a=commitdiff_plain;h=fd7aea36b8da702df87be3ed055f9a1f59c9f4da;p=vchess.git GameStorage.getCorrGame ok + add watchers for st.variants --- diff --git a/client/src/utils/gameStorage.js b/client/src/utils/gameStorage.js index 77acf943..031f62f5 100644 --- a/client/src/utils/gameStorage.js +++ b/client/src/utils/gameStorage.js @@ -16,6 +16,8 @@ // score: string (several options; '*' == running), // } +import { ajax } from "@/utils/ajax"; + function dbOperation(callback) { let db = null; @@ -85,34 +87,46 @@ export const GameStorage = }); }, - // 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) diff --git a/client/src/views/Game.vue b/client/src/views/Game.vue index 47605c47..6cb75f47 100644 --- a/client/src/views/Game.vue +++ b/client/src/views/Game.vue @@ -106,6 +106,11 @@ export default { } }, 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"]) @@ -287,7 +292,16 @@ export default { // - 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 { diff --git a/client/src/views/Hall.vue b/client/src/views/Hall.vue index 2fbb6e6d..b7526ccd 100644 --- a/client/src/views/Hall.vue +++ b/client/src/views/Hall.vue @@ -93,6 +93,20 @@ export default { }, }; }, + 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 @@ -165,23 +179,6 @@ export default { ); } ); - // 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 = () => { diff --git a/client/src/views/MyGames.vue b/client/src/views/MyGames.vue index ddb689d0..c630668f 100644 --- a/client/src/views/MyGames.vue +++ b/client/src/views/MyGames.vue @@ -2,4 +2,4 @@ // 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() diff --git a/server/models/Game.js b/server/models/Game.js index 78827e29..f99dbdef 100644 --- a/server/models/Game.js +++ b/server/models/Game.js @@ -30,8 +30,8 @@ const GameModel = { 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); @@ -53,39 +53,33 @@ const GameModel = { 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); }); });