Commit | Line | Data |
---|---|---|
4edfed6c BA |
1 | router.get("/games", access.logged, access.ajax, (req,res) => { |
2 | const excluded = req.query["excluded"]; //TODO: think about query params here | |
3 | }); | |
4 | ||
8d7e2786 BA |
5 | var router = require("express").Router(); |
6 | var UserModel = require("../models/User"); | |
5d04793e | 7 | var sendEmail = require('../utils/mailer'); |
8d7e2786 BA |
8 | var GameModel = require('../models/Game'); |
9 | var VariantModel = require('../models/Variant'); | |
8d7e2786 | 10 | var access = require("../utils/access"); |
5d04793e | 11 | var params = require("../config/parameters"); |
8d7e2786 BA |
12 | |
13 | // Notify about a game (start, new move) | |
14 | function tryNotify(uid, gid, vname, subject) | |
15 | { | |
16 | UserModel.getOne("id", uid, (err,user) => { | |
17 | if (!!err && user.notify) | |
18 | { | |
5d04793e BA |
19 | sendEmail(params.mailFrom, user.email, subject, |
20 | params.siteURL + "?v=" + vname + "&g=" + gid, err => { | |
21 | res.json(err || {}); // TODO: log error somewhere. | |
22 | } | |
23 | ); | |
8d7e2786 BA |
24 | } |
25 | )}; | |
26 | } | |
27 | ||
5d04793e | 28 | // From main hall, start game between player 0 and 1 |
8d7e2786 | 29 | router.post("/games", access.logged, access.ajax, (req,res) => { |
5d04793e BA |
30 | const gameInfo = JSON.parse(req.body.gameInfo); |
31 | if (!gameInfo.players.some(p => p.id == req.user.id)) | |
8d7e2786 BA |
32 | return res.json({errmsg: "Cannot start someone else's game"}); |
33 | let fen = req.body.fen; | |
5d04793e BA |
34 | GameModel.create(gameInfo.vid, |
35 | gameInfo.fen, gameInfo.mainTime, gameInfo.increment, gameInfo.players, | |
8d7e2786 BA |
36 | (err,game) => { |
37 | access.checkRequest(res, err, game, "Cannot create game", () => { | |
38 | if (!!req.body.offlineOpp) | |
5d04793e BA |
39 | UserModel.tryNotify(req.body.offlineOpp, game.id, variant.name, |
40 | "New game: " + "game link"); //TODO: give game link | |
8d7e2786 BA |
41 | res.json({game: game}); |
42 | }); | |
43 | } | |
44 | ); | |
45 | }); | |
46 | ||
47 | // game page | |
48 | router.get("/games", access.ajax, (req,res) => { | |
5d04793e BA |
49 | const gameId = req.query["gid"]; |
50 | if (!!gameId) | |
51 | { | |
52 | GameModel.getOne(gameId, (err,game) => { | |
53 | access.checkRequest(res, err, game, "Game not found", () => { | |
54 | res.json({game: game}); | |
55 | }); | |
56 | }); | |
57 | } | |
58 | else | |
59 | { | |
60 | // Get by (non-)user ID: | |
61 | const userId = req.query["uid"]; | |
62 | const excluded = !!req.query["excluded"]; | |
63 | GameModel.getByUser(userId, excluded, (err,games) => { | |
64 | access.checkRequest(res, err, games, "Games not found", () => { | |
65 | res.json({games: games}); | |
66 | }); | |
67 | }); | |
68 | } | |
8d7e2786 BA |
69 | }); |
70 | ||
5d04793e | 71 | // TODO: |
8d7e2786 BA |
72 | router.put("/games", access.logged, access.ajax, (req,res) => { |
73 | let gid = ObjectId(req.body.gid); | |
74 | let result = req.body.result; | |
75 | // NOTE: only game-level life update is "gameover" | |
76 | GameModel.gameOver(gid, result, ObjectId(req.user._id), (err,game) => { | |
77 | access.checkRequest(res, err, game, "Cannot find game", () => { | |
78 | res.json({}); | |
79 | }); | |
80 | }); | |
81 | }); | |
82 | ||
83 | // variant page | |
84 | router.get("/gamesbyvariant", access.logged, access.ajax, (req,res) => { | |
85 | if (req.query["uid"] != req.user._id) | |
86 | return res.json({errmsg: "Not your games"}); | |
87 | let uid = ObjectId(req.query["uid"]); | |
88 | let vid = ObjectId(req.query["vid"]); | |
89 | GameModel.getByVariant(uid, vid, (err,gameArray) => { | |
90 | // NOTE: res.json already stringify, no need to do it manually | |
91 | res.json(err || {games: gameArray}); | |
92 | }); | |
93 | }); | |
94 | ||
95 | // For index: only moves count + myColor | |
96 | router.get("/gamesbyplayer", access.logged, access.ajax, (req,res) => { | |
97 | if (req.query["uid"] != req.user._id) | |
98 | return res.json({errmsg: "Not your games"}); | |
99 | let uid = ObjectId(req.query["uid"]); | |
100 | GameModel.getByPlayer(uid, (err,games) => { | |
101 | res.json(err || {games: games}); | |
102 | }); | |
103 | }); | |
104 | ||
00f2759e BA |
105 | // TODO: if newmove fail, takeback in GUI |
106 | // TODO: check move structure | |
fd08ab2c | 107 | // TODO: for corr games, move should contain an optional "message" field ("corr chat" !) |
8d7e2786 BA |
108 | router.post("/moves", access.logged, access.ajax, (req,res) => { |
109 | let gid = ObjectId(req.body.gid); | |
110 | let fen = req.body.fen; | |
111 | let vname = req.body.vname; //defined only if !!offlineOpp | |
112 | // NOTE: storing the moves encoded lead to double stringify --> error at parsing | |
113 | let move = JSON.parse(req.body.move); | |
114 | GameModel.addMove(gid, move, fen, req._user._id, (err,game) => { | |
115 | access.checkRequest(res, err, game, "Cannot find game", () => { | |
116 | if (!!req.body.offlineOpp) | |
117 | UserModel.tryNotify(ObjectId(req.body.offlineOpp), gid, vname, "New move"); | |
118 | res.json({}); | |
119 | }); | |
120 | }); | |
121 | }); | |
122 | ||
8d7e2786 | 123 | module.exports = router; |