schulle334 / rotmg-Realmstock-Event-Filter

A Chrome extension for filtering events on the https://realmstock.com/pages/event-notifier website. This extension displays only events with a score above 70% and a player count greater than 60 out of a maximum of 85.
MIT License
1 stars 0 forks source link

issues of the filter #1

Closed schulle334 closed 2 months ago

schulle334 commented 2 months ago

Current problems that could be solved, but I don't have the time or brainpower.

schulle334 commented 2 months ago

Maybe it works so that the closed realms are displayed, but I can't test it at the moment

// Function to filter the events
function filterEvents(userSettings) {
    const { minScore, minPopulation, timeLimitMinutes } = userSettings;
    const events = document.querySelectorAll('.realmstock-panel.event');
    const seenEvents = new Set();
    const currentTime = new Date();

    events.forEach(event => {
        const scoreElement = event.querySelector('.event-score');
        const populationElement = event.querySelector('.event-population');
        const titleElement = event.querySelector('.event-title');
        const serverElement = event.querySelector('.event-server');
        const timeElement = event.querySelector('.event-time');

        // Check that the elements exist
        if (scoreElement && populationElement && titleElement && serverElement && timeElement) {
            // Extract event details
            const scoreText = scoreElement.textContent.trim();
            const scoreMatch = scoreText.match(/Score:\s*(\d+)%/);
            const score = scoreMatch ? parseInt(scoreMatch[1], 10) : 0;

            const populationText = populationElement.textContent.trim();
            const populationMatch = populationText.match(/(\d+)\/(\d+)/);
            const currentPopulation = populationMatch ? parseInt(populationMatch[1], 10) : 0;
            const maxPopulationValue = populationMatch ? parseInt(populationMatch[2], 10) : 0;

            const eventTimeText = timeElement.textContent.trim();
            const [eventHours, eventMinutes] = eventTimeText.split(':').map(num => parseInt(num, 10));
            const eventTime = new Date(currentTime);
            eventTime.setHours(eventHours);
            eventTime.setMinutes(eventMinutes);
            eventTime.setSeconds(0);

            const timeDifference = (currentTime - eventTime) / (1000 * 60); // in minutes

            const eventKey = `${titleElement.textContent.trim()}-${serverElement.textContent.trim()}-${currentPopulation}/${maxPopulationValue}`;

            // Check if the event is "Realm Closed"
            const isRealmClosed = titleElement.textContent.trim() === "Realm Closed";

            // Logic to determine if event should be displayed
            if (isRealmClosed) {
                // Always display "Realm Closed" events
                event.style.display = 'block';
            } else {
                // Apply filtering for non-"Realm Closed" events
                const shouldHideEvent = timeDifference > timeLimitMinutes || 
                                        seenEvents.has(eventKey) || 
                                        score < minScore || 
                                        currentPopulation < minPopulation || 
                                        maxPopulationValue !== 85;

                // Toggle visibility based on filters
                if (shouldHideEvent) {
                    event.style.display = 'none';
                } else {
                    event.style.display = 'block';
                    seenEvents.add(eventKey);
                }
            }
        } else {
            console.warn('One or more required elements are missing:', {
                scoreElement,
                populationElement,
                titleElement,
                serverElement,
                timeElement
            });
        }
    });
}