-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandomTeam.html
More file actions
204 lines (170 loc) · 6.49 KB
/
randomTeam.html
File metadata and controls
204 lines (170 loc) · 6.49 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
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
<!DOCTYPE html>
<html>
<head>
<title>랜덤런치 조편성</title>
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">
<style>
.team-name {
font-size: 24px;
font-weight: bold;
margin-top: 20px;
}
.participant-animation {
animation: letter-fade-in 1s forwards;
}
@keyframes letter-fade-in {
from {
opacity: 0;
transform: translateY(10px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
</style>
</head>
<body>
<div class="container">
<h2>랜덤런치 조편성</h2>
<div class="row">
<div class="col s6">
<h4>참여자</h4>
<ul id="participant-list" class="collection"></ul>
<div class="input-field">
<input type="text" id="participant-name" placeholder="이름">
<a class="waves-effect waves-light btn" onclick="addParticipant()">추가</a>
</div>
</div>
<div class="col s6">
<h4>팀 이름</h4>
<div>
<input type="number" id="team-count" placeholder="팀 수">
<a class="waves-effect waves-light btn" onclick="generateTeams()">팀 배정</a>
</div>
<div id="team-names"></div>
<div>
<a class="waves-effect waves-light btn" onclick="assignParticipants()">참여자 배정</a>
</div>
</div>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>
<script>
const participants = [];
const colors = ["blue", "green", "red", "purple", "orange", "teal", "pink"];
function addParticipant() {
const nameInput = document.getElementById("participant-name");
const participantName = nameInput.value.trim();
if (participantName !== "") {
participants.push(participantName);
updateParticipantList();
nameInput.value = "";
}
}
function updateParticipantList() {
const participantList = document.getElementById("participant-list");
participantList.innerHTML = "";
for (const participant of participants) {
const li = document.createElement("li");
li.className = "collection-item";
li.innerHTML = `${participant} <a href="#!" class="secondary-content" onclick="removeParticipant('${participant}')"><i class="material-icons">delete</i></a>`;
participantList.appendChild(li);
}
}
function removeParticipant(name) {
const index = participants.indexOf(name);
if (index !== -1) {
participants.splice(index, 1);
updateParticipantList();
}
}
let assignedParticipants = [];
let teamSizes = {};
let currentTeamIndex = 0;
function assignParticipants() {
if (participants.length === 0) {
alert("참여자가 없습니다.");
return;
}
if (assignedParticipants.length === participants.length) {
alert("모든 참여자가 이미 배정되었습니다.");
return;
}
const teamNamesElement = document.getElementById("team-names");
const teamNameElements = teamNamesElement.querySelectorAll(".team-name");
if (teamNameElements.length === 0) {
alert("팀을 먼저 배정하세요.");
return;
}
const randomParticipantIndex = Math.floor(Math.random() * participants.length);
const randomParticipant = participants[randomParticipantIndex];
if (assignedParticipants.includes(randomParticipant)) {
assignParticipants();
return;
}
const currentTeamElement = teamNameElements[currentTeamIndex];
const teamName = currentTeamElement.textContent.slice(0, -2); // Remove " 팀"
const participantNameElement = document.createElement("p");
participantNameElement.textContent = `${randomParticipant}`;
participantNameElement.classList.add("participant-animation"); // Add animation class
currentTeamElement.parentNode.insertBefore(participantNameElement, currentTeamElement.nextSibling);
assignedParticipants.push(randomParticipant);
teamSizes[teamName]++;
currentTeamIndex = (currentTeamIndex + 1) % teamNameElements.length;
}
function getTeamWithSmallestSize() {
let smallestSize = Infinity;
let teamNameWithSmallestSize = null;
const teamNameElements = document.querySelectorAll(".team-name");
teamNameElements.forEach((element) => {
const teamName = element.textContent.slice(0, -2); // Remove " 팀"
const teamSize = teamSizes[teamName] || 0;
if (teamSize < smallestSize) {
smallestSize = teamSize;
teamNameWithSmallestSize = element;
}
});
return teamNameWithSmallestSize;
}
function generateTeams() {
const teamCountInput = document.getElementById("team-count");
const teamCount = parseInt(teamCountInput.value);
if (isNaN(teamCount) || teamCount <= 0) {
alert("유효한 팀 수를 입력하세요.");
return;
}
if (participants.length < teamCount) {
alert("팀 수가 참여자 수보다 많을 수 없습니다.");
return;
}
const teamNamesElement = document.getElementById("team-names");
teamNamesElement.innerHTML = "";
for (let i = 0; i < teamCount; i++) {
const teamName = generateRandomName();
const teamColor = colors[i % colors.length];
const teamNameElement = document.createElement("p");
teamNameElement.className = "team-name";
teamNameElement.style.color = teamColor;
teamNameElement.textContent = `${teamName} 팀`;
teamNamesElement.appendChild(teamNameElement);
}
}
function generateRandomName() {
const adjectives = ["강력한", "유연한", "창의적인", "지능적인", "활기찬", "화려한", "단단한"];
const nouns = ["호랑이", "사자", "독수리", "매", "늑대", "코끼리", "펭귄"];
const randomAdjective = adjectives[Math.floor(Math.random() * adjectives.length)];
const randomNoun = nouns[Math.floor(Math.random() * nouns.length)];
return `${randomAdjective} ${randomNoun}`;
}
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array;
}
</script>
</body>
</html>