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