-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
177 lines (153 loc) · 5.12 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
let players = [
{
name: 'Игрок №1',
team: [null, null, null, null, null, null],
},
{
name: 'Игрок №2',
team: [null, null, null, null, null, null],
},
{
name: 'Игрок №3',
team: [null, null, null, null, null, null],
},
{
name: 'Игрок №4',
team: [null, null, null, null, null, null],
},
{
name: 'Игрок №5',
team: [null, null, null, null, null, null],
},
{
name: 'Игрок №6',
team: [null, null, null, null, null, null],
},
{
name: 'Игрок №7',
team: [null, null, null, null, null, null],
},
{
name: 'Игрок №8',
team: [null, null, null, null, null, null],
},
];
let teams = [
'Манчестер Сити',
'ПСЖ',
'Манчестер Юнайтед',
'Ювентус',
'Реал Мадрид',
'Челси',
'Тотенхэм',
'Ливерпуль',
'Бавария',
'Барселона',
'Боруссия',
'Севилья',
'Лейпциг',
'Милан',
'Интер',
'Атлетико',
];
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
// возвращаем рандомное значение из массива
Array.prototype.sample = function () {
return this[Math.floor(Math.random() * this.length)];
};
// перемешать массив
function shuffle(array) {
for (let i = array.length - 1; i > 0; i--) {
let j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
}
// отслеживанием нажатие кнопки рандома
document.getElementById('button-random').addEventListener('click', async function (e) {
e.preventDefault();
let divResult = document.getElementById('result');
divResult.innerHTML = 'Распределяю команды по игрокам...';
await sleep(1000);
divResult.innerHTML = '';
for (let i = 0; i < 6; i++) {
const div = document.createElement('div');
div.className = 'flex flex-col max-w-xs p-6 mb-6 border border-slate-200 rounded';
const h2 = document.createElement('h2');
h2.appendChild(document.createTextNode(`Тур №${i + 1}`));
h2.className = 'text-lg text-slate-600 mb-4 grow';
div.appendChild(h2);
cloneTeams = teams.slice();
clonePlayers = players.slice();
const indexTeam = i;
for (let i = 0; i < players.length; i++) {
let team = cloneTeams.sample();
if (clonePlayers[i].team[indexTeam] === null) {
if (players[i].team.includes(team, 0) === true) {
do {
let newTeam = cloneTeams.sample();
team = newTeam;
} while (players[i].team.includes(team, 0) === true);
clonePlayers[i].team[indexTeam] = team;
} else {
clonePlayers[i].team[indexTeam] = team;
}
}
let p = document.createElement('p');
p.appendChild(document.createTextNode(`${players[i].name}`));
p.className = 'text-base text-slate-500 mb-4';
div.appendChild(p);
p.appendChild(document.createTextNode(` — ${clonePlayers[i].team[indexTeam]}`));
p.className = 'text-base text-slate-500 mb-4';
div.appendChild(p);
let myIndex = cloneTeams.indexOf(team);
if (myIndex !== -1) {
cloneTeams.splice(myIndex, 1);
}
shuffle(cloneTeams);
}
divResult.appendChild(div);
}
// if (cloneTeams.length < clonePlayers.length) {
// return alert('Команд меньше, чем игроков. Распределить невозможно. Добавьте ещё команд.');
// }
// i = 0;
// while (i < players.length) {
// team = cloneTeams.sample();
// console.log(players[i] + ' - ' + team);
// let p = document.createElement('p');
// p.appendChild(document.createTextNode(`${players[i]} — ${team}`));
// divResult.appendChild(p);
// i++;
//
// }
});
// отслеживаем добавление игрока и добавляем значение в массив
// document.getElementById('form-player').addEventListener('submit', function (e) {
// e.preventDefault();
// const playerNameValue = document.getElementById('input-player').value;
// const playerNameForm = document.getElementById('form-player');
// console.log(playerNameValue);
// if (playerNameValue !== null) {
// players.push(playerNameValue);
// playerNameForm.reset();
// p.appendChild(document.createTextNode(`${playerNameValue} `));
// divResult.appendChild(p);
// }
// console.log(players);
// });
// // отслеживаем добавление команд и добавляем значение в массив
// document.getElementById('form-team').addEventListener('submit', function (e) {
// e.preventDefault();
// const teamNameValue = document.getElementById('input-team').value;
// const teamNameForm = document.getElementById('form-team');
// console.log(teamNameValue);
// if (teamNameValue !== null) {
// teams.push(teamNameValue);
// teamNameForm.reset();
// p.appendChild(document.createTextNode(`${teamNameValue} `));
// divResult.appendChild(p);
// }
// console.log(teams);
// });