Widget:AvailableCritters: Difference between revisions

m
no edit summary
mNo edit summary
mNo edit summary
Line 60: Line 60:
};
};


// Function to check if a critter is available
    // Function to check if a critter is available
function isAvailable(critter, day, hour) {
    function isAvailable(critter, day, hour) {
    const times = critters[critter][day];
        const times = critters[critter][day];
    if (!times) return false; // 'n/a' or undefined
        if (!times) return false; // 'n/a' or undefined
    if (times === 'All day') return true;
        if (times === 'All day') return true;
    const [start, end] = times.split(' to ');
        const [start, end] = times.split(' to ');
    const startHour = parseHour(start);
        const startHour = parseHour(start);
    const endHour = parseHour(end);
        const endHour = parseHour(end);
    return hour >= startHour && hour < endHour;
        return hour >= startHour && hour < endHour;
}
    }


// Helper function to parse hour string to 24-hour format number
    // Helper function to parse hour string to 24-hour format number
function parseHour(timeStr) {
    function parseHour(timeStr) {
    const [time, period] = timeStr.split(' ');
        const [time, period] = timeStr.split(' ');
    let [hours, minutes] = time.split(':');
        let [hours, minutes] = time.split(':');
    hours = parseInt(hours);
        hours = parseInt(hours);
    if (period === 'PM' && hours !== 12) hours += 12;
        if (period === 'PM' && hours !== 12) hours += 12;
    if (period === 'AM' && hours === 12) hours = 0;
        if (period === 'AM' && hours === 12) hours = 0;
    return hours;
        return hours;
}
    }


// Function to update the gallery
    // Function to update the critter list based on the current time
function updateGallery() {
    function updateCritterList() {
    const now = new Date();
        const now = new Date();
    const dayOfWeek = now.toLocaleString('en-US', { weekday: 'long' }); // Get day name
        const dayOfWeek = now.toLocaleString('en-US', { weekday: 'long' }); // Get day name
    const hour = now.getHours(); // Get current hour in 24-hour format
        const hour = now.getHours(); // Get current hour in 24-hour format


    let galleryContent = '<gallery>';
        let listContent = '';
    for (const [critter, _] of Object.entries(critters)) {
        for (const [critterName, _] of Object.entries(critters)) {
        if (isAvailable(critter, dayOfWeek, hour)) {
            if (isAvailable(critterName, dayOfWeek, hour)) {
            galleryContent += `File:${critter.replace(/ /g, '_')}.png|${critter}\n`;
                listContent += `<p>{{name|${critterName}}}</p>`;
            }
         }
         }
    }
    galleryContent += '</gallery>';


    const galleryContainer = document.getElementById('critterGallery');
        const listContainer = document.getElementById('critterList');
    if (galleryContainer) {
        if (listContainer) {
        galleryContainer.innerHTML = galleryContent;
            listContainer.innerHTML = listContent;
    } else {
        } else {
        console.log('Gallery container not found');
            console.log('List container not found');
    }
         }
}
 
// Ensure script runs after MediaWiki and its modules are available
window.addEventListener('load', function() {
    if (typeof mw === 'object' && typeof mw.hook === 'function') {
        mw.hook('wikipage.content').add(function ($content) {
            updateGallery();
         });
    } else {
        console.error('MediaWiki scripts not loaded');
     }
     }
});


// Attempt to update gallery immediately on script load
    // Ensure the script runs after the page has loaded
updateGallery();
    window.addEventListener('load', function() {
        updateCritterList(); // Update the list immediately upon loading
        setInterval(updateCritterList, 60000); // Update the list every minute
    });
</script>
</script>