Widget:AvailableCritters: Difference between revisions

Jump to navigation Jump to search
m
no edit summary
mNo edit summary
mNo edit summary
 
(6 intermediate revisions by the same user not shown)
Line 60: Line 60:
};
};


// Function to check if a critter is available
    function isAvailable(critter, day, hour) {
function isAvailable(critter, day, hour) {
        const availability = critters[critter][day];
  const times = critters[critter][day];
        if (!availability) return false; // 'n/a' or undefined
  if (!times) return false; // 'n/a' or undefined
        if (availability === 'All day') return true;
  if (times === 'All day') return true;
        const [start, end] = availability.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
    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 updateCritterList() {
function updateGallery() {
        const now = new Date();
  const now = new Date();
        const dayOfWeek = now.toLocaleString('en-US', { weekday: 'long' });
  const dayOfWeek = now.toLocaleString('en-US', { weekday: 'long' }); // Get day name
        const hour = now.getHours();
  const hour = now.getHours(); // Get current hour in 24-hour format
        let listContent = '';


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


  // Assuming you have a div with id 'critterGallery' to host the gallery
    function parseContent(wikitext) {
  document.getElementById('critterGallery').innerHTML = `<gallery widths=50px heights=50px class="clothesgallery">${galleryContent}</gallery>`;
        const apiEndpoint = "/api.php";
}
        const params = new URLSearchParams({
            action: 'parse',
            format: 'json',
            contentmodel: 'wikitext',
            text: wikitext,
            prop: 'text'
        });
 
        fetch(apiEndpoint + "?" + params.toString(), {
            method: 'POST',
            credentials: 'same-origin'
        })
        .then(response => response.json())
        .then(data => {
            const parsedHtml = data.parse.text['*'];
            document.getElementById('critterList').innerHTML = parsedHtml;
        })
        .catch(error => console.error('Error parsing content:', error));
    }


// Set an interval to update the gallery every hour
    window.addEventListener('load', function() {
setInterval(updateGallery, 3600000); // Update every hour
        updateCritterList(); // Initial update
updateGallery(); // Initial update
        setInterval(updateCritterList, 60000); // Refresh every minute
    });
</script>
</script>

Navigation menu