Ongoing work on challenges + games for correspondance play
[vchess.git] / server / routes / games.js
1 router.get("/games", access.logged, access.ajax, (req,res) => {
2 const excluded = req.query["excluded"]; //TODO: think about query params here
3 });
4
5 // TODO: adapt for correspondance play
6
7 var router = require("express").Router();
8 var UserModel = require("../models/User");
9 var GameModel = require('../models/Game');
10 var VariantModel = require('../models/Variant');
11 var ObjectId = require("bson-objectid");
12 var access = require("../utils/access");
13
14 // Notify about a game (start, new move)
15 function tryNotify(uid, gid, vname, subject)
16 {
17 UserModel.getOne("id", uid, (err,user) => {
18 if (!!err && user.notify)
19 {
20 maild.send({
21 from: params.mailFrom,
22 to: user.email,
23 subject: subject,
24 body: params.siteURL + "?v=" + vname + "&g=" + gid
25 }, err => {
26 // TODO: log error somewhere.
27 });
28 }
29 )};
30 }
31
32 // From variant page, start game between player 0 and 1
33 router.post("/games", access.logged, access.ajax, (req,res) => {
34 let variant = JSON.parse(req.body.variant);
35 let players = JSON.parse(req.body.players);
36 if (!players.includes(req.user._id.toString())) //TODO: should also check challenge...
37 return res.json({errmsg: "Cannot start someone else's game"});
38 let fen = req.body.fen;
39 // Randomly shuffle colors white/black
40 if (Math.random() < 0.5)
41 players = [players[1],players[0]];
42 GameModel.create(
43 ObjectId(variant._id), [ObjectId(players[0]),ObjectId(players[1])], fen,
44 (err,game) => {
45 access.checkRequest(res, err, game, "Cannot create game", () => {
46 if (!!req.body.offlineOpp)
47 UserModel.tryNotify(ObjectId(req.body.offlineOpp), game._id, variant.name, "New game");
48 game.movesLength = game.moves.length; //no need for all moves here
49 delete game["moves"];
50 res.json({game: game});
51 });
52 }
53 );
54 });
55
56 // game page
57 router.get("/games", access.ajax, (req,res) => {
58 let gameID = req.query["gid"];
59 GameModel.getById(ObjectId(gameID), (err,game) => {
60 access.checkRequest(res, err, game, "Game not found", () => {
61 res.json({game: game});
62 });
63 });
64 });
65
66 router.put("/games", access.logged, access.ajax, (req,res) => {
67 let gid = ObjectId(req.body.gid);
68 let result = req.body.result;
69 // NOTE: only game-level life update is "gameover"
70 GameModel.gameOver(gid, result, ObjectId(req.user._id), (err,game) => {
71 access.checkRequest(res, err, game, "Cannot find game", () => {
72 res.json({});
73 });
74 });
75 });
76
77 // variant page
78 router.get("/gamesbyvariant", access.logged, access.ajax, (req,res) => {
79 if (req.query["uid"] != req.user._id)
80 return res.json({errmsg: "Not your games"});
81 let uid = ObjectId(req.query["uid"]);
82 let vid = ObjectId(req.query["vid"]);
83 GameModel.getByVariant(uid, vid, (err,gameArray) => {
84 // NOTE: res.json already stringify, no need to do it manually
85 res.json(err || {games: gameArray});
86 });
87 });
88
89 // For index: only moves count + myColor
90 router.get("/gamesbyplayer", access.logged, access.ajax, (req,res) => {
91 if (req.query["uid"] != req.user._id)
92 return res.json({errmsg: "Not your games"});
93 let uid = ObjectId(req.query["uid"]);
94 GameModel.getByPlayer(uid, (err,games) => {
95 res.json(err || {games: games});
96 });
97 });
98
99 // TODO: if newmove fail, takeback in GUI
100 // TODO: check move structure
101 // TODO: for corr games, move should contain an optional "message" field ("corr chat" !)
102 router.post("/moves", access.logged, access.ajax, (req,res) => {
103 let gid = ObjectId(req.body.gid);
104 let fen = req.body.fen;
105 let vname = req.body.vname; //defined only if !!offlineOpp
106 // NOTE: storing the moves encoded lead to double stringify --> error at parsing
107 let move = JSON.parse(req.body.move);
108 GameModel.addMove(gid, move, fen, req._user._id, (err,game) => {
109 access.checkRequest(res, err, game, "Cannot find game", () => {
110 if (!!req.body.offlineOpp)
111 UserModel.tryNotify(ObjectId(req.body.offlineOpp), gid, vname, "New move");
112 res.json({});
113 });
114 });
115 });
116
117 module.exports = router;