<?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'] != "0" && $data_user['status'] != "1"){
		$adminka_a = "<a href='adm_panel/index.php'>Админка</a>";
	}else{false;}
{
?>
<?include_once 'inc/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){
		$ress_mon = $data_user['money']+$turnir_settings_row['baks_win'];
		$ress_gol = $data_user['gold']+$turnir_settings_row['gold_win'];
		$ress_raiting = $data_user['raiting']+$turnir_settings_row['raiting_win'];
		mysql_query("UPDATE users SET money='$ress_mon',raiting='$ress_raiting' WHERE `id` = $turnir_row_win[id_user]") or die(mysql_query());
		mysql_query("UPDATE users SET gold='$ress_gol',raiting='$ress_raiting' WHERE `id` = $turnir_row_win[id_user]") or die(mysql_query());
		mysql_query("UPDATE turnir_settings SET `off`='1'");
		echo "<meta http-equiv='Refresh' content='0; URL=turnir.php'>";
		exit();
	}else{
	echo "<div class='tt3' style='background:green;b'>Победитель:</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>
			<img src='".$turnir_row_win['avatar']."' alt='.' width='50' height='50' /><br/>
			<b><a href='info.php?id=".$turnir_row_win['id_user']."'>".$turnir_row_win['nick']."</a></b><br/>
			<b>Побед:</b> <img src='/icons/flag.png' alt='raiting' /> <font color='#f00'>".$turnir_row_win['win']."</font><br/>
			<img src='img/gar.png' alt='.' style='margin-top:3px;'/> <a href='cars.php?id=".$turnir_row_win['id_user']."' style='color:#333;text-decoration:underline; '>Гараж</a>
		</td>
		</center>
	</tr>
	</table>";
}
	$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='tt3' style='background:green;b'>Топ:$turnir_settings_row[top]</div>";
	do{
	if($turnir_settings_row['off_top'] == 0){
		$ress_mon2 = $data_user['money']+$turnir_settings_row['baks_top'];
		$ress_gold2 = $data_user['gold']+$turnir_settings_row['gold_top'];
		$ress_raiting2 = $data_user['raiting']+$turnir_settings_row['raiting_top'];
		mysql_query("UPDATE users SET money='$ress_mon2',raiting='$ress_raiting2' WHERE `id` = $turnir_row_win_top[id_user]") or die(mysql_query());
		mysql_query("UPDATE users SET gold='$ress_gold2',raiting='$ress_raiting2' WHERE `id` = $turnir_row_win_top[id_user]") or die(mysql_query());
		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>
			<img src='".$turnir_row_win_top['avatar']."' alt='.' width='50' height='50' /><br/>
			<b><a href='info.php?id=".$turnir_row_win_top['id_user']."'>".$turnir_row_win_top['nick']."</a></b><br/>
			<b>Побед:</b> <img src='/icons/flag.png' alt='raiting' /> <font color='#f00'>".$turnir_row_win_top['win']."</font><br/>
			<img src='img/gar.png' alt='.' style='margin-top:3px;'/> <a href='cars.php?id=".$turnir_row_win_top['id_user']."' style='color:#333;text-decoration:underline; '>Гараж</a>
		</td>
		</center>
	</tr>
	</table>";
	}
	}while($turnir_row_win_top = mysql_fetch_array($turnir_win_top));
	include 'inc/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_car[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['money'] < 3000){
			echo "Нехватает монет!";
		}else{
			$res_money = $data_user['money']-3000;
			mysql_query("UPDATE turnir SET vilet='0' WHERE id_user='$data_user[id]'") or die(mysql_query());
			mysql_query("UPDATE users SET `money`='$res_money' WHERE `nick`='".$_SESSION['user']."'") or die(mysql_query());
			echo "<meta http-equiv='Refresh' content='0; URL=turnir.php'>";
			exit();
		}
}
}
if(isset($_GET['suds'])){
	$resm_coc = abs($data_user['gold'])-10;
	if(abs($data_user['gold'])< 10){
		$err = "Извини но у тебя не хватает денег";
	}else{
	$coctail = mysql_query("UPDATE users SET gold='".$resm_coc."' WHERE nick='".$_SESSION['user']."'") or die(mysql_error());
	if($coctail == true){
		$coctail = "Держи свой напиток. С тебя <font color='#23b900'>10</font> баксов";
	}
	}
}
if($data_turnir['vilet']==1){
	echo "
	<div class='online'><img src='http://stracingstat1.stracing.mobi/race/img/face-her.png' alt='.' style='float:left;margin:4px;margin-right:8px;width:50px;height:50px;'/><div style='border:1px solid #ccc;border-radius:4px;padding:20px;'><font color='#f00'><b>Ты вылетел из турнира!</b></font>
	<form action='' method='post'></br>
		<input type='submit' class='btn' name='pop' value='Вернуться' />
		 за <font color='black'><b>3000</b></font><img src='/icons/head-icons/money.png' /></div></div>";
	include_once 'inc/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_car['speed'] >= $data_vs['speed']){
	$res_win = $data_turnir['win']+1;
	$res_fuel = $data_user['fuel']-$turnir_settings_row['fuel'];
	$res_tur_money = $data_user['tur_money']+$turnir_settings_row['tur_money'];
	$res_tur_m = $data_turnir['tur_m']+$turnir_settings_row['tur_money'];
		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 tur_money='$res_tur_money' WHERE nick = '".$_SESSION['user']."'") or die(mysql_error());
		mysql_query("UPDATE turnir SET tur_m='$res_tur_m' WHERE id_user = $data_user[id]") 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();
		}
	}
}
}

