<?php
session_start();
include_once "inc/db_connect.php";
include_once "inc/fnc.php";
header('Content-Type: text/html;charset=utf8');
$err = "";
if(isset($_POST['regok'])){unset($_SESSION['reg_ok']);}
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());        $currentDate = time();
        /* Раскоментируй если нужно чтоб счетчик после каждой гонки не сбивался
           т.е. юзер подождал 3 минуты , проехал гонку, и надеется что через
         * 2 минуты будет +1 к бензину, а оно сначала начнет считать 300сек
        if (($currentDate - $data_user['fuel_change']) < 300)
        {
            $currentDate = $data_user['fuel_change'];
        }*/
		include ('head.php');
?>
<div class="title">Гонки</div>
<?php
$date = date("H:i:s");
if(isset($newlevel)){
	echo "<div class='menu'>$newlevel</div>";
}
if(isset($_GET['fuel'])){
	if($data_user['money'] < 3000){
		echo "<div class='err'>Нехватает баксов</div>";
include_once ('foot.php');
exit();
	}else{
		if($data_user['fuel'] > 100){
			echo "<meta http-equiv='refresh' content='0; URL=index.php?' />";
			exit();
		}else{
			$resm = $data_user['money']-3000;
			mysql_query("UPDATE `users` SET `fuel`=(`fuel`+10),`money`='$resm' WHERE nick='".$_SESSION['user']."'") or die(mysql_error());
			echo "<meta http-equiv='refresh' content='0; URL=race.php?' />";
			exit();
		}
	}
}


if(isset($_GET['race_ok'])){
if(isset($_SESSION['res'])){
if(isset($_POST['go'])){
	unset($_SESSION['res']);
	header("Location: race.php");
	exit();
}else{
if($_SESSION['res'] == 1){
	echo '
	<div class="mono">
		<center>
			<b>
				Вы выиграли этот заезд
			</b>
		<hr>
			<b>
				Ваша награда:
			</b>
		</center>
		<br/>
			<img src="img/money.png" alt="." width="16" height="16"/> Баксы: 
				<font color="green">
					+250
				</font>
		<br>
			<img src="img/raiting.png" alt="." width="16" height="16"/> Рейтинг: 
				<font color="green">
					+1
				</font>
		<br>';
		$ku=mysql_fetch_array(mysql_query("SELECT * FROM `users` WHERE `id` = '$ku[id]'"));
                if($ku['bonus_time'] >= 1 ) {
			echo '<img src="img/lev.png" alt="." width="16" height="16"/> Опыт: 
				<font color="green">
					+6 ';
					} else 
					{ echo '<img src="img/lev.png" alt="." width="16" height="16"/> Опыт: 
				<font color="green">
					+3 ';
					}
					echo "
				</font>
		<br/>
			<img src='img/benz.png' alt='fuel' width='16' height='16'/> Бензин: 
				<font color='#F00'>
					-10
				</font>
				<br/><br/>
		<center>
			<form action='' method='post'>
				<input type='submit' name='go' class='btn' value='Продолжить' />
			</form>
		</center>
	</div>

</body>
</html>
		";
		include 'foot.php';
		unset($_SESSION['res']);
		exit();
}elseif($_SESSION['res']==2){
	echo "
<div class='mono'>
		<center>
			<font color='#ff0000'>
				Вы проиграли этот заезд
			</font>
		<hr>
		Твоя награда:
		</center>
		<br/>
		<img src='img/raiting.png' alt='.' width='16' height='16'/> Рейтинг: 
			<font color='#F00'>
				-1
			</font><br/>";
			
			$ku=mysql_fetch_array(mysql_query("SELECT * FROM `users` WHERE `id` = '$ku[id]'"));
                if($ku['bonus_time'] >= 1 ) {
			echo '<img src="img/lev.png" alt="." width="16" height="16"/> Опыт: 
				<font color="green">
					+2 ';
					} else 
					{ echo '<img src="img/lev.png" alt="." width="16" height="16"/> Опыт: 
				<font color="green">
					+1 ';
					}
echo "
			</font><br/>
		<img src='img/benz.png' alt='fuel' width='16' height='16'/> Бензин: 
			<font color='#cc0000'>
				-10
			</font>
		<br/><br/>
	<center>
		<form action='' method='post'>
			<input type='submit' name='go' class='btn' value='Продолжить' />
		</form>
	</center>
</div>
</body>
</html>
		";
		include 'foot.php';
		unset($_SESSION['res']);
		exit();
}
}
}
}
if(isset($_POST['gonka'])){
if($data_user['fuel'] < 10){
	echo "<div class='menu'><center><img src='img/bar.png'><br>Вы можете пополнить бензин в <a href='bar.php'><b><font color='#FFF'>баре</font></b></a></center><br></div>
	
</body>
</html>";
include 'foot.php';
exit();
}else{
	echo "<div class='mono'><center><img src='img/race.gif' alt='.' width='80' height='80'/><br/><h2>Гонка...</h2></center></div>
</body>
</html>";
include 'foot.php';
	if(isset($_POST['speed'])){
	if($_POST['speed'] <= $data_user['speed']){
		$res_money = $data_user['money']+250;
		$res_raiting = $data_user['raiting']+1;
		$res_fuel = $data_user['fuel']-10;
		if($ku['bonus_time'] >= 1 ) {
		$res_opit = $data_user['opit']+6;
		} else {
		$res_opit = $data_user['opit']+3;
		}
        $res_vgonka = $data_user['vgonka']+1;
		$res_gonki = mysql_query("UPDATE `users` SET `money`='".$res_money."',`raiting`='".$res_raiting."',`opit`='".$res_opit."',`fuel`='".$res_fuel."',`fuel_change`='$currentDate' WHERE nick='".$_SESSION['user']."'") or die(mysql_error());
		if($res_gonki == TRUE){
		$_SESSION['res'] = 1;
			echo "<meta http-equiv='refresh' content='1; URL=race.php?race_ok' />";
			exit();
	}
	}elseif($_POST['speed'] > $data_user['speed']){
			if($ku['bonus_time'] >= 1 ) {
		$res_opit = $data_user['opit']+2;
		} else {
		$res_opit = $data_user['opit']+1;
		}
			$res_raiting = $data_user['raiting']-1;
			$res_fuel = $data_user['fuel']-10;
                        $res_vgonka2 = $data_user['vgonka']+1;
			$res_gonki = mysql_query("UPDATE `users` SET `opit`='".$res_opit."',`raiting`='".$res_raiting."',`fuel`='".$res_fuel."',`fuel_change`='$currentDate' WHERE nick='".$_SESSION['user']."'") or die(mysql_error());
			if($res_gonki == TRUE){
			$_SESSION['res'] = 2;
			echo "<meta http-equiv='refresh' content='0; URL=race.php?race_ok' />";
			exit();
		}
	}else{
		echo "<meta http-equiv='refresh' content='0; URL=race.php?' />";
		exit();
}
}
}
}
$data_vs = mysql_fetch_array(mysql_query("SELECT * FROM `users` ORDER BY RAND() LIMIT 1")) or die(mysql_error());

