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"}));
// 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)
});
},
+ 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);
+ });
}
});
});
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)
});
});
+// 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)
+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();