<?php
@session_start();
include_once "inc/db_connect.php";
include_once "inc/fnc.php";
header('Content-Type: text/html;charset=utf8');
if(!isset($_SESSION['user'])){
	header("Location: index.php");
	exit();
}else{
	$data_user = mysql_fetch_array(mysql_query("SELECT * FROM users WHERE nick='".$_SESSION['user']."'")) or die(mysql_error());
	if($data_user['status'] != "Гонщик" && $data_user['status'] != "VIP"){
		$adminka_a = "<a href='adm_panel/index.php'>Админка</a>";
	}else{false;}
{
?>
<?include_once 'head.php'?>
<div class="title"><b>Турнир</b></div>
<?php
$turnir_settings_row = mysql_fetch_array(mysql_query("SELECT * FROM `turnir_settings`"));
if($turnir_settings_row['status']==0){
	$turnir_win = mysql_query("SELECT * FROM `turnir` ORDER BY `win` DESC LIMIT 1");
	$turnir_row_win = mysql_fetch_array($turnir_win);
	if($turnir_settings_row['off']==0){
		mysql_query("UPDATE `users` SET `money` = `money` + '".$turnir_settings_row['baks_win']."',`raiting` = `raiting` + '".$turnir_settings_row['raiting_win']."' WHERE `id` = '".$turnir_row_win['id_user']."'");
		mysql_query("UPDATE turnir_settings SET `off`='1'");
		echo "<meta http-equiv='Refresh' content='0; URL=turnir.php'>";
		exit();
	}else{
echo "<div class='title'>Победитель:</div>
	<table class='wdt menu'>
	<tr>
	<td>
		<img src='".$turnir_row_win['car']."' alt='".$turnir_row_win['carname']."' width='170' style='margin-top:8px;' />
	</td>
		<td>
			<b><a href='info.php?id=".$turnir_row_win['id_user']."'>".$turnir_row_win['nick']."</a></b><br/>
			<b>Побед:</b> <font color='#f00'>".$turnir_row_win['win']."</font><br/><br/>
<a href='cars.php?id=".$turnir_row_win['id_user']."' style='color:#333;text-decoration:underline;color:'black'; '>Гараж</a>
		</td>
		</center>
	</tr>
	</table>";
}
	$turnir_settings_row = mysql_fetch_array(mysql_query("SELECT * FROM `turnir_settings`"));
$turnir_win_top = mysql_query("SELECT * FROM `turnir` WHERE `id_user` != '$turnir_row_win[id_user]' ORDER BY `win` DESC LIMIT $turnir_settings_row[top]");
	$turnir_row_win_top = mysql_fetch_array($turnir_win_top);
	echo "<div class='title'>$turnir_settings_row[top] лучших</div>";
	do{
	if($turnir_settings_row['off_top'] == 0){
		$ress_mon2 = $data_user['money']+$turnir_settings_row['baks_top'];
		$ress_raiting2 = $data_user['raiting']+$turnir_settings_row['raiting_top'];
		mysql_query("UPDATE `users` SET `money` = `money` + '".$turnir_settings_row['baks_top']."',`raiting` = `raiting` + '".$turnir_settings_row['raiting_top']."'  WHERE `id` = '".$turnir_row_win_top['id_user']."'");
		mysql_query("UPDATE turnir_settings SET off_top='1'");
		echo "<meta http-equiv='Refresh' content='0; URL=turnir.php'>";
		exit();
	}else{
		echo "
		<table class='wdt menu'>
	<tr>
	<td>
		<img src='".$turnir_row_win_top['car']."' alt='".$turnir_row_win_top['carname']."' width='170' style='margin-top:8px;' />
	</td>
		<td>
			<b><a href='info.php?id=".$turnir_row_win_top['id_user']."'>".$turnir_row_win_top['nick']."</a></b><br/>
			<b>Побед:</b> <font color='#f00'>".$turnir_row_win_top['win']."</font><br/>
<a href='cars.php?id=".$turnir_row_win_top['id_user']."' style='color:'black';text-decoration:underline; '>Гараж</a>
		</td>
		</center>
	</tr>
	</table>";
	}
	}while($turnir_row_win_top = mysql_fetch_array($turnir_win_top));
{
	
if(isset($_GET['priz']))
{
if($data_user['status'] != "Гонщик" && $data_user['status'] != "VIP" && $data_user['status'] != "Помощник" && $data_user['status'] != "Админ" && $data_user['status'] != "Модератор"){
mysql_query("UPDATE `users` SET `money2` = `money2` + '$turnir_settings_row[baks_win]',`raiting` = `raiting` + '$turnir_settings_row[raiting_win]' WHERE `id` = '$turnir_row_win[id_user]'");
mysql_query("UPDATE `users` SET `money2` = `money2` + '$turnir_settings_row[baks_top]' WHERE wintur != '0' AND id != '$turnir_row_win[id_user]' ORDER BY `wintur` DESC LIMIT 10");
echo "<div class='inform'><center>Приз выдан!</center></div>";
}
}
if($data_user['status'] != "Гонщик" && $data_user['status'] != "VIP" && $data_user['status'] != "Помощник") echo "<div class='inform'>
<img src='/img/money.png' alt='Баксы' /> <a href='/turnir.php?priz'>Выдать приз</a>
</div>";
}
include 'foot.php';
}else{
if($turnir_settings_row['status']==1){
if(isset($_POST['start'])){
$query22 = mysql_query("SELECT COUNT(id_user) FROM turnir WHERE `id_user`='".$data_user['id']."'") or die(mysql_error());
if(mysql_result($query22, 0) > 0){
			echo "<meta http-equiv='Refresh' content='0; URL=turnir.php'>";
			exit();
}else{
	$insert_sql = mysql_query("INSERT INTO `turnir`(id_user,car,speed,avatar,nick,win,vilet) VALUES('$data_user[id]','$data_user[car]','$data_user[speed]','$data_user[avatar]','$data_user[nick]','0','0')") or die(mysql_error());
}
}
$data_turnir = mysql_fetch_assoc(mysql_query("SELECT * FROM turnir WHERE id_user = $data_user[id]"));
$query22 = mysql_query("SELECT COUNT(id_user) FROM turnir WHERE `id_user`='".$data_user['id']."'") or die(mysql_error());
if(mysql_result($query22, 0) > 0){
	if(isset($_POST['pop'])){
	if($data_turnir['vilet'] == 1){
		if($data_user['money2'] < 3){
			echo "Нехватает золотых!";
		}else{
			$res_money = $data_user['money2']-3;
			mysql_query("UPDATE turnir SET vilet='0' WHERE id_user='$data_user[id]'") or die(mysql_query());
			mysql_query("UPDATE users SET `money2`='$res_money' WHERE `nick`='".$_SESSION['user']."'") or die(mysql_query());
			echo "<meta http-equiv='Refresh' content='0; URL=turnir.php'>";
			exit();
		}
}
}
if($data_turnir['vilet']==1){
	echo "
<div class='menu'><font color='#f00'>Ты вылетел из турнира!</font><br/>
	<form action='' method='post'>
		<input type='submit' name='pop' value='Вернуться' />
		 за 3 <img src='img/money2.png' /></div></div>";
	include_once 'foot.php';
	exit();
}
$data_vs = mysql_fetch_array(mysql_query("SELECT * FROM turnir WHERE id_user != $data_user[id] ORDER BY RAND() LIMIT 1"));
if(isset($_POST['prin'])){
if($data_user['fuel'] < $turnir_settings_row['fuel'])
{
	echo "<meta http-equiv='Refresh' content='0; URL=turnir.php'>";
	exit();
}else{
$data_turnir = mysql_fetch_assoc(mysql_query("SELECT * FROM turnir WHERE id_user = $data_user[id]"));
$data_vs = mysql_fetch_array(mysql_query("SELECT * FROM turnir WHERE id_user != $data_user[id] ORDER BY RAND() LIMIT 1"));
if($data_user['speed'] >= $data_vs['speed']){
	$res_win = $data_turnir['win']+1;
	$res_to = $data_user['turnirnie']+$turnir_settings_row['turnirnie'];

	$res_fuel = $data_user['fuel']-$turnir_settings_row['fuel'];
		mysql_query("UPDATE turnir SET win='$res_win' WHERE id_user = $data_user[id]") or die(mysql_error());
		mysql_query("UPDATE users SET fuel='$res_fuel' WHERE nick = '".$_SESSION['user']."'") or die(mysql_error());
		mysql_query("UPDATE users SET turnirnie='$res_to' WHERE nick = '".$_SESSION['user']."'") or die(mysql_error());

		echo "<meta http-equiv='Refresh' content='0; URL=/turnir.php'>";
		exit();
	}else{
	if($data_car['speed'] < $data_vs['speed']){
			mysql_query("UPDATE turnir SET vilet='1',win='0' WHERE id_user = $data_user[id]") or die(mysql_error());
			echo "<meta http-equiv='Refresh' content='0; URL=/turnir.php'>";
			exit();
		}
	}
}
}
		$Lider = mysql_fetch_array(mysql_query("SELECT * FROM turnir ORDER BY `win` DESC"));
		echo "<div class='menu'>Лидирует: <font color=#f00>$Lider[nick]</font><br/>
		Побед: <b><font color=#f00>$Lider[win]</font><b/></div>";
echo <<<tur
<div class='menu'>Побед в турнире: <font color='#f00'>$data_turnir[win]</font></div>
<div class='title'>Следующий соперник:</div>
<table class="wdt" style="background-image: url(style/default/icon/racefon.png); color:#ff0000; background-position: bottom center; height:157px">
<tbody>
<tr> 
<td> 
<br><br>
<div style="padding-top:40px;"><img src="$data_turnir[car]" alt="1" width="150px"></div>
<span class="corners"><span></span></span>
<img src="$data_vs[car]" alt="2" width="150px"> 
</td>
</tr> 
</tbody>
</table>
<div class='menu'>
	<font color='#f00'>$data_vs[nick]</font><br/>
	<form action='' method='post' >
		<center><input type='submit' class='btn' name='smen' value='Сменить' />
		<input type='submit' name='prin' class='btn' value='Принять' /></center>
	</form>
	</div>
tur;
}else{
?>
<div class="menu">
	<b>
		<center>
				Заезд на автомобиле: <?=$turnir_settings_row['car']?>
		</center>
	</b>
</div>
<div class="title">
	Описание:
</div>
<div class="menu">
	Победитель турнира получит:
		<b>
			<font color=#5ca13a>
				<?=$turnir_settings_row['baks_win']?> <img src="img/money.png" alt="." />
			</font>
		</b>
 и 
		<b>
			<?=$turnir_settings_row['raiting_win']?> <img src="img/raiting.png" alt="." />
		</b>
		<br/>
		<b>
			<?=$turnir_settings_row['top']?>
		</b>
лучших получат: 
		<b>
			<font color=#5ca13a>
				<?=$turnir_settings_row['baks_top']?> <img src="img/money.png" alt="." />
			</font>
		</b>
 и 		<b>
			<?=$turnir_settings_row['raiting_top']?> <img src="img/raiting.png" alt="." />
		</b>
		<br/>
		<br/>
	<b>
		<font color="#f00">
			<center>
				УДАЧИ!
			</center>
		</font>
	<b/>
	<?
	if($data_user['carname'] != $turnir_settings_row['car']){
		echo "<br/><center><input type='submit' class='btn_no' value='Неподходящее авто' style='width:180px;'/></center></div>";
	}else{
			echo "<form action='' method='post'>
				<br/><center><input type='submit' name='start' class='btn' value='Начать турнир' /></center>
			</form></div>";
		}
	?>
<div class="title">
	Порядок проведения турнира:
</div>
<div class='menu'>
<font size='1'>
На каждый заезд тратиться <?=$turnir_settings_row[fuel]?> <img src='img/benz.png' alt='.' /> бензина.<br/>
Побеждает тот, кто к концу чемпионата одержит больше всего <img src='img/flag_tur.png' width='9' height='12'/> побед!<br/>
В случае поражения ты выбываешь из турнира.
Побежденный тобой гонщик не выбывает из турнира пока сам не проиграет.</font>
</div>
<?
}
}
?>
<?
}
}
include_once 'foot.php'?>
</body>
</html>
<?
}
?>