opentibiabr / canary

Canary Server 13.x for OpenTibia community.
https://docs.opentibiabr.com/
GNU General Public License v2.0
384 stars 638 forks source link

feat: add event scheduler by json #3085

Open dudantas opened 3 weeks ago

dudantas commented 3 weeks ago

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:

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

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.

Checklist

sonarcloud[bot] commented 2 weeks ago

Quality Gate Passed Quality Gate passed

Issues
9 New issues
0 Accepted issues

Measures
0 Security Hotspots
0.0% Coverage on New Code
0.0% Duplication on New Code

See analysis details on SonarQube Cloud