Spaces:
Running
on
Zero
Running
on
Zero
File size: 9,114 Bytes
a303b0f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
let miniMap, guessMarker, currentRounds = [], currentRoundIdx = 0, aiEventSource = null;
function initIndexApp() {
hydrateAuth();
document.getElementById('start-btn').addEventListener('click', async () => {
const difficulty = document.getElementById('difficulty-select-lobby').value;
const url = new URL(window.location.origin + '/game');
url.searchParams.set('difficulty', difficulty);
window.location.href = url.toString();
});
}
async function initGameApp() {
const params = new URLSearchParams(window.location.search);
const difficulty = params.get('difficulty') || 'easy';
const res = await fetch('/api/start', {
method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ difficulty })
});
const data = await res.json();
if (!res.ok) { alert(data.error || 'Failed to start game'); window.location.href = '/'; return; }
currentRounds = data.rounds;
currentRoundIdx = 0;
document.getElementById('game-container').style.display = 'block';
document.getElementById('chat-container').style.display = 'none';
document.getElementById('validate-btn').addEventListener('click', validateGuess);
document.getElementById('next-round-btn').addEventListener('click', nextRound);
const wrapEl = document.getElementById('mini-map-wrap');
document.getElementById('map-size-plus').addEventListener('click', (e) => { e.stopPropagation(); resizeMiniMap(wrapEl, 1); });
document.getElementById('map-size-minus').addEventListener('click', (e) => { e.stopPropagation(); resizeMiniMap(wrapEl, -1); });
showRound();
}
async function hydrateAuth() {
const res = await fetch('/api/me');
const me = await res.json();
const signedin = document.getElementById('signedin');
const signinBtn = document.getElementById('signin-btn');
const startBtn = document.getElementById('start-btn');
const limitMsg = document.getElementById('limit-msg');
if (me.authenticated) {
signinBtn.style.display = 'none';
signedin.style.display = 'block';
document.getElementById('username').textContent = me.username;
if (me.can_play_today) {
startBtn.disabled = false;
limitMsg.style.display = 'none';
} else {
startBtn.disabled = true;
limitMsg.style.display = 'block';
limitMsg.textContent = `You already played today. Try again in ${formatCountdown(me.seconds_until_midnight)}.`;
startCountdown(limitMsg, me.seconds_until_midnight);
}
} else {
signinBtn.style.display = 'inline-block';
signedin.style.display = 'none';
startBtn.disabled = true;
}
}
function formatCountdown(seconds) {
const h = Math.floor(seconds / 3600);
const m = Math.floor((seconds % 3600) / 60);
const s = seconds % 60;
return `${pad(h)}:${pad(m)}:${pad(s)}`;
}
function pad(n) { return n.toString().padStart(2, '0'); }
function startCountdown(el, seconds) {
let remaining = seconds;
const id = setInterval(() => {
remaining -= 1;
if (remaining <= 0) { clearInterval(id); location.reload(); return; }
el.textContent = `You already played today. Try again in ${formatCountdown(remaining)}.`;
}, 1000);
}
async function startGame() {
const difficulty = document.getElementById('difficulty-select-lobby').value;
const res = await fetch('/api/start', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ difficulty })
});
const data = await res.json();
if (!res.ok) {
alert(data.error || 'Failed to start game');
return;
}
currentRounds = data.rounds;
currentRoundIdx = 0;
document.getElementById('game-container').style.display = 'block';
document.getElementById('chat-container').style.display = 'none';
showRound();
}
function showRound() {
const round = currentRounds[currentRoundIdx];
const img = document.getElementById('street-image');
img.src = round.image_url;
document.getElementById('validate-btn').style.display = 'none';
initMiniMap();
}
function initMiniMap() {
const miniEl = document.getElementById('mini-map');
miniMap = new google.maps.Map(miniEl, {
center: { lat: 0, lng: 0 },
zoom: 1,
streetViewControl: false,
mapTypeControl: false,
fullscreenControl: false,
});
miniMap.addListener('click', (e) => {
if (miniMap._locked) return;
placeGuessMarker(e.latLng);
document.getElementById('validate-btn').style.display = 'inline-block';
});
}
function resizeMiniMap(el, delta) {
const sizes = ['size-small', 'size-medium', 'size-large'];
let idx = sizes.findIndex(c => el.classList.contains(c));
if (idx === -1) idx = 1;
idx = Math.min(2, Math.max(0, idx + (delta > 0 ? 1 : -1)));
sizes.forEach(c => el.classList.remove(c));
el.classList.add(sizes[idx]);
if (miniMap) {
const miniEl = document.getElementById('mini-map');
google.maps.event.trigger(miniMap, 'resize');
const center = miniMap.getCenter();
miniMap.setCenter(center);
}
}
function placeGuessMarker(latLng) {
if (guessMarker) guessMarker.setMap(null);
guessMarker = new google.maps.Marker({ position: latLng, map: miniMap });
miniMap.setCenter(latLng);
}
async function validateGuess() {
if (!guessMarker) { alert('Click on the mini-map to pick your guess.'); return; }
const round = currentRounds[currentRoundIdx];
const pos = guessMarker.getPosition();
const payload = { round_id: round.id, lat: pos.lat(), lng: pos.lng() };
const res = await fetch('/api/guess', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(payload) });
const result = await res.json();
if (!res.ok) { alert(result.error || 'Guess failed'); return; }
miniMap._locked = true;
document.getElementById('validate-btn').style.display = 'none';
document.getElementById('chat-container').style.display = 'block';
const chat = document.getElementById('chat-log');
chat.textContent = '';
chat.textContent += 'Analyzing image...\n';
const aiRes = await fetch('/api/ai_analyze', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ round_id: round.id }) });
const aiData = await aiRes.json();
if (!aiRes.ok) {
chat.textContent += `[Error] ${aiData.error || 'AI failed'}\n`;
document.getElementById('next-round-btn').disabled = false;
openResultsPopup({ actual: result.actual_location, human: result.guess_location });
return;
}
chat.textContent += (aiData.text || '') + '\n';
openResultsPopup({ actual: result.actual_location, human: result.guess_location });
if (aiData.ai_guess) renderAIGuessOnPopup(aiData.ai_guess);
document.getElementById('next-round-btn').disabled = false;
}
function openResultsPopup(points) {
const overlay = document.getElementById('popup-overlay');
overlay.style.display = 'flex';
const popupMap = new google.maps.Map(document.getElementById('popup-map'), { zoom: 3, center: points.actual });
new google.maps.Marker({ position: points.actual, map: popupMap, label: 'A' });
new google.maps.Marker({ position: points.human, map: popupMap, label: 'H' });
new google.maps.Polyline({ path: [points.actual, points.human], geodesic: true, strokeColor: '#F97316', strokeOpacity: 1.0, strokeWeight: 2, map: popupMap });
document.getElementById('next-round-btn').disabled = true;
}
function appendChatToken(t) {
const el = document.getElementById('chat-log');
const span = document.createElement('span');
span.textContent = t;
el.appendChild(span);
el.scrollTop = el.scrollHeight;
}
function renderAIGuessOnPopup(ai) {
const mapEl = document.getElementById('popup-map');
const popupMap = mapEl._map || new google.maps.Map(mapEl, { zoom: 3, center: ai });
mapEl._map = popupMap;
new google.maps.Marker({ position: ai, map: popupMap, label: 'AI' });
}
async function nextRound() {
if (aiEventSource) { try { aiEventSource.close(); } catch (e) {} }
currentRoundIdx += 1;
if (currentRoundIdx >= currentRounds.length) {
await showFinalResults();
return;
}
document.getElementById('popup-overlay').style.display = 'none';
showRound();
}
async function showFinalResults() {
const res = await fetch('/api/session_summary');
const data = await res.json();
document.getElementById('popup-overlay').style.display = 'none';
document.getElementById('game-container').style.display = 'none';
const fin = document.getElementById('final-results');
fin.style.display = 'block';
const total = data.total_score?.toFixed ? data.total_score.toFixed(0) : data.total_score;
document.getElementById('final-summary').textContent = `Your total score: ${total} / 15000`;
}
function sleep(ms) { return new Promise(r => setTimeout(r, ms)); } |