?>
<table class="wdtrace" style="background-image: url(style/<?=$data_user['theme']?>/icon/racefon.png); color : #ff0000;background-position: bottom center; ">
	<tr> 
		<td> 
			<br/><br/>  
			<img src="<?=$data_vs['car']?>" alt="<?=$data_vs['carname']?>" width="150px" /> 
			<span class='corners'><span></span></span>
			<img src="<?=$data_user['car']?>" alt="<?=$data_user['carname']?>" width="150px" /> 
		</td>
	</tr> 
</table>
<div class='mono'>
<table style='width:100%' cellspacing='0' cellpadding='0'>
<td style='text-align:center;font-weight: bold;width:40%;'>
<img src='<?=$data_user['avatar']?>' width='50' height='50' alt='' style='padding:0;width:46px;border-radius:23px;-moz-border-radius:23px;-webkit-border-radius:23px;-khtml-border-radius:23px;'/>
<br/>
<font color="#cc0000"><?=$data_user['nick']?></font></br>
<font color="#000"><?=$data_user['level']?> ур.</font><br/>
<?=$data_user['speed']?> л.с<br/>
</td>
<td style='text-align:center;width:20%;'>
<h2 style="color:red">VS</h2>
</td>
<td style='text-align:center;font-weight: bold;width:40%;'>
<img src='<?=$data_vs['avatar']?>' width='50' height='50' alt='' style='padding:0;width:46px;border-radius:23px;-moz-border-radius:23px;-webkit-border-radius:23px;-khtml-border-radius:23px;'/> 
<br/>
<a href='info.php?id=<?=$data_vs['id']?>'><font color="#cc0000"><?=$data_vs['nick']?></font></a></br>
<font color="#000"><?=$data_vs['level']?> ур.</font><br/>
<?=$data_vs['speed']?> л.с<br/>
</td>
</table>
<br/>
<table style='width:100%' cellspacing='0' cellpadding='0'>
<form action="race.php" method="post">
<td style='vertical-align:submit;width:50%;white-space:nowrap;'>
<center><input type="submit" name="ref" class="btn" value="Обновить" /></center>
</td>
<td style='vertical-align:submit;width:50%;white-space:nowrap;'>
<input type="hidden" name="speed" value="<?=$data_vs['speed']?>" />
<center><input type="submit" class="btn" name="gonka" value="Гонка" /></center>
</div></a>
</td>
</form>
</table>
</div>
</div>
<?php
}
include 'foot.php';
?>