malicious-wheel-of-fortune/script.js

157 lines
4.9 KiB
JavaScript
Raw Normal View History

2023-03-03 11:11:03 +00:00
/* --------------- Spin Wheel --------------------- */
const spinWheel = document.getElementById("spinWheel");
const spinBtn = document.getElementById("spin_btn");
const text = document.getElementById("text");
let spinValues_func = (display) => {
let return_values = [];
let idx = 0;
let degrees = 360 / display.length;
for (let val of display) {
return_values.push({minDegree: idx*degrees, maxDegree: (idx+1) * degrees, value: val})
idx += 1;
}
return return_values
}
const spinValues = spinValues_func(display);
const size = (display) => {
let return_val = [];
for (let i of display) {
return_val.push(10);
}
return return_val;
}
/* --------------- Background Colors --------------------- */
var spinColors = [
"#E74C3C",
"#7D3C98",
"#2E86C1",
"#138D75",
"#F1C40F",
"#D35400",
"#138D75",
"#F1C40F",
"#b163da",
"#E74C3C",
"#7D3C98",
"#138D75",
];
let spinChart = new Chart(spinWheel, {
plugins: [ChartDataLabels],
type: "pie",
data: {
labels: display,
datasets: [
{
backgroundColor: spinColors,
data: size(display),
},
],
},
options: {
responsive: true,
animation: { duration: 0 },
2023-03-03 12:08:48 +00:00
hover: {mode: null},
2023-03-03 11:11:03 +00:00
plugins: {
tooltip: false,
legend: {
display: false,
},
datalabels: {
//rotation: 0,
color: "#ffffff",
formatter: (_, context) => display[context.dataIndex],
font: { size: 24 },
},
},
},
});
console.log(spinChart.options.rotation);
/* --------------- Spinning Code --------------------- */
let counter = 0;
2023-03-03 11:11:03 +00:00
spinBtn.addEventListener("click", () => {
let spinAmount = 71;
2023-03-03 11:11:03 +00:00
spinBtn.disabled = true;
text.innerHTML = `<p>Held og lykke!</p>`;
2023-03-04 15:59:34 +00:00
let slowing_down = false;
let keep_speed = false;
2023-03-03 11:11:03 +00:00
let pickable = spinValues.filter(item => !not_pickable.includes(item.value))
let target = pickable[Math.floor(Math.random() * pickable.length)]
2023-03-04 15:59:34 +00:00
let target_index = spinValues.findIndex(item => item.value === target.value);
target_degree = Math.floor(Math.random() * (target.maxDegree - target.minDegree - 5))
console.log(spinValues[target_index]);
let slowing_down_target = spinValues[target_index + 1];
2023-03-03 11:11:03 +00:00
console.log(target)
2023-03-04 18:38:42 +00:00
let degrees = 360 / display.length;
2023-03-03 11:11:03 +00:00
let rotationInterval = window.setInterval(() => {
2023-03-04 15:59:34 +00:00
spinAmount *= 0.99
if (slowing_down && !keep_speed) {
2023-03-04 15:59:34 +00:00
spinAmount = Math.max(0.1, spinAmount)
} else if (keep_speed) {
spinAmount = Math.max(4, spinAmount)
2023-03-04 15:59:34 +00:00
} else {
2023-03-04 18:38:42 +00:00
spinAmount = Math.max(4, spinAmount)
2023-03-04 15:59:34 +00:00
}
2023-03-03 11:11:03 +00:00
spinChart.options.rotation += spinAmount;
spinChart.update();
if (spinChart.options.rotation >= 360) {
spinChart.options.rotation -= 360;
}
// it takes like 40-ish ticks to slow it down, so we need to compute when we need to start slowing down for this to always work
// Add a check, if it is too high when it passes halfway through or something like then continue at that speed for the next round
//if (spinAmount <= 8 && target.minDegree <= 360-spinChart.options.rotation && 360-spinChart.options.rotation < target.maxDegree + (degrees/display.length) - target_degree + 3 ) {
if (spinAmount <= 8 && target.minDegree <= 360-spinChart.options.rotation && 360-spinChart.options.rotation < (target.maxDegree - target_degree) + 50 ) {
console.log((360 - spinChart.options.rotation) + " ---- " + (target.maxDegree - target_degree));
2023-03-04 15:59:34 +00:00
console.log(spinAmount + " Decreasing more")
slowing_down = true;
2023-03-04 18:38:42 +00:00
spinAmount *= 0.9
counter = counter + 1;
if (spinAmount <= 0.1 && target.minDegree-5 <= 360-spinChart.options.rotation && 360-spinChart.options.rotation < target.maxDegree) {
spinChart.update()
text.innerHTML = `<p>Gruppe nummer ${target.value} skal præsentere!</p>`;
clearInterval(rotationInterval);
spinBtn.disabled = false;
console.log("counter " + counter);
} else if ( spinAmount <= 1 && target.minDegree + 3 > 360-spinChart.options.rotation) {
console.log("halting " + spinAmount );
spinChart.update()
text.innerHTML = `<p>Gruppe nummer ${target.value} skal præsentere!</p>`;
clearInterval(rotationInterval);
spinBtn.disabled = false;
console.log("counter " + counter);
2023-03-04 15:59:34 +00:00
}
else if ( spinAmount >= 1 && target.minDegree + 3 > 360-spinChart.options.rotation) {
keep_speed = true;
/*console.log("halting");
2023-03-04 15:59:34 +00:00
spinChart.update()
text.innerHTML = `<p>Gruppe nummer ${target.value} skal præsentere!</p>`;
clearInterval(rotationInterval);
spinBtn.disabled = false;
console.log("counter " + counter);*/
2023-03-04 15:59:34 +00:00
}
2023-03-03 11:11:03 +00:00
}
}, 10);
});
/* --------------- End Spin Wheel --------------------- */