Fix ajax for challenge creation
[vchess.git] / server / routes / games.js
CommitLineData
8d7e2786
BA
1var router = require("express").Router();
2var UserModel = require("../models/User");
5d04793e 3var sendEmail = require('../utils/mailer');
8d7e2786
BA
4var GameModel = require('../models/Game');
5var VariantModel = require('../models/Variant');
8d7e2786 6var access = require("../utils/access");
5d04793e 7var params = require("../config/parameters");
8d7e2786
BA
8
9// Notify about a game (start, new move)
10function tryNotify(uid, gid, vname, subject)
11{
12 UserModel.getOne("id", uid, (err,user) => {
13 if (!!err && user.notify)
14 {
5d04793e
BA
15 sendEmail(params.mailFrom, user.email, subject,
16 params.siteURL + "?v=" + vname + "&g=" + gid, err => {
17 res.json(err || {}); // TODO: log error somewhere.
18 }
19 );
8d7e2786 20 }
88667455 21 });
8d7e2786
BA
22}
23
25996aed 24// From main hall, start game between players 0 and 1
8d7e2786 25router.post("/games", access.logged, access.ajax, (req,res) => {
5d04793e
BA
26 const gameInfo = JSON.parse(req.body.gameInfo);
27 if (!gameInfo.players.some(p => p.id == req.user.id))
8d7e2786
BA
28 return res.json({errmsg: "Cannot start someone else's game"});
29 let fen = req.body.fen;
25996aed
BA
30 GameModel.create(
31 gameInfo.vid, gameInfo.fen, gameInfo.timeControl, gameInfo.players,
8d7e2786
BA
32 (err,game) => {
33 access.checkRequest(res, err, game, "Cannot create game", () => {
34 if (!!req.body.offlineOpp)
5d04793e
BA
35 UserModel.tryNotify(req.body.offlineOpp, game.id, variant.name,
36 "New game: " + "game link"); //TODO: give game link
8d7e2786
BA
37 res.json({game: game});
38 });
39 }
40 );
41});
42
8d7e2786 43router.get("/games", access.ajax, (req,res) => {
5d04793e
BA
44 const gameId = req.query["gid"];
45 if (!!gameId)
46 {
47 GameModel.getOne(gameId, (err,game) => {
48 access.checkRequest(res, err, game, "Game not found", () => {
49 res.json({game: game});
50 });
51 });
52 }
53 else
54 {
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) => {
098cd7f1
BA
59 if (!!err)
60 return res.json({errmsg: err.errmsg || err.toString()});
61 res.json({games: games});
5d04793e
BA
62 });
63 }
8d7e2786
BA
64});
65
5d04793e 66// TODO:
8d7e2786
BA
67router.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", () => {
73 res.json({});
74 });
75 });
76});
77
00f2759e
BA
78// TODO: if newmove fail, takeback in GUI
79// TODO: check move structure
a9b131f1 80// TODO: move should contain an optional "message" field ("corr chat" !)
8d7e2786
BA
81router.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");
91 res.json({});
92 });
93 });
94});
95
8d7e2786 96module.exports = router;