Advance on client side
[vchess.git] / server / models / Challenge.js
index 96db0a2..e1fb448 100644 (file)
@@ -18,6 +18,39 @@ var db = require("../utils/database");
 
 const ChallengeModel =
 {
+       checkChallenge: function(c)
+       {
+               const vid = parseInt(c.vid);
+               if (isNaN(vid) || vid <= 0)
+                       return "Please select a variant";
+
+               const mainTime = parseInt(c.mainTime);
+               const increment = parseInt(c.increment);
+               if (isNaN(mainTime) || mainTime <= 0)
+                       return "Main time should be strictly positive";
+               if (isNaN(increment) || increment < 0)
+                       return "Increment must be positive";
+
+               // Basic alphanumeric check for players names
+               let playerCount = 0;
+               for (p of c.players)
+               {
+                       if (p.name.length > 0)
+                       {
+                               if (!p.name.match(/^[\w]+$/))
+                                       return "Wrong characters in players names";
+                               playerCount++;
+                       }
+               }
+
+               if (playerCount > 0 && playerCount != c.nbPlayers-1)
+                       return "None, or all of the opponent names must be filled"
+
+               // Just characters check on server:
+               if (!c.fen.match(/^[a-zA-Z0-9, /-]*$/))
+                       return "Bad FEN string";
+       },
+
        // fen cannot be undefined; TODO: generate fen on server instead
        create: function(c, cb)
        {