{
db.serialize(function() {
let query =
- "INSERT INTO Challenges (added, uid, vid, nbPlayers, fen, mainTime, addTime) " +
- "VALUES (" + Date.now() + "," + c.uid + "," + c.vid + "," + c.nbPlayers + "," +
- c.fen + "," + c.mainTime + "," + c.increment + ")";
+ "INSERT INTO Challenges " +
+ "(added, uid, vid, nbPlayers, fen, mainTime, addTime) VALUES " +
+ "(" + Date.now() + "," + c.uid + "," + c.vid + "," + c.nbPlayers +
+ ",'" + c.fen + "'," + c.mainTime + "," + c.increment + ")";
db.run(query, err => {
if (!!err)
return cb(err);
db.get("SELECT last_insert_rowid() AS rowid", (err2,lastId) => {
query =
"INSERT INTO WillPlay VALUES " +
- "(" + lastId["rowid"] + "," + uid + ")";
+ "(" + lastId["rowid"] + "," + c.uid + ")";
db.run(query, (err,ret) => {
cb(err, lastId); //all we need is the challenge ID
});
return cb(err2);
const challenge = {
id: id,
+ uid: challengeInfo.uid,
vname: challengeInfo.name,
added: challengeInfo.added,
nbPlayers: challengeInfo.nbPlayers,
fen: "",
vid: 0,
nbPlayers: 0,
- players: ["","",""],
+ players: [{id:0,name:""},{id:0,name:""},{id:0,name:""}],
mainTime: 0,
increment: 0,
},
<fieldset>
<label for="selectVariant">{{ translate("Variant") }}</label>
<select id="selectVariant" v-model="newgameInfo.vid">
- <option v-for="v in variants" :value="v.id">{{ v.name }}</option>
+ <option v-for="v in variants" :value="v.id">
+ {{ v.name }}
+ </option>
</select>
</fieldset>
<fieldset>
- <label for="selectNbPlayers">{{ translate("Number of players") }}</label>
+ <label for="selectNbPlayers">
+ {{ translate("Number of players") }}
+ </label>
<select id="selectNbPlayers" v-model="newgameInfo.nbPlayers">
<option v-show="possibleNbplayers(2)" value="2">2</option>
<option v-show="possibleNbplayers(3)" value="3">3</option>
<fieldset>
<label for="timeControl">Time control (in days)</label>
<div id="timeControl">
- <input type="number" v-model="newgameInfo.mainTime" placeholder="Main time"/>
- <input type="number" v-model="newgameInfo.increment" placeholder="Increment"/>
+ <input type="number" v-model="newgameInfo.mainTime"
+ placeholder="Main time"/>
+ <input type="number" v-model="newgameInfo.increment"
+ placeholder="Increment"/>
</div>
</fieldset>
<fieldset>
<label for="selectPlayers">{{ translate("Play with?") }}</label>
<div id="selectPlayers">
- <input type="text" v-model="newgameInfo.players[0]"/>
+ <input type="text" v-model="newgameInfo.players[0].name"/>
<input v-show="newgameInfo.nbPlayers>=3" type="text"
- v-model="newgameInfo.players[1]"/>
+ v-model="newgameInfo.players[1].name"/>
<input v-show="newgameInfo.nbPlayers==4" type="text"
- v-model="newgameInfo.players[2]"/>
+ v-model="newgameInfo.players[2].name"/>
</div>
</fieldset>
<fieldset>
- <label for="inputFen">{{ translate("FEN (ignored if players fields are blank)") }}</label>
+ <label for="inputFen">
+ {{ translate("FEN (ignored if players fields are blank)") }}
+ </label>
<input id="inputFen" type="text" v-model="newgameInfo.fen"/>
</fieldset>
<button @click="newGame">Launch game</button>
</div>
</div>
- <p v-if="!userId">Correspondance play is reserved to registered users</p>
+ <p v-if="!userId">
+ Correspondance play is reserved to registered users
+ </p>
<div v-if="!!userId">
- <my-challenge-list :challenges="challenges" @click-challenge="clickChallenge">
+ <my-challenge-list :challenges="challenges"
+ @click-challenge="clickChallenge">
</my-challenge-list>
<button onClick="doClick('modalNewgame')">New game</button>
<my-game-list :games="games" @show-game="showGame">
location.href="/variant#game?id=" + g.id;
},
newGame: function() {
- // NOTE: side-effect = set FEN
- // TODO: (to avoid any cheating option) separate the GenRandInitFen() functions
- // in separate files, load on server and generate FEN on server.
- const error = checkChallenge(this.newgameInfo);
- if (!!error)
- return alert(error);
- // Possible (server) error if filled player does not exist
- ajax(
- "/challenges/" + this.newgameInfo.vid,
- "POST",
- this.newgameInfo,
- response => {
- this.challenges.push(response.challenge);
- }
- );
+ const afterRulesAreLoaded = () => {
+ // NOTE: side-effect = set FEN
+ // TODO: (to avoid any cheating option) separate the GenRandInitFen() functions
+ // in separate files, load on server and generate FEN on server.
+ const error = checkChallenge(this.newgameInfo, vname);
+ if (!!error)
+ return alert(error);
+ // Possible (server) error if filled player does not exist
+ ajax(
+ "/challenges/" + this.newgameInfo.vid,
+ "POST",
+ this.newgameInfo,
+ response => {
+ const chall = Object.assign({},
+ this.newgameInfo,
+ {
+ id: response.cid,
+ uid: user.id,
+ added: Date.now(),
+ vname: vname,
+ },
+ this.challenges.push(response.challengei);
+ }
+ );
+ };
+ const idxInVariants =
+ variantArray.findIndex(v => v.id == this.newgameInfo.vid);
+ const vname = variantArray[idxInVariants].name;
+ const scriptId = vname + "RulesScript";
+ if (!document.getElementById(scriptId))
+ {
+ // Load variant rules (only once)
+ var script = document.createElement("script");
+ script.id = scriptId;
+ script.onload = afterRulesAreLoaded;
+ //script.addEventListener ("load", afterRulesAreLoaded, false);
+ script.src = "/javascripts/variants/" + vname + ".js";
+ document.body.appendChild(script);
+ }
+ else
+ afterRulesAreLoaded();
},
possibleNbplayers: function(nbp) {
if (this.newgameInfo.vid == 0)
return false;
- const idxInVariants = variantArray.findIndex(v => v.id == this.newgameInfo.vid);
+ const idxInVariants =
+ variantArray.findIndex(v => v.id == this.newgameInfo.vid);
return NbPlayers[variantArray[idxInVariants].name].includes(nbp);
},
},
-function checkChallenge(c)
+// 'vname' for 'variant name' is defined when run on client side
+function checkChallenge(c, vname)
{
const vid = parseInt(c.vid);
if (isNaN(vid) || vid <= 0)
let playerCount = 0;
for (p of c.players)
{
- if (p.length > 0)
+ if (p.name.length > 0)
{
- if (!p.match(/^[\w]+$/))
+ if (!p.name.match(/^[\w]+$/))
return "Wrong characters in players names";
playerCount++;
}
}
- if (playerCount > 0 && playerCount != c.nbPlayers)
+ if (playerCount > 0 && playerCount != c.nbPlayers-1)
return "None, or all of the opponent names must be filled"
- if (!!document) //client side
+ if (typeof document !== "undefined") //client side
{
- const idxInVariants = variantArray.findIndex(v => v.id == c.vid);
- const vname = variantArray[idxInVariants].name;
- const scriptId = vname + "RulesScript";
- const afterRulesAreLoaded = () => {
- const V = eval(vname + "Rules");
- // Allow custom FEN (and check it) only for individual challenges
- if (c.fen.length > 0 && playerCount > 0)
- {
- if (!V.IsGoodFen(c.fen))
- return "Bad FEN string";
- }
- else
- {
- // Generate a FEN
- c.fen = V.GenRandInitFen();
- }
- };
- if (!document.getElementById(scriptId))
+ const V = eval(vname + "Rules");
+ // Allow custom FEN (and check it) only for individual challenges
+ if (c.fen.length > 0 && playerCount > 0)
{
- // Load variant rules (only once)
- var script = document.createElement("script");
- script.id = scriptId;
- script.src = "/javascripts/variants/" + vname + ".js";
- document.body.appendChild(script);
- script.onload = afterRulesAreLoaded;
+ if (!V.IsGoodFen(c.fen))
+ return "Bad FEN string";
+ }
+ else
+ {
+ // Generate a FEN
+ c.fen = V.GenRandInitFen();
}
}
else