+ loadVariant: async function(vid, cb) {
+ // Condition: vid is a valid variant ID
+ this.loadedVar = 0;
+ const variant = this.st.variants.find(v => v.id == vid);
+ await import("@/variants/" + variant.name + ".js")
+ .then((vModule) => {
+ window.V = vModule[variant.name + "Rules"];
+ this.loadedVar = vid;
+ cb();
+ });
+ this.rulesContent =
+ afterRawLoad(
+ require(
+ "raw-loader!@/translations/rules/" + variant.name + "/" +
+ this.st.lang + ".pug"
+ ).default
+ ).replace(/(fen:)([^:]*):/g, replaceByDiag);
+ },
+ trySetDiagram: function(prob) {
+ // Problem edit: FEN could be wrong or incomplete,
+ // variant could not be ready, or not defined
+ if (prob.vid > 0 && this.loadedVar == prob.vid && V.IsGoodFen(prob.fen))
+ this.setDiagram(prob);
+ else prob.diag = "";
+ },
+ setDiagram: function(prob) {
+ // Condition: prob.fen is correct and global V is ready
+ const parsedFen = V.ParseFen(prob.fen);
+ const args = {
+ position: parsedFen.position,
+ orientation: parsedFen.turn
+ };
+ prob.diag = getDiagram(args);
+ },
+ showProblem: function(p_id) {
+ const processWhenWeHaveProb = () => {
+ this.loadVariant(p.vid, () => {
+ this.onlyMine = (p.uid == this.st.user.id);
+ // The FEN is already checked at this stage:
+ this.game.vname = p.vname;
+ this.game.mycolor = V.ParseFen(p.fen).turn; //diagram orientation
+ this.game.fenStart = p.fen;
+ this.game.fen = p.fen;
+ this.showOne = true;
+ // $nextTick to be sure $refs["basegame"] exists
+ this.$nextTick(() => {
+ this.$refs["basegame"].re_setVariables(this.game); });
+ this.curproblem.showSolution = false; //in case of
+ this.copyProblem(p, this.curproblem);
+ });
+ };
+ let p = undefined;
+ if (typeof p_id == "object") p = p_id;
+ else {
+ const problems = this.problems["others"].concat(this.problems["mine"]);
+ p = problems.find(prob => prob.id == p_id);
+ }
+ if (!p) {
+ // Bad luck: problem not in list. Get from server
+ ajax(
+ "/problems",
+ "GET",
+ {
+ data: { id: p_id },
+ success: (res) => {
+ this.decorate([res.problem], () => {
+ p = res.problem;
+ const mode = (p.uid == this.st.user.id ? "mine" : "others");
+ this.problems[mode].push(p);
+ processWhenWeHaveProb();
+ });
+ }
+ }
+ );
+ }
+ else processWhenWeHaveProb();
+ },
+ gotoPrevNext: function(prob, dir) {
+ const mode = (this.onlyMine ? "mine" : "others");
+ const problems = this.problems[mode];
+ const startIdx = problems.findIndex(p => p.id == prob.id);
+ const nextIdx = startIdx + dir;
+ if (nextIdx >= 0 && nextIdx < problems.length)
+ this.setHrefPid(problems[nextIdx]);
+ else if (this.hasMore[mode]) {
+ this.loadMore(
+ mode,
+ (nbProbs) => {
+ if (nbProbs > 0) this.gotoPrevNext(prob, dir);
+ else alert(this.st.tr["No more problems"]);
+ }
+ );
+ }
+ else alert(this.st.tr["No more problems"]);
+ },
+ prepareNewProblem: function() {
+ this.resetCurProb();
+ this.adjustHeight("instructions");
+ this.adjustHeight("solution");
+ window.doClick("modalNewprob");
+ },
+ sendProblem: function() {
+ const error = checkProblem(this.curproblem);
+ if (error) {
+ alert(this.st.tr[error]);
+ return;
+ }
+ const edit = this.curproblem.id > 0;
+ this.infoMsg = "Processing... Please wait";
+ ajax(
+ "/problems",
+ edit ? "PUT" : "POST",
+ {
+ data: { prob: this.curproblem },
+ success: (ret) => {
+ if (edit) {
+ let editedP = this.problems["mine"]
+ .find(p => p.id == this.curproblem.id);
+ if (!editedP)
+ // I'm an admin and edit another user' problem
+ editedP = this.problems["others"]
+ .find(p => p.id == this.curproblem.id);
+ this.copyProblem(this.curproblem, editedP);
+ this.showProblem(editedP);
+ }
+ else {
+ let newProblem = Object.assign({}, this.curproblem);
+ newProblem.id = ret.id;
+ newProblem.uid = this.st.user.id;
+ newProblem.uname = this.st.user.name;
+ this.problems["mine"] =
+ [newProblem].concat(this.problems["mine"]);
+ }
+ document.getElementById("modalNewprob").checked = false;
+ this.infoMsg = "";
+ }
+ }
+ );
+ },
+ canIedit: function(puid) {
+ return params.devs.concat([puid]).includes(this.st.user.id);
+ },
+ editProblem: function(prob) {
+ // prob.diag might correspond to some other problem or be empty:
+ this.setDiagram(prob); //V is loaded at this stage
+ this.copyProblem(prob, this.curproblem);
+ this.adjustHeight("instructions");
+ this.adjustHeight("solution");
+ window.doClick("modalNewprob");
+ },
+ deleteProblem: function(prob) {
+ if (confirm(this.st.tr["Are you sure?"])) {
+ ajax(
+ "/problems",
+ "DELETE",
+ {
+ data: { id: prob.id },
+ success: () => {
+ const mode = prob.uid == (this.st.user.id ? "mine" : "others");
+ ArrayFun.remove(this.problems[mode], p => p.id == prob.id);
+ this.backToList();
+ }
+ }
+ );
+ }
+ },
+ loadMore: function(mode, cb) {
+ ajax(
+ "/problems",
+ "GET",
+ {
+ data: {
+ uid: this.st.user.id,
+ mode: mode,
+ cursor: this.cursor[mode]
+ },
+ success: (res) => {
+ const L = res.problems.length;
+ if (L > 0) {
+ this.cursor[mode] = res.problems[L - 1].added;
+ // Remove potential duplicates:
+ const pids = this.problems[mode].map(p => p.id);
+ ArrayFun.remove(res.problems, p => pids.includes(p.id), "all");
+ this.decorate(res.problems);
+ this.problems[mode] =
+ this.problems[mode].concat(res.problems)
+ // TODO: problems are already sorted, would just need to insert
+ // the current individual problem in list; more generally
+ // there is probably only one misclassified problem.
+ // (Unless the user navigated several times by URL to show a
+ // single problem...)
+ .sort((p1, p2) => p2.added - p1.added);
+ }
+ else this.hasMore[mode] = false;
+ if (!!cb) cb(L);
+ }
+ }
+ );
+ }
+ }