Commit | Line | Data |
---|---|---|
8d7e2786 BA |
1 | var router = require("express").Router(); |
2 | var UserModel = require("../models/User"); | |
2be5d614 | 3 | var ChallengeModel = require('../models/Challenge'); |
8d7e2786 BA |
4 | var GameModel = require('../models/Game'); |
5 | var VariantModel = require('../models/Variant'); | |
8d7e2786 | 6 | var access = require("../utils/access"); |
5d04793e | 7 | var params = require("../config/parameters"); |
8d7e2786 | 8 | |
25996aed | 9 | // From main hall, start game between players 0 and 1 |
8d7e2786 | 10 | router.post("/games", access.logged, access.ajax, (req,res) => { |
8c564f46 BA |
11 | const gameInfo = req.body.gameInfo; |
12 | if (!gameInfo.players.some(p => p.id == req.userId)) | |
8d7e2786 | 13 | return res.json({errmsg: "Cannot start someone else's game"}); |
8c564f46 | 14 | const cid = req.body.cid; |
2be5d614 BA |
15 | ChallengeModel.remove(cid); |
16 | const fen = req.body.fen; | |
25996aed BA |
17 | GameModel.create( |
18 | gameInfo.vid, gameInfo.fen, gameInfo.timeControl, gameInfo.players, | |
2be5d614 | 19 | (err,ret) => { |
8c564f46 BA |
20 | access.checkRequest(res, err, ret, "Cannot create game", () => { |
21 | const oppIdx = (gameInfo.players[0].id == req.userId ? 1 : 0); | |
2be5d614 BA |
22 | const oppId = gameInfo.players[oppIdx].id; |
23 | UserModel.tryNotify(oppId, | |
8c564f46 | 24 | "New game: " + params.siteURL + "/game/" + ret.gid); |
2be5d614 | 25 | res.json({gameId: ret.gid}); |
8d7e2786 BA |
26 | }); |
27 | } | |
28 | ); | |
29 | }); | |
30 | ||
8d7e2786 | 31 | router.get("/games", access.ajax, (req,res) => { |
5d04793e BA |
32 | const gameId = req.query["gid"]; |
33 | if (!!gameId) | |
34 | { | |
35 | GameModel.getOne(gameId, (err,game) => { | |
36 | access.checkRequest(res, err, game, "Game not found", () => { | |
37 | res.json({game: game}); | |
38 | }); | |
39 | }); | |
40 | } | |
41 | else | |
42 | { | |
43 | // Get by (non-)user ID: | |
44 | const userId = req.query["uid"]; | |
45 | const excluded = !!req.query["excluded"]; | |
46 | GameModel.getByUser(userId, excluded, (err,games) => { | |
098cd7f1 BA |
47 | if (!!err) |
48 | return res.json({errmsg: err.errmsg || err.toString()}); | |
49 | res.json({games: games}); | |
5d04793e BA |
50 | }); |
51 | } | |
8d7e2786 BA |
52 | }); |
53 | ||
3d55deea | 54 | // New move + fen update + score, potentially |
00f2759e | 55 | // TODO: if newmove fail, takeback in GUI |
3d55deea BA |
56 | router.put("/games", access.logged, access.ajax, (req,res) => { |
57 | const gid = req.body.gid; | |
411d23cd | 58 | const oppId = req.body.oppId; |
3d55deea BA |
59 | const obj = req.body.newObj; |
60 | GameModel.update(gid, obj, (err) => { | |
61 | if (!!err) | |
62 | return res.json(err); | |
411d23cd BA |
63 | // Notify opponent if he enabled notifications: |
64 | GameModel.getPlayers(gid, (err2,players) => { | |
65 | if (!!err2) | |
66 | return res.json(err); | |
67 | const oppid = (players[0].id == req.userId ? players[1].id : players[0].id); | |
68 | UserModel.tryNotify(oppid, | |
69 | "New move in game: " + params.siteURL + "/game/" + gid); | |
70 | }); | |
3d55deea | 71 | res.json({}); |
8d7e2786 BA |
72 | }); |
73 | }); | |
74 | ||
8d7e2786 | 75 | module.exports = router; |