-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.js
More file actions
73 lines (63 loc) · 2.48 KB
/
Copy pathconfig.js
File metadata and controls
73 lines (63 loc) · 2.48 KB
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
// convert form value to map configuration JSON ans store configuration in local storage
const setConfig = () => {
let conf = {};
// Store conf to be reuse next time
localStorage.setItem("lastSubmittedConf", document.forms["difficulty"]["difficulty"].value);
switch(document.forms["difficulty"]["difficulty"].value){
case "easy":
conf["n"] = 9;
conf["m"] = 9;
conf["bombs"] = 10;
break;
case "normal":
conf["n"] = 16;
conf["m"] = 16;
conf["bombs"] = 40;
break;
case "hard":
conf["n"] = 30;
conf["m"] = 16;
conf["bombs"] = 99;
break;
}
localStorage.setItem("conf", JSON.stringify(conf));
}
// store a custom configuration in the local storage
const setCustomConfig = () => {
// Gather form value
let m = document.forms["customConf"]["nbRows"].value;
let n = document.forms["customConf"]["nbCols"].value;
let bombs = document.forms["customConf"]["nbBombs"].value;
// Validation for nbCols
if(n > 1 && n <= 30){
document.getElementById("nbCols").className = "form-control is-valid";
}else{
document.getElementById("nbCols").className = "form-control is-invalid";
return
}
// Validation for nbRows
if(m > 1 && m <= 24){
document.getElementById("nbRows").className = "form-control is-valid";
}else{
document.getElementById("nbRows").className = "form-control is-invalid";
return
}
// Validation for nbBombs
if(bombs > 1 && bombs < n*m){
// If all fields are valid, set conf in localStorage
localStorage.setItem("conf", JSON.stringify({"n":n, "m":m, "bombs":bombs}));
localStorage.setItem("lastSubmittedCustomConf", JSON.stringify({"n":n, "m":m, "bombs":bombs}));
document.getElementById("nbBombs").classList = "form-control is-valid";
}else{
document.getElementById("nbBombs").className = "form-control is-invalid";
}
}
// Display last used configuration at page reload
const displayLastConf = () => {
document.getElementById(localStorage.getItem("lastSubmittedConf")).checked = true;
let lastConf = JSON.parse(localStorage.getItem("lastSubmittedCustomConf"));
document.getElementById("nbRows").value = lastConf["m"];
document.getElementById("nbCols").value = lastConf["n"];
document.getElementById("nbBombs").value = lastConf["bombs"];
}
displayLastConf();