Notifications, div fixes, kekse für last location

This commit is contained in:
2025-09-06 12:37:10 +02:00
parent 61d5ef2e6f
commit 8342d95a13
13 changed files with 1325 additions and 39 deletions

View File

@@ -10,6 +10,58 @@ const socket = io();
// Global variable to store locations with coordinates
let locationsData = [];
let lastSelectedLocation = null;
// Cookie Functions (inline implementation)
function setCookie(name, value, days = 30) {
const expires = new Date();
expires.setTime(expires.getTime() + (days * 24 * 60 * 60 * 1000));
document.cookie = `${name}=${value};expires=${expires.toUTCString()};path=/;SameSite=Lax`;
}
function getCookie(name) {
const nameEQ = name + "=";
const ca = document.cookie.split(';');
for (let i = 0; i < ca.length; i++) {
let c = ca[i];
while (c.charAt(0) === ' ') c = c.substring(1, c.length);
if (c.indexOf(nameEQ) === 0) return c.substring(nameEQ.length, c.length);
}
return null;
}
function loadLastSelectedLocation() {
try {
const cookieValue = getCookie('ninjacross_last_location');
if (cookieValue) {
const lastLocation = JSON.parse(cookieValue);
lastSelectedLocation = lastLocation;
console.log('📍 Last selected location loaded:', lastLocation.name);
return lastLocation;
}
} catch (error) {
console.error('Error loading last location:', error);
}
return null;
}
function saveLocationSelection(locationId, locationName) {
try {
// Remove emoji from location name for storage
const cleanName = locationName.replace(/^📍\s*/, '');
const locationData = {
id: locationId,
name: cleanName,
timestamp: new Date().toISOString()
};
setCookie('ninjacross_last_location', JSON.stringify(locationData), 90);
lastSelectedLocation = { id: locationId, name: cleanName };
console.log('💾 Location saved to cookie:', cleanName);
} catch (error) {
console.error('Error saving location:', error);
}
}
// WebSocket Event Handlers
socket.on('connect', () => {
@@ -129,6 +181,23 @@ async function loadLocations() {
locationSelect.appendChild(option);
});
// Load and set last selected location
const lastLocation = loadLastSelectedLocation();
if (lastLocation) {
// Find the option that matches the last location name
const matchingOption = Array.from(locationSelect.options).find(option =>
option.textContent === `📍 ${lastLocation.name}` || option.value === lastLocation.name
);
if (matchingOption) {
locationSelect.value = matchingOption.value;
console.log('📍 Last selected location restored:', lastLocation.name);
// Update the current selection display
updateCurrentSelection();
// Load data for the restored location
loadData();
}
}
} catch (error) {
console.error('Error loading locations:', error);
}
@@ -489,7 +558,15 @@ function updateLeaderboard(data) {
// Event Listeners Setup
function setupEventListeners() {
// Location select event listener
document.getElementById('locationSelect').addEventListener('change', loadData);
document.getElementById('locationSelect').addEventListener('change', function() {
// Save location selection to cookie
const selectedOption = this.options[this.selectedIndex];
if (selectedOption.value) {
saveLocationSelection(selectedOption.value, selectedOption.textContent);
}
// Load data
loadData();
});
// Time tab event listeners
document.querySelectorAll('.time-tab').forEach(tab => {