<?php
/*
 * Top servers display script
 * Made by starky
*/

/* Script security */
if(!defined("MONENGINE")) {
	header("Location: index.php");
	exit();
}
/* Other code */

if($settings['top_rows'] > 0) {
	$top_servers = dbquery("SELECT * FROM ".DB_SERVERS." WHERE server_new = '0' AND server_top != '0'");
	while($top_servers_array = dbarray($top_servers)) {
		$i = $top_servers_array['server_top'];
		foreach($top_servers_array as $k => $v) {
			$tops_array[$i][$k] = $v;
		}
        }
	// Максимальное кол-во строк с топ-серверами
	define("LINES_NUM", $settings['top_rows']);
	// Шаблон занятого места
	$template_got = '
		<div class="unit">
			<div class="map_section">
				<img width="100" height="75" src="/'.MAPS.'{game}/{map}.jpg">
			</div>
			<div class="info_section">
				<span class="title">Игроки:</span><br>
				{players}/{players_max}<br>
				<span class="title">Карта:</span><br>
				{map}<br>
			</div>
			<div class="clearfix"></div>
			<div class="naruto">
				<a href="/server/{id}">{name}</a><br>
				{address}
			</div>
		</div>
	';
	// Шаблон пустого места
	$template_free = '
		<div class="unit">
			<div class="map_section">
				<img width="100" height="75" src="/images/top_free.png">
			</div>
			<div class="info_section">
				<span class="title">Игроки:</span><br>
				Unknown<br>
				<span class="title">Карта:</span><br>
				Unknown<br>
			</div>
			<div class="clearfix"></div>
			<div class="naruto">
				<a href="skype:vitalik199211?call"> Заказать ТОП статус </a>
			</div>
		</div>
	';

	function use_top_tpl($tpl) {

	global $server_game;
	global $server_map;
	global $server_players_num;
	global $server_players_num_max;
	global $server_id;
	global $server_name;
	global $server_address;

		$vars = Array(
		'{game}'		=> $server_game,
		'{map}'			=> $server_map,
		'{players}'		=> $server_players_num,
		'{players_max}'	=> $server_players_num_max,
		'{id}'			=> $server_id,
		'{name}'		=> $server_name,
		'{address}'		=> $server_address
		);
		$tpl = strtr($tpl, $vars);
		return $tpl;
	}

	// Строим топовые места
	$line = 0;
	for($i = 1; $i <= 5 * LINES_NUM; $i++) {
		if(@is_array($tops_array[$i])) {
			$server_id = $tops_array[$i]['server_id'];
			$server_name = $tops_array[$i]['server_name'];
			
			/*
			 * Обрезание имени сервера
			 *
			 
			if(mb_strlen($server_name, 'UTF-8') > 27) {
				$server_name = mb_substr($server_name, 0, 27, 'UTF-8')."...";
			}
			*/
			
			$server_name = htmlspecialchars($server_name);
			$server_address = $tops_array[$i]['server_ip'];
			$server_players_num = $tops_array[$i]['server_players'];
			$server_players_num_max = $tops_array[$i]['server_maxplayers'];
			$server_map = $tops_array[$i]['server_map'];
			$server_game = $tops_array[$i]['server_game'];
			if($tops_array[$i]['server_off'] == 1) $server_address = "Сервер заблокирован";
			if($tops_array[$i]['server_status'] == 0) {
				$server_address .= " <span style='color:#AAAAAA;cursor:help;' title='Сервер выключен'>[OFF]</span>";
				$server_players_num = "N";
				$server_players_num_max = "A";
				$server_map = "unknown";
			}
			$place_free = false;
		} else {
			$place_free = true;
		}
		
		if(($i - 1) % 5 == 0 or $i == 1) {
			$line++;
			$class = ($line % 2 == 0) ? '2' : '1';
			echo "\n<div class='top_zebra_".$class."'>";
		}
		echo "\n<!-- TOP SERVER № ".$i." -->\n";
		if($place_free) {
			echo $template_free;
		} else {
			echo use_top_tpl($template_got);
		}
		echo "\n<!-- /TOP SERVER -->\n";
		if($i % 5 == 0) {
			echo "\n<div class='clearfix'></div>\n</div>\n";
		}
	}
}
?>