1 let router
= require("express").Router();
2 const UserModel
= require("../models/User");
3 const ChallengeModel
= require('../models/Challenge');
4 const GameModel
= require('../models/Game');
5 const access
= require("../utils/access");
6 const params
= require("../config/parameters");
8 // From main hall, start game between players 0 and 1
9 router
.post("/games", access
.logged
, access
.ajax
, (req
,res
) => {
10 const gameInfo
= req
.body
.gameInfo
;
11 // Challenge ID is provided if game start from Hall:
12 const cid
= req
.body
.cid
;
14 Array
.isArray(gameInfo
.players
) &&
15 gameInfo
.players
.some(p
=> p
.id
== req
.userId
) &&
16 (!cid
|| !!cid
.toString().match(/^[0-9]+$/)) &&
17 GameModel
.checkGameInfo(gameInfo
)
19 if (!!cid
) ChallengeModel
.remove(cid
);
21 gameInfo
.vid
, gameInfo
.fen
, gameInfo
.randomness
,
22 gameInfo
.cadence
, gameInfo
.players
,
24 const oppIdx
= (gameInfo
.players
[0].id
== req
.userId
? 1 : 0);
25 const oppId
= gameInfo
.players
[oppIdx
].id
;
26 UserModel
.tryNotify(oppId
,
27 "Game started: " + params
.siteURL
+ "/#/game/" + ret
.id
);
34 // Get only one game (for Game page)
35 router
.get("/games", access
.ajax
, (req
,res
) => {
36 const gameId
= req
.query
["gid"];
37 if (!!gameId
&& gameId
.match(/^[0-9]+$/)) {
38 GameModel
.getOne(gameId
, (err
, game
) => {
39 res
.json(err
|| { game: game
});
44 // Get by (non-)user ID, for Hall
45 router
.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]+$/)) {
49 GameModel
.getObserved(userId
, cursor
, (err
, games
) => {
50 res
.json({ games: games
});
55 // Get by user ID, for MyGames page
56 router
.get("/runninggames", access
.logged
, access
.ajax
, (req
,res
) => {
57 GameModel
.getRunning(req
.userId
, (err
, games
) => {
58 res
.json({ games: games
});
62 router
.get("/completedgames", access
.logged
, access
.ajax
, (req
,res
) => {
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
});
71 // FEN update + score(Msg) + draw status / and new move + chats
72 router
.put("/games", access
.logged
, access
.ajax
, (req
,res
) => {
73 const gid
= req
.body
.gid
;
74 let obj
= req
.body
.newObj
;
75 if (gid
.toString().match(/^[0-9]+$/) && GameModel
.checkGameUpdate(obj
)) {
76 GameModel
.getPlayers(gid
, (err
, players
) => {
78 if (players
.white
== req
.userId
) myColor
= 'w';
79 else if (players
.black
== req
.userId
) myColor
= 'b';
81 // Did I mark the game for deletion?
82 if (!!obj
.removeFlag
) {
83 obj
.deletedBy
= myColor
;
84 delete obj
["removeFlag"];
86 GameModel
.update(gid
, obj
, (err
) => {
87 if (!err
&& (!!obj
.move || !!obj
.score
)) {
88 // Notify opponent if he enabled notifications:
89 const oppid
= (myColor
== 'w' ? players
.black : players
.white
);
92 ? "New move in game : "
96 messagePrefix
+ params
.siteURL
+ "/#/game/" + gid
106 // TODO: chats deletion here, but could/should be elsewhere.
107 // Moves update also could, although logical unit in a game.
108 router
.delete("/chats", access
.logged
, access
.ajax
, (req
,res
) => {
109 const gid
= req
.query
["gid"];
110 GameModel
.getPlayers(gid
, (err
, players
) => {
111 if ([players
.white
, players
.black
].includes(req
.userId
))
113 GameModel
.update(gid
, { delchat: true }, () => {
120 module
.exports
= router
;