• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Help PHP Script not reloading cache or something is wrong

ForgottenNot

Member
Joined
Feb 10, 2023
Messages
237
Reaction score
23
Hello as title says

im editing znoteacc spells parser script in order to get combat effect values from lua files, the strange part is that it obtain values from some spells but not from others, that might be very similar .
when the table is generated i noticed that for example haste has it's effect found in the table. but not strong haste , the script are almost the same so have no clue what is happening.
can somebody lend me a hand with this? it seems to be loading everything properly but something is occurring while generating the table or so....
another thing that i thought was that something with cache may not be working as it should

here is the script:
Lua:
<?php
require_once 'engine/init.php';
include 'layout/overall/header.php';

// Función para obtener los tipos de combate de un archivo script
function getCombatTypes($fullPath) {
    if (file_exists($fullPath)) {
        $content = file_get_contents($fullPath);

        if ($content === false) {
            echo "Error al leer el archivo: $fullPath<br>";
            return [
                'combatParams' => ['Unknown'],
                'effectParams' => ['Unknown'],
                'distanceEffectParams' => ['Unknown']
            ];
        }

        $combatParams = [];
        $effectParams = [];
        $distanceEffectParams = [];

        // Mejorar las expresiones regulares
        preg_match_all('/combat:setParameter\((COMBAT_PARAM_TYPE|COMBAT_PARAM_EFFECT|COMBAT_PARAM_DISTANCEEFFECT),\s*([A-Z0-9_]+)\)/', $content, $matches, PREG_SET_ORDER);

        foreach ($matches as $match) {
            $paramType = $match[1];
            $paramValue = $match[2];

            switch ($paramType) {
                case 'COMBAT_PARAM_TYPE':
                    $combatParams[] = $paramValue;
                    break;
                case 'COMBAT_PARAM_EFFECT':
                    $effectParams[] = $paramValue;
                    break;
                case 'COMBAT_PARAM_DISTANCEEFFECT':
                    $distanceEffectParams[] = $paramValue;
                    break;
            }
        }

        if (empty($combatParams)) {
            $combatParams = ['Unknown'];
        }
        if (empty($effectParams)) {
            $effectParams = ['Unknown'];
        }
        if (empty($distanceEffectParams)) {
            $distanceEffectParams = ['Unknown'];
        }

        return [
            'combatParams' => array_unique($combatParams),
            'effectParams' => array_unique($effectParams),
            'distanceEffectParams' => array_unique($distanceEffectParams)
        ];
    }

    return [
        'combatParams' => ['Unknown'],
        'effectParams' => ['Unknown'],
        'distanceEffectParams' => ['Unknown']
    ];
}

function getAllScripts($dir) {
    $scripts = [];
    if (!is_dir($dir)) {
        throw new Exception("El directorio no es válido: " . $dir);
    }

    $rii = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir));
    foreach ($rii as $file) {
        if ($file->isDir()) {
            continue;
        }
        if (pathinfo($file->getPathname(), PATHINFO_EXTENSION) == "lua") {
            $scripts[] = $file->getPathname();
            echo "Archivo encontrado: " . $file->getPathname() . "<br>";
        }
    }
    return $scripts;
}

// Cargar todos los tipos de combate
$combatTypes = [];
$scriptDir = 'C:/Users/felip/Documents/GitHub/pro-ot/data/spells/scripts/';
try {
    $scripts = getAllScripts($scriptDir);
    foreach ($scripts as $script) {
        $scriptName = basename($script);
        $combatTypes[$scriptName] = getCombatTypes($script);
    }
} catch (Exception $e) {
    echo "Error: " . $e->getMessage();
}

