|
| 1 | +// SPDX-FileCopyrightText: Copyright (C) 2025 ARDUINO SA <http://www.arduino.cc> |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: MPL-2.0 |
| 4 | + |
| 5 | +const socket = io(`http://${window.location.host}`); |
| 6 | +const errorContainer = document.getElementById('error-container'); |
| 7 | + |
| 8 | +// Game state |
| 9 | +const targetObjects = ['book', 'bottle', 'chair', 'cup', 'cell phone']; |
| 10 | +let foundObjects = []; |
| 11 | +let gameStarted = false; |
| 12 | + |
| 13 | +// UI Elements |
| 14 | +const gameIntro = document.getElementById('game-intro'); |
| 15 | +const gameContent = document.getElementById('game-content'); |
| 16 | +const startGameBtn = document.getElementById('start-game-btn'); |
| 17 | +const objectsToFindList = document.getElementById('objects-to-find-list'); |
| 18 | +const videoFeedContainer = document.getElementById('videoFeedContainer'); |
| 19 | +const winScreen = document.getElementById('win-screen'); |
| 20 | +const playAgainBtn = document.getElementById('play-again-btn'); |
| 21 | + |
| 22 | +document.addEventListener('DOMContentLoaded', () => { |
| 23 | + initSocketIO(); |
| 24 | + initializeConfidenceSlider(); |
| 25 | + renderObjectsToFind(); |
| 26 | + |
| 27 | + startGameBtn.addEventListener('click', startGame); |
| 28 | + playAgainBtn.addEventListener('click', resetGame); |
| 29 | +}); |
| 30 | + |
| 31 | +function initSocketIO() { |
| 32 | + socket.on('connect', () => { |
| 33 | + if (errorContainer) { |
| 34 | + errorContainer.style.display = 'none'; |
| 35 | + errorContainer.textContent = ''; |
| 36 | + } |
| 37 | + }); |
| 38 | + |
| 39 | + socket.on('disconnect', () => { |
| 40 | + if (errorContainer) { |
| 41 | + errorContainer.textContent = 'Connection to the board lost. Please check the connection.'; |
| 42 | + errorContainer.style.display = 'block'; |
| 43 | + } |
| 44 | + }); |
| 45 | + |
| 46 | + socket.on('detection', async (message) => { |
| 47 | + if (gameStarted) { |
| 48 | + handleDetection(message); |
| 49 | + } |
| 50 | + }); |
| 51 | +} |
| 52 | + |
| 53 | +function startGame() { |
| 54 | + gameStarted = true; |
| 55 | + gameIntro.classList.add('hidden'); |
| 56 | + gameContent.classList.remove('hidden'); |
| 57 | + updateFoundCounter(); |
| 58 | +} |
| 59 | + |
| 60 | +function resetGame() { |
| 61 | + gameStarted = false; |
| 62 | + foundObjects = []; |
| 63 | + winScreen.classList.add('hidden'); |
| 64 | + videoFeedContainer.classList.remove('hidden'); |
| 65 | + gameIntro.classList.remove('hidden'); |
| 66 | + gameContent.classList.add('hidden'); |
| 67 | + renderObjectsToFind(); |
| 68 | + updateFoundCounter(); |
| 69 | +} |
| 70 | + |
| 71 | +function updateFoundCounter() { |
| 72 | + const chip = document.getElementById('found-counter-chip'); |
| 73 | + if (chip) { |
| 74 | + chip.textContent = `${foundObjects.length}/5 found`; |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +function renderObjectsToFind() { |
| 79 | + objectsToFindList.innerHTML = ''; |
| 80 | + targetObjects.forEach(obj => { |
| 81 | + const item = document.createElement('div'); |
| 82 | + item.id = `obj-${obj}`; |
| 83 | + item.className = 'object-item'; |
| 84 | + |
| 85 | + const icon = document.createElement('img'); |
| 86 | + icon.src = `./img/${obj}.svg`; |
| 87 | + icon.alt = `${obj} icon`; |
| 88 | + item.appendChild(icon); |
| 89 | + |
| 90 | + const text = document.createElement('span'); |
| 91 | + text.textContent = obj; |
| 92 | + item.appendChild(text); |
| 93 | + |
| 94 | + objectsToFindList.appendChild(item); |
| 95 | + }); |
| 96 | +} |
| 97 | + |
| 98 | +function handleDetection(detection) { |
| 99 | + const detectedObject = detection.content.toLowerCase(); |
| 100 | + if (targetObjects.includes(detectedObject) && !foundObjects.includes(detectedObject)) { |
| 101 | + foundObjects.push(detectedObject); |
| 102 | + const foundItem = document.getElementById(`obj-${detectedObject}`); |
| 103 | + foundItem.classList.add('found'); |
| 104 | + |
| 105 | + const foundIcon = document.createElement('img'); |
| 106 | + foundIcon.src = './img/found-icon.svg'; |
| 107 | + foundIcon.alt = 'Found'; |
| 108 | + foundIcon.className = 'found-icon'; |
| 109 | + foundItem.appendChild(foundIcon); |
| 110 | + |
| 111 | + updateFoundCounter(); |
| 112 | + checkWinCondition(); |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +function checkWinCondition() { |
| 117 | + if (foundObjects.length === targetObjects.length) { |
| 118 | + gameStarted = false; |
| 119 | + videoFeedContainer.classList.add('hidden'); |
| 120 | + winScreen.classList.remove('hidden'); |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +function initializeConfidenceSlider() { |
| 125 | + const confidenceSlider = document.getElementById('confidenceSlider'); |
| 126 | + const confidenceInput = document.getElementById('confidenceInput'); |
| 127 | + const confidenceResetButton = document.getElementById('confidenceResetButton'); |
| 128 | + |
| 129 | + if (!confidenceSlider) return; |
| 130 | + |
| 131 | + confidenceSlider.addEventListener('input', updateConfidenceDisplay); |
| 132 | + confidenceInput.addEventListener('input', handleConfidenceInputChange); |
| 133 | + confidenceInput.addEventListener('blur', validateConfidenceInput); |
| 134 | + updateConfidenceDisplay(); |
| 135 | + |
| 136 | + confidenceResetButton.addEventListener('click', (e) => { |
| 137 | + if (e.target.classList.contains('reset-icon') || e.target.closest('.reset-icon')) { |
| 138 | + resetConfidence(); |
| 139 | + } |
| 140 | + }); |
| 141 | +} |
| 142 | + |
| 143 | +function handleConfidenceInputChange() { |
| 144 | + const confidenceInput = document.getElementById('confidenceInput'); |
| 145 | + const confidenceSlider = document.getElementById('confidenceSlider'); |
| 146 | + |
| 147 | + let value = parseFloat(confidenceInput.value); |
| 148 | + |
| 149 | + if (isNaN(value)) value = 0.5; |
| 150 | + if (value < 0) value = 0; |
| 151 | + if (value > 1) value = 1; |
| 152 | + |
| 153 | + confidenceSlider.value = value; |
| 154 | + updateConfidenceDisplay(); |
| 155 | +} |
| 156 | + |
| 157 | +function validateConfidenceInput() { |
| 158 | + const confidenceInput = document.getElementById('confidenceInput'); |
| 159 | + let value = parseFloat(confidenceInput.value); |
| 160 | + |
| 161 | + if (isNaN(value)) value = 0.5; |
| 162 | + if (value < 0) value = 0; |
| 163 | + if (value > 1) value = 1; |
| 164 | + |
| 165 | + confidenceInput.value = value.toFixed(2); |
| 166 | + |
| 167 | + handleConfidenceInputChange(); |
| 168 | +} |
| 169 | + |
| 170 | +function updateConfidenceDisplay() { |
| 171 | + const confidenceSlider = document.getElementById('confidenceSlider'); |
| 172 | + const confidenceInput = document.getElementById('confidenceInput'); |
| 173 | + const confidenceValueDisplay = document.getElementById('confidenceValueDisplay'); |
| 174 | + const sliderProgress = document.getElementById('sliderProgress'); |
| 175 | + |
| 176 | + if (!confidenceSlider) return; |
| 177 | + |
| 178 | + const value = parseFloat(confidenceSlider.value); |
| 179 | + socket.emit('override_th', value); // Send confidence to backend |
| 180 | + const percentage = (value - confidenceSlider.min) / (confidenceSlider.max - confidenceSlider.min) * 100; |
| 181 | + |
| 182 | + const displayValue = value.toFixed(2); |
| 183 | + confidenceValueDisplay.textContent = displayValue; |
| 184 | + |
| 185 | + if (document.activeElement !== confidenceInput) { |
| 186 | + confidenceInput.value = displayValue; |
| 187 | + } |
| 188 | + |
| 189 | + sliderProgress.style.width = percentage + '%'; |
| 190 | + confidenceValueDisplay.style.left = percentage + '%'; |
| 191 | +} |
| 192 | + |
| 193 | +function resetConfidence() { |
| 194 | + |
| 195 | + const confidenceSlider = document.getElementById('confidenceSlider'); |
| 196 | + |
| 197 | + const confidenceInput = document.getElementById('confidenceInput'); |
| 198 | + |
| 199 | + |
| 200 | + |
| 201 | + if (!confidenceSlider) return; |
| 202 | + |
| 203 | + |
| 204 | + |
| 205 | + confidenceSlider.value = '0.5'; |
| 206 | + |
| 207 | + confidenceInput.value = '0.50'; |
| 208 | + |
| 209 | + updateConfidenceDisplay(); |
| 210 | + |
| 211 | +} |
| 212 | + |
| 213 | + |
0 commit comments