-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
248 lines (199 loc) · 6.57 KB
/
utils.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
const nodeList = {};
export const getNode = (attribute) => {
return document.querySelector(attribute);
};
export const newRow = (stats) => {
const roll = rollDice(2, 10);
stats.roll = roll;
stats.total = roll.sum + stats.value;
const tBodyPrimary = getNode("#tBodyPrimary");
const tr = document.createElement("tr");
const td1 = document.createElement("td");
const td2 = document.createElement("td");
const td3 = document.createElement("td");
const td4 = document.createElement("td");
const btn = document.createElement("button");
td1.innerText = stats.name;
td2.innerText = stats.abr;
td3.innerText = stats.name !== "Magie" ? stats.value + roll.sum : stats.value;
td3.setAttribute("id", stats.id);
btn.className = "mdi mdi-dice-multiple-outline custom-icon";
btn.setAttribute("id", "rerollBtn");
btn.addEventListener("click", () => {
resetRow(td3, stats);
setSecondaryStats();
});
btn.style.backgroundColor = "white";
btn.style.borderRadius = "6px";
if (stats.id !== "magie") {
td4.appendChild(btn);
addTooltip(stats, td3, roll);
}
tr.appendChild(td1);
tr.appendChild(td2);
tr.appendChild(td3);
tr.appendChild(td4);
tBodyPrimary.appendChild(tr);
setItemStorage(stats.id, stats);
};
export const rollDice = (nbrOfDice, valueOfDice, isMinZero = false) => {
let score = { dice: [], sum: 0 };
const minimum = isMinZero ? 0 : 1;
for (let i = 0; i < nbrOfDice; i++) {
const rand = Math.floor(Math.random() * valueOfDice) + minimum;
score["sum"] += rand;
score["dice"].push(rand);
}
return score;
};
export const resetRow = (row, stats) => {
const roll = rollDice(2, 10);
stats.roll = roll;
stats.total = roll.sum + stats.value;
row.innerText = stats.total;
setItemStorage(stats.id, stats);
addTooltip(stats, row, roll);
};
export const addTooltip = (stats, node, roll) => {
node.style.textDecoration = "underline dotted lightgrey 2px";
node.style.fontWeight = "bold";
node.setAttribute(
"data-tooltip",
`Score de base : ${stats.value}
Résultat dé 1 : ${roll.dice[0]}
Résultat dé 2 : ${roll.dice[1]}`
);
node.className = "has-tooltip-right";
};
function setNodeList() {
// display race name
nodeList.race = getNode("#race");
// primary stats
nodeList.combat = getNode("#combat");
nodeList.connaissances = getNode("#connaissances");
nodeList.discretion = getNode("#discretion");
nodeList.endurance = getNode("#endurance");
nodeList.force = getNode("#force");
nodeList.habilite = getNode("#habilite");
nodeList.magie = getNode("#magie");
nodeList.mouvement = getNode("#mouvement");
nodeList.perception = getNode("#perception");
nodeList.sociabilite = getNode("#sociabilite");
nodeList.survie = getNode("#survie");
nodeList.tir = getNode("#tir");
nodeList.volonte = getNode("#volonte");
// secondary stats
nodeList.initiative = getNode("#initiative");
nodeList.vitalite = getNode("#vitalite");
nodeList.sangfroid = getNode("#sangfroid");
nodeList.destin = getNode("#destin");
// other
nodeList.printBtn = getNode("#printBtn");
nodeList.displayArchetype = getNode("#displayArchetype");
nodeList.archetypeImg = getNode("#archetypeImg");
nodeList.archetypeName = getNode("#archetypeName");
nodeList.archetypeDescription = getNode("#archetypeDescription");
}
export const setSecondaryStats = (special = []) => {
setNodeList();
const { stats } = special;
stats?.forEach((stat) => {
let newValue = +nodeList[stat.name].innerText;
newValue += stat.value;
nodeList[stat.name].innerText = newValue;
});
setInitiative();
setVitalite();
setSangFroid();
setDestin();
};
export const calculateIndice = (number) => {
return +String(number)[0];
};
export const setInitiative = (competence) => {
const cmb = getItemStorage("combat");
const mouv = getItemStorage("mouvement");
const per = getItemStorage("perception");
nodeList.initiative.innerText =
calculateIndice(cmb.total) +
calculateIndice(mouv.total) +
calculateIndice(per.total);
};
export const setVitalite = (competence) => {
const force = getItemStorage("force");
const end = getItemStorage("endurance");
const vol = getItemStorage("volonte");
nodeList.vitalite.innerText =
Math.floor(force.total / 5) +
Math.floor(end.total / 5) +
calculateIndice(vol.total);
};
export const setSangFroid = (competence) => {
const cmb = getItemStorage("combat");
const vol = getItemStorage("volonte");
const con = getItemStorage("connaissances");
nodeList.sangfroid.innerText =
Math.floor(cmb.total / 5) +
Math.floor(vol.total / 5) +
calculateIndice(con.total);
};
export const setDestin = (competence) => {
if (race.innerText === "Humain" || race.innerText === "Halfelin") {
nodeList.destin.innerText = 3;
} else {
nodeList.destin.innerText = 2;
}
};
export const setArchetype = (archetype) => {
archetype.bonus.forEach((a) => {
const key = Object.keys(a)[0];
const statUpdated = a[key] > 0 ? "#4C8B55" : "red";
nodeList[key].innerText = Number(nodeList[key].innerText) + a[key];
nodeList[key].style.color = statUpdated;
});
archetype.random?.forEach((rand) => {
const { sum } = rollDice(1, 2, true);
const key = Object.keys(rand[sum])[0];
const statUpdated = rand[sum][key] > 0 ? "#4C8B55" : "red";
nodeList[key].innerText = Number(nodeList[key].innerText) + rand[sum][key];
nodeList[key].style.color = statUpdated;
});
nodeList.printBtn.removeAttribute("disabled");
nodeList.displayArchetype.setAttribute("hidden", "");
disableRollButtons();
setSecondaryStats();
setImgArchetype(archetype);
};
const disableRollButtons = () => {
const rerollBtn = document.querySelectorAll("#rerollBtn");
rerollBtn.forEach((btn) => {
btn.setAttribute("disabled", "");
});
};
const setImgArchetype = (archetype) => {
const { name, description } = archetype;
const upperName = name[0].toUpperCase() + name.slice(1);
nodeList.archetypeName.innerText = upperName;
nodeList.archetypeDescription.innerText = description;
nodeList.archetypeDescription.style.fontStyle = "italic";
nodeList.archetypeImg.src = `./medias/archetypes/${name}.jpg`;
};
/**
*
* @param {String || Array} keys
* @returns json object
*/
const getItemStorage = (keys) => {
if (Array.isArray(keys)) {
const output = [];
keys.forEach((key) => {
output.push(JSON.parse(localStorage.getItem(key)));
});
return output;
}
return JSON.parse(localStorage.getItem(keys));
};
const setItemStorage = (key, data) => {
const stringifiedData = JSON.stringify(data);
localStorage.setItem(key, stringifiedData);
};