Widget:AvailableCritters: Difference between revisions

From Dreamlight Valley Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 13: Line 13:
         const currentTime = now.getHours() * 100 + now.getMinutes();
         const currentTime = now.getHours() * 100 + now.getMinutes();
         const times = critter.times[dayOfWeek];
         const times = critter.times[dayOfWeek];
         if (!times) return false; // Not available today
         if (!times) return false; // Not available today


         return times.some(timeRange => {
         return times.some(timeRange => {
             const [start, end] = timeRange.split('-').map(time => {
             const [start, end] = timeRange.map(time => {
                 const [hours, minutes] = time.split(':');
                 const [hours, minutes] = time.split(':');
                 return parseInt(hours, 10) * 100 + parseInt(minutes, 10);
                 return parseInt(hours, 10) * 100 + parseInt(minutes, 10);
Line 28: Line 27:
     function generateGalleryContent(critters) {
     function generateGalleryContent(critters) {
         return critters.filter(isCritterAvailable).map(critter =>  
         return critters.filter(isCritterAvailable).map(critter =>  
             `File:${critter.name.replace(/ /g, '_')}.png|link=${critter.name}`
             `{{CollectionGallery|${critter.name.replace(/ /g, '_')}}}`
         ).join('\n');
         ).join('\n');
     }
     }


     // Function to update the gallery
     // Insert the gallery content into the page
     function updateGallery() {
     function insertGalleryContent() {
         const galleryElement = document.getElementById('crittergallery');
         const galleryContent = generateGalleryContent(critters);
         if (galleryElement) {
        // Assuming there's an element with an ID where you want to insert the gallery content
             const newGalleryContent = generateGalleryContent(critters);
        const targetElement = document.getElementById('crittergallery');
            galleryElement.innerHTML = `<gallery widths=60px heights=60px class="clothesgallery">\n${newGalleryContent}\n</gallery>`;
         if (targetElement) {
             targetElement.innerHTML = galleryContent;
         }
         }
     }
     }


     // Update the gallery
     // Insert the gallery content
     updateGallery();
     insertGalleryContent();
});
});
</script>

Revision as of 22:32, 26 February 2024

<script> document.addEventListener('DOMContentLoaded', function() {

 // Critter availability
 const critters = [
   { "name": "Emerald Sunbird", "times": { "Monday": ["12:00", "24:00"] } },
   { "name": "Golden Sunbird", "times": { "Monday": ["00:00", "12:00"] } }

];

   // Function to check if a critter is available based on the current day and time
   function isCritterAvailable(critter) {
       const now = new Date();
       const dayOfWeek = now.toLocaleString('en-us', { weekday: 'long' });
       const currentTime = now.getHours() * 100 + now.getMinutes();
       const times = critter.times[dayOfWeek];
       if (!times) return false; // Not available today
       return times.some(timeRange => {
           const [start, end] = timeRange.map(time => {
               const [hours, minutes] = time.split(':');
               return parseInt(hours, 10) * 100 + parseInt(minutes, 10);
           });
           return currentTime >= start && currentTime <= end;
       });
   }
   // Function to generate gallery content string based on availability
   function generateGalleryContent(critters) {
       return critters.filter(isCritterAvailable).map(critter => 

`

  • [[File:${critter.name.replace(/ /g, '_').png|120x120px|link=${critter.name.replace(/ /g, '_')]]

    [[${critter.name.replace(/ /g, '_')]]

  • }`

           ).join('\n');
       }
    
       // Insert the gallery content into the page
       function insertGalleryContent() {
           const galleryContent = generateGalleryContent(critters);
           // Assuming there's an element with an ID where you want to insert the gallery content
           const targetElement = document.getElementById('crittergallery');
           if (targetElement) {
               targetElement.innerHTML = galleryContent;
           }
       }
    
       // Insert the gallery content
       insertGalleryContent();
    

    });