From 28ccaa49ea2035ab3ba73f3393735eaef5e42b5f Mon Sep 17 00:00:00 2001 From: Miwory Date: Sun, 17 Aug 2025 09:14:33 +0300 Subject: [PATCH] sadkawaai prediction gen 1 --- Predictions/sadkawaai/generation 1/index.html | 18 +++ Predictions/sadkawaai/generation 1/script.js | 138 ++++++++++++++++++ Predictions/sadkawaai/generation 1/style.css | 133 +++++++++++++++++ 3 files changed, 289 insertions(+) create mode 100644 Predictions/sadkawaai/generation 1/index.html create mode 100644 Predictions/sadkawaai/generation 1/script.js create mode 100644 Predictions/sadkawaai/generation 1/style.css diff --git a/Predictions/sadkawaai/generation 1/index.html b/Predictions/sadkawaai/generation 1/index.html new file mode 100644 index 0000000..4bff2ef --- /dev/null +++ b/Predictions/sadkawaai/generation 1/index.html @@ -0,0 +1,18 @@ +
+
+
+
+
+
+
+
+
+ +
diff --git a/Predictions/sadkawaai/generation 1/script.js b/Predictions/sadkawaai/generation 1/script.js new file mode 100644 index 0000000..c93a483 --- /dev/null +++ b/Predictions/sadkawaai/generation 1/script.js @@ -0,0 +1,138 @@ +let widgetNode = document.getElementById('widget'); +let predictionNode = document.getElementById('prediction'); +let titleNode = document.getElementById('title'); +let pointsNode = document.getElementById('points'); +let timeNode = document.getElementById('time'); +let outcomesNode = document.getElementById('outcomes'); +let templateNode = document.getElementById('outcome'); + +let willEndAt = new Date(); +let locksAt = null; +let endedAt = null; + +window.addEventListener('message', function (event) { + let message = event.detail; + widgetNode.classList.add('active'); + + switch (message.type) { + case 'prediction.end': + if (message.status == "canceled") { + predictionNode.classList.remove('active'); + predictionNode.classList.remove('ended'); + } else { + predictionNode.classList.remove('locked'); + predictionNode.classList.add('ended'); + endedAt = new Date(message.ended_at); + + let winnerNode = document.querySelector(`[outcomeId="${message.winning_outcome_id}"]`); + winnerNode.classList.add('winner'); + }; + break; + case 'prediction.lock': + predictionNode.classList.add('active'); + locksAt = new Date(); + endedAt = null; + break; + case 'prediction.begin': + case 'prediction.progress': + var totalPoints = message.outcomes.reduce((sum, outcome) => sum + (outcome.channel_points ?? 0), 0); + endedAt = null; + + if (message.id != predictionNode.getAttribute('predictionId')) { + outcomesNode.innerHTML = ''; + locksAt = new Date(message.ends_at); + + predictionNode.setAttribute('predictionId', message.id); + titleNode.innerText = message.title; + pointsNode.innerHTML = `Поинтов: ${totalPoints}`; + + predictionNode.classList.add('active'); + predictionNode.classList.remove('locked'); + predictionNode.classList.remove('ended'); + + message.outcomes.forEach(outcome => { + let root = templateNode.content.cloneNode(true); + let outcomeNode = root.querySelector('.outcome'); + let title = root.querySelector('.title'); + let points = root.querySelector('.points'); + + let outcomePoints = outcome.channel_points ?? 0; + let percent = (outcomePoints / (totalPoints || 1) * 100).toFixed(2); + + outcomeNode.setAttribute('outcomeId', outcome.id); + outcomeNode.style.setProperty('--progress', `${percent}%`); + + title.innerText = outcome.title; + points.innerText = `${percent}%`; + + outcomesNode.appendChild(root); + }); + } else { + pointsNode.innerHTML = `Поинтов: ${totalPoints}`; + message.outcomes.forEach(outcome => { + let outcomeNode = document.querySelector(`[outcomeId="${outcome.id}"]`); + let title = outcomeNode.querySelector('.title'); + let points = outcomeNode.querySelector('.points'); + + let outcomePoints = outcome.channel_points ?? 0; + let percent = (outcomePoints / (totalPoints || 1) * 100).toFixed(2); + + outcomeNode.style.setProperty('--progress', `${percent}%`); + + title.innerText = outcome.title; + points.innerText = `${percent}%`; + }); + }; + + locksAt = new Date(message.locks_at); + + break; + + default: break; + }; +}); + +function convertSecondsToMMSS(seconds) { + seconds = Math.round(seconds); + if (seconds <= 0) { + seconds = 0; + }; + const minutes = Math.floor((seconds % 3600) / 60); + const secs = seconds % 60; + const formattedMinutes = String(minutes).padStart(2, '0'); + const formattedSeconds = String(secs).padStart(2, '0'); + return `${formattedMinutes}м:${formattedSeconds}с`; +}; + +function delay(ms) { + return new Promise(resolve => setTimeout(resolve, ms)); +}; + +async function updateTimer() { + let now = new Date().getTime(); + let left = (locksAt - now) / 1000; + + timeNode.innerHTML = `Осталось: ${convertSecondsToMMSS(left)}`; + + if (!endedAt && left <= 0) { + timeNode.innerHTML = `Ожидание результата`; + predictionNode.classList.add('locked'); + }; + + left = (now - endedAt) / 1000; + + if (endedAt) { + timeNode.innerHTML = `Закончилось`; + predictionNode.classList.remove('locked'); + + if (left > 5) { + predictionNode.classList.remove('active'); + predictionNode.classList.remove('ended'); + }; + }; + + await delay(100); + updateTimer(); +}; + +updateTimer(); diff --git a/Predictions/sadkawaai/generation 1/style.css b/Predictions/sadkawaai/generation 1/style.css new file mode 100644 index 0000000..615b409 --- /dev/null +++ b/Predictions/sadkawaai/generation 1/style.css @@ -0,0 +1,133 @@ +@font-face { + font-family: 'SF Pro Rounded'; + src: url('https://raw.githubusercontent.com/chris-short/apple-san-francisco-pro-fonts/refs/heads/main/SF-Pro-Rounded-Black.otf') format('opentype'); + font-weight: normal; + font-style: normal; + display: swap; +} + +#widget { + display: flex; + align-items: center; + justify-content: center; + visibility: hidden; +} + +#widget.active { + visibility: visible; +} + +#prediction { + color: white; + margin-top: 70px; + padding: 20px 30px; + width: 900px; + border-radius: 50px; + font-family: 'SF Pro Rounded'; + font-weight: bolder; + letter-spacing: .1rem; + background-color: transparent; + transition: opacity 500ms ease-out; + font-size: 40px; + opacity: 0; + filter: drop-shadow(0 0 5px rgba(0, 0, 0, .55)) drop-shadow(0 0 8px rgba(0, 0, 0, .3)); +} + +#prediction::before { + position: absolute; + content: ''; + inset: 0; + opacity: .7; + border-radius: inherit; + background: #D4A8B9; + z-index: -1; +} + +#prediction.active, +#prediction.ended +{ + opacity: 1; +} + +#prediction.locked { + opacity: 0!important; +} + +#prediction .info { + display: flex; + justify-content: space-evenly; + align-items: center; + font-size: 25px; + padding-block: 15px; +} + +#prediction #title { + word-wrap: break-word; + word-break: break-all; + text-align: center; + padding-inline: 50px; +} + +#prediction .info #points, #prediction .info #time { + text-align: center; + max-width: fit-content; +} + +#prediction #outcomes { + display: flex; + flex-direction: column; + padding-block: 15px; + padding-bottom: 25px; + font-size: 24px; + gap: 25px; +} + +#prediction #outcomes .outcome { + transition: filter 300ms ease-out; +} + +#prediction.ended #outcomes .outcome { + filter: brightness(50%); +} + +#prediction.ended #outcomes .outcome.winner { + filter: brightness(125%); +} + +#prediction #outcomes .outcome .title { + font-size: 25px; +} + +#prediction #outcomes .outcome .progressbar { + position: relative; + width: calc(100% - 130px); + height: 18px; + border-radius: 25px; +} + +#prediction #outcomes .outcome .progressbar::before { + position: absolute; + content: ''; + inset: 0; + border-radius: inherit; + background-color: #D9D9D9; + opacity: 0.3; +} + +#prediction #outcomes .outcome .progressbar::after { + position: absolute; + content: ''; + inset: 0; + width: 100%; + max-width: var(--progress); + transition: max-width 1.5s ease-in-out; + border-radius: inherit; + background-color: #E18E85; +} + +#prediction #outcomes .outcome .progressbar .points { + position: absolute; + top: 0; + right: -130px; + transform: translateY(-20%); +}