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