Fix Empire chess. Still need to finish writing Synochess rules
[vchess.git] / server / routes / games.js
CommitLineData
fe4c7e67
BA
1let router = require("express").Router();
2const UserModel = require("../models/User");
3const ChallengeModel = require('../models/Challenge');
4const GameModel = require('../models/Game');
fe4c7e67
BA
5const access = require("../utils/access");
6const params = require("../config/parameters");
8d7e2786 7
25996aed 8// From main hall, start game between players 0 and 1
8d7e2786 9router.post("/games", access.logged, access.ajax, (req,res) => {
8c564f46 10 const gameInfo = req.body.gameInfo;
c292ebb2 11 // Challenge ID is provided if game start from Hall:
8c564f46 12 const cid = req.body.cid;
866842c3
BA
13 if (
14 Array.isArray(gameInfo.players) &&
0234201f 15 gameInfo.players.some(p => p.id == req.userId) &&
f14572c4 16 (!cid || !!cid.toString().match(/^[0-9]+$/)) &&
866842c3
BA
17 GameModel.checkGameInfo(gameInfo)
18 ) {
c292ebb2 19 if (!!cid) ChallengeModel.remove(cid);
866842c3 20 GameModel.create(
f14572c4
BA
21 gameInfo.vid, gameInfo.fen, gameInfo.randomness,
22 gameInfo.cadence, gameInfo.players,
0234201f 23 (err, ret) => {
8c564f46 24 const oppIdx = (gameInfo.players[0].id == req.userId ? 1 : 0);
2be5d614
BA
25 const oppId = gameInfo.players[oppIdx].id;
26 UserModel.tryNotify(oppId,
0234201f
BA
27 "Game started: " + params.siteURL + "/#/game/" + ret.id);
28 res.json(err || ret);
866842c3
BA
29 }
30 );
31 }
8d7e2786
BA
32});
33
0234201f 34// Get only one game (for Game page)
8d7e2786 35router.get("/games", access.ajax, (req,res) => {
dac39588 36 const gameId = req.query["gid"];
0234201f
BA
37 if (!!gameId && gameId.match(/^[0-9]+$/)) {
38 GameModel.getOne(gameId, (err, game) => {
f14572c4 39 res.json(err || { game: game });
0234201f 40 });
5d04793e 41 }
0234201f
BA
42});
43
44// Get by (non-)user ID, for Hall
45router.get("/observedgames", access.ajax, (req,res) => {
46 const userId = req.query["uid"];
47 const cursor = req.query["cursor"];
48 if (!!userId.match(/^[0-9]+$/) && !!cursor.match(/^[0-9]+$/)) {
f14572c4 49 GameModel.getObserved(userId, cursor, (err, games) => {
0234201f
BA
50 res.json({ games: games });
51 });
52 }
53});
54
55// Get by user ID, for MyGames page
f14572c4 56router.get("/runninggames", access.logged, access.ajax, (req,res) => {
0234201f
BA
57 GameModel.getRunning(req.userId, (err, games) => {
58 res.json({ games: games });
59 });
60});
61
f14572c4 62router.get("/completedgames", access.logged, access.ajax, (req,res) => {
0234201f
BA
63 const cursor = req.query["cursor"];
64 if (!!cursor.match(/^[0-9]+$/)) {
65 GameModel.getCompleted(req.userId, cursor, (err, games) => {
66 res.json({ games: games });
67 });
5d04793e 68 }
8d7e2786
BA
69});
70
db1f1f9a 71// FEN update + score(Msg) + draw status / and new move + chats
3d55deea 72router.put("/games", access.logged, access.ajax, (req,res) => {
f41ce580 73 const gid = req.body.gid;
aae89b49 74 let obj = req.body.newObj;
0234201f 75 if (gid.toString().match(/^[0-9]+$/) && GameModel.checkGameUpdate(obj)) {
f14572c4 76 GameModel.getPlayers(gid, (err, players) => {
0234201f
BA
77 let myColor = '';
78 if (players.white == req.userId) myColor = 'w';
79 else if (players.black == req.userId) myColor = 'b';
80 if (!!myColor) {
aae89b49
BA
81 // Did I mark the game for deletion?
82 if (!!obj.removeFlag) {
0234201f 83 obj.deletedBy = myColor;
aae89b49
BA
84 delete obj["removeFlag"];
85 }
fb68b0c2 86 GameModel.update(gid, obj, (err) => {
0234201f 87 if (!err && (!!obj.move || !!obj.score)) {
fb68b0c2 88 // Notify opponent if he enabled notifications:
0234201f
BA
89 const oppid = (myColor == 'w' ? players.black : players.white);
90 const messagePrefix =
91 !!obj.move
f14572c4
BA
92 ? "New move in game : "
93 : "Game ended : ";
0234201f
BA
94 UserModel.tryNotify(
95 oppid,
96 messagePrefix + params.siteURL + "/#/game/" + gid
97 );
fb68b0c2
BA
98 }
99 res.json(err || {});
100 });
1ad003ff
BA
101 }
102 });
103 }
8d7e2786
BA
104});
105
db1f1f9a
BA
106// TODO: chats deletion here, but could/should be elsewhere.
107// Moves update also could, although logical unit in a game.
108router.delete("/chats", access.logged, access.ajax, (req,res) => {
109 const gid = req.query["gid"];
0234201f
BA
110 GameModel.getPlayers(gid, (err, players) => {
111 if ([players.white, players.black].includes(req.userId))
db1f1f9a 112 {
0234201f 113 GameModel.update(gid, { delchat: true }, () => {
23ecf008 114 res.json({});
db1f1f9a
BA
115 });
116 }
117 });
118});
119
8d7e2786 120module.exports = router;