From: Benjamin Auder Date: Fri, 6 Sep 2019 13:30:16 +0000 (+0200) Subject: Drop 'WillPlay' table (no multiplayers games) X-Git-Url: https://git.auder.net/%7B%7B%20path%28%27fos_user_profile_show%27%29%20%7D%7D?a=commitdiff_plain;h=bf20f404705c622a7bb7e458dacce37ecb7405a9;p=vchess.git Drop 'WillPlay' table (no multiplayers games) --- diff --git a/server/db/create.sql b/server/db/create.sql index b98cfbb0..e5cbfd7e 100644 --- a/server/db/create.sql +++ b/server/db/create.sql @@ -1,86 +1,77 @@ -- Database should be in this folder, and named 'vchess.sqlite' create table Variants ( - id integer primary key, - name varchar unique, - description text + id integer primary key, + name varchar unique, + description text ); create table Users ( - id integer primary key, - name varchar unique, - email varchar unique, - loginToken varchar, - loginTime datetime, - sessionToken varchar, - notify boolean + id integer primary key, + name varchar unique, + email varchar unique, + loginToken varchar, + loginTime datetime, + sessionToken varchar, + notify boolean ); create table Problems ( - id integer primary key, - added datetime, - uid integer, - vid integer, - fen varchar, - instructions text, - solution text, - foreign key (uid) references Users(id), - foreign key (vid) references Variants(id) + id integer primary key, + added datetime, + uid integer, + vid integer, + fen varchar, + instructions text, + solution text, + foreign key (uid) references Users(id), + foreign key (vid) references Variants(id) ); -- All the following tables are for correspondance play only -- (Live games are stored only in browsers) create table Challenges ( - id integer primary key, - added datetime, - uid integer, - vid integer, - nbPlayers integer, - fen varchar, - timeControl varchar, - foreign key (uid) references Users(id), - foreign key (vid) references Variants(id) -); - --- Store informations about players who (potentially) accept a challenge -create table WillPlay ( - yes boolean, - cid integer, - uid integer, - foreign key (cid) references Challenges(id), - foreign key (uid) references Users(id) + id integer primary key, + added datetime, + uid integer, + target integer, + vid integer, + fen varchar, + timeControl varchar, + foreign key (uid) references Users(id), + foreign key (vid) references Variants(id) ); create table Games ( - id integer primary key, - vid integer, - fenStart varchar, --initial state - fen varchar, --current state - score varchar, - mainTime integer, - addTime integer, - foreign key (vid) references Variants(id) + id integer primary key, + vid integer, + fenStart varchar, --initial state + fen varchar, --current state + score varchar, + mainTime integer, + addTime integer, + foreign key (vid) references Variants(id) ); -- Store informations about players in a corr game create table Players ( - gid integer, - uid integer, - color character, - rtime integer, --remaining time in milliseconds - foreign key (gid) references Games(id), - foreign key (uid) references Users(id) + gid integer, + uid integer, + color character, + rtime integer, --remaining time in milliseconds + foreign key (gid) references Games(id), + foreign key (uid) references Users(id) ); create table Moves ( - gid integer, - move varchar, - message varchar, - played datetime, --when was this move played? - idx integer, --index of the move in the game - color character, --required for e.g. Marseillais Chess - foreign key (gid) references Games(id) + gid integer, + move varchar, + message varchar, + played datetime, --when was this move played? + idx integer, --index of the move in the game + color character, --required for e.g. Marseillais Chess + foreign key (gid) references Games(id) ); pragma foreign_keys = on; diff --git a/server/models/Challenge.js b/server/models/Challenge.js index a5fbf63e..602531f4 100644 --- a/server/models/Challenge.js +++ b/server/models/Challenge.js @@ -1,19 +1,14 @@ var db = require("../utils/database"); /* - * Structure table Challenges: + * Structure: * id: integer * added: datetime * uid: user id (int) + * target: recipient id (optional) * vid: variant id (int) - * nbPlayers: integer * fen: varchar (optional) * timeControl: string (3m+2s, 7d+1d ...) - * - * Structure table WillPlay: - * cid: ref challenge id - * uid: ref user id - * yes: boolean (false means "not decided yet") */ const ChallengeModel = @@ -30,39 +25,23 @@ const ChallengeModel = return "Bad FEN string"; }, - initializeWillPlay: function(uids, cid, cb) - { - let query = "INSERT INTO WillPlay VALUES "; - for (let i=0; i { if (!!err) return cb(err); db.get("SELECT last_insert_rowid() AS rowid", (err2,lastId) => { - query = - "INSERT INTO WillPlay VALUES " + - "(true," + lastId["rowid"] + "," + c.uid + ")"; - db.run(query, (err,ret) => { - cb(err, lastId); //all we need is the challenge ID - }); - }); - }); + return cb(err2, lastId); + }); + }); }); }, @@ -71,32 +50,29 @@ const ChallengeModel = db.serialize(function() { let query = "SELECT * " + - "FROM Challenges c " + - "JOIN Variants v " + - " ON c.vid = v.id " + "FROM Challenges " + "WHERE id = " + id; db.get(query, (err,challengeInfo) => { if (!!err) return cb(err); let condition = ""; - if (!!challengeInfo.to[0]) - condition = " AND u.name in (" + challengeInfo.to.join(",") + ")"; + if (!!challengeInfo.to) + condition = "IN (" + challengeInfo.uid + "," + challengeInfo.to + ")"; + else + condition = "= " + challengeInfo.uid; query = - "SELECT w.uid AS id, u.name " + - "FROM WillPlay w " + - "JOIN Users u " + - " ON w.uid = u.id " + - "WHERE w.cid = " + id + condition; + "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, - vname: challengeInfo.name, + uid: challengeInfo.uid, //sender (but we don't know who ask) + vid: challengeInfo.vid, added: challengeInfo.added, - nbPlayers: challengeInfo.nbPlayers, - players: players, //currently in + players: players, //sender + potential receiver fen: challengeInfo.fen, timeControl: challengeInfo.timeControl, }; @@ -130,40 +106,6 @@ 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() { @@ -172,20 +114,12 @@ const ChallengeModel = "FROM Challenges " + "WHERE id = " + id + " AND uid = " + uid; db.run(query, (err,rows) => { - if (rows.length > 0) //it's my challenge - { - 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); - }); - } + if (rows.length == 0) + return res.json({errmsg: "Not your challenge"}); + query = + "DELETE FROM Challenges " + + "WHERE id = " + id; + db.run(query); }); }); }, diff --git a/server/routes/challenges.js b/server/routes/challenges.js index 1348cf45..03fc1500 100644 --- a/server/routes/challenges.js +++ b/server/routes/challenges.js @@ -15,56 +15,37 @@ router.post("/challenges", access.logged, access.ajax, (req,res) => { const error = ChallengeModel.checkChallenge(req.body.chall); if (!!error) return res.json({errmsg:error}); - const challenge = + let challenge = { fen: req.body.chall.fen, timeControl: req.body.chall.timeControl, vid: req.body.chall.vid, uid: req.userId, - nbPlayers: req.body.chall.to.length, + to: req.body.chall.to, //string: user name (may be empty) }; - ChallengeModel.create(challenge, (err,lastId) => { - if (!!err) - return res.json(err); - if (!!req.body.chall.to[0]) - { - UserModel.getByName(req.body.chall.to, (err,users) => { - if (!!err) - return res.json(err); - if (users.length < req.body.chall.to.length) - return res.json({errmsg: "Typo in player(s) name(s)"}); - ChallengeModel.initializeWillPlay( - users.map(u => u.id), - lastId["rowid"], - (err) => { - if (!!err) - return res.json(err); - res.json({cid: lastId["rowid"]}); - } - ); - }); - } - else - res.json({cid: lastId["rowid"]}); - }); -}); - -// Nothing to do if challenge is refused (just removal) -router.put("/challenges", access.logged, access.ajax, (req,res) => { - // Accept challenge: 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); - }); - res.json({}); + const insertChallenge = () => { + ChallengeModel.create(challenge, (err) => { + if (!!err) + return res.json(err); + }); + }; + if (!!req.body.chall.to) + { + UserModel.getOne("name", challenge.to, (err,user) => { + if (!!err || !user) + return res.json(err | {errmsg: "Typo in player name"}); + challenge.to = user.id; //ready now to insert challenge + }); + insertChallenge(); + } + else + insertChallenge(); }); function launchGame(cid, uid) { - // TODO: gather challenge infos + WillPlay - // Then create game, and remove challenge + WillPlay + // TODO: gather challenge infos + // Then create game, and remove challenge } //// index