<?php if (!defined('FLUX_ROOT')) exit; 
$cache = FLUX_DATA_DIR.'/tmp/ServerStatus.cache';
if (file_exists($cache) && (time() - filemtime($cache)) < (Flux::config('ServerStatusCache') * 60)) {
	$serverStatus = unserialize(file_get_contents($cache));
}
else {
	$serverStatus = array();
	foreach (Flux::$loginAthenaGroupRegistry as $groupName => $loginAthenaGroup) {
		if (!array_key_exists($groupName, $serverStatus)) {
			$serverStatus[$groupName] = array();
		}
		$loginServerUp = $loginAthenaGroup->loginServer->isUp();
		foreach ($loginAthenaGroup->athenaServers as $athenaServer) {
			$serverName = $athenaServer->serverName;

			$sql = "SELECT COUNT(char_id) AS players_online FROM {$athenaServer->charMapDatabase}.char WHERE online > 0";
			$sth = $loginAthenaGroup->connection->getStatement($sql);
			$sth->execute();
			$res = $sth->fetch();


			$sql = "SELECT COUNT(char_id) AS autotrade_merchant FROM {$athenaServer->charMapDatabase}.vendings";
			$sth = $loginAthenaGroup->connection->getStatement($sql);
			$sth->execute();
			$res2 = $sth->fetch();

			$serverStatus[$groupName][$serverName] = array(
				'loginServerUp' => $loginServerUp,
				'charServerUp' => $athenaServer->charServer->isUp(),
				'mapServerUp' => $athenaServer->mapServer->isUp(),
				'playersOnline' => intval($res ? $res->players_online : 0),
				'autotrade' => intval($res2 ? $res->autotrade_merchant : 0)
			);
			if(Flux::config('EnableHarmonyLogs') == 1){
				$sth = $server->connection->getStatement("SELECT users FROM {$server->charMapDatabase}.?");
				$sth->execute(array($onlinepeaktbl));
				$peak = $sth->fetchAll();
			}
		}
	}
	
	$fp = fopen($cache, 'w');
	if (is_resource($fp)) {
		fwrite($fp, serialize($serverStatus));
		fclose($fp);
	}
}


$online = "<span class=\"online\">ON</span>";
$offline = "<span class=\"offline\">OFF</span>";
	foreach ($serverStatus as $privServerName => $gameServers): 
		foreach ($gameServers as $serverName => $gameServer): 
			if ($gameServer['loginServerUp']) { $loginonline = true; } else { $loginonline = false; } 
			if ($gameServer['charServerUp']) { $charonline = true; } else { $charonline = false; }
			if ($gameServer['mapServerUp']) { $maponline = true; } else { $maponline = false; } 
			$playersOnline = $gameServer['playersOnline'];
			$autotrade = $gameServer['autotrade'];
		endforeach;
	endforeach;
?>

<div class="serverStatus">
	<div class="onlineCount"><?php echo $playersOnline-$autotrade; ?></div>
	<div class="trade"><?php echo $autotrade; ?></div>
	<div class="population"><?php echo $playersOnline; ?></div>
	<div class="time">
		<iframe src="http://free.timeanddate.com/clock/i58m2mgy/n145/fs11/fc0f0/tct/pct" frameborder="0" width="86" height="16" allowTransparency="true"></iframe>


	</div>
	<div class="login"><?php if( $loginonline ) { echo $online; } else { echo $offline; }; ?></div>
	<div class="char"><?php if( $charonline ) { echo $online; } else { echo $offline; }; ?></div>
	<div class="map"><?php if( $maponline ) { echo $online; } else { echo $offline; }; ?></div>
	<div class="woe"></div>
</div>

