X-Git-Url: https://git.auder.net/?p=vchess.git;a=blobdiff_plain;f=server%2Froutes%2Fgames.js;h=77877a2eebbcf6c8aeda9b84754fdc4ee94e4dc6;hp=a86a76ddedce9301a40c44232774252373f45356;hb=0234201fb338fc239d6f613c677fa932c7c3697c;hpb=585d095517ca2aedab8ad125cc7c39b90e13d5cc diff --git a/server/routes/games.js b/server/routes/games.js index a86a76dd..77877a2e 100644 --- a/server/routes/games.js +++ b/server/routes/games.js @@ -12,46 +12,58 @@ router.post("/games", access.logged, access.ajax, (req,res) => { const cid = req.body.cid; if ( Array.isArray(gameInfo.players) && - gameInfo.players.some(p => p.uid == req.userId) && + gameInfo.players.some(p => p.id == req.userId) && (!cid || cid.toString().match(/^[0-9]+$/)) && GameModel.checkGameInfo(gameInfo) ) { if (!!cid) ChallengeModel.remove(cid); GameModel.create( gameInfo.vid, gameInfo.fen, gameInfo.cadence, gameInfo.players, - (err,ret) => { + (err, ret) => { const oppIdx = (gameInfo.players[0].id == req.userId ? 1 : 0); const oppId = gameInfo.players[oppIdx].id; UserModel.tryNotify(oppId, - "Game started: " + params.siteURL + "/#/game/" + ret.gid); - res.json({gameId: ret.gid}); + "Game started: " + params.siteURL + "/#/game/" + ret.id); + res.json(err || ret); } ); } }); +// Get only one game (for Game page) router.get("/games", access.ajax, (req,res) => { const gameId = req.query["gid"]; - if (gameId) - { - if (gameId.match(/^[0-9]+$/)) - { - GameModel.getOne(gameId, (err,game) => { - res.json({game: game}); - }); - } + if (!!gameId && gameId.match(/^[0-9]+$/)) { + GameModel.getOne(gameId, (err, game) => { + res.json({ game: game }); + }); } - else - { - // Get by (non-)user ID: - const userId = req.query["uid"]; - if (userId.match(/^[0-9]+$/)) - { - const excluded = !!req.query["excluded"]; - GameModel.getByUser(userId, excluded, (err,games) => { - res.json({games: games}); - }); - } +}); + +// Get by (non-)user ID, for Hall +router.get("/observedgames", access.ajax, (req,res) => { + const userId = req.query["uid"]; + const cursor = req.query["cursor"]; + if (!!userId.match(/^[0-9]+$/) && !!cursor.match(/^[0-9]+$/)) { + GameModel.getObserved(userId, (err, games) => { + res.json({ games: games }); + }); + } +}); + +// Get by user ID, for MyGames page +router.get("/runninggames", access.ajax, access.logged, (req,res) => { + GameModel.getRunning(req.userId, (err, games) => { + res.json({ games: games }); + }); +}); + +router.get("/completedgames", access.ajax, access.logged, (req,res) => { + const cursor = req.query["cursor"]; + if (!!cursor.match(/^[0-9]+$/)) { + GameModel.getCompleted(req.userId, cursor, (err, games) => { + res.json({ games: games }); + }); } }); @@ -59,28 +71,29 @@ router.get("/games", access.ajax, (req,res) => { router.put("/games", access.logged, access.ajax, (req,res) => { const gid = req.body.gid; let obj = req.body.newObj; - if (gid.toString().match(/^[0-9]+$/) && GameModel.checkGameUpdate(obj)) - { + if (gid.toString().match(/^[0-9]+$/) && GameModel.checkGameUpdate(obj)) { GameModel.getPlayers(gid, (err,players) => { - const myIdx = players.findIndex(p => p.uid == req.userId) - if (myIdx >= 0) { + let myColor = ''; + if (players.white == req.userId) myColor = 'w'; + else if (players.black == req.userId) myColor = 'b'; + if (!!myColor) { // Did I mark the game for deletion? if (!!obj.removeFlag) { - obj.deletedBy = ["w","b"][myIdx]; + obj.deletedBy = myColor; delete obj["removeFlag"]; } GameModel.update(gid, obj, (err) => { - if (!err && (!!obj.move || !!obj.score)) - { + if (!err && (!!obj.move || !!obj.score)) { // Notify opponent if he enabled notifications: - const oppid = players[0].uid == req.userId - ? players[1].uid - : players[0].uid; - const messagePrefix = obj.move - ? "New move in game: " - : "Game ended: "; - UserModel.tryNotify(oppid, - messagePrefix + params.siteURL + "/#/game/" + gid); + const oppid = (myColor == 'w' ? players.black : players.white); + const messagePrefix = + !!obj.move + ? "New move in game: " + : "Game ended: "; + UserModel.tryNotify( + oppid, + messagePrefix + params.siteURL + "/#/game/" + gid + ); } res.json(err || {}); }); @@ -93,10 +106,10 @@ router.put("/games", access.logged, access.ajax, (req,res) => { // Moves update also could, although logical unit in a game. router.delete("/chats", access.logged, access.ajax, (req,res) => { const gid = req.query["gid"]; - GameModel.getPlayers(gid, (err,players) => { - if (players.some(p => p.uid == req.userId)) + GameModel.getPlayers(gid, (err, players) => { + if ([players.white, players.black].includes(req.userId)) { - GameModel.update(gid, {delchat: true}, () => { + GameModel.update(gid, { delchat: true }, () => { res.json({}); }); }