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) && | |
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, | |
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, | |
479e6df4 | 26 | "Game started: " + params.siteURL + "/#/game/" + ret.gid); |
dac39588 | 27 | res.json({gameId: ret.gid}); |
866842c3 BA |
28 | } |
29 | ); | |
30 | } | |
8d7e2786 BA |
31 | }); |
32 | ||
8d7e2786 | 33 | router.get("/games", access.ajax, (req,res) => { |
dac39588 | 34 | const gameId = req.query["gid"]; |
866842c3 | 35 | if (gameId) |
5d04793e | 36 | { |
866842c3 BA |
37 | if (gameId.match(/^[0-9]+$/)) |
38 | { | |
aae89b49 | 39 | GameModel.getOne(gameId, (err,game) => { |
92b82def | 40 | res.json({game: game}); |
dac39588 | 41 | }); |
866842c3 | 42 | } |
5d04793e BA |
43 | } |
44 | else | |
45 | { | |
46 | // Get by (non-)user ID: | |
47 | const userId = req.query["uid"]; | |
866842c3 BA |
48 | if (userId.match(/^[0-9]+$/)) |
49 | { | |
50 | const excluded = !!req.query["excluded"]; | |
51 | GameModel.getByUser(userId, excluded, (err,games) => { | |
52 | res.json({games: games}); | |
53 | }); | |
54 | } | |
5d04793e | 55 | } |
8d7e2786 BA |
56 | }); |
57 | ||
db1f1f9a | 58 | // FEN update + score(Msg) + draw status / and new move + chats |
3d55deea | 59 | router.put("/games", access.logged, access.ajax, (req,res) => { |
f41ce580 | 60 | const gid = req.body.gid; |
aae89b49 | 61 | let obj = req.body.newObj; |
866842c3 | 62 | if (gid.toString().match(/^[0-9]+$/) && GameModel.checkGameUpdate(obj)) |
1ad003ff | 63 | { |
1ad003ff | 64 | GameModel.getPlayers(gid, (err,players) => { |
aae89b49 BA |
65 | const myIdx = players.findIndex(p => p.uid == req.userId) |
66 | if (myIdx >= 0) { | |
67 | // Did I mark the game for deletion? | |
68 | if (!!obj.removeFlag) { | |
69 | obj.deletedBy = ["w","b"][myIdx]; | |
70 | delete obj["removeFlag"]; | |
71 | } | |
fb68b0c2 | 72 | GameModel.update(gid, obj, (err) => { |
aae89b49 | 73 | if (!err && (!!obj.move || !!obj.score)) |
fb68b0c2 BA |
74 | { |
75 | // Notify opponent if he enabled notifications: | |
76 | const oppid = players[0].uid == req.userId | |
77 | ? players[1].uid | |
78 | : players[0].uid; | |
79 | const messagePrefix = obj.move | |
80 | ? "New move in game: " | |
81 | : "Game ended: "; | |
82 | UserModel.tryNotify(oppid, | |
83 | messagePrefix + params.siteURL + "/#/game/" + gid); | |
84 | } | |
85 | res.json(err || {}); | |
86 | }); | |
1ad003ff BA |
87 | } |
88 | }); | |
89 | } | |
8d7e2786 BA |
90 | }); |
91 | ||
db1f1f9a BA |
92 | // TODO: chats deletion here, but could/should be elsewhere. |
93 | // Moves update also could, although logical unit in a game. | |
94 | router.delete("/chats", access.logged, access.ajax, (req,res) => { | |
95 | const gid = req.query["gid"]; | |
96 | GameModel.getPlayers(gid, (err,players) => { | |
97 | if (players.some(p => p.uid == req.userId)) | |
98 | { | |
23ecf008 BA |
99 | GameModel.update(gid, {delchat: true}, () => { |
100 | res.json({}); | |
db1f1f9a BA |
101 | }); |
102 | } | |
103 | }); | |
104 | }); | |
105 | ||
8d7e2786 | 106 | module.exports = router; |