From 7b626bdd29da0927354c92526fc5e03095877351 Mon Sep 17 00:00:00 2001
From: Benjamin Auder <benjamin.auder@somewhere>
Date: Fri, 8 Mar 2019 18:13:48 +0100
Subject: [PATCH] Start work on challenges + games server side

---
 client/src/views/Game.vue   |  1 +
 client/src/views/Hall.vue   | 26 ++++++++++++++++---------
 server/routes/challenges.js | 38 ++++++++++++++++---------------------
 3 files changed, 34 insertions(+), 31 deletions(-)

diff --git a/client/src/views/Game.vue b/client/src/views/Game.vue
index f467b3b7..151c347b 100644
--- a/client/src/views/Game.vue
+++ b/client/src/views/Game.vue
@@ -7,4 +7,5 @@ quand on arrive dans la partie, on poll les sids pour savoir qui est en ligne (p
 ensuite quand qqun se deco il suffit d'écouter "disconnect"
 pareil quand quelqu'un reco.
 (c'est assez rudimentaire et écoute trop de messages, mais dans un premier temps...)
+      // TODO: [in game] send move + elapsed time (in milliseconds); in case of "lastate" message too
 -->
diff --git a/client/src/views/Hall.vue b/client/src/views/Hall.vue
index 89663b75..c91fb1e8 100644
--- a/client/src/views/Hall.vue
+++ b/client/src/views/Hall.vue
@@ -292,8 +292,8 @@ export default {
             {
               // Minimal game informations: (fen+clock not required)
               id: localStorage["gid"],
-              players: JSON.parse(localStorage["players"]), //array sid+name
-              vid: localStorage["vid"],
+              players: JSON.parse(localStorage["players"]), //array sid+id+name
+              vname: localStorage["vname"],
               timeControl: localStorage["timeControl"],
             };
             this.st.conn.send(JSON.stringify({code:"game",
@@ -326,7 +326,7 @@ export default {
           // NOTE: it may be correspondance (if newgame while we are connected)
           let newGame = data.game;
           newGame.type = this.classifyObject(data.game);
-          newGame.vname = this.getVname(newGame.vid);
+          newGame.vname = newGame.vname;
           this.games.push(newGame);
           break;
         }
@@ -550,15 +550,23 @@ export default {
       ArrayFun.remove(this.challenges, ch => ch.id == c.id);
       this.newGame(gameInfo); //also!
     },
-    // NOTE: for live games only (corr games are laucnhed on server)
+    // NOTE: for live games only (corr games are launched on server)
     newGame: function(gameInfo) {
+      localStorage["gid"] = getRandString();
       // Extract times (in [milli]seconds), set clocks, store in localStorage
       const tc = extractTime(gameInfo.timeControl);
-      dddddddd
-      // TODO: [in game] send move + elapsed time (in milliseconds); in case of "lastate" message too
-      //      //setStorage(game); //TODO
-//      if (this.settings.sound >= 1)
-//        new Audio("/sounds/newgame.mp3").play().catch(err => {});
+      localStorage["timeControl"] = gameInfo.timeControl;
+      localStorage["clocks"] = JSON.stringify(
+        [...Array(gameInfo.players.length)].fill(tc.mainTime));
+      localStorage["increment"] = tc.increment;
+      localStorage["started"] = JSON.stringify(
+        [...Array(gameInfo.players.length)].fill(false));
+      localStorage["mysid"] = this.st.user.sid;
+      localStorage["vname"] = this.getVname(gameInfo.vid);
+      localStorage["fenInit"] = gameInfo.fen;
+      localStorage["players"] = JSON.stringify(gameInfo.players);
+      if (this.st.settings.sound >= 1)
+        new Audio("/sounds/newgame.mp3").play().catch(err => {});
     },
   },
 };
diff --git a/server/routes/challenges.js b/server/routes/challenges.js
index e64e8901..663072f5 100644
--- a/server/routes/challenges.js
+++ b/server/routes/challenges.js
@@ -9,6 +9,22 @@ router.post("/challenges", access.logged, access.ajax, (req,res) => {
 	const error = ChallengeModel.checkChallenge(req.body.chall);
   // TODO: treat "to" field separately (search users by name)
   // --> replace "to" by an array of uid (in chall), then call:
+	const from = req.userId;
+//	let to = !!req.body.to ? ObjectID(req.body.to) : undefined;
+//	let nameTo = !!req.body.nameTo ? req.body.nameTo : undefined;
+//	let vid = ObjectID(req.body.vid);
+//	if (!to && !!nameTo)
+//	{
+//		UserModel.getByName(nameTo, (err,user) => {
+//			access.checkRequest(res, err, user, "Opponent not found", () => {
+//				createChallenge(vid, from, user._id, res);
+//			});
+//		});
+//	}
+//	else if (!!to)
+//		createChallenge(vid, from, to, res);
+//	else
+//		createChallenge(vid, from, undefined, res); //automatch
 	ChallengeModel.create(req.body.chall, (err,lastId) => {
 		res.json(err || {cid: lastId["rowid"]});
 	});
@@ -35,28 +51,6 @@ router.post("/challenges", access.logged, access.ajax, (req,res) => {
 //	});
 //}
 //
-//// from[, to][,nameTo]
-//router.post("/challenges", access.logged, access.ajax, (req,res) => {
-//	if (req.body.from != req.user._id)
-//		return res.json({errmsg: "Identity usurpation"});
-//	let from = ObjectID(req.body.from);
-//	let to = !!req.body.to ? ObjectID(req.body.to) : undefined;
-//	let nameTo = !!req.body.nameTo ? req.body.nameTo : undefined;
-//	let vid = ObjectID(req.body.vid);
-//	if (!to && !!nameTo)
-//	{
-//		UserModel.getByName(nameTo, (err,user) => {
-//			access.checkRequest(res, err, user, "Opponent not found", () => {
-//				createChallenge(vid, from, user._id, res);
-//			});
-//		});
-//	}
-//	else if (!!to)
-//		createChallenge(vid, from, to, res);
-//	else
-//		createChallenge(vid, from, undefined, res); //automatch
-//});
-//
 //router.delete("/challenges", access.logged, access.ajax, (req,res) => {
 //	let cid = ObjectID(req.query.cid);
 //	ChallengeModel.getById(cid, (err,chall) => {
-- 
2.44.0