1 var router
= require("express").Router();
2 var UserModel
= require("../models/User");
3 var GameModel
= require('../models/Game');
4 var VariantModel
= require('../models/Variant');
5 var ObjectId
= require("bson-objectid");
6 var access
= require("../utils/access");
8 // Notify about a game (start, new move)
9 function tryNotify(uid
, gid
, vname
, subject
)
11 UserModel
.getOne("id", uid
, (err
,user
) => {
12 if (!!err
&& user
.notify
)
15 from: params
.mailFrom
,
18 body: params
.siteURL
+ "?v=" + vname
+ "&g=" + gid
20 // TODO: log error somewhere.
26 // From variant page, start game between player 0 and 1
27 router
.post("/games", access
.logged
, access
.ajax
, (req
,res
) => {
28 let variant
= JSON
.parse(req
.body
.variant
);
29 let players
= JSON
.parse(req
.body
.players
);
30 if (!players
.includes(req
.user
._id
.toString())) //TODO: should also check challenge...
31 return res
.json({errmsg: "Cannot start someone else's game"});
32 let fen
= req
.body
.fen
;
33 // Randomly shuffle colors white/black
34 if (Math
.random() < 0.5)
35 players
= [players
[1],players
[0]];
37 ObjectId(variant
._id
), [ObjectId(players
[0]),ObjectId(players
[1])], fen
,
39 access
.checkRequest(res
, err
, game
, "Cannot create game", () => {
40 if (!!req
.body
.offlineOpp
)
41 UserModel
.tryNotify(ObjectId(req
.body
.offlineOpp
), game
._id
, variant
.name
, "New game");
42 game
.movesLength
= game
.moves
.length
; //no need for all moves here
44 res
.json({game: game
});
51 router
.get("/games", access
.ajax
, (req
,res
) => {
52 let gameID
= req
.query
["gid"];
53 GameModel
.getById(ObjectId(gameID
), (err
,game
) => {
54 access
.checkRequest(res
, err
, game
, "Game not found", () => {
55 res
.json({game: game
});
60 router
.put("/games", access
.logged
, access
.ajax
, (req
,res
) => {
61 let gid
= ObjectId(req
.body
.gid
);
62 let result
= req
.body
.result
;
63 // NOTE: only game-level life update is "gameover"
64 GameModel
.gameOver(gid
, result
, ObjectId(req
.user
._id
), (err
,game
) => {
65 access
.checkRequest(res
, err
, game
, "Cannot find game", () => {
72 router
.get("/gamesbyvariant", access
.logged
, access
.ajax
, (req
,res
) => {
73 if (req
.query
["uid"] != req
.user
._id
)
74 return res
.json({errmsg: "Not your games"});
75 let uid
= ObjectId(req
.query
["uid"]);
76 let vid
= ObjectId(req
.query
["vid"]);
77 GameModel
.getByVariant(uid
, vid
, (err
,gameArray
) => {
78 // NOTE: res.json already stringify, no need to do it manually
79 res
.json(err
|| {games: gameArray
});
83 // For index: only moves count + myColor
84 router
.get("/gamesbyplayer", 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 GameModel
.getByPlayer(uid
, (err
,games
) => {
89 res
.json(err
|| {games: games
});
94 router
.get("/rules/:variant([a-zA-Z0-9]+)", access
.ajax
, (req
,res
) => {
95 res
.render("rules/" + req
.params
["variant"]);
98 // TODO: if newmove fail, takeback in GUI // TODO: check move structure
99 router
.post("/moves", access
.logged
, access
.ajax
, (req
,res
) => {
100 let gid
= ObjectId(req
.body
.gid
);
101 let fen
= req
.body
.fen
;
102 let vname
= req
.body
.vname
; //defined only if !!offlineOpp
103 // NOTE: storing the moves encoded lead to double stringify --> error at parsing
104 let move = JSON
.parse(req
.body
.move);
105 GameModel
.addMove(gid
, move, fen
, req
._user
._id
, (err
,game
) => {
106 access
.checkRequest(res
, err
, game
, "Cannot find game", () => {
107 if (!!req
.body
.offlineOpp
)
108 UserModel
.tryNotify(ObjectId(req
.body
.offlineOpp
), gid
, vname
, "New move");
114 //TODO: if new chat fails, do not show chat message locally
115 router
.post("/chats", access
.logged
, access
.ajax
, (req
,res
) => {
116 let gid
= ObjectId(req
.body
.gid
);
117 let uid
= ObjectId(req
.body
.uid
);
118 let msg
= req
.body
.msg
; //TODO: sanitize HTML (strip all tags...)
119 GameModel
.addChat(gid
, uid
, msg
, (err
,game
) => {
120 access
.checkRequest(res
, err
, game
, "Cannot find game", () => {
126 module
.exports
= router
;