// Loading spell list
$spellsCache = new Cache('engine/cache/spells');
if (user_logged_in() && is_admin($user_data)) {
    if (isset($_GET['update'])) {
        echo "<p><strong>Logged in as admin, loading engine/XML/spells.xml file and updating cache.</strong></p>";
        // SPELLS XML TO PHP ARRAY
        $spellsXML = simplexml_load_file("engine/XML/spells.xml");
        if ($spellsXML !== false) {
            $types = array();
            $type_attr = array();
            $groups = array();

            // This empty array will eventually contain all spells grouped by type and indexed by spell name
            $spells = array();

            // Loop through each XML spell object
            foreach ($spellsXML as $type => $spell) {
                // Get spell types
                if (!in_array($type, $types)) {
                    $types[] = $type;
                    $type_attr[$type] = array();
                }
                // Get spell attributes
                $attributes = array();
                // Extract attribute values from the XML object and store it in a more manage friendly way $attributes
                foreach ($spell->attributes() as $aName => $aValue)
                    $attributes["$aName"] = "$aValue";

                // Alias attributes
                if (isset($attributes['level'])) $attributes['lvl'] = $attributes['level'];
                if (isset($attributes['magiclevel'])) $attributes['maglv'] = $attributes['magiclevel'];

                // Populate type attributes
                foreach (array_keys($attributes) as $attr) {
                    if (!in_array($attr, $type_attr[$type]))
                        $type_attr[$type][] = $attr;
                }
                // Get spell groups
                if (isset($attributes['group'])) {
                    if (!in_array($attributes['group'], $groups))
                        $groups[] = $attributes['group'];
                }
                // Get spell vocations
                $vocations = array();
                foreach ($spell->vocation as $vocation) {
                    foreach ($vocation->attributes() as $attributeName => $attributeValue) {
                        if ("$attributeName" == "name") {
                            $vocId = vocation_name_to_id("$attributeValue");
                            $vocations[] = ($vocId !== false) ? $vocId : "$attributeValue";
                        } elseif ("$attributeName" == "id") {
                            $vocations[] = (int)"$attributeValue";
                        }
                    }
                }
                // Exclude monster spells and house spells
                $words = (isset($attributes['words'])) ? $attributes['words'] : false;
                $name = (isset($attributes['name'])) ? $attributes['name'] : false;
                if (substr($words, 0, 3) !== '###' && substr($name, 0, 5) !== 'House') {
                    $spells[$type][$name] = array('vocations' => $vocations);
                    foreach ($type_attr[$type] as $att)
                        $spells[$type][$name][$att] = (isset($attributes[$att])) ? $attributes[$att] : false;
                    
              // Assign combat type
if (isset($attributes['script'])) {
    $scriptName = basename($attributes['script']);
    $combatTypeData = isset($combatTypes[$scriptName]) ? $combatTypes[$scriptName] : [
        'combatParams' => ['Unknown'],
        'effectParams' => ['Unknown'],
        'distanceEffectParams' => ['Unknown'],
        //'combatTypeParams' => ['Unknown']
    ];
    $spells[$type][$name]['combatType'] = [
        'combatParams' => $combatTypeData['combatParams'],
        'effectParams' => $combatTypeData['effectParams']
    ];
} else {
    $spells[$type][$name]['combatType'] = [
        'combatParams' => ['Unknown'],
        'effectParams' => ['Unknown']
    ];
}
                }
            }

            // Sort the spell list properly
            foreach (array_keys($spells) as $type) {
                usort($spells[$type], function ($a, $b) {
                    if (isset($a['lvl']))
                        return $a['lvl'] - $b['lvl'];
                    if (isset($a['maglv']))
                        return $a['maglv'] - $b['maglv'];
                    return -1;
                });
            }
            $spellsCache->setContent($spells);
            $spellsCache->save();
        } else {
            echo "<p><strong>Failed to load engine/XML/spells.xml file.</strong></p>";
        }
    } else {
        $spells = $spellsCache->load();
        ?>
        <form action="">
            <input type="submit" name="update" value="Generate new cache">
        </form>
        <?php
    }
} else {
    $spells = $spellsCache->load();
}

