<?php
/**
 * Actualiza la lista de personajes conectados para el reino activo.
 */

$file_name = basename(__FILE__);

$me = Connection::Database('Me_MuOnline');
$charactersDB = str_replace(']', ']]', NexoraRealm::databaseName('MuOnline'));
$accountsDB = str_replace(']', ']]', NexoraRealm::databaseName('Me_MuOnline'));

$query = "SELECT t2."._CLMN_GAMEIDC_."
	FROM [".$accountsDB."].[dbo]."._TBL_MS_." t1
	INNER JOIN [".$charactersDB."].[dbo]."._TBL_AC_." t2
		ON t1."._CLMN_MS_MEMBID_." = t2."._CLMN_AC_ID_."
	WHERE t1."._CLMN_CONNSTAT_." = 1";

$onlineCharactersList = $me->query_fetch($query);
$result = array();
if(is_array($onlineCharactersList)) {
	foreach($onlineCharactersList as $row) {
		$value = $row[_CLMN_GAMEIDC_];
		if(in_array($value, $result, true)) continue;
		$result[] = $value;
	}
}

updateCacheFile('online_characters.cache', encodeCache($result));
updateCronLastRun($file_name);
