-- 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;
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 =
return "Bad FEN string";
},
- initializeWillPlay: function(uids, cid, cb)
- {
- let query = "INSERT INTO WillPlay VALUES ";
- for (let i=0; i<uids.length; i++)
- {
- query += "(false," + cid + "," + uids[i] + ")";
- if (i < uids.length-1)
- query += ",";
- }
- db.run(query, cb);
- },
-
// fen cannot be undefined
create: function(c, cb)
{
db.serialize(function() {
let query =
"INSERT INTO Challenges " +
- "(added, uid, vid, nbPlayers, fen, timeControl) VALUES " +
- "(" + Date.now() + "," + c.uid + "," + c.vid + "," + c.nbPlayers +
- ",'" + c.fen + "'," + c.timeControl + ")";
+ "(added, uid, " + (!!c.to ? "target, " : "") +
+ "vid, fen, timeControl) VALUES " +
+ "(" + 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) => {
- 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);
+ });
+ });
});
},
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,
};
});
},
- 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() {
"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);
});
});
},
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