if ($spells) {
    // Preparing data
    $configVoc = $config['vocations'];
    $types = array_keys($spells);
    $itemServer = 'https://forgottenot.online/'.$config['shop']['imageServer'].'/';

    // Filter spells by vocation
    $getVoc = (isset($_GET['vocation'])) ? getValue($_GET['vocation']) : 'all';
    if ($getVoc !== 'all') {
        $getVoc = (int)$getVoc;
        foreach ($types as $type) {
            foreach ($spells[$type] as $name => $spell) {
                if (!empty($spell['vocations'])) {
                    if (!in_array($getVoc, $spell['vocations'])) {
                        unset($spells[$type][$name]);
                    }
                }
            }
        }
    }
    // Render HTML
    ?>

    <h1 id="spells">Spells<?php if ($getVoc !== 'all') echo ' ('.$configVoc[$getVoc]['name'].')';?></h1>

    <form action="#spells" class="filter_spells">
        <label for="vocation">Filter vocation:</label>
        <select id="vocation" name="vocation">
            <option value="all">All</option>
            <?php foreach ($config['vocations'] as $id => $vocation): ?>
                <option value="<?php echo $id; ?>" <?php if ($getVoc === $id) echo "selected"; ?>><?php echo $vocation['name']; ?></option>
            <?php endforeach; ?>
        </select>
        <input type="submit" value="Search">
    </form>

    <h2>Spell types:</h2>
    <ul>
        <?php foreach ($types as $type): ?>
        <li><a href="#spell_<?php echo $type; ?>"><?php echo ucfirst($type); ?></a></li>
        <?php endforeach; ?>
    </ul>

    <h2 id="spell_instant">Instant Spells</h2>
    <a href="#spells">Jump to top</a>
    <table class="table tbl-hover">
        <tbody>
            <tr class="yellow">
                <td>Name</td>
                <td>Combat Type</td>
                <td>Words</td>
                <td>Level</td>
                <td>Mana</td>
                <td>Vocations</td>
            </tr>
            <?php foreach ($spells['instant'] as $spell): ?>
            <tr>
                <td><?php echo $spell['name']; ?></td>
               <td>
    <?php
    if (isset($spell['combatType']) && is_array($spell['combatType'])) {
        echo "Combat: " . (empty($spell['combatType']['combatParams']) ? 'Unknown' : implode(', ', $spell['combatType']['combatParams'])) . "<br>";
        echo "Effect: " . (empty($spell['combatType']['effectParams']) ? 'Unknown' : implode(', ', $spell['combatType']['effectParams'])) . "<br>";
        echo "Distance Effect: " . (empty($spell['combatType']['distanceEffectParams']) ? 'Unknown' : implode(', ', $spell['combatType']['distanceEffectParams'])) . "<br>";
        //echo "Combat Type: " . (empty($spell['combatType']['combatTypeParams']) ? 'Unknown' : implode(', ', $spell['combatType']['combatTypeParams']));
    } else {
        echo 'Unknown';
    }
    ?>
</td>
                <td><?php echo $spell['words']; ?></td>
                <td><?php echo $spell['lvl']; ?></td>
                <td><?php echo $spell['mana']; ?></td>
                <td><?php
                if (!empty($spell['vocations'])) {
                    if ($getVoc !== 'all') {
                        echo $configVoc[$getVoc]['name'];
                    } else {
                        $names = array();
                        foreach ($spell['vocations'] as $id) {
                            if (isset($configVoc[$id]))
                                $names[] = $configVoc[$id]['name'];
                        }
                        echo implode(',<br>', $names);
                    }
                }
                ?></td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>

    <h2 id="spell_rune">Magical Runes</h2>
    <a href="#spells">Jump to top</a>
    <table class="table tbl-hover">
        <tbody>
            <tr class="yellow">
                <td>Name</td>
                <td>Combat Type</td>
                <td>Level</td>
                <td>Magic Level</td>
                <td>Image</td>
                <td>Vocations</td>
            </tr>
            <?php foreach ($spells['rune'] as $spell): ?>
            <tr>
                <td><?php echo $spell['name']; ?></td>
               <td>
    <?php
    if (isset($spell['combatType']) && is_array($spell['combatType'])) {
        echo "Combat: " . (empty($spell['combatType']['combatParams']) ? 'Unknown' : implode(', ', $spell['combatType']['combatParams'])) . "<br>";
        echo "Effect: " . (empty($spell['combatType']['effectParams']) ? 'Unknown' : implode(', ', $spell['combatType']['effectParams']));
    } else {
        echo 'Unknown';
    }
    ?>
</td>
                <td><?php echo $spell['lvl']; ?></td>
                <td><?php echo $spell['maglv']; ?></td>
                <td><img src="<?php echo $itemServer.$spell['id'].'.gif'; ?>" alt="Rune image"></td>
                <td><?php
                if (!empty($spell['vocations'])) {
                    if ($getVoc !== 'all') {
                        echo $configVoc[$getVoc]['name'];
                    } else {
                        $names = array();
                        foreach ($spell['vocations'] as $id) {
                            if (isset($configVoc[$id]))
                                $names[] = $configVoc[$id]['name'];
                        }
                        echo implode(',<br>', $names);
                    }
                }
                ?></td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>

    <?php if (isset($spells['conjure'])): ?>
    <h2 id="spell_conjure">Conjure Spells</h2>
    <a href="#spells">Jump to top</a>
    <table class="table tbl-hover">
        <tbody>
            <tr class="yellow">
                <td>Name</td>
                <td>Combat Type</td>
                <td>Words</td>
                <td>Level</td>
                <td>Mana</td>
                <td>Soul</td>
                <td>Charges</td>
                <td>Image</td>
                <td>Vocations</td>
            </tr>
            <?php foreach ($spells['conjure'] as $spell): ?>
            <tr>
                <td><?php echo $spell['name']; ?></td>
                <td>
    <?php
    if (isset($spell['combatType']) && is_array($spell['combatType'])) {
        echo "Combat: " . (empty($spell['combatType']['combatParams']) ? 'Unknown' : implode(', ', $spell['combatType']['combatParams'])) . "<br>";
        echo "Effect: " . (empty($spell['combatType']['effectParams']) ? 'Unknown' : implode(', ', $spell['combatType']['effectParams']));
    } else {
        echo 'Unknown';
    }
    ?>
</td>
                <td><?php echo $spell['words']; ?></td>
                <td><?php echo $spell['lvl']; ?></td>
                <td><?php echo $spell['mana']; ?></td>
                <td><?php echo $spell['soul']; ?></td>
                <td><?php echo $spell['conjureCount']; ?></td>
                <td><img src="<?php echo $itemServer.$spell['conjureId'].'.gif'; ?>" alt="Rune image"></td>
                <td><?php
                if (!empty($spell['vocations'])) {
                    if ($getVoc !== 'all') {
                        echo $configVoc[$getVoc]['name'];
                    } else {
                        $names = array();
                        foreach ($spell['vocations'] as $id) {
                            if (isset($configVoc[$id]))
                                $names[] = $configVoc[$id]['name'];
                        }
                        echo implode(',<br>', $names);
                    }
                }
                ?></td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
    <a href="#spells">Jump to top</a>
    <?php endif; ?>
    <?php
} else {
    ?>
    <h1>Spells</h1>
    <p>Spells have currently not been loaded into the website by the server admin.</p>
    <?php
}

/* Debug tests
foreach ($spells as $type => $spells) {
    data_dump($spells, false, "Type: $type");
}

// All spell attributes?
'group', 'words', 'lvl', 'level', 'maglv', 'magiclevel', 'charges', 'allowfaruse', 'blocktype', 'mana', 'soul', 'prem', 'aggressive', 'range', 'selftarget', 'needtarget', 'blockwalls', 'needweapon', 'exhaustion', 'groupcooldown', 'needlearn', 'casterTargetOrDirection', 'direction', 'params', 'playernameparam', 'conjureId', 'reagentId', 'conjureCount', 'vocations'
*/
include 'layout/overall/footer.php'; ?>
 
Back
Top