Fix SQL query to get completed games
[vchess.git] / client / src / views / Problems.vue
CommitLineData
89021f18
BA
1<template lang="pug">
2main
910d631b
BA
3 input#modalNewprob.modal(
4 type="checkbox"
09d37571 5 @change="fenFocusIfOpened($event)"
910d631b
BA
6 )
7 div#newprobDiv(
8 role="dialog"
9 data-checkbox="modalNewprob"
10 )
866842c3 11 .card
89021f18 12 label#closeNewprob.modal-close(for="modalNewprob")
604b951e
BA
13 fieldset
14 label(for="selectVariant") {{ st.tr["Variant"] }}
15 select#selectVariant(
16 v-model="curproblem.vid"
17 @change="changeVariant(curproblem)"
18 )
19 option(
20 v-for="v in [emptyVar].concat(st.variants)"
21 :value="v.id"
22 :selected="curproblem.vid==v.id"
23 )
24 | {{ v.name }}
25 fieldset
604b951e
BA
26 input#inputFen(
27 type="text"
bd76b456 28 placeholder="FEN"
604b951e
BA
29 v-model="curproblem.fen"
30 @input="trySetDiagram(curproblem)"
31 )
bd76b456 32 #diagram(v-html="curproblem.diag")
604b951e 33 fieldset
bd76b456 34 textarea(
604b951e
BA
35 :placeholder="st.tr['Instructions']"
36 v-model="curproblem.instruction"
37 )
38 p(v-html="parseHtml(curproblem.instruction)")
39 fieldset
bd76b456 40 textarea(
604b951e
BA
41 :placeholder="st.tr['Solution']"
42 v-model="curproblem.solution"
43 )
44 p(v-html="parseHtml(curproblem.solution)")
45 button(@click="sendProblem()") {{ st.tr["Send"] }}
89021f18 46 #dialog.text-center {{ st.tr[infoMsg] }}
604b951e 47 .row(v-if="showOne")
866842c3 48 .col-sm-12.col-md-10.col-md-offset-1.col-lg-8.col-lg-offset-2
bd76b456 49 #topPage
d7c00f6a
BA
50 .button-group(v-if="st.user.id == curproblem.uid")
51 button(@click="editProblem(curproblem)") {{ st.tr["Edit"] }}
52 button(@click="deleteProblem(curproblem)") {{ st.tr["Delete"] }}
2f258c37 53 span.vname {{ curproblem.vname }}
98fb976e 54 span.uname ({{ curproblem.uname }})
bd76b456 55 button.marginleft(@click="backToList()") {{ st.tr["Back to list"] }}
d7c00f6a
BA
56 button.nomargin(@click="gotoPrevNext($event,curproblem,1)")
57 | {{ st.tr["Previous"] }}
58 button.nomargin(@click="gotoPrevNext($event,curproblem,-1)")
feaf1bf7 59 | {{ st.tr["Next_p"] }}
866842c3 60 p.oneInstructions.clickable(
2f258c37 61 v-html="parseHtml(curproblem.instruction)"
bd76b456
BA
62 @click="curproblem.showSolution=!curproblem.showSolution"
63 )
604b951e
BA
64 | {{ st.tr["Show solution"] }}
65 p(
66 v-show="curproblem.showSolution"
67 v-html="parseHtml(curproblem.solution)"
68 )
69 .row(v-else)
89021f18 70 .col-sm-12.col-md-10.col-md-offset-1.col-lg-8.col-lg-offset-2
bd76b456 71 #controls
f37790f7 72 button#newProblem(@click="prepareNewProblem()")
bd76b456
BA
73 | {{ st.tr["New problem"] }}
74 label(for="checkboxMine") {{ st.tr["My problems"] }}
75 input#checkboxMine(
76 type="checkbox"
77 v-model="onlyMines"
604b951e 78 )
bd76b456
BA
79 label(for="selectVariant") {{ st.tr["Variant"] }}
80 select#selectVariant(v-model="selectedVar")
81 option(
82 v-for="v in [emptyVar].concat(st.variants)"
83 :value="v.id"
84 )
85 | {{ v.name }}
9b35f4a9 86 table#tProblems
bd76b456
BA
87 tr
88 th {{ st.tr["Variant"] }}
89 th {{ st.tr["Instructions"] }}
2f258c37 90 th {{ st.tr["Number"] }}
bd76b456 91 tr(
6808d7a1 92 v-for="p in problems"
bd76b456
BA
93 v-show="displayProblem(p)"
94 @click="setHrefPid(p)"
95 )
96 td {{ p.vname }}
2f258c37
BA
97 td {{ firstChars(p.instruction) }}
98 td {{ p.id }}
910d631b 99 BaseGame(
b1e46b33 100 ref="basegame"
910d631b
BA
101 v-if="showOne"
102 :game="game"
910d631b 103 )
89021f18
BA
104</template>
105
106<script>
107import { store } from "@/store";
108import { ajax } from "@/utils/ajax";
109import { checkProblem } from "@/data/problemCheck";
110import { getDiagram } from "@/utils/printDiagram";
bd76b456
BA
111import { processModalClick } from "@/utils/modalClick";
112import { ArrayFun } from "@/utils/array";
604b951e 113import BaseGame from "@/components/BaseGame.vue";
89021f18
BA
114export default {
115 name: "my-problems",
604b951e 116 components: {
6808d7a1 117 BaseGame
604b951e 118 },
89021f18
BA
119 data: function() {
120 return {
604b951e 121 st: store.state,
89021f18
BA
122 emptyVar: {
123 vid: 0,
6808d7a1 124 vname: ""
89021f18 125 },
604b951e
BA
126 // Problem currently showed, or edited:
127 curproblem: {
128 id: 0, //used in case of edit
89021f18
BA
129 vid: 0,
130 fen: "",
604b951e 131 diag: "",
89021f18
BA
132 instruction: "",
133 solution: "",
6808d7a1 134 showSolution: false
89021f18 135 },
604b951e
BA
136 loadedVar: 0, //corresponding to loaded V
137 selectedVar: 0, //to filter problems based on variant
89021f18 138 problems: [],
604b951e
BA
139 onlyMines: false,
140 showOne: false,
89021f18 141 infoMsg: "",
604b951e 142 game: {
6808d7a1
BA
143 players: [{ name: "Problem" }, { name: "Problem" }],
144 mode: "analyze"
145 }
89021f18
BA
146 };
147 },
148 created: function() {
e57c4de4
BA
149 ajax(
150 "/problems",
151 "GET",
152 {
153 success: (res) => {
154 // Show newest problem first:
155 this.problems = res.problems.sort((p1, p2) => p2.added - p1.added);
156 if (this.st.variants.length > 0)
157 this.problems.forEach(p => this.setVname(p));
158 // Retrieve all problems' authors' names
159 let names = {};
c9c0c57a 160 this.problems.forEach(p => {
e57c4de4
BA
161 if (p.uid != this.st.user.id) names[p.uid] = "";
162 else p.uname = this.st.user.name;
c9c0c57a 163 });
e57c4de4
BA
164 const showOneIfPid = () => {
165 const pid = this.$route.query["id"];
b1e46b33 166 if (!!pid) this.showProblem(this.problems.find(p => p.id == pid));
e57c4de4
BA
167 };
168 if (Object.keys(names).length > 0) {
169 ajax(
170 "/users",
171 "GET",
172 {
173 data: { ids: Object.keys(names).join(",") },
174 success: (res2) => {
175 res2.users.forEach(u => {
176 names[u.id] = u.name;
177 });
178 this.problems.forEach(p => {
179 if (!p.uname)
180 p.uname = names[p.uid];
181 });
182 showOneIfPid();
183 }
184 }
185 );
186 } else showOneIfPid();
187 }
188 }
189 );
89021f18 190 },
bd76b456 191 mounted: function() {
42a92848 192 document.getElementById("newprobDiv")
6808d7a1 193 .addEventListener("click", processModalClick);
bd76b456 194 },
604b951e
BA
195 watch: {
196 // st.variants changes only once, at loading from [] to [...]
6808d7a1 197 "st.variants": function() {
604b951e
BA
198 // Set problems vname (either all are set or none)
199 if (this.problems.length > 0 && this.problems[0].vname == "")
200 this.problems.forEach(p => this.setVname(p));
201 },
6808d7a1 202 $route: function(to) {
bd76b456 203 const pid = to.query["id"];
b1e46b33 204 if (!!pid) this.showProblem(this.problems.find(p => p.id == pid));
6808d7a1
BA
205 else this.showOne = false;
206 }
98fb976e 207 },
89021f18 208 methods: {
09d37571
BA
209 fenFocusIfOpened: function(event) {
210 if (event.target.checked) {
211 this.infoMsg = "";
212 document.getElementById("inputFen").focus();
213 }
214 },
604b951e
BA
215 setVname: function(prob) {
216 prob.vname = this.st.variants.find(v => v.id == prob.vid).name;
217 },
2f258c37
BA
218 firstChars: function(text) {
219 let preparedText = text
220 // Replace line jumps and <br> by spaces
6808d7a1
BA
221 .replace(/\n/g, " ")
222 .replace(/<br\/?>/g, " ")
2f258c37
BA
223 .replace(/<[^>]+>/g, "") //remove remaining HTML tags
224 .replace(/[ ]+/g, " ") //remove series of spaces by only one
225 .trim();
226 const maxLength = 32; //arbitrary...
227 if (preparedText.length > maxLength)
6808d7a1 228 return preparedText.substr(0, 32) + "...";
2f258c37
BA
229 return preparedText;
230 },
604b951e 231 copyProblem: function(p1, p2) {
6808d7a1 232 for (let key in p1) p2[key] = p1[key];
604b951e 233 },
bd76b456
BA
234 setHrefPid: function(p) {
235 // Change href => $route changes, watcher notices, call showProblem
236 const curHref = document.location.href;
237 document.location.href = curHref.split("?")[0] + "?id=" + p.id;
238 },
239 backToList: function() {
240 // Change href => $route change, watcher notices, reset showOne to false
241 document.location.href = document.location.href.split("?")[0];
242 },
604b951e
BA
243 resetCurProb: function() {
244 this.curproblem.id = 0;
245 this.curproblem.uid = 0;
246 this.curproblem.vid = "";
247 this.curproblem.vname = "";
248 this.curproblem.fen = "";
249 this.curproblem.diag = "";
250 this.curproblem.instruction = "";
251 this.curproblem.solution = "";
252 this.curproblem.showSolution = false;
89021f18 253 },
604b951e
BA
254 parseHtml: function(txt) {
255 return !txt.match(/<[/a-zA-Z]+>/)
256 ? txt.replace(/\n/g, "<br/>") //no HTML tag
257 : txt;
258 },
259 changeVariant: function(prob) {
260 this.setVname(prob);
6808d7a1
BA
261 this.loadVariant(prob.vid, () => {
262 // Set FEN if possible (might not be correct yet)
263 if (V.IsGoodFen(prob.fen)) this.setDiagram(prob);
264 });
604b951e
BA
265 },
266 loadVariant: async function(vid, cb) {
267 // Condition: vid is a valid variant ID
268 this.loadedVar = 0;
269 const variant = this.st.variants.find(v => v.id == vid);
89021f18
BA
270 const vModule = await import("@/variants/" + variant.name + ".js");
271 window.V = vModule.VariantRules;
604b951e
BA
272 this.loadedVar = vid;
273 cb();
274 },
275 trySetDiagram: function(prob) {
276 // Problem edit: FEN could be wrong or incomplete,
277 // variant could not be ready, or not defined
278 if (prob.vid > 0 && this.loadedVar == prob.vid && V.IsGoodFen(prob.fen))
279 this.setDiagram(prob);
89021f18 280 },
604b951e
BA
281 setDiagram: function(prob) {
282 // Condition: prob.fen is correct and global V is ready
283 const parsedFen = V.ParseFen(prob.fen);
284 const args = {
285 position: parsedFen.position,
6808d7a1 286 orientation: parsedFen.turn
604b951e
BA
287 };
288 prob.diag = getDiagram(args);
89021f18 289 },
604b951e 290 displayProblem: function(p) {
6808d7a1 291 return (
866842c3 292 (!this.selectedVar || p.vid == this.selectedVar) &&
6808d7a1
BA
293 ((this.onlyMines && p.uid == this.st.user.id) ||
294 (!this.onlyMines && p.uid != this.st.user.id))
295 );
604b951e
BA
296 },
297 showProblem: function(p) {
6808d7a1
BA
298 this.loadVariant(p.vid, () => {
299 // The FEN is already checked at this stage:
6808d7a1 300 this.game.vname = p.vname;
e71161fb 301 this.game.mycolor = V.ParseFen(p.fen).turn; //diagram orientation
b1e46b33 302 this.game.fenStart = p.fen;
6808d7a1 303 this.game.fen = p.fen;
6808d7a1 304 this.showOne = true;
b1e46b33
BA
305 // $nextTick to be sure $refs["basegame"] exists
306 this.$nextTick(() => {
307 this.$refs["basegame"].re_setVariables(this.game); });
308 this.copyProblem(p, this.curproblem);
6808d7a1 309 });
604b951e 310 },
d7c00f6a
BA
311 gotoPrevNext: function(e, prob, dir) {
312 const startIdx = this.problems.findIndex(p => p.id == prob.id);
313 let nextIdx = startIdx + dir;
314 while (
315 nextIdx >= 0 &&
316 nextIdx < this.problems.length &&
317 ((this.onlyMines && this.problems[nextIdx].uid != this.st.user.id) ||
318 (!this.onlyMines && this.problems[nextIdx].uid == this.st.user.id))
319 )
320 nextIdx += dir;
321 if (nextIdx >= 0 && nextIdx < this.problems.length)
322 this.setHrefPid(this.problems[nextIdx]);
323 else
324 alert(this.st.tr["No more problems"]);
325 },
f37790f7
BA
326 prepareNewProblem: function() {
327 this.resetCurProb();
328 window.doClick("modalNewprob");
329 },
604b951e
BA
330 sendProblem: function() {
331 const error = checkProblem(this.curproblem);
6808d7a1 332 if (error) {
866842c3 333 alert(this.st.tr[error]);
6808d7a1
BA
334 return;
335 }
604b951e
BA
336 const edit = this.curproblem.id > 0;
337 this.infoMsg = "Processing... Please wait";
338 ajax(
339 "/problems",
340 edit ? "PUT" : "POST",
e57c4de4
BA
341 {
342 data: { prob: this.curproblem },
343 success: (ret) => {
344 if (edit) {
345 let editedP = this.problems.find(p => p.id == this.curproblem.id);
346 this.copyProblem(this.curproblem, editedP);
347 this.showProblem(editedP);
348 }
349 else {
350 let newProblem = Object.assign({}, this.curproblem);
351 newProblem.id = ret.id;
352 newProblem.uid = this.st.user.id;
353 newProblem.uname = this.st.user.name;
354 this.problems = [newProblem].concat(this.problems);
355 }
356 document.getElementById("modalNewprob").checked = false;
357 this.infoMsg = "";
0cd02605 358 }
604b951e
BA
359 }
360 );
361 },
362 editProblem: function(prob) {
bec548e7
BA
363 // prob.diag might correspond to some other problem or be empty:
364 this.setDiagram(prob); //V is loaded at this stage
604b951e 365 this.copyProblem(prob, this.curproblem);
6808d7a1 366 window.doClick("modalNewprob");
604b951e
BA
367 },
368 deleteProblem: function(prob) {
6808d7a1 369 if (confirm(this.st.tr["Are you sure?"])) {
e57c4de4
BA
370 ajax(
371 "/problems",
372 "DELETE",
373 {
374 data: { id: prob.id },
375 success: () => {
376 ArrayFun.remove(this.problems, p => p.id == prob.id);
377 this.backToList();
378 }
379 }
380 );
bd76b456 381 }
6808d7a1
BA
382 }
383 }
89021f18
BA
384};
385</script>
386
387<style lang="sass" scoped>
bd76b456
BA
388[type="checkbox"].modal+div .card
389 max-width: 767px
390 max-height: 100%
910d631b 391
bd76b456
BA
392#inputFen
393 width: 100%
910d631b 394
bd76b456
BA
395textarea
396 width: 100%
910d631b 397
bd76b456
BA
398#diagram
399 margin: 0 auto
400 max-width: 400px
910d631b 401
9b35f4a9
BA
402table#tProblems
403 max-height: 100%
404
bd76b456
BA
405#controls
406 margin: 0
407 width: 100%
408 text-align: center
409 & > *
410 margin: 0
2f258c37 411
866842c3
BA
412p.oneInstructions
413 margin: 0
414 padding: 2px 5px
415 background-color: lightgreen
416
bd76b456 417#topPage
2f258c37 418 span.vname
bd76b456 419 font-weight: bold
feae89d3 420 padding-left: var(--universal-margin)
2f258c37
BA
421 span.uname
422 padding-left: var(--universal-margin)
bd76b456
BA
423 margin: 0 auto
424 & > .nomargin
425 margin: 0
426 & > .marginleft
427 margin: 0 0 0 15px
428
2f258c37
BA
429@media screen and (max-width: 767px)
430 #topPage
431 text-align: center
89021f18 432</style>