echo <<<tur
</div>
<div class='menu'><div class='cit'>Побед в турнире: <font color='#f00'>$data_turnir[win]<img src='/icons/flag.png' alt='.' width='10' height='10'/></br>
<img src='http://stracingstat2.stracing.mobi/race/img/hp-ico.png' alt='.' width='10' height='10'/>$data_turnir[tur_m]</div></div>
<div class='tt3' style='background:green;b'>Следущий соперник: <font color='#f00'>$data_vs[nick]</font><br/></div>
<table class="wdt" style="background-image: url(img/race.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'>	
	<form action='' method='post' >
		<center><input type='submit' class='btn' name='smen' value='Сменить' />
		<input type='submit' name='prin' class='btn' value='Принять' /></center>
	</form>
</div><div class='tt3' style='background:green;b'></div>
	</div><div class='menu'>
	</br>
  <td align="left" class="orange">
                                <a class="footer-font"               
                                <a href="?refill" class="footer-font">
                   <font color="red">Заправить бак?</font>?</a> 
                   ㅤㅤㅤㅤㅤ
                                <a href="/lturnir.php" class="footer-font">
                   <font color="red">Подкупить судью за<img src="/icons/head-icons/gold.png" width="16" height="16" alt="" align="center"><font color="GREEN">0</font></font>?</a>              
