<?php
/** Event Rankings — Nexora multi-realm cache refresh. */
$file_name = basename(__FILE__);

$realms = array(null);
if(class_exists('NexoraRealm') && NexoraRealm::enabled()) {
    $realms = defined('NEXORA_CRON_REALM_RUNNER')
        ? array(NexoraRealm::active())
        : NexoraRealm::ids();
}

$processed = false;
foreach($realms as $realm) {
    try {
        if($realm !== null) NexoraRealm::selectPublic($realm, false);
        $rankings = new \Plugin\EventRankings\EventRankings($realm);
        foreach(array('bloodcastle', 'devilsquare', 'chaoscastle') as $type) {
            $rankings->setRankingType($type);
            $rankings->updateCache();
        }
        $processed = true;
        updateCronLastRun($file_name);
    } catch(Throwable $ex) {
        @error_log('[Event Rankings][' . ($realm === null ? 'default' : $realm) . '] ' . $ex->getMessage());
    }
}

if(!$processed && !(class_exists('NexoraRealm') && NexoraRealm::enabled())) {
    updateCronLastRun($file_name);
}
