1 var router
= require("express").Router();
2 var UserModel
= require("../models/User");
3 var sendEmail
= require('../utils/mailer');
4 var GameModel
= require('../models/Game');
5 var VariantModel
= require('../models/Variant');
6 var access
= require("../utils/access");
7 var params
= require("../config/parameters");
9 // Notify about a game (start, new move)
10 function tryNotify(uid
, gid
, vname
, subject
)
12 UserModel
.getOne("id", uid
, (err
,user
) => {
13 if (!!err
&& user
.notify
)
15 sendEmail(params
.mailFrom
, user
.email
, subject
,
16 params
.siteURL
+ "?v=" + vname
+ "&g=" + gid
, err
=> {
17 res
.json(err
|| {}); // TODO: log error somewhere.
24 // From main hall, start game between players 0 and 1
25 router
.post("/games", access
.logged
, access
.ajax
, (req
,res
) => {
26 const gameInfo
= JSON
.parse(req
.body
.gameInfo
);
27 if (!gameInfo
.players
.some(p
=> p
.id
== req
.user
.id
))
28 return res
.json({errmsg: "Cannot start someone else's game"});
29 let fen
= req
.body
.fen
;
31 gameInfo
.vid
, gameInfo
.fen
, gameInfo
.timeControl
, gameInfo
.players
,
33 access
.checkRequest(res
, err
, game
, "Cannot create game", () => {
34 if (!!req
.body
.offlineOpp
)
35 UserModel
.tryNotify(req
.body
.offlineOpp
, game
.id
, variant
.name
,
36 "New game: " + "game link"); //TODO: give game link
37 res
.json({game: game
});
43 router
.get("/games", access
.ajax
, (req
,res
) => {
44 const gameId
= req
.query
["gid"];
47 GameModel
.getOne(gameId
, (err
,game
) => {
48 access
.checkRequest(res
, err
, game
, "Game not found", () => {
49 res
.json({game: game
});
55 // Get by (non-)user ID:
56 const userId
= req
.query
["uid"];
57 const excluded
= !!req
.query
["excluded"];
58 GameModel
.getByUser(userId
, excluded
, (err
,games
) => {
59 access
.checkRequest(res
, err
, games
, "Games not found", () => {
60 res
.json({games: games
});
67 router
.put("/games", access
.logged
, access
.ajax
, (req
,res
) => {
68 let gid
= ObjectId(req
.body
.gid
);
69 let result
= req
.body
.result
;
70 // NOTE: only game-level life update is "gameover"
71 GameModel
.gameOver(gid
, result
, ObjectId(req
.user
._id
), (err
,game
) => {
72 access
.checkRequest(res
, err
, game
, "Cannot find game", () => {
78 // TODO: if newmove fail, takeback in GUI
79 // TODO: check move structure
80 // TODO: move should contain an optional "message" field ("corr chat" !)
81 router
.post("/moves", access
.logged
, access
.ajax
, (req
,res
) => {
82 let gid
= ObjectId(req
.body
.gid
);
83 let fen
= req
.body
.fen
;
84 let vname
= req
.body
.vname
; //defined only if !!offlineOpp
85 // NOTE: storing the moves encoded lead to double stringify --> error at parsing
86 let move = JSON
.parse(req
.body
.move);
87 GameModel
.addMove(gid
, move, fen
, req
._user
._id
, (err
,game
) => {
88 access
.checkRequest(res
, err
, game
, "Cannot find game", () => {
89 if (!!req
.body
.offlineOpp
)
90 UserModel
.tryNotify(ObjectId(req
.body
.offlineOpp
), gid
, vname
, "New move");
96 module
.exports
= router
;