</div>
tur;
}else{
?>
<div class="menu">
	<b><div class="" style="margin-bottom:-7px;"><center>Заезд на автомобиле: <span style="color:#cc0000;"><?=$turnir_settings_row['car']?></span></center></div></b><br/>
	</div><div class='tt3' style='background:green;b'>Описание:</div><div class='menu' style='background:;b'>
	<div class='cit privet dhint' style="border:1px solid #444;padding:8px;font-size:11px;">
	Победитель турнира получит  <img src="icons/head-icons/gold.png" alt="." /><b><font color=LIMEGREEN><?=$turnir_settings_row['gold_win']?></font></b> <img src="/icons/head-icons/money.png" alt="." /><font color=GRAY><?=$turnir_settings_row['baks_win']?></font></b> и <b><?=$turnir_settings_row['raiting_win']?></b> <img src="img/raiting.png" alt="." /><br/>
	<b>Топ <?=$turnir_settings_row['top']?></b> лучщих получат <img src="/icons/head-icons/gold.png" alt="." /><b><font color=LIMEGREEN><?=$turnir_settings_row['gold_top']?></b></font>  <img src="/icons/head-icons/money.png" alt="." /><b><font color=gray><?=$turnir_settings_row['baks_top']?></b></font> и <b><?=$turnir_settings_row['raiting_top']?></b> <img src="img/raiting.png" alt="." /><br/>
	<b><font color="#f00">УДАЧИ!</font><b/>
	<?
	if($data_user['carname'] != $turnir_settings_row['car']){
		echo "<br/><center><td style='vertical-align:submit;width:50%;white-space:nowrap;'><input type='submit' class='gmenu' value='Неподходящее авто' style='width:180px;'/></center></td></div>";
	}else{
			echo "<form action='' method='post'>
				<center><input type='submit' name='start' class='btn' value='Начать турнир' /></center>
			</form></div>";
		}
	?>
		</div><div class='tt3' style='background:green;b'>Порядок проведения турнира:</div><div class='menu' style='background:;b'>
		<div class='cit privet dhint' style='border:1px solid #444;padding:8px;'>
<center><font color='#f00'>Турнир начнеться сегодня в <?=$turnir_settings_row[st]?> и закончиться в <?=$turnir_settings_row[ok]?> </font></center>
			<font size='1'>На каждый заезд тратиться <?=$turnir_settings_row[fuel]?> <img src='/icons/head-icons/fuel.gif' alt='.' /> бензина<br/>
			Побеждает тот, кто к концу чемпионата одержит больше всего <img src='/icons/flag.png' width='9' height='12'/> побед!<br/>
			<font color='#f00'>В случае поражения ты выбываешь из турнира.
			Побежденный тобой гонщик не выбывает из турнира пока сам не проиграет.</font></font>
			</div>
		</div>
<?php
	if(isset($_GET['refill'])){
	if($data_user['gold'] < 2){
		echo "</div><div class='menu'><span style='color:red'>Нехватает баксов чтоб залить 100 лтр</span></div>		
</body>
</html></html></div>
<?include_once 'inc/foot.php';?>";
exit();
	}else{
		if($data_user['fuel'] > 100){
			echo "<meta http-equiv='refresh' content='0; URL=index.php?' />";
			exit();
		}else{
			$resm = $data_user['gold']-2;
			mysql_query("UPDATE `users` SET `fuel`='100',`gold`='$resm' WHERE nick='".$_SESSION['user']."'") or die(mysql_error());
			echo "<meta http-equiv='refresh' content='0; URL=index.php?' />";
			exit();
		}
	}
}
?>		
<?
}
}
?>
</div><div class='tt3' style='background:green;b'>Анонс заездов:</div><div class='menu' style='background:;b'>
		<div class='cit privet dhint' style='border:1px solid #444;padding:8px;'>
Расписание турниров на сегодня:</br>
<?=$turnir_settings_row[t1]?>-<?=$turnir_settings_row[k1]?>:ㅤ<?=$turnir_settings_row[1]?></br>	
<?=$turnir_settings_row[t2]?>-<?=$turnir_settings_row[k2]?>:ㅤ<?=$turnir_settings_row[2]?></br>
<?=$turnir_settings_row[t3]?>-<?=$turnir_settings_row[k3]?>:ㅤ<?=$turnir_settings_row[3]?></br>
<?=$turnir_settings_row[t4]?>-<?=$turnir_settings_row[k4]?>:ㅤ<?=$turnir_settings_row[4]?></br>	
<?=$turnir_settings_row[t5]?>-<?=$turnir_settings_row[k5]?>:ㅤ<?=$turnir_settings_row[5]?></br>			</div>
		</div>
<?
}
}
?>
<?include_once 'inc/foot.php'?>
</body>
</html>
<?
}
?>