TODO: finish draw offer logic + fix inCheck bug (no highlight)
[vchess.git] / server / routes / games.js
CommitLineData
8d7e2786
BA
1var router = require("express").Router();
2var UserModel = require("../models/User");
2be5d614 3var ChallengeModel = require('../models/Challenge');
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 8
25996aed 9// From main hall, start game between players 0 and 1
8d7e2786 10router.post("/games", access.logged, access.ajax, (req,res) => {
8c564f46 11 const gameInfo = req.body.gameInfo;
dac39588 12 if (!Array.isArray(gameInfo.players) ||
58e7b94e
BA
13 !gameInfo.players.some(p => p.id == req.userId))
14 {
dac39588 15 return res.json({errmsg: "Cannot start someone else's game"});
58e7b94e 16 }
8c564f46 17 const cid = req.body.cid;
58e7b94e
BA
18 // Check all entries of gameInfo + cid:
19 let error = GameModel.checkGameInfo(gameInfo);
20 if (!error)
21 {
22 if (!cid.toString().match(/^[0-9]+$/))
23 error = "Wrong challenge ID";
24 }
25 if (!!error)
26 return res.json({errmsg:error});
2be5d614 27 ChallengeModel.remove(cid);
dac39588 28 GameModel.create(
25996aed 29 gameInfo.vid, gameInfo.fen, gameInfo.timeControl, gameInfo.players,
dac39588
BA
30 (err,ret) => {
31 access.checkRequest(res, err, ret, "Cannot create game", () => {
8c564f46 32 const oppIdx = (gameInfo.players[0].id == req.userId ? 1 : 0);
2be5d614
BA
33 const oppId = gameInfo.players[oppIdx].id;
34 UserModel.tryNotify(oppId,
8c564f46 35 "New game: " + params.siteURL + "/game/" + ret.gid);
dac39588
BA
36 res.json({gameId: ret.gid});
37 });
38 }
39 );
8d7e2786
BA
40});
41
8d7e2786 42router.get("/games", access.ajax, (req,res) => {
dac39588
BA
43 const gameId = req.query["gid"];
44 if (!!gameId)
5d04793e
BA
45 {
46 GameModel.getOne(gameId, (err,game) => {
dac39588 47 access.checkRequest(res, err, game, "Game not found", () => {
92b82def 48 res.json({game: game});
dac39588
BA
49 });
50 });
5d04793e
BA
51 }
52 else
53 {
54 // Get by (non-)user ID:
55 const userId = req.query["uid"];
56 const excluded = !!req.query["excluded"];
57 GameModel.getByUser(userId, excluded, (err,games) => {
dac39588 58 if (!!err)
098cd7f1 59 return res.json({errmsg: err.errmsg || err.toString()});
dac39588
BA
60 res.json({games: games});
61 });
5d04793e 62 }
8d7e2786
BA
63});
64
3d55deea 65// New move + fen update + score, potentially
00f2759e 66// TODO: if newmove fail, takeback in GUI
3d55deea 67router.put("/games", access.logged, access.ajax, (req,res) => {
f41ce580 68 const gid = req.body.gid;
58e7b94e
BA
69 let error = "";
70 if (!gid.toString().match(/^[0-9]+$/))
71 error = "Wrong game ID";
72 const obj = req.body.newObj;
dac39588 73 error = GameModel.checkGameUpdate(obj);
58e7b94e
BA
74 if (!!error)
75 return res.json({errmsg: error});
dac39588
BA
76 GameModel.update(gid, obj, (err) => {
77 if (!!err)
3d55deea 78 return res.json(err);
dfeb96ea
BA
79 if (!!obj.move || !!obj.score)
80 {
81 // Notify opponent if he enabled notifications:
82 GameModel.getPlayers(gid, (err2,players) => {
83 if (!err2)
84 {
85 const oppid = (players[0].id == req.userId
86 ? players[1].id
87 : players[0].id);
88 const messagePrefix = (!!obj.move
89 ? "New move in game: "
90 : "Game ended: ");
91 UserModel.tryNotify(oppid,
92 messagePrefix + params.siteURL + "/game/" + gid);
93 }
94 });
95 }
3d55deea 96 res.json({});
dac39588 97 });
8d7e2786
BA
98});
99
8d7e2786 100module.exports = router;