[WIP] Lua + TOML stages loading

Lua extension required for .lua (canary)
This commit is contained in:
slawkens
2026-04-19 17:22:32 +02:00
parent 5dd97ad8fd
commit 6dc16397a1
6 changed files with 180 additions and 23 deletions

View File

@@ -11,20 +11,20 @@
defined('MYAAC') or die('Direct access not allowed!');
$title = 'Experience Stages';
if(file_exists($config['data_path'] . 'XML/stages.xml')) {
$stages = new DOMDocument();
$stages->load($config['data_path'] . 'XML/stages.xml');
}
if(!isset($config['lua']['experienceStages']) || !getBoolean($config['lua']['experienceStages']))
{
if((!isset($config['lua']['experienceStages']) || !getBoolean($config['lua']['experienceStages']))
&& (!isset($config['lua']['rateUseStages']) || !getBoolean($config['lua']['rateUseStages']))
) {
$enabled = false;
if(isset($stages)) {
if(file_exists($config['data_path'] . 'XML/stages.xml')) {
$stages = new DOMDocument();
$stages->load($config['data_path'] . 'XML/stages.xml');
foreach($stages->getElementsByTagName('config') as $node) {
/** @var DOMElement $node */
if($node->getAttribute('enabled'))
if($node->getAttribute('enabled')) {
$enabled = true;
}
}
}
@@ -42,21 +42,12 @@ if(!isset($config['lua']['experienceStages']) || !getBoolean($config['lua']['exp
}
}
if(!$stages)
{
echo 'Error: cannot load <b>stages.xml</b>!';
$stages = new MyAAC\Server\ExpStages();
$stagesArray = $stages->get();
if (empty($stagesArray)) {
echo 'Error when loading experience stages.';
return;
}
$stagesArray = [];
foreach($stages->getElementsByTagName('stage') as $stage)
{
/** @var DOMElement $stage */
$maxLevel = $stage->getAttribute('maxlevel');
$stagesArray[] = [
'levels' => $stage->getAttribute('minlevel') . (isset($maxLevel[0]) ? '-' . $maxLevel : '+'),
'multiplier' => $stage->getAttribute('multiplier')
];
}
$twig->display('experience_stages.html.twig', ['stages' => $stagesArray]);

View File

@@ -0,0 +1,35 @@
<?php
namespace MyAAC\Server;
use MyAAC\Cache\Cache;
class ExpStages
{
private static array $stages = [];
public static function get() {
if (count(self::$stages) == 0) {
self::$stages = Cache::remember('exp_stages', 10 * 60, function () {
if (file_exists(config('server_path') . TOML\ExpStages::FILE)) {
$expStages = new TOML\ExpStages();
}
elseif (file_exists(config('data_path') . XML\ExpStages::FILE)) {
$expStages = new XML\ExpStages();
}
elseif (file_exists(config('data_path') . Lua\ExpStages::FILE)) {
$expStages = new Lua\ExpStages();
}
else {
return [];
}
$expStages->load();
return $expStages->get();
});
}
return self::$stages;
}
}

View File

@@ -0,0 +1,46 @@
<?php
namespace MyAAC\Server\Lua;
class ExpStages
{
private array $stages = [];
const FILE = 'stages.lua';
public function load(): void
{
$file = config('data_path') . self::FILE;
if(!@file_exists($file)) {
return;
}
if (!extension_loaded('lua')) {
return;
}
$lua = new \Lua();
try {
$stagesContent = file_get_contents($file);
$stagesContent .= 'return experienceStages';
$stages = $lua->eval($stagesContent);
}
catch (\Exception $e) {
error('Error: Cannot load stages.lua. More info in system/logs/error.log file.');
log_append('error.log', "[" . __CLASS__ . "] Fatal error: Cannot load stages.lua - $file. Error: " . $e->getMessage());
return;
}
foreach ($stages as $stage) {
$this->stages[] = [
'levels' => $stage['minlevel'] . (isset($stage['maxlevel']) ? '-' . $stage['maxlevel'] : '+'),
'multiplier' => $stage['multiplier']
];
}
}
public function get(): array {
return $this->stages;
}
}

View File

@@ -50,6 +50,7 @@ class Config
$this->config['freePremium'] = $this->config['server']['accounts']['free_premium'] ?? false;
$this->config['ip'] = $this->config['server']['network']['ip'] ?? '127.0.0.1';
$this->config['worldType'] = $this->config['server']['world']['type'] ?? 'unknown';
$this->config['experienceStages'] = $this->config['stages']['config']['enabled'] ?? false;
$this->config['houseRentPeriod'] = $this->config['server']['houses']['rent_period'] ?? 'never';
$this->config['pzLocked'] = $this->config['combat']['skull']['pz_locked'] ?? 60 * 1000;
$this->config['url'] = $this->config['server']['identity']['url'] ?? 'http://localhost';

View File

@@ -0,0 +1,44 @@
<?php
namespace MyAAC\Server\TOML;
use Devium\Toml\Toml;
class ExpStages
{
private array $stages = [];
const FILE = 'config/stages.toml';
public function load(): void
{
$file = config('server_path') . self::FILE;
if(!@file_exists($file)) {
return;
}
$toml = file_get_contents($file);
try {
$stages = Toml::decode($toml, asArray: true);
}
catch (\Exception $e) {
error('Error: Cannot load stages.toml. More info in system/logs/error.log file.');
log_append('error.log', "[" . __CLASS__ . "] Fatal error: Cannot load stages.toml - $file. Error: " . $e->getMessage());
return;
}
foreach ($stages['stage'] as $stage) {
$this->stages[] = [
'levels' => $stage['minlevel'] . (isset($stage['maxlevel']) ? '-' . $stage['maxlevel'] : '+'),
'multiplier' => $stage['multiplier']
];
}
}
public function get(): array {
return $this->stages;
}
}

View File

@@ -0,0 +1,40 @@
<?php
namespace MyAAC\Server\XML;
class ExpStages
{
private array $stages = [];
const FILE = 'XML/stages.xml';
public function load(): void
{
$file = config('data_path') . self::FILE;
if(!@file_exists($file)) {
return;
}
$xml = new \DOMDocument();
if(!$xml->load($file)) {
error('Error: Cannot load stages.xml. More info in system/logs/error.log file.');
log_append('error.log', "[" . __CLASS__ . "] Fatal error: Cannot load stages.xml - $file. Error: " . print_r(error_get_last(), true));
return;
}
foreach($xml->getElementsByTagName('stage') as $stage)
{
/** @var DOMElement $stage */
$maxLevel = $stage->getAttribute('maxlevel');
$this->stages[] = [
'levels' => $stage->getAttribute('minlevel') . (isset($maxLevel[0]) ? '-' . $maxLevel : '+'),
'multiplier' => $stage->getAttribute('multiplier')
];
}
}
public function get(): array {
return $this->stages;
}
}