1 router
.get("/games", access
.logged
, access
.ajax
, (req
,res
) => {
2 const excluded
= req
.query
["excluded"]; //TODO: think about query params here
5 var router
= require("express").Router();
6 var UserModel
= require("../models/User");
7 var sendEmail
= require('../utils/mailer');
8 var GameModel
= require('../models/Game');
9 var VariantModel
= require('../models/Variant');
10 var access
= require("../utils/access");
11 var params
= require("../config/parameters");
13 // Notify about a game (start, new move)
14 function tryNotify(uid
, gid
, vname
, subject
)
16 UserModel
.getOne("id", uid
, (err
,user
) => {
17 if (!!err
&& user
.notify
)
19 sendEmail(params
.mailFrom
, user
.email
, subject
,
20 params
.siteURL
+ "?v=" + vname
+ "&g=" + gid
, err
=> {
21 res
.json(err
|| {}); // TODO: log error somewhere.
28 // From main hall, start game between player 0 and 1
29 router
.post("/games", access
.logged
, access
.ajax
, (req
,res
) => {
30 const gameInfo
= JSON
.parse(req
.body
.gameInfo
);
31 if (!gameInfo
.players
.some(p
=> p
.id
== req
.user
.id
))
32 return res
.json({errmsg: "Cannot start someone else's game"});
33 let fen
= req
.body
.fen
;
34 GameModel
.create(gameInfo
.vid
,
35 gameInfo
.fen
, gameInfo
.mainTime
, gameInfo
.increment
, gameInfo
.players
,
37 access
.checkRequest(res
, err
, game
, "Cannot create game", () => {
38 if (!!req
.body
.offlineOpp
)
39 UserModel
.tryNotify(req
.body
.offlineOpp
, game
.id
, variant
.name
,
40 "New game: " + "game link"); //TODO: give game link
41 res
.json({game: game
});
48 router
.get("/games", access
.ajax
, (req
,res
) => {
49 const gameId
= req
.query
["gid"];
52 GameModel
.getOne(gameId
, (err
,game
) => {
53 access
.checkRequest(res
, err
, game
, "Game not found", () => {
54 res
.json({game: game
});
60 // Get by (non-)user ID:
61 const userId
= req
.query
["uid"];
62 const excluded
= !!req
.query
["excluded"];
63 GameModel
.getByUser(userId
, excluded
, (err
,games
) => {
64 access
.checkRequest(res
, err
, games
, "Games not found", () => {
65 res
.json({games: games
});
72 router
.put("/games", access
.logged
, access
.ajax
, (req
,res
) => {
73 let gid
= ObjectId(req
.body
.gid
);
74 let result
= req
.body
.result
;
75 // NOTE: only game-level life update is "gameover"
76 GameModel
.gameOver(gid
, result
, ObjectId(req
.user
._id
), (err
,game
) => {
77 access
.checkRequest(res
, err
, game
, "Cannot find game", () => {
84 router
.get("/gamesbyvariant", access
.logged
, access
.ajax
, (req
,res
) => {
85 if (req
.query
["uid"] != req
.user
._id
)
86 return res
.json({errmsg: "Not your games"});
87 let uid
= ObjectId(req
.query
["uid"]);
88 let vid
= ObjectId(req
.query
["vid"]);
89 GameModel
.getByVariant(uid
, vid
, (err
,gameArray
) => {
90 // NOTE: res.json already stringify, no need to do it manually
91 res
.json(err
|| {games: gameArray
});
95 // For index: only moves count + myColor
96 router
.get("/gamesbyplayer", access
.logged
, access
.ajax
, (req
,res
) => {
97 if (req
.query
["uid"] != req
.user
._id
)
98 return res
.json({errmsg: "Not your games"});
99 let uid
= ObjectId(req
.query
["uid"]);
100 GameModel
.getByPlayer(uid
, (err
,games
) => {
101 res
.json(err
|| {games: games
});
105 // TODO: if newmove fail, takeback in GUI
106 // TODO: check move structure
107 // TODO: for corr games, move should contain an optional "message" field ("corr chat" !)
108 router
.post("/moves", access
.logged
, access
.ajax
, (req
,res
) => {
109 let gid
= ObjectId(req
.body
.gid
);
110 let fen
= req
.body
.fen
;
111 let vname
= req
.body
.vname
; //defined only if !!offlineOpp
112 // NOTE: storing the moves encoded lead to double stringify --> error at parsing
113 let move = JSON
.parse(req
.body
.move);
114 GameModel
.addMove(gid
, move, fen
, req
._user
._id
, (err
,game
) => {
115 access
.checkRequest(res
, err
, game
, "Cannot find game", () => {
116 if (!!req
.body
.offlineOpp
)
117 UserModel
.tryNotify(ObjectId(req
.body
.offlineOpp
), gid
, vname
, "New move");
123 module
.exports
= router
;