<?php if (!defined('FLUX_ROOT')) exit; 

$title = Flux::message('ServerStatusTitle');
$cache = FLUX_DATA_DIR.'/tmp/ServerStatus.cache';

if (file_exists($cache) && (time() - filemtime($cache)) < (Flux::config('ServerStatusCache') * 600)) {
    $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();

            $serverStatus[$groupName][$serverName] = array(
                'loginServerUp' => $loginServerUp,
                'charServerUp' => $athenaServer->charServer->isUp(),
                'mapServerUp' => $athenaServer->mapServer->isUp(),
                'playersOnline' => intval($res ? $res->players_online : 0)
            );
        }
    }
    
    $fp = fopen($cache, 'w');
    if (is_resource($fp)) {
        fwrite($fp, serialize($serverStatus));
        fclose($fp);
    }
}
$online = "<img src='".$this->themePath('img/online.png')."' alt=\"Online\"/>";
$offline = "<img src='".$this->themePath('img/offline.png')."' alt=\"Offline\"/>";
?> 
<?php foreach ($serverStatus as $privServerName => $gameServers): ?>
<?php foreach ($gameServers as $serverName => $gameServer): ?>

		
<div class="online_count">
<table width="100%" border="0">
  <tr>
    <td align="center" style="font-size:15px;">
	<p style="font-size:15px; margin-top:0px; font-weight:normal;" align="center">
	<?php echo $gameServer['playersOnline'] ?>
	</p>
</td>
  </tr>
</table>
</div>
<div class="online_peak">
<table width="100%" border="0">
  <tr>
    <td align="center" style="font-size:15px;">
				<?php include('highest_peak.php'); ?>

</td>
  </tr>
</table>
</div>

		<div>
			<div class="status">
	<div class="char"><?php if ( $charserver[0] == $online ) { echo $online; } else { echo $offline; } ?></div>
	<div class="map"><?php if ( $mapserver[0] == $online ) { echo $online; } else { echo $offline; } ?></div>
	<div class="login"><?php if ( $loginserver[0] == $online ) { echo $online; } else { echo $offline; } ?></div>
			</div>
		</div>

    <?php endforeach ?>
<?php endforeach ?>