-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
125 lines (92 loc) · 3.04 KB
/
index.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
const dialog = document.getElementById('dialog');
const dialogContent = dialog.querySelector('.dialog--content');
const dialogCross = dialog.querySelector('.dialog--cross');
const personnages = [
'Plongeur',
'Éclaireur',
"Géologue",
'Ingénieur',
'Grimpeur',
'Médecin',
'Garde du corps',
'Chef',
];
const getRandom = (arr) => {
const max = arr.length - 1;
return Math.floor(Math.random() * (max + 1));
};
// Créé un tableau avec le nombre de joueur indiqué
const generatePlayersArray = (array) => {
const players = [];
for (let i = 0; i < array.length; i++) {
const player = array[i];
players.push(player);
}
if (array.length === 2) {
players.push(...players);
}
return players;
};
const getRandomArray = (count, arr = personnages) => {
const array = [...arr];
const randomArray = [];
for (let i = 0; i < count; i++) {
const index = getRandom(array);
const character = array[index];
randomArray.push(character);
array.splice(index, 1);
}
return randomArray;
};
const getLogs = (players, characters) => {
let logs = '';
for (let i = 0; i < players.length; i++) {
const player = players[i];
const character = characters[i];
logs += `<li>n°${i + 1} - ${player} joue "${character}".</li>`;
}
return logs;
};
const hideDialog = (e) => {
dialog.classList.remove('open');
};
dialogCross.addEventListener('click', hideDialog);
const randomGame = (playersInput) => {
const players = generatePlayersArray(playersInput);
const randomPlayers = getRandomArray(players.length, players);
const randomCharacters = getRandomArray(players.length);
const logs = getLogs(randomPlayers, randomCharacters);
dialogContent.innerHTML = `<ul>${logs}</ul>`;
dialog.classList.add('open');
};
document.getElementById('sub-terra').addEventListener('submit', (e) => {
e.preventDefault();
const inputPlayers = e.target.elements.players.value;
const investigation = e.target.elements.investigation.checked;
const extraction = e.target.elements.extraction.checked;
const annihilation = e.target.elements.annihilation.checked;
const hasAgent = personnages.indexOf('Agent');
const hasExterminateur = personnages.indexOf('Exterminateur');
if ((investigation || extraction) && hasAgent == -1) {
personnages.push('Agent');
}
if (!(investigation || extraction) && hasAgent != -1) {
personnages.splice(hasAgent, 1);
}
if (annihilation && hasExterminateur == -1) {
personnages.push('Exterminateur');
}
if (!annihilation && hasExterminateur != -1) {
personnages.splice(hasExterminateur, 1);
}
if (!inputPlayers.trim()) {
window.alert('Il faut au minimum 2 joueurs !');
return;
}
const players = inputPlayers.split(/[,;\.]/).map(player => player.trim());
if (players.length < 2) {
window.alert('Il faut au minimum 2 joueurs !');
return;
}
randomGame(players);
});