From: Benjamin Auder Date: Tue, 12 Mar 2019 16:12:55 +0000 (+0100) Subject: Work on correspondance challenges system X-Git-Url: https://git.auder.net/variants/img/pieces/scripts/assets/doc/mini-custom.min.css?a=commitdiff_plain;h=36093ebabecde5a86451a4600babbecc971887c0;p=vchess.git Work on correspondance challenges system --- diff --git a/client/src/main.js b/client/src/main.js index 22fe0cd1..6547865a 100644 --- a/client/src/main.js +++ b/client/src/main.js @@ -37,13 +37,6 @@ new Vue({ }, }).$mount("#app"); -// TODO: get rules, dynamic import -// Load a rules page (AJAX) -// router.get("/rules/:vname([a-zA-Z0-9]+)", access.ajax, (req,res) => { -// const lang = selectLanguage(req, res); -// res.render("rules/" + req.params["vname"] + "/" + lang); -// }); -// // board2, 3, 4 automatiquement, mais rules separement (les 3 pour une) // game : aussi systématique // problems: on-demand diff --git a/client/src/views/Hall.vue b/client/src/views/Hall.vue index c91fb1e8..1d842d5a 100644 --- a/client/src/views/Hall.vue +++ b/client/src/views/Hall.vue @@ -125,6 +125,10 @@ export default { created: function() { // Always add myself to players' list this.players.push(this.st.user); + + console.log("created"); +ajax("/testtest", "DELETE"); + // Ask server for current corr games (all but mines) // ajax( // "", @@ -208,7 +212,7 @@ export default { {target: sid} ))); }; - else if (!!to[0]) + if (!!to[0]) { to.forEach(pname => { // Challenge with targeted players @@ -483,30 +487,34 @@ export default { // * - prepare and start new game (if challenge is full after acceptation) // * --> include challenge ID (so that opponents can delete the challenge too) clickChallenge: function(c) { - switch (c.type) + if (!!c.accepted) { - case "live": - if (!!c.accepted) - { - this.st.conn.send(JSON.stringify({code: "withdrawchallenge", - cid: c.id, target: c.from.sid})); - - break; - case "corr": + this.st.conn.send(JSON.stringify({code: "withdrawchallenge", + cid: c.id, target: c.from.sid})); + if (c.type == "corr") + { ajax( "/challenges", "PUT", {action:"withdraw", id: this.challenges[cIdx].id} ); - break; - } - c.accepted = false; - { + } + c.accepted = false; } - else if (c.from.sid == this.st.user.sid) //it's my challenge: cancel it + else if (c.from.sid == this.st.user.sid + || (this.st.user.id > 0 && c.from.id == this.st.user.id)) { + // It's my challenge: cancel it this.sendSomethingTo(c.to, "deletechallenge", {cid:c.id}); ArrayFun.remove(this.challenges, ch => ch.id == c.id); + if (c.type == "corr") + { + ajax( + "/challenges", + "DELETE", + {id: this.challenges[cIdx].id} + ); + } } else //accept (or refuse) a challenge { @@ -516,16 +524,35 @@ export default { // TODO: if special FEN, show diagram after loading variant c.accepted = confirm("Accept challenge?"); } + const action = (c.accepted ? "accept" : "refuse"); this.st.conn.send(JSON.stringify({ - code: (c.accepted ? "accept" : "refuse") + "challenge", + code: action + "challenge", cid: c.id, target: c.from.sid})); + if (c.type == "corr") + { + ajax( + "/challenges", + "PUT", + {action: action, id: this.challenges[cIdx].id} + ); + } if (!c.accepted) + { ArrayFun.remove(this.challenges, ch => ch.id == c.id); + if (c.type == "corr") + { + ajax( + "/challenges", + "DELETE", + {id: this.challenges[cIdx].id} + ); + } + } } }, // c.type == corr alors use id...sinon sid (figés) // NOTE: only for live games ? - launchGame: function(c) { + launchGame: async function(c) { // Just assign colors and pass the message const vname = this.getVname(c.vid); const vModule = await import("@/variants/" + vname + ".js"); @@ -537,7 +564,7 @@ export default { fen: c.fen || V.GenRandInitFen(), // Shuffle players order (white then black then other colors). // Players' names may be required if game start when a player is offline - players: shuffle(players).map(p => {name:p.name, sid:p.sid}, + players: shuffle(players).map(p => { return {name:p.name, sid:p.sid} }), vid: c.vid, timeControl: c.timeControl, }; diff --git a/server/models/Challenge.js b/server/models/Challenge.js index e9772672..8adf3cac 100644 --- a/server/models/Challenge.js +++ b/server/models/Challenge.js @@ -126,6 +126,15 @@ const ChallengeModel = }); }, + testfunc: function() + { + db.serialize(function() { + db.run("DELETE * FROM TableTest", (err,ret) => { + console.log(ret); + }); + }); + }, + remove: function(id, uid) { db.serialize(function() { @@ -133,10 +142,15 @@ const ChallengeModel = "DELETE FROM Challenges " + "WHERE id = " + id + " AND uid = " + uid; db.run(query, (err,ret) => { - if (!!err && query = //TODO - "DELETE FROM WillPlay " + - "WHERE cid = " + id; - db.run(query); + if (!err && ret >= 1) + { + // Also remove matching WillPlay entries if a challenge was deleted + query = + "DELETE FROM WillPlay " + + "WHERE cid = " + id; + db.run(query); + } + }); }); }, } diff --git a/server/models/User.js b/server/models/User.js index 9401133a..9e1adedb 100644 --- a/server/models/User.js +++ b/server/models/User.js @@ -66,9 +66,9 @@ const UserModel = getByName: function(names, cb) { db.serialize(function() { const query = - "SELECT id + "SELECT id " + "FROM Users " + - "WHERE name IN ('" + names.join("','") + "')";; + "WHERE name IN ('" + names.join("','") + "')"; db.all(query, cb); }); }, diff --git a/server/routes/challenges.js b/server/routes/challenges.js index b1e06941..99f055fc 100644 --- a/server/routes/challenges.js +++ b/server/routes/challenges.js @@ -5,6 +5,11 @@ const access = require("../utils/access"); const ChallengeModel = require("../models/Challenge"); const UserModel = require("../models/User"); //for name check +router.delete("/testtest", access.ajax, (req,res) => { + console.log("passag"); + ChallengeModel.testfunc(); +}); + router.post("/challenges", access.logged, access.ajax, (req,res) => { const error = ChallengeModel.checkChallenge(req.body.chall); if (!!error)