-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
232 lines (191 loc) · 6.9 KB
/
script.js
File metadata and controls
232 lines (191 loc) · 6.9 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
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
// Application State
let allTeams = [];
let availableTeams = [];
let hiddenTeams = new Set();
let isSpinning = false;
let currentRotation = 0;
let selectedTeam = null;
// DOM Elements
const canvas = document.getElementById('wheel');
const ctx = canvas.getContext('2d');
const spinBtn = document.getElementById('spin-btn');
const modal = document.getElementById('result-modal');
const teamCountEl = document.getElementById('team-count');
const minStarsSlider = document.getElementById('min-stars');
const maxStarsSlider = document.getElementById('max-stars');
const minValueDisplay = document.getElementById('min-value');
const maxValueDisplay = document.getElementById('max-value');
const minStarsDisplay = document.getElementById('min-stars-display');
const maxStarsDisplay = document.getElementById('max-stars-display');
// Colors for wheel segments
const colors = [
'#FF6B6B', '#4ECDC4', '#45B7D1', '#FFA07A', '#98D8C8',
'#F7DC6F', '#BB8FCE', '#85C1E2', '#F8B739', '#52B788'
];
// Load teams from JSON
async function loadTeams() {
try {
const response = await fetch('teams.json');
const data = await response.json();
allTeams = data.teams;
updateAvailableTeams();
drawWheel();
} catch (error) {
console.error('Error loading teams:', error);
alert('Failed to load teams. Please make sure teams.json exists.');
}
}
// Update available teams based on filters
function updateAvailableTeams() {
const minStars = parseFloat(minStarsSlider.value);
const maxStars = parseFloat(maxStarsSlider.value);
availableTeams = allTeams.filter(team => {
const notHidden = !hiddenTeams.has(team.id);
const matchesFilter = team.stars >= minStars && team.stars <= maxStars;
return notHidden && matchesFilter;
});
teamCountEl.textContent = availableTeams.length;
drawWheel();
}
// Update range displays
function updateRangeDisplays() {
const minValue = parseFloat(minStarsSlider.value);
const maxValue = parseFloat(maxStarsSlider.value);
// Ensure min doesn't exceed max
if (minValue > maxValue) {
minStarsSlider.value = maxValue;
}
// Ensure max doesn't go below min
if (maxValue < minValue) {
maxStarsSlider.value = minValue;
}
// Update all displays
const finalMin = parseFloat(minStarsSlider.value);
const finalMax = parseFloat(maxStarsSlider.value);
minValueDisplay.textContent = finalMin;
maxValueDisplay.textContent = finalMax;
minStarsDisplay.textContent = finalMin;
maxStarsDisplay.textContent = finalMax;
updateAvailableTeams();
}
// Draw the wheel on canvas
function drawWheel() {
if (availableTeams.length === 0) {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = '#333';
ctx.font = '20px Arial';
ctx.textAlign = 'center';
ctx.fillText('No teams available', canvas.width / 2, canvas.height / 2);
spinBtn.disabled = true;
return;
}
spinBtn.disabled = false;
const centerX = canvas.width / 2;
const centerY = canvas.height / 2;
const radius = canvas.width / 2 - 10;
const sliceAngle = (2 * Math.PI) / availableTeams.length;
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw segments
availableTeams.forEach((team, index) => {
const startAngle = index * sliceAngle;
const endAngle = startAngle + sliceAngle;
// Draw segment
ctx.beginPath();
ctx.moveTo(centerX, centerY);
ctx.arc(centerX, centerY, radius, startAngle, endAngle);
ctx.closePath();
ctx.fillStyle = colors[index % colors.length];
ctx.fill();
ctx.strokeStyle = 'white';
ctx.lineWidth = 3;
ctx.stroke();
// Draw text
ctx.save();
ctx.translate(centerX, centerY);
ctx.rotate(startAngle + sliceAngle / 2);
ctx.textAlign = 'center';
ctx.fillStyle = 'white';
ctx.font = 'bold 14px Arial';
ctx.shadowColor = 'rgba(0, 0, 0, 0.5)';
ctx.shadowBlur = 3;
// Draw team name
const textRadius = radius * 0.7;
ctx.fillText(team.name, textRadius, 0);
// Draw stars
ctx.font = '12px Arial';
const stars = '⭐'.repeat(team.stars);
ctx.fillText(stars, textRadius, 15);
ctx.restore();
});
// Draw center circle
ctx.beginPath();
ctx.arc(centerX, centerY, 30, 0, 2 * Math.PI);
ctx.fillStyle = 'white';
ctx.fill();
ctx.strokeStyle = '#333';
ctx.lineWidth = 3;
ctx.stroke();
}
// Spin the wheel
function spinWheel() {
if (isSpinning || availableTeams.length === 0) return;
isSpinning = true;
spinBtn.disabled = true;
// Random spin: 5-8 full rotations plus random angle
const spins = 5 + Math.random() * 3;
const randomAngle = Math.random() * 360;
const totalRotation = spins * 360 + randomAngle;
// Apply rotation
currentRotation += totalRotation;
canvas.style.transform = `rotate(${currentRotation}deg)`;
// Calculate which team was selected
setTimeout(() => {
// The pointer is at the top (270 degrees or -90 degrees)
// After rotation, calculate which segment is under the pointer
const pointerAngle = 270; // Top of the wheel
const finalRotation = currentRotation % 360;
// Calculate the angle of the segment under the pointer
// We subtract because the wheel rotates clockwise
let targetAngle = (pointerAngle - finalRotation) % 360;
if (targetAngle < 0) targetAngle += 360;
const sliceAngle = 360 / availableTeams.length;
const selectedIndex = Math.floor(targetAngle / sliceAngle) % availableTeams.length;
selectedTeam = availableTeams[selectedIndex];
showResult();
isSpinning = false;
spinBtn.disabled = false;
}, 4000); // Match CSS transition duration
}
// Show result modal
function showResult() {
const resultTeam = document.getElementById('result-team');
const resultStars = document.getElementById('result-stars');
resultTeam.textContent = selectedTeam.name;
resultStars.textContent = '⭐'.repeat(selectedTeam.stars);
modal.classList.add('show');
}
// Hide result modal
function hideModal() {
modal.classList.remove('show');
}
// Hide team from future spins
function hideTeam() {
if (selectedTeam) {
hiddenTeams.add(selectedTeam.id);
updateAvailableTeams();
hideModal();
}
}
// Keep team for future spins
function keepTeam() {
hideModal();
}
// Event Listeners
spinBtn.addEventListener('click', spinWheel);
document.getElementById('hide-team-btn').addEventListener('click', hideTeam);
document.getElementById('keep-team-btn').addEventListener('click', keepTeam);
// Range slider event listeners
minStarsSlider.addEventListener('input', updateRangeDisplays);
maxStarsSlider.addEventListener('input', updateRangeDisplays);
// Initialize the application
loadTeams();