- // 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();