}
if (this.st.user.id > 0)
{
- // Ask server for current corr games (all but mines)
-// ajax(
-// "/games",
-// "GET",
-// {excluded: this.st.user.id},
-// response => {
-// this.games = this.games.concat(response.games);
-// }
-// );
- // Also ask for corr challenges (open + sent to me)
+ // Ask server for current corr games (all but mines)
+ ajax(
+ "/games",
+ "GET",
+ {uid: this.st.user.id, excluded: true},
+ response => {
+ this.games = this.games.concat(response.games);
+ }
+ );
+ // Also ask for corr challenges (open + sent to me)
ajax(
"/challenges",
"GET",
id: game.id,
players: game.players.map(p => p.name),
vname: game.vname,
+
+
+
+
+// TODO: timeControl only in challenge - no need in game (info in mainTime + increment)
+
+
+
+
timeControl: game.timeControl,
};
this.st.conn.send(JSON.stringify({code:"game",
}
case "newgame":
{
- // New game just started: data contain all informations
- this.startNewGame(data.gameInfo);
- // TODO: if live, redirect to game
- // if corr, notify with link but do not redirect
+ // New game just started: data contain all information
+ if (data.gameInfo.type == "live")
+ {
+ this.startNewGame(data.gameInfo);
+ // TODO: redirect to game
+ }
+ else
+ {
+ // TODO: notify with game link but do not redirect
+ }
break;
}
case "refusechallenge":
const vname = this.getVname(c.vid);
const vModule = await import("@/variants/" + vname + ".js");
window.V = vModule.VariantRules;
- let players = [c.from, c.seat];
// These game informations will be sent to other players
const gameInfo =
{
gameId: getRandString(),
fen: c.fen || V.GenRandInitFen(),
- // Players' names may be required if game start when a player is offline
- // Shuffle players order (white then black).
- players: shuffle(players.map(p => { return {name:p.name, sid:p.sid} })),
+ players: shuffle([c.from, c.seat]), //white then black
vid: c.vid,
timeControl: c.timeControl,
+ type: c.type,
};
this.st.conn.send(JSON.stringify({code:"newgame",
gameInfo:gameInfo, target:c.seat.sid}));
vname: this.getVname(gameInfo.vid),
fenStart: gameInfo.fen,
players: gameInfo.players,
- timeControl: gameInfo.timeControl,
+ mainTime: tc.mainTime,
increment: tc.increment,
mode: "live", //function for live games only
// Game state: will be updated
const excluded = req.query["excluded"]; //TODO: think about query params here
});
-// TODO: adapt for correspondance play
-
var router = require("express").Router();
var UserModel = require("../models/User");
+var sendEmail = require('../utils/mailer');
var GameModel = require('../models/Game');
var VariantModel = require('../models/Variant');
-var ObjectId = require("bson-objectid");
var access = require("../utils/access");
+var params = require("../config/parameters");
// Notify about a game (start, new move)
function tryNotify(uid, gid, vname, subject)
UserModel.getOne("id", uid, (err,user) => {
if (!!err && user.notify)
{
- maild.send({
- from: params.mailFrom,
- to: user.email,
- subject: subject,
- body: params.siteURL + "?v=" + vname + "&g=" + gid
- }, err => {
- // TODO: log error somewhere.
- });
+ sendEmail(params.mailFrom, user.email, subject,
+ params.siteURL + "?v=" + vname + "&g=" + gid, err => {
+ res.json(err || {}); // TODO: log error somewhere.
+ }
+ );
}
)};
}
-// From variant page, start game between player 0 and 1
+// From main hall, start game between player 0 and 1
router.post("/games", access.logged, access.ajax, (req,res) => {
- let variant = JSON.parse(req.body.variant);
- let players = JSON.parse(req.body.players);
- if (!players.includes(req.user._id.toString())) //TODO: should also check challenge...
+ const gameInfo = JSON.parse(req.body.gameInfo);
+ if (!gameInfo.players.some(p => p.id == req.user.id))
return res.json({errmsg: "Cannot start someone else's game"});
let fen = req.body.fen;
- // Randomly shuffle colors white/black
- if (Math.random() < 0.5)
- players = [players[1],players[0]];
- GameModel.create(
- ObjectId(variant._id), [ObjectId(players[0]),ObjectId(players[1])], fen,
+ GameModel.create(gameInfo.vid,
+ gameInfo.fen, gameInfo.mainTime, gameInfo.increment, gameInfo.players,
(err,game) => {
access.checkRequest(res, err, game, "Cannot create game", () => {
if (!!req.body.offlineOpp)
- UserModel.tryNotify(ObjectId(req.body.offlineOpp), game._id, variant.name, "New game");
- game.movesLength = game.moves.length; //no need for all moves here
- delete game["moves"];
+ UserModel.tryNotify(req.body.offlineOpp, game.id, variant.name,
+ "New game: " + "game link"); //TODO: give game link
res.json({game: game});
});
}
// game page
router.get("/games", access.ajax, (req,res) => {
- let gameID = req.query["gid"];
- GameModel.getById(ObjectId(gameID), (err,game) => {
- access.checkRequest(res, err, game, "Game not found", () => {
- res.json({game: game});
- });
- });
+ const gameId = req.query["gid"];
+ if (!!gameId)
+ {
+ GameModel.getOne(gameId, (err,game) => {
+ access.checkRequest(res, err, game, "Game not found", () => {
+ res.json({game: game});
+ });
+ });
+ }
+ else
+ {
+ // Get by (non-)user ID:
+ const userId = req.query["uid"];
+ const excluded = !!req.query["excluded"];
+ GameModel.getByUser(userId, excluded, (err,games) => {
+ access.checkRequest(res, err, games, "Games not found", () => {
+ res.json({games: games});
+ });
+ });
+ }
});
+// TODO:
router.put("/games", access.logged, access.ajax, (req,res) => {
let gid = ObjectId(req.body.gid);
let result = req.body.result;