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