Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add event scheduler by json #3085

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

dudantas
Copy link
Member

@dudantas dudantas commented Nov 8, 2024

Description

This PR introduces a new feature for the Event Scheduler system that enables it to read event configurations from a JSON file. This change modernizes and simplifies the way events are configured, moving away from XML files to a more developer-friendly JSON format. The update aims to improve readability, maintainability, and flexibility when managing event data.

Additionally, the new implementation enhances consistency by providing a unified approach for defining event details, loot tables, and other parameters. The JSON-based configuration also helps reduce parsing errors and makes the addition of new event properties easier.

New case from login.php (myaac):

case 'eventschedule':
        $eventlist = [];

        $json_file_path = config('server_path') . 'data/json/eventscheduler/events.json';
        $xml_file_path = config('server_path') . 'data/XML/events.xml';

        if (file_exists($json_file_path)) {
            $jsonContent = file_get_contents($json_file_path);
            $events = json_decode($jsonContent, true);

            if (json_last_error() !== JSON_ERROR_NONE) {
                error_log("Error parsing JSON: " . json_last_error_msg());
                die(json_encode([]));
            }

            foreach ($events['events'] as $event) {
                if ($event) {
                    $startdate = strtotime($event['startdate'] ?? ''); // Convert start date to timestamp
                    $enddate = strtotime($event['enddate'] ?? '');     // Convert end date to timestamp

                    $eventData = [
                        'colorlight'      => $event['colors']['colorlight'] ?? '',
                        'colordark'       => $event['colors']['colordark'] ?? '',
                        'description'     => $event['description'] ?? '',
                        'displaypriority' => intval($event['details']['displaypriority'] ?? 0),
                        'enddate'         => $enddate,
                        'isseasonal'      => getBoolean(intval($event['details']['isseasonal'] ?? 0)),
                        'name'            => $event['name'] ?? '',
                        'startdate'       => $startdate,
                        'specialevent'    => getBoolean(intval($event['details']['specialevent'] ?? 0))
                    ];
                    $eventlist[] = $eventData;
                }
            }

        } elseif (file_exists($xml_file_path)) {
            $xml = new DOMDocument;
            $xml->load($xml_file_path);
            $tableevent = $xml->getElementsByTagName('event');

            foreach ($tableevent as $event) {
                if ($event) {
                    $eventData = [
                        'colorlight'      => parseEvent($event->getElementsByTagName('colors'), false, 'colorlight'),
                        'colordark'       => parseEvent($event->getElementsByTagName('colors'), false, 'colordark'),
                        'description'     => parseEvent($event->getElementsByTagName('description'), false, 'description'),
                        'displaypriority' => intval(parseEvent($event->getElementsByTagName('details'), false, 'displaypriority')),
                        'enddate'         => strtotime(parseEvent($event, true, false)), // Convert end date to timestamp
                        'isseasonal'      => getBoolean(intval(parseEvent($event->getElementsByTagName('details'), false, 'isseasonal'))),
                        'name'            => $event->getAttribute('name'),
                        'startdate'       => strtotime(parseEvent($event, true, true)), // Convert start date to timestamp
                        'specialevent'    => getBoolean(intval(parseEvent($event->getElementsByTagName('details'), false, 'specialevent')))
                    ];
                    $eventlist[] = $eventData;
                }
            }
        } else {
            die(json_encode([]));
        }

        error_log("Event list prepared: " . print_r($eventlist, true));
        error_log("Last update timestamp: " . time());

        $response = [
            'eventlist' => $eventlist,
            'lastupdatetimestamp' => time()
        ];
        header('Content-Type: application/json');
        echo json_encode($response);
        break;

Feature Highlights:

  • New JSON-based event scheduler configuration.
  • Automatic loading of event details such as name, start and end dates, and loot settings from a JSON file.
  • Backward-compatible approach that ensures current functionality remains intact.

Behaviour

Actual

Currently, the Event Scheduler uses an XML file to manage events, which makes it more difficult to add or modify configurations due to the verbosity and inflexibility of XML.

Expected

The Event Scheduler should now use a JSON file (events.json) to manage events. JSON provides a more compact, readable, and maintainable structure for event configuration. Users should be able to add, edit, and manage events more easily with this new format.

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested

Tests were conducted to ensure that the new JSON format loads correctly and events are scheduled as expected. The tests included validating successful parsing of the JSON file, checking the correct initialization of event properties, and ensuring existing functionality remains stable.

  • Loaded various JSON configurations with multiple events to verify accurate parsing.
  • Confirmed that events are correctly registered, including start and end dates, and associated loot tables.
  • Tested with malformed JSON to ensure appropriate error handling.

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I checked the PR checks reports
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my feature works effectively

@dudantas dudantas force-pushed the dudantas/improve-eventscheduler-by-json branch from af9e0d0 to e5eb93e Compare November 8, 2024 23:26
@dudantas dudantas force-pushed the dudantas/improve-eventscheduler-by-json branch from 03f9c93 to 837bb04 Compare November 13, 2024 04:52
@dudantas dudantas force-pushed the dudantas/improve-eventscheduler-by-json branch from 0821470 to ef78a00 Compare November 13, 2024 16:58
Copy link

sonarqubecloud bot commented Dec 7, 2024

@Codemaufeito
Copy link

can it be tested?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants