A few fixes, drop planned problems support (replaced by forum + mode analyze)
[vchess.git] / client / next_src / views / Problems.vue
CommitLineData
8d61fc4a
BA
1<template>
2 <div class="test">
3 <TestComp :vname="variant.name"/>
4 </div>
5</template>
6
7<script>
8// @ is an alias to /src
9import TestComp from "@/components/TestComp.vue";
10
11export default {
12 name: "test",
13 components: {
14 TestComp,
15 },
16 data: function() {
17 return {
18 variant: {name: "Atomic", id: 3},
19 };
20 }
21};
22</script>
4ecf423b 23Vue.component('my-problems', {
97da8720 24 props: ["probId","settings"],
da06a6eb
BA
25 data: function () {
26 return {
ff1d4c3f 27 userId: user.id,
81da2786 28 problems: [], //oldest first
ff1d4c3f 29 myProblems: [], //same, but only mine
936dc463
BA
30 singletons: [], //requested problems (using #num)
31 display: "others", //or "mine"
32 curProb: null, //(reference to) current displayed problem (if any)
ff1d4c3f 33 showSolution: false,
60d9063f 34 nomoreMessage: "",
97da8720 35 mode: "analyze", //for game component
936dc463 36 pbNum: 0, //to navigate directly to some problem
ff1d4c3f
BA
37 // New problem (to upload), or existing problem to edit:
38 modalProb: {
39 id: 0, //defined if it's an edit
badeb466 40 uid: 0, //...also
77fa6d1f 41 fen: "",
45109880
BA
42 instructions: "",
43 solution: "",
a9f262f3 44 preview: false,
45109880 45 },
da06a6eb
BA
46 };
47 },
be5efe81 48 // NOTE: always modals first, because otherwise "scroll to the end" undesirable effect
4ecf423b 49 template: `
a5d56686 50 <div id="problemControls" class="button-group">
badeb466
BA
51 <button :aria-label='translate("Previous problem(s)")' class="tooltip"
52 @click="showNext('backward')"
53 >
a5d56686
BA
54 <i class="material-icons">skip_previous</i>
55 </button>
badeb466
BA
56 <button v-if="!!userId" :aria-label='translate("Add a problem")'
57 class="tooltip" onClick="doClick('modal-newproblem')"
58 >
247356cd 59 {{ translate("New") }}
a5d56686 60 </button>
badeb466
BA
61 <button :aria-label='translate("Next problem(s)")' class="tooltip"
62 @click="showNext('forward')"
63 >
a5d56686
BA
64 <i class="material-icons">skip_next</i>
65 </button>
66 </div>
60d9063f 67 <div id="mainBoard" v-if="!!curProb">
ff1d4c3f 68 <div id="instructions-div" class="section-content">
badeb466 69 <p id="problem-instructions">{{ curProb.instructions }}</p>
ff1d4c3f 70 </div>
97da8720
BA
71 <my-game :fen="curProb.fen" :mode="mode" :allowMovelist="true"
72 :settings="settings">
60d9063f 73 </my-game>
ff1d4c3f
BA
74 <div id="solution-div" class="section-content">
75 <h3 class="clickable" @click="showSolution = !showSolution">
97da8720 76 {{ translate("Show solution") }}
ff1d4c3f 77 </h3>
badeb466 78 <p id="problem-solution" v-show="showSolution">{{ curProb.solution }}</p>
ff1d4c3f 79 </div>
badeb466 80 <button @click="displayList">Back to list display</button>
936dc463
BA
81 </div>
82 <div>
83 <input type="text" placeholder="Type problem number" v-model="pbNum"/>
be5efe81 84 <button @click="() => showProblem(pbNum)">Show problem</button>
ff1d4c3f 85 </div>
badeb466
BA
86 <button v-if="!!userId" @click="toggleListDisplay"
87 :class="{'only-mine':display=='mine'}"
88 >
89 My problems (only)
8ef618ef 90 </button>
936dc463
BA
91 <my-problem-summary v-show="!curProb"
92 v-on:edit-problem="editProblem(p)" v-on:delete-problem="deleteProblem(p.id)"
badeb466
BA
93 v-on:show-problem="() => showProblem(p.id)"
94 v-for="p in curProblems()" @click="curProb=p"
8ef618ef 95 v-bind:prob="p" v-bind:userid="userId" v-bind:key="p.id">
da06a6eb 96 </my-problem-summary>
4ecf423b
BA
97 </div>
98 `,
582df349 99 watch: {
97da8720
BA
100 probId: function() {
101 this.showProblem(this.probId);
582df349
BA
102 },
103 },
298c42e6 104 created: function() {
97da8720
BA
105 if (!!this.probId)
106 this.showProblem(this.probId);
ff1d4c3f 107 else
936dc463 108 this.firstFetch();
298c42e6 109 },
da06a6eb 110 methods: {
97da8720 111 translate: translate,
936dc463
BA
112 firstFetch: function() {
113 // Fetch most recent problems from server, for both lists
114 this.fetchProblems("others", "bacwkard");
115 this.fetchProblems("mine", "bacwkard");
116 this.listsInitialized = true;
117 },
be5efe81 118 showProblem: function(pid) {
badeb466
BA
119 location.hash = "#problems?id=" + pid;
120 for (let parray of [this.singletons,this.problems,this.myProblems])
121 {
122 const pIdx = parray.findIndex(p => p.id == pid);
123 if (pIdx >= 0)
124 {
97da8720 125 this.curProb = parray[pIdx];
badeb466
BA
126 break;
127 }
128 }
97da8720 129 if (!this.curProb)
936dc463
BA
130 {
131 // Cannot find problem in current set; get from server, and add to singletons.
132 ajax(
97da8720 133 "/problems/" + variant.id + "/" + pid, //TODO: variant ID should not be required
936dc463
BA
134 "GET",
135 response => {
136 if (!!response.problem)
137 {
138 this.singletons.push(response.problem);
139 this.curProb = response.problem;
6b519a84 140 this.display = (response.problem.uid == this.userId ? "mine" : "others");
936dc463
BA
141 }
142 else
143 this.noMoreProblems("Sorry, problem " + pid + " does not exist");
144 }
145 );
146 }
6b519a84
BA
147 else
148 this.display = (this.curProb.uid == this.userId ? "mine" : "others");
ff1d4c3f 149 },
ff1d4c3f
BA
150 curProblems: function() {
151 switch (this.display)
152 {
936dc463 153 case "others":
ff1d4c3f 154 return this.problems;
936dc463 155 case "mine":
ff1d4c3f
BA
156 return this.myProblems;
157 }
81da2786 158 },
8ef618ef 159 // TODO?: get 50 from server but only show 10 at a time (for example)
ff1d4c3f 160 showNext: function(direction) {
9e76c73c
BA
161 const nomorePb =
162 problems => {
163 if (!problems || problems.length == 0)
164 this.noMoreProblems("No more problems in this direction");
165 };
936dc463 166 if (!this.curProb)
9e76c73c 167 return this.fetchProblems(this.display, direction, nomorePb);
8ef618ef 168 // Show next problem (older or newer):
ff1d4c3f 169 let curProbs = this.curProblems();
936dc463
BA
170 // Try to find a neighbour problem in the direction, among current set
171 const neighbor = this.findClosestNeighbor(this.curProb, curProbs, direction);
172 if (!!neighbor)
ff1d4c3f 173 {
936dc463
BA
174 this.curProb = neighbor;
175 return;
ff1d4c3f 176 }
936dc463
BA
177 // Boundary case: nothing in current set, need to fetch from server
178 const curSize = curProbs.length;
9e76c73c
BA
179 this.fetchProblems(this.display, direction, problems => {
180 if (problems.length > 0)
181 {
182 // Ok, found something:
183 this.curProb =
184 this.findClosestNeighbor(this.curProb, curProbs, direction);
185 }
186 else
187 nomorePb();
6b519a84 188 });
936dc463
BA
189 },
190 findClosestNeighbor: function(problem, probList, direction) {
191 let neighbor = undefined;
192 let smallestDistance = Number.MAX_SAFE_INTEGER;
193 for (let prob of probList)
ff1d4c3f 194 {
936dc463
BA
195 const delta = Math.abs(prob.id - problem.id);
196 if (delta < smallestDistance &&
197 ((direction == "backward" && prob.id < problem.id)
198 || (direction == "forward" && prob.id > problem.id)))
8ef618ef 199 {
936dc463
BA
200 neighbor = prob;
201 smallestDistance = delta;
8ef618ef 202 }
ff1d4c3f 203 }
936dc463
BA
204 return neighbor;
205 },
206 noMoreProblems: function(message) {
207 this.nomoreMessage = message;
208 let modalNomore = document.getElementById("modalNomore");
209 modalNomore.checked = true;
210 setTimeout(() => modalNomore.checked = false, 2000);
211 },
212 displayList: function() {
213 this.curProb = null;
badeb466 214 location.hash = "#problems";
936dc463
BA
215 // Fetch problems if first call (if #num, and then lists)
216 if (!this.listsInitialized)
217 this.firstFetch();
ff1d4c3f
BA
218 },
219 toggleListDisplay: function() {
badeb466
BA
220 const displays = ["mine","others"];
221 const curIndex = displays.findIndex(item => item == this.display);
222 this.display = displays[1-curIndex];
81da2786 223 },
6b519a84 224 fetchProblems: function(type, direction, cb) {
936dc463 225 let problems = (type == "others" ? this.problems : this.myProblems);
badeb466 226 // "last datetime" set at a value OK for an empty initial array
936dc463 227 let last_dt = (direction=="forward" ? 0 : Number.MAX_SAFE_INTEGER);
badeb466 228 if (problems.length > 0)
7931e479 229 {
936dc463
BA
230 // Search for newest date (or oldest)
231 last_dt = problems[0].added;
232 for (let i=1; i<problems.length; i++)
7931e479 233 {
badeb466
BA
234 if ((direction == "forward" && problems[i].added > last_dt) ||
235 (direction == "backward" && problems[i].added < last_dt))
936dc463 236 {
badeb466 237 last_dt = problems[i].added;
936dc463 238 }
7931e479
BA
239 }
240 }
936dc463 241 ajax(
582df349 242 "/problems/" + variant.id,
936dc463 243 "GET",
81da2786 244 {
936dc463
BA
245 type: type,
246 direction: direction,
247 last_dt: last_dt,
248 },
249 response => {
9e76c73c
BA
250 if (response.problems.length > 0)
251 {
252 Array.prototype.push.apply(problems, response.problems.sort(
253 (p1,p2) => { return p2.added - p1.added; }));
254 // If one list is empty but not the other, show the non-empty
255 const otherArray =
256 (type == "mine" ? this.problems : this.myProblems);
257 if (otherArray.length == 0)
258 this.display = type;
6b519a84 259 this.$forceUpdate(); //TODO...
9e76c73c
BA
260 }
261 if (!!cb)
262 cb(response.problems);
81da2786 263 }
936dc463 264 );
da06a6eb 265 },
da06a6eb 266 },
4ecf423b 267})