From: Benjamin Auder Date: Wed, 13 Mar 2019 16:45:22 +0000 (+0100) Subject: Ongoing work on challenges + games for correspondance play X-Git-Url: https://git.auder.net/assets/%7B%7B%20asset%28%27mixstore/css/doc/current/git-logo.png?a=commitdiff_plain;h=4edfed6c011cd97d58d5bd8e0451cc0c1006a0a0;p=vchess.git Ongoing work on challenges + games for correspondance play --- diff --git a/client/src/views/Hall.vue b/client/src/views/Hall.vue index 19f2e77d..e499d7ec 100644 --- a/client/src/views/Hall.vue +++ b/client/src/views/Hall.vue @@ -127,20 +127,24 @@ export default { this.players.push(this.st.user); // Ask server for current corr games (all but mines) // ajax( -// "", +// "/games", // "GET", +// {excluded: this.st.user.id}, // response => { -// -// } -// ); - // // Also ask for corr challenges (all) --> + accepted status if I play -// ajax( -// "", -// "GET", -// response => { -// +// this.games = this.games.concat(response.games); // } // ); + // Also ask for corr challenges (open + personal to me) + ajax( + "/challenges", + "GET", + {uid: this.st.user.id}, + response => { + console.log(response.challenges); + // TODO: post-treatment on challenges ? + this.challenges = this.challenges.concat(response.challenges); + } + ); // 0.1] Ask server for room composition: const socketOpenListener = () => { this.st.conn.send(JSON.stringify({code:"pollclients"})); @@ -520,16 +524,15 @@ 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: action + "challenge", + code: (c.accepted ? "accept" : "refuse") + "challenge", cid: c.id, target: c.from.sid})); - if (c.type == "corr") + if (c.type == "corr" && c.accepted) { ajax( "/challenges", "PUT", - {action: action, id: this.challenges[cIdx].id} + {action: "accept", id: this.challenges[cIdx].id} ); } if (!c.accepted) diff --git a/server/models/Challenge.js b/server/models/Challenge.js index de2818ba..6f8ba0be 100644 --- a/server/models/Challenge.js +++ b/server/models/Challenge.js @@ -126,20 +126,61 @@ const ChallengeModel = }); }, + getSeatCount: function(id, cb) + { + db.serialize(function() { + let query = + "SELECT COUNT(*) AS scount " + + "FROM WillPlay " + + "WHERE cid = " + id; + db.get(query, (err,scRow) => { + if (!!err) + return cb(err); + query = + "SELECT nbPlayers " + + "FROM Challenges " + + "WHERE id = " + id; + db.get(query, (err2,chRow) => { + if (!!err2) + return cb(err2); + cb(chRow["nbPlayers"] - scRow["scount"]); + }); + }); + }); + }, + + setSeat: function(id, uid) + { + // TODO: remove extra "db.serialize" (parallelize by default) + //db.serialize(function() { + const query = + "INSERT OR REPLACE INTO WillPlay " + + "VALUES (true," + id + "," + uid +")"; + db.run(query); + //}); + }, + remove: function(id, uid) { db.serialize(function() { let query = - "DELETE FROM Challenges " + - "WHERE id = " + id + " AND uid = " + uid; - db.run(query, (err,ret) => { - if (!err && ret >= 1) + "SELECT 1 " + + "FROM Challenges " + + "WHERE id = " + id + " AND uid = " + uid; + db.run(query, (err,rows) => { + if (rows.length > 0) //it's my challenge { - // Also remove matching WillPlay entries if a challenge was deleted - query = - "DELETE FROM WillPlay " + - "WHERE cid = " + id; - db.run(query); + db.parallelize(function() { + query = + "DELETE FROM Challenges " + + "WHERE id = " + id; + db.run(query); + // Also remove matching WillPlay entries if a challenge was deleted + query = + "DELETE FROM WillPlay " + + "WHERE cid = " + id; + db.run(query); + }); } }); }); diff --git a/server/routes/challenges.js b/server/routes/challenges.js index 40c50ec8..84b2c83b 100644 --- a/server/routes/challenges.js +++ b/server/routes/challenges.js @@ -5,6 +5,12 @@ const access = require("../utils/access"); const ChallengeModel = require("../models/Challenge"); const UserModel = require("../models/User"); //for name check +router.get("/challenges", access.logged, access.ajax, (req,res) => { + ChallengeModel.getByUser(req.query["uid"], (err,challenges) => { + res.json(err || {challenges:challenges}); + }); +}); + router.post("/challenges", access.logged, access.ajax, (req,res) => { const error = ChallengeModel.checkChallenge(req.body.chall); if (!!error) @@ -43,6 +49,32 @@ router.post("/challenges", access.logged, access.ajax, (req,res) => { }); }); +// Nothing to do if challenge is refused (just removal) +router.put("/challenges", access.logged, access.ajax, (req,res) => { + switch (req.body.action) + { + case "withdraw": + // turn WillPlay to false (TODO?) + break; + case "accept": + // turn WillPlay to true; if then challenge is full, launch game + ChallengeModel.getSeatCount(req.body.id, (scount) => { + if (scount == 1) + launchGame(req.body.id, req.userId); + else + ChallengeModel.setSeat(req.body.id, req.userId); + }) + break; + } + res.json({}); +}); + +function launchGame(cid, uid) +{ + // TODO: gather challenge infos + WillPlay + // Then create game, and remove challenge + WillPlay +} + //// index //router.get("/challenges", access.logged, access.ajax, (req,res) => { // if (req.query["uid"] != req.user._id) diff --git a/server/routes/games.js b/server/routes/games.js index 808c258c..35beb250 100644 --- a/server/routes/games.js +++ b/server/routes/games.js @@ -1,3 +1,7 @@ +router.get("/games", access.logged, access.ajax, (req,res) => { + const excluded = req.query["excluded"]; //TODO: think about query params here +}); + // TODO: adapt for correspondance play var router = require("express").Router();