Widget:AvailableCritters: Difference between revisions

m
no edit summary
mNo edit summary
mNo edit summary
Line 62: Line 62:
// 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 gallery
function updateGallery() {
function updateGallery() {
  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 = '';
    let galleryContent = '';
  for (const [critter, _] of Object.entries(critters)) {
    for (const [critter, _] of Object.entries(critters)) {
    if (isAvailable(critter, dayOfWeek, hour)) {
        if (isAvailable(critter, dayOfWeek, hour)) {
      galleryContent += `File:${critter.replace(/ /g, '_')}.png|link=${critter}\n`;
            galleryContent += `File:${critter.replace(/ /g, '_')}.png|link=${critter}\n`;
        }
     }
     }
  }


  // Assuming you have a div with id 'critterGallery' to host the gallery
    // Assuming you have a div with id 'critterGallery' to host the gallery
  document.getElementById('critterGallery').innerHTML = `<gallery widths=50px heights=50px class="clothesgallery">${galleryContent}</gallery>`;
    const galleryContainer = document.getElementById('critterGallery');
    if (galleryContainer) {
        galleryContainer.innerHTML = `<gallery widths="50px" heights="50px" class="clothesgallery">${galleryContent}</gallery>`;
    }
}
}


// Set an interval to update the gallery every hour
// Listen for the wikipage.content hook to update the gallery after MediaWiki processes the content
setInterval(updateGallery, 3600000); // Update every hour
mw.hook('wikipage.content').add(function ($content) {
updateGallery(); // Initial update
    updateGallery();
});
 
// Update the gallery immediately on script load, in case it's not triggered by mw.hook
updateGallery();
</script>
</script>