<?php
/**
 * Nexora character country cache updater.
 * Account country metadata is shared Platform data; characters remain realm data.
 */

$file_name = basename(__FILE__);

$platform = Connection::Database('Platform');
$realm = Connection::Database('MuOnline');

$countryRows = $platform->query_fetch("SELECT account, country FROM ".WEBENGINE_ACCOUNT_COUNTRY);
$result = array();
if(is_array($countryRows)) {
    foreach($countryRows as $countryRow) {
        $characters = $realm->query_fetch(
            "SELECT "._CLMN_CHR_NAME_." FROM "._TBL_CHR_." WHERE "._CLMN_CHR_ACCID_." = ?",
            array($countryRow['account'])
        );
        if(!is_array($characters)) continue;
        foreach($characters as $characterRow) {
            $result[$characterRow[_CLMN_CHR_NAME_]] = $countryRow['country'];
        }
    }
}

$cacheData = encodeCache($result);
updateCacheFile('character_country.cache', $cacheData);
updateCronLastRun($file_name);
