-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
502 lines (456 loc) · 15.3 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
'use strict';
const form = document.querySelector('.form');
const containerWorkouts = document.querySelector('.workouts');
const inputType = document.querySelector('.form__input--type');
const modal = document.querySelector('.modal');
const inputWilaya = document.querySelector('.province__input--wilaya');
const provinceInput = document.querySelector('.province__input');
const wilaysList = document.querySelector('.wilayas__list');
const score = document.querySelector('.province__score');
const startBtn = document.querySelector('.button__start');
const replayBtn = document.querySelector('.modal__button__replay');
const closeModalBtn = document.querySelector('.modal__button__close');
const capitalizeWords = function (str) {
return str.replace(/(^|\s|')([a-z\u00E0-\u00FC])/g, function (match) {
return match.toUpperCase();
});
};
// const provinces = [
// ['adrar', 'أدرار'],
// ['chlef', 'Chlaf', 'الشلف', 'شلف'],
// ['laghwat', 'laghouat', 'لقواط', 'لغواط', 'الأغواط', 'أغواط'],
// ['oum el bouaki', 'bouaki', 'Oum El Bouaghi', 'أم البواقي', 'البواقي'],
// ['betna', 'batna', 'باتنة'],
// ['bejaia', 'béjaïa', 'بجاية'],
// ['biskra', 'بسكرة'],
// ['bachar', 'bechar', 'béchar', 'بشار'],
// ['blida', 'البليدة', 'بليدة'],
// ['bouira', 'البويرة', 'بويرة'],
// ['tamanrasat', 'tamanrasset', 'tamanrassat', 'tamanrasset', 'تمنراست'],
// ['tebessa', 'tbessa', 'tébessa', 'تبسة'],
// ['telemcen', 'tilimsan', 'tlamcen', 'tlemcen', 'تلمسان'],
// ['tiaret', 'تيارت'],
// ['thizi wezzu', 'tizi wezou', 'tizi wezo', 'tizi Ouzou', 'تيزي وزو'],
// ['alger', 'الجزائر', 'جزائر', 'العاصمة', 'عاصمة'],
// ['djalfa', 'jalfa', 'jelfa', 'djelfa', 'الجلفة'],
// ['jijal', 'jijel', 'جيجل'],
// ['setif', 'stif', 'sétif', 'سطيف'],
// ['saida', 'saïda', 'سعيدة'],
// ['skikda', 'سكيكدة'],
// [
// 'sba',
// 'sidi bel abbas',
// 'sidi bel abbes',
// 'sidi bel abbès',
// 'سيدي بلعباس',
// 'بلعباس',
// ],
// ['anneba', 'annaba', 'عنابة'],
// ['galma', 'gualma', 'guelma', 'قالمة'],
// ['Constantine', 'قسنطينة'],
// ['mdia', 'mdea', 'medea', 'médéa', 'المدية', 'مدية'],
// ['mosta', 'mostaganam', 'mostaganam', 'مستغانم'],
// ['Msila', "M'Sila", 'المسيلة', 'مسيلة'],
// ['Mascara', 'معسكر'],
// ['ouaregla', 'ouergla', 'ouaregla', 'ouargla', 'ورقلة'],
// ['wahran', 'wahren', 'oran', 'وهران'],
// ['bayad', 'bayadh', 'el bayedh', 'el bayadh', 'البيض', 'بيض'],
// ['ilizi', 'illizi', 'إليزي'],
// [
// 'bordj',
// 'bourdj',
// 'bourdj bou arreridj',
// 'bordj bou arrerij',
// 'bordj bou arreridj',
// 'bordj bou arréridj',
// 'برج بوعريريج',
// 'برج',
// ],
// ['boumerdes', 'boumerdas', 'boumerdès', 'بومرداس'],
// ['tarf', 'taref', 'el tarf', 'الطارف', 'طارف'],
// ['tindouf', 'تندوف'],
// ['tissemsilet', 'tissemsilt', 'تيسمسيلت'],
// ['loued', 'oued', 'el oued', 'الوادي', 'وادي', 'واد'],
// ['khanchela', 'khanchla', 'khenchla', 'khenchela', 'خنشلة'],
// ['souk ahras', 'سوق أهراس'],
// ['tipaza', 'تيبازة'],
// ['Mila', 'ميلة'],
// ['ain dafla', 'ain defla', 'aïn defla', 'عين الدفلى'],
// ['naama', 'Naâma', 'النعامة', 'نعامة'],
// ['tmouchent', 'ain temouchent', 'aïn témouchent', 'عين تموشنت', 'تموشنت'],
// ['ghardaya', 'gherdaia', 'ghardaia', 'ghardaïa', 'غرداية'],
// ['ghelizane', 'relizene', 'relizane', 'غليزان'],
// ['timimoun', 'تيميمون'],
// ['bordj badji', 'bordj badji bokhtar', 'برج باجي مختار', 'برج باجي'],
// ['djellal', 'ouled djellal', 'أولاد جلال', 'ولاد جلال'],
// ['Beni Abbes', 'Béni Abbès', 'بني عباس'],
// ['in salah', 'عين صالح'],
// ['in guezzam', 'عين قزام', "عين ڨزام'"],
// ['touggourt', 'تقرت', 'تڨرت'],
// ['djanet', 'جانت'],
// ['mghayer', 'el mghair', 'el mghayer', "el m'ghair", 'المغير', 'مغير'],
// ['meniaa', 'el meniaa', 'المنيعة', 'منيعة'],
// ];
const provinces = [
{ geoInd: 0, names: ['adrar', 'أدرار'] },
{ geoInd: 27, names: ['chlef', 'chlaf', 'الشلف', 'شلف'] },
{
geoInd: 23,
names: ['laghouat', 'laghwat', 'لقواط', 'لغواط', 'الأغواط', 'أغواط'],
},
{
geoInd: 49,
names: [
'oum el bouaghi',
'oum el bouaki',
'bouaki',
'أم البواقي',
'البواقي',
],
},
{ geoInd: 42, names: ['batna', 'betna', 'باتنة'] },
{ geoInd: 38, names: ['béjaïa', 'bejaia', 'بجاية'] },
{ geoInd: 43, names: ['biskra', 'بسكرة'] },
{ geoInd: 11, names: ['béchar', 'bachar', 'bechar', 'بشار'] },
{ geoInd: 33, names: ['blida', 'البليدة', 'بليدة'] },
{ geoInd: 35, names: ['bouira', 'البويرة', 'بويرة'] },
{
geoInd: 4,
names: [
'tamanrasset',
'tamanrasat',
'tamanrasset',
'tamanrassat',
'تمنراست',
],
},
{ geoInd: 48, names: ['tébessa', 'tebessa', 'tbessa', 'تبسة'] },
{
geoInd: 14,
names: ['tlemcen', 'telemcen', 'tilimsan', 'tlamcen', 'تلمسان'],
},
{ geoInd: 20, names: ['tiaret', 'تيارت'] },
{
geoInd: 34,
names: ['tizi ouzou', 'thizi wezzu', 'tizi wezou', 'tizi wezo', 'تيزي وزو'],
},
{ geoInd: 32, names: ['alger', 'الجزائر', 'جزائر', 'العاصمة', 'عاصمة'] },
{
geoInd: 37,
names: ['djelfa', 'djalfa', 'jalfa', 'jelfa', 'الجلفة', 'جلفة'],
},
{ geoInd: 41, names: ['jijel', 'jijal', 'جيجل'] },
{ geoInd: 39, names: ['sétif', 'setif', 'stif', 'سطيف'] },
{ geoInd: 17, names: ['saïda', 'saida', 'سعيدة'] },
{ geoInd: 54, names: ['skikda', 'سكيكدة'] },
{
geoInd: 13,
names: [
'sidi bel abbès',
'sba',
'sidi bel abbas',
'sidi bel abbes',
'سيدي بلعباس',
'بلعباس',
],
},
{ geoInd: 53, names: ['annaba', 'anneba', 'عنابة'] },
{ geoInd: 51, names: ['guelma', 'galma', 'gualma', 'قالمة'] },
{ geoInd: 56, names: ['constantine', 'قسنطينة'] },
{ geoInd: 31, names: ['médéa', 'mdia', 'mdea', 'medea', 'المدية', 'مدية'] },
{ geoInd: 22, names: ['mostaganam', 'mosta', 'mostaganam', 'مستغانم'] },
{ geoInd: 36, names: ["m'sila", 'msila', 'المسيلة', 'مسيلة'] },
{ geoInd: 18, names: ['mascara', 'معسكر'] },
{
geoInd: 7,
names: ['ouargla', 'ouaregla', 'ouergla', 'ouaregla', 'ورقلة'],
},
{ geoInd: 16, names: ['oran', 'wahran', 'wahren', 'وهران'] },
{
geoInd: 19,
names: ['el bayadh', 'bayad', 'bayadh', 'el bayedh', 'البيض', 'بيض'],
},
{ geoInd: 6, names: ['illizi', 'ilizi', 'إليزي'] },
{
geoInd: 40,
names: [
'bordj bou arréridj',
'bourdj bou arreridj',
'bordj bou arrerij',
'bordj bou arreridj',
'برج بوعريريج',
'برج',
],
},
{ geoInd: 57, names: ['boumerdès', 'boumerdes', 'boumerdas', 'بومرداس'] },
{ geoInd: 52, names: ['el tarf', 'tarf', 'taref', 'الطارف', 'طارف'] },
{ geoInd: 10, names: ['tindouf', 'تندوف'] },
{ geoInd: 25, names: ['tissemsilt', 'tissemsilet', 'تيسمسيلت'] },
{ geoInd: 46, names: ['el oued', 'loued', 'oued', 'الوادي', 'وادي', 'واد'] },
{
geoInd: 47,
names: ['khenchela', 'khanchela', 'khanchla', 'khenchla', 'خنشلة'],
},
{ geoInd: 50, names: ['souk ahras', 'سوق أهراس'] },
{ geoInd: 30, names: ['tipaza', 'تيبازة'] },
{ geoInd: 55, names: ['mila', 'ميلة'] },
{ geoInd: 28, names: ['aïn defla', 'ain dafla', 'ain defla', 'عين الدفلى'] },
{ geoInd: 12, names: ['naâma', 'naama', 'النعامة', 'نعامة'] },
{
geoInd: 15,
names: [
'aïn témouchent',
'tmouchent',
'ain temouchent',
'عين تموشنت',
'تموشنت',
],
},
{
geoInd: 26,
names: ['ghardaïa', 'ghardaya', 'gherdaia', 'ghardaia', 'غرداية'],
},
{ geoInd: 21, names: ['relizane', 'ghelizane', 'relizene', 'غليزان'] },
{ geoInd: 1, names: ['timimoun', 'تيميمون'] },
{
geoInd: 2,
names: [
'bordj badji mokhtar',
'bourdj badji mokhtar',
'bordj badji',
'bourdj badji mokhtar',
'برج باجي مختار',
'برج باجي',
],
},
{
geoInd: 29,
names: ['ouled djellal', 'djellal', 'أولاد جلال', 'ولاد جلال'],
},
{ geoInd: 9, names: ['béni abbès', 'beni abbes', 'بني عباس'] },
{ geoInd: 8, names: ['in salah', 'عين صالح'] },
{ geoInd: 3, names: ['in guezzam', 'عين قزام', "عين ڨزام'"] },
{ geoInd: 45, names: ['touggourt', 'تقرت', 'تڨرت'] },
{ geoInd: 5, names: ['djanet', 'جانت'] },
{
geoInd: 44,
names: [
"el m'ghair",
'mghayer',
'el mghair',
'el mghayer',
'المغير',
'مغير',
],
},
{ geoInd: 24, names: ['el meniaa', 'meniaa', 'المنيعة', 'منيعة'] },
];
class Quiz {
#remainingProvinces;
#counter;
#geoJsonLayer;
#timerInterval;
constructor(geoJsonLayer) {
this.#counter = 0;
this.#remainingProvinces = JSON.parse(JSON.stringify(provinces));
this.#remainingProvinces = this.#remainingProvinces.map(el => ({
...el,
found: false,
}));
this.#geoJsonLayer = geoJsonLayer;
closeModalBtn.addEventListener('click', this._hideScoreModal);
// const targetTime = new Date().getTime() + 5 * 60 * 1000;
const targetTime = new Date().getTime() + 5 * 1000 * 60 + 1000;
this.#timerInterval = setInterval(() => {
this._countdownTimer(targetTime);
}, 1000);
replayBtn.addEventListener('click', () => location.reload());
}
_styleLayer(ind, style) {
this.#geoJsonLayer
.getLayers()
[provinces[ind].geoInd].setStyle(style)
.bringToFront();
}
_checkProvince() {
let ind = this.#remainingProvinces.findIndex(el => {
return el.names?.includes(inputWilaya.value?.toLowerCase());
});
// Check if province already guessed
if (this.#remainingProvinces[ind]?.found) {
inputWilaya.value = '';
return;
}
if (ind > -1) {
const style = {
color: '#19a629',
fillColor: '#19a629',
weight: 1,
};
this._styleLayer(ind, style);
inputWilaya.value = '';
// Reveal province name in wilayas list
this._revealProvinceName(ind, true);
score.textContent = `${++this.#counter} / 58 trouvées`;
this.#remainingProvinces[ind].found = true;
if (this.#counter === 58) {
this._endGame();
}
}
}
_revealRemainingProvinces() {
this.#remainingProvinces.forEach((el, i) => {
if (!el.found) {
this._revealProvinceName(i, false);
const style = {
color: '#fa493c',
fillColor: '#fa493c',
weight: 1,
};
this._styleLayer(i, style);
el.found = true;
}
});
}
_revealProvinceName(ind, isNotFound) {
wilaysList.children[ind].insertAdjacentHTML(
'beforeend',
`<span class="province__name ${
isNotFound ? 'isFound' : 'isNotFound'
}">${capitalizeWords(provinces[ind].names[0])}</span>`
);
}
_revealProvincesOnMap() {
this.#geoJsonLayer.eachLayer(layer => {
this._onEachFeature(layer.feature, layer);
});
}
_onEachFeature(feature, layer) {
if (feature.properties && feature.properties.name) {
layer.bindPopup(feature.properties.name);
}
}
_countdownTimer(targetTime) {
// Get the current time in milliseconds.
const currentTime = new Date().getTime();
const timeRemaining = targetTime - currentTime;
if (timeRemaining <= 0) {
this._endGame();
} else {
const minutes = Math.floor(timeRemaining / (1000 * 60));
const seconds = Math.floor((timeRemaining % (1000 * 60)) / 1000);
const formattedTime = `${minutes.toString().padStart(2, '0')}:${seconds
.toString()
.padStart(2, '0')}`;
document.getElementById('timer').innerHTML = formattedTime;
}
}
_endGame() {
clearInterval(this.#timerInterval);
this._revealRemainingProvinces();
this._showScoreModal();
this._revealProvincesOnMap();
inputWilaya.disabled = true;
document.getElementById('timer').innerHTML = '00:00';
}
_showScoreModal() {
modal.style.display = 'flex';
document.body.classList.add('modal-open');
modal.querySelector('.score__label').textContent = `Votre score final est ${
this.#counter
} / 58`;
}
_hideScoreModal() {
modal.style.display = 'none';
document.body.classList.remove('modal-open');
}
}
class App {
#geoJsonLayer;
#map;
#quiz;
constructor() {
// load map
this._loadMap();
this._loadGeoJson().then(() => {
startBtn.addEventListener('click', this._startQuiz.bind(this));
});
for (let i = 0; i < 58; i++) {
wilaysList.insertAdjacentHTML(
'beforeend',
`<div class="wilaya">
<span class="province__code">${i + 1}.</span>
</div>`
);
}
// Attach event handlers
}
async _loadGeoJson() {
try {
this._showLoadingSpinner(); // Show loading spinner
const response = await fetch(
`https://raw.githubusercontent.com/Yakine7/Wilayas-Algeria/64ea464cb3ac0819c6d16e1d5e374dcc0fc7ba30/geoData.geojson`
);
this._showLoadingSpinner();
if (!response.ok) {
throw new Error('Failed to fetch data');
}
const data = await response.json();
const style = {
color: '#2b9615',
fillColor: '#fafaf8',
weight: 1,
zIndex: 500,
};
this.#geoJsonLayer = L.geoJSON(data, {
style: style,
}).addTo(this.#map);
this._hideLoadingSpinner();
} catch (error) {
this._hideLoadingSpinner();
console.error('Error fetching data:', error);
}
}
_loadMap() {
this.#map = L.map('map', { zoomDelta: 0.5, zoomSnap: 0.5 })
.fitBounds([
[37.06818, 7.203128],
[19.117887, 3.236204],
[27.962401, -8.598042],
[23.517678, 11.925805],
])
.setMaxBounds([
[34.72259, 3.387151],
[19.117887, 3.236204],
[27.962401, -8.598042],
[23.517678, 11.925805],
]);
L.tileLayer(
'https://{s}.basemaps.cartocdn.com/light_nolabels/{z}/{x}/{y}.png',
{
attribution: '© OpenStreetMap contributors',
minZoom: 4,
maxZoom: 6,
}
).addTo(this.#map);
}
_startQuiz() {
this.#quiz = new Quiz(this.#geoJsonLayer);
inputWilaya.addEventListener('keyup', e => {
this.#quiz._checkProvince(this.#geoJsonLayer, e);
});
wilaysList.classList.toggle('hidden');
provinceInput.classList.toggle('hidden');
startBtn.disabled = true;
startBtn.parentElement.classList.add('d-none');
}
_showLoadingSpinner() {
const loadingSpinner = document.getElementById('loading-spinner');
loadingSpinner.style.display = 'flex';
}
_hideLoadingSpinner() {
const loadingSpinner = document.getElementById('loading-spinner');
loadingSpinner.style.display = 'none';
}
}
const app = new App();