<?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 population FROM {$athenaServer->charMapDatabase}.char WHERE online > 0";
			$sth = $loginAthenaGroup->connection->getStatement($sql);
			$sth->execute();
			$res = $sth->fetch();
			$population = intval($res ? $res->population : 0);

			$sql = "SELECT COUNT(account_id) AS autotrade_merchants FROM {$athenaServer->charMapDatabase}.autotrade_merchants";
			$sth = $loginAthenaGroup->connection->getStatement($sql);
			$sth->execute();
			$res = $sth->fetch();
			$autotrade_merchants = intval($res ? $res->autotrade_merchants : 0);

			$sql = "SELECT num_users AS peak FROM {$athenaServer->charMapDatabase}.cp_highest_peak";
			$sth = $loginAthenaGroup->connection->getStatement($sql);
			$sth->execute();
			$res = $sth->fetch();
			$peak = intval($res? $res->peak : 0);

			$sql = "SELECT padding AS pad FROM {$athenaServer->charMapDatabase}.pad_main";
			$sth = $loginAthenaGroup->connection->getStatement($sql);
			$sth->execute();
			$res = $sth->fetch();
			$pad = intval($res? $res->pad : 0);



			$serverStatus[$groupName][$serverName] = array(
				'loginServerUp' => $loginServerUp,
				'charServerUp' => $athenaServer->charServer->isUp(),
				'mapServerUp' => $athenaServer->mapServer->isUp(),

				'playersOnline' => $population + $pad - $autotrade_merchants,
				'autotradeMerchants' => $autotrade_merchants,
				'population' => $population + $pad,
				'onlinepeak' => $peak
			);


		}
	}
	
	$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['autotradeMerchants'];
			$population = $gameServer['population'];
			$peak = $gameServer['onlinepeak'];

		endforeach;
	endforeach;
?>



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

</div>

