document.addEventListener("DOMContentLoaded", function () { // ====== 上班天數 ====== const startDate = new Date("2025-04-07"); const today = new Date(); today.setHours(0, 0, 0, 0); startDate.setHours(0, 0, 0, 0); const diffDays = Math.floor((today - startDate) / (1000 * 60 * 60 * 24)); const workDayElem = document.getElementById("work-day-count"); if (workDayElem) { workDayElem.textContent = diffDays; } // ====== 刷餐倒數 ====== const timerWrapper = document.getElementById("ayou-action-timer"); const actionElem = document.getElementById("action-name"); const countdownElem = document.getElementById("countdown-minutes"); const activeRanges = [ { start: [11, 0], end: [11, 30] }, { start: [16, 30], end: [17, 0] } ]; function updateAyouTimer() { if (!timerWrapper || !actionElem || !countdownElem) return; const now = new Date(); const nowMins = now.getHours() * 60 + now.getMinutes(); const r = activeRanges.find(R => { const s = R.start[0] * 60 + R.start[1]; const e = R.end[0] * 60 + R.end[1]; return nowMins >= s && nowMins < e; }); if (r) { timerWrapper.style.display = "block"; actionElem.textContent = "刷餐"; const endMins = r.end[0] * 60 + r.end[1]; countdownElem.textContent = endMins - nowMins; } else { timerWrapper.style.display = "none"; } } updateAyouTimer(); setInterval(updateAyouTimer, 60000); // ====== 打魷魚(CF Worker) ====== const USE_SERVER = true; const WORKER_BASE = "https://squid-counter.wuyiulin.workers.dev"; const layer = document.getElementById("squid-layer"); const badge = document.getElementById("hit-badge"); const OSpan = document.getElementById("O"); const LS_KEY = "ayou_squid_hits"; const SQUID_W = 180; const SQUID_H = 242; const SQUID_SRC = "https://wuyiulin.com/wp-content/uploads/2025/09/ofcin_242.png"; let respawnTimer = null; async function loadCount() { if (USE_SERVER) { try { const res = await fetch(`${WORKER_BASE}/count`, { method: "GET", credentials: "omit" }); const data = await res.json(); OSpan.textContent = Number.isFinite(data?.count) ? data.count : 0; return; } catch (e) { console.warn("Worker /count 失敗,退回 localStorage", e); } } OSpan.textContent = Number(localStorage.getItem(LS_KEY) || 0); } function overlapsBadge(x, y, pad = 16) { const bw = badge.offsetWidth, bh = badge.offsetHeight; const rectB = { left: window.innerWidth - 12 - bw - pad, right: window.innerWidth - 12 + pad, top: 12 - pad, bottom:12 + bh + pad }; const rectS = { left: x, right: x + SQUID_W, top: y, bottom: y + SQUID_H }; const no = rectS.right < rectB.left || rectS.left > rectB.right || rectS.bottom < rectB.top || rectS.top > rectB.bottom; return !no; } function randomSafePos() { const vw = Math.max(window.innerWidth || document.documentElement.clientWidth || 0, 1); const vh = Math.max(window.innerHeight || document.documentElement.clientHeight || 0, 1); const maxX = Math.max(0, vw - SQUID_W); const maxY = Math.max(0, vh - SQUID_H); let x = 0, y = 0; for (let i = 0; i < 30; i++) { x = Math.floor(Math.random() * (maxX + 1)); y = Math.floor(Math.random() * (maxY + 1)); if (!overlapsBadge(x, y, 16)) return { x, y }; } return { x, y }; } function bindHit(el) { let used = false; const onHit = (evt) => { if (used) return; used = true; hitOnce(evt, el); }; el.addEventListener("pointerdown", onHit, { passive: true }); el.addEventListener("click", onHit, { passive: true }); } async function hitOnce(evt, el) { el.classList.remove("whacked"); void el.offsetWidth; el.classList.add("whacked"); spawnSplat(evt.clientX, evt.clientY); if (USE_SERVER) { try { const res = await fetch(`${WORKER_BASE}/hit`, { method: "POST", credentials: "omit" }); const data = await res.json(); OSpan.textContent = Number.isFinite(data?.count) ? data.count : Number(OSpan.textContent || 0) + 1; } catch (e) { console.warn("Worker /hit 失敗,退回 localStorage", e); const v = Number(localStorage.getItem(LS_KEY) || 0) + 1; localStorage.setItem(LS_KEY, v); OSpan.textContent = v; } } else { const v = Number(localStorage.getItem(LS_KEY) || 0) + 1; localStorage.setItem(LS_KEY, v); OSpan.textContent = v; } setTimeout(() => { el.remove(); clearTimeout(respawnTimer); respawnTimer = setTimeout(spawnSquid, 300); }, 200); } function spawnSquid() { const old = document.getElementById("squid-img"); if (old) old.remove(); const { x, y } = randomSafePos(); const squid = document.createElement("img"); squid.id = "squid-img"; squid.src = SQUID_SRC; squid.alt = "阿魷(點我用鎚子打~)"; squid.style.left = x + "px"; squid.style.top = y + "px"; bindHit(squid); layer.appendChild(squid); clearTimeout(respawnTimer); respawnTimer = setTimeout(() => { const cur = document.getElementById("squid-img"); if (cur === squid) { cur.remove(); spawnSquid(); } }, 3000); } function spawnSplat(clientX, clientY) { const splat = document.createElement("div"); splat.className = "splat"; splat.style.left = clientX + "px"; splat.style.top = clientY + "px"; layer.appendChild(splat); splat.addEventListener("animationend", () => splat.remove()); } loadCount(); spawnSquid(); });