const rows = 8;
const cols = 6;
let target = 1;
const threshold = 4;
const seatTable = document.querySelector("#seat-table");
const heatMap = new Map();
for (let i = 0; i < rows; i++) {
const row = document.createElement("div");
row.className = "row";
seatTable.appendChild(row);
for (let j = 0; j < cols; j++) {
const seat = document.createElement("div");
seat.className = "seat";
seat.textContent = `${i + 1}/${j + 1}`;
row.appendChild(seat);
}
}
document.querySelector("#start-button").addEventListener('click', run);
async function wait(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function run() {
let current = 0;
const newTarget = parseInt(document.querySelector("#target").value);
if (!isNaN(newTarget) && newTarget > 0) {
target = newTarget;
}
heatMap.clear();
for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
const seat = seatTable.querySelector(`.row:nth-child(${i + 1}) .seat:nth-child(${j + 1})`);
seat.style.backgroundColor = "white";
seat.style.transform = "scale(1)";
}
}
while (current < target) {
await wait(45);
const randomRow = Math.floor(Math.random() * rows);
const randomCol = Math.floor(Math.random() * cols);
const seat = seatTable.querySelector(`.row:nth-child(${randomRow + 1}) .seat:nth-child(${randomCol + 1})`);
const id = `${randomRow},${randomCol}`;
if (!heatMap.has(id)) {
heatMap.set(id, 0);
}
const heat = heatMap.get(id) + 1;
if (heat > threshold) {
continue;
}
heatMap.set(id, heat);
const hue = heat === threshold ? 40 : ((heat === threshold - 1) ? 80 : 120);
const color = `hsl(${hue},50%,${100 - heat * 30 / threshold}%)`;
seat.style.backgroundColor = color;
if (heat >= threshold) {
seat.style.transform = "scale(1.2)";
current++;
}
}
await wait(300);
for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
const id = `${i},${j}`;
if (heatMap.get(id) !== threshold) {
seatTable.querySelector(`.row:nth-child(${i + 1}) .seat:nth-child(${j + 1})`).style.backgroundColor = "white";
}
}
}
}