Update on challenges
authorBenjamin Auder <benjamin.auder@somewhere>
Fri, 6 Sep 2019 13:58:46 +0000 (15:58 +0200)
committerBenjamin Auder <benjamin.auder@somewhere>
Fri, 6 Sep 2019 13:58:46 +0000 (15:58 +0200)
server/models/Challenge.js
server/routes/challenges.js

index 602531f..3f71320 100644 (file)
@@ -13,103 +13,65 @@ var db = require("../utils/database");
 
 const ChallengeModel =
 {
-       checkChallenge: function(c)
-       {
+  checkChallenge: function(c)
+  {
     if (!c.vid.match(/^[0-9]+$/))
-                       return "Wrong variant ID";
+      return "Wrong variant ID";
 
     if (!c.timeControl.match(/^[0-9dhms +]+$/))
       return "Wrong characters in time control";
 
-               if (!c.fen.match(/^[a-zA-Z0-9, /-]+$/))
-                       return "Bad FEN string";
-       },
+    if (!c.fen.match(/^[a-zA-Z0-9, /-]+$/))
+      return "Bad FEN string";
+  },
 
-       // fen cannot be undefined
-       create: function(c, cb)
-       {
-               db.serialize(function() {
-                       let query =
-                               "INSERT INTO Challenges " +
-                               "(added, uid, " + (!!c.to ? "target, " : "") +
+  // fen cannot be undefined
+  create: function(c, cb)
+  {
+    db.serialize(function() {
+      let query =
+        "INSERT INTO Challenges " +
+        "(added, uid, " + (!!c.to ? "target, " : "") +
           "vid, fen, timeControl) VALUES " +
-                               "(" + Date.now() + "," + c.uid + "," + (!!c.to ? c.to + "," : "") +
+        "(" + Date.now() + "," + c.uid + "," + (!!c.to ? c.to + "," : "") +
           c.vid + ",'" + c.fen + "','" + c.timeControl + "')";
-                       db.run(query, err => {
-                               if (!!err)
-                                       return cb(err);
-                               db.get("SELECT last_insert_rowid() AS rowid", (err2,lastId) => {
-                           return cb(err2, lastId);
-        });
+      db.run(query, err => {
+        return cb(err);
       });
-               });
-       },
+    });
+  },
 
-       getOne: function(id, cb)
-       {
-               db.serialize(function() {
-                       let query =
-                               "SELECT * " +
-                               "FROM Challenges " +
-                               "WHERE id = " + id;
-                       db.get(query, (err,challengeInfo) => {
-                               if (!!err)
-                                       return cb(err);
-        let condition = "";
-        if (!!challengeInfo.to)
-          condition = "IN (" + challengeInfo.uid + "," + challengeInfo.to + ")";
-        else
-          condition = "= " + challengeInfo.uid;
-                               query =
-                                       "SELECT id, name " +
-                                       "FROM Users " +
-                                       "WHERE id " + condition;
-                               db.run(query, (err2,players) => {
-                                       if (!!err2)
-                                               return cb(err2);
-                                       const challenge = {
-                                               id: id,
-                                               uid: challengeInfo.uid, //sender (but we don't know who ask)
-                                               vid: challengeInfo.vid,
-                                               added: challengeInfo.added,
-                                               players: players, //sender + potential receiver
-                                               fen: challengeInfo.fen,
-                                               timeControl: challengeInfo.timeControl,
-                                       };
-                                       return cb(null, challenge);
-                               });
-                       });
-               });
-       },
+  getOne: function(id, cb)
+  {
+    db.serialize(function() {
+      let query =
+        "SELECT * " +
+        "FROM Challenges " +
+        "WHERE id = " + id;
+      db.get(query, (err,challenge) => {
+        return cb(err, challenge);
+      });
+    });
+  },
 
-       getByUser: function(uid, cb)
-       {
-               db.serialize(function() {
-                       const query =
-                               "SELECT cid " +
-                               "FROM WillPlay " +
-                               "WHERE uid = " + uid;
-                       db.run(query, (err,challIds) => {
-                               if (!!err)
-                                       return cb(err);
-        challIds = challIds || [];
-                               let challenges = [];
-                               challIds.forEach(cidRow => {
-                                       ChallengeModel.getOne(cidRow["cid"], (err2,chall) => {
-                                               if (!!err2)
-                                                       return cb(err2);
-                                               challenges.push(chall);
-                                       });
-                               });
-                               return cb(null, challenges);
-                       });
-               });
-       },
+  // all challenges except where target is defined and not me
+  getByUser: function(uid, cb)
+  {
+    db.serialize(function() {
+      const query =
+        "SELECT * " +
+        "FROM Challenges " +
+        "WHERE target IS NULL OR target = " + uid;
+      db.run(query, (err,challenges) => {
+        return cb(err, challenges);
+      });
+    });
+  },
 
-       remove: function(id, uid)
-       {
-               db.serialize(function() {
-                       let query =
+  remove: function(id, uid)
+  {
+    db.serialize(function() {
+      let query =
         "SELECT 1 " +
         "FROM Challenges " +
         "WHERE id = " + id + " AND uid = " + uid;
@@ -121,8 +83,8 @@ const ChallengeModel =
           "WHERE id = " + id;
         db.run(query);
       });
-               });
-       },
+    });
+  },
 }
 
 module.exports = ChallengeModel;
index 03fc150..7675689 100644 (file)
@@ -42,37 +42,20 @@ router.post("/challenges", access.logged, access.ajax, (req,res) => {
     insertChallenge();
 });
 
-function launchGame(cid, uid)
-{
+// "Challenge update" --> someone accepted a challenge
+router.put("/challenges", access.logged, access.ajax, (req,res) => {
+  // launchGame(cid, uid)
   // TODO: gather challenge infos
-  // Then create game, and remove challenge
-}
-
-//// index
-//router.get("/challenges", access.logged, access.ajax, (req,res) => {
-//  if (req.query["uid"] != req.user._id)
-//    return res.json({errmsg: "Not your challenges"});
-//  let uid = ObjectID(req.query["uid"]);
-//  ChallengeModel.getByPlayer(uid, (err, challengeArray) => {
-//    res.json(err || {challenges: challengeArray});
-//  });
-//});
-//
-//function createChallenge(vid, from, to, res)
-//{
-//  ChallengeModel.create(vid, from, to, (err, chall) => {
-//    res.json(err || {
-//      // A challenge can be sent using only name, thus 'to' is returned
-//      to: chall.to,
-//      cid: chall._id
-//    });
-//  });
-//}
+  // Then create game, and remove challenge:
+  ChallengeModel.remove(cid, req.userId, err => {
+    res.json(err || {});
+  });
+});
 
 router.delete("/challenges", access.logged, access.ajax, (req,res) => {
   const cid = req.query.id;
   ChallengeModel.remove(cid, req.userId, err => {
-    res.json(err || {});
+    res.json(err || {}); //TODO: just "return err" because is empty if no errors
   });
 });