<?
$auto=mysql_fetch_array(mysql_query("SELECT * FROM `rice_garage` WHERE `id_user`=$ku[id]"));
$rice=mysql_fetch_array(mysql_query("SELECT * FROM `rice_start` WHERE `timeEnd`>$time"));
if($rice['timeEnd']<$time)
{
    $timeS=''.(date('H')*60*60)+(date('i')*60).'';
$timing=''.($time-$timeS).'';
$timeStart=''.($timing+(3600*24*3)+3600*19).'';
$timeEnd=''.($timing+(3600*24*3)+3600*19+600).'';
$nado=mysql_fetch_array(mysql_query("SELECT * FROM `rice_start` ORDER BY `id` ASC"));
$one=mysql_fetch_assoc(mysql_query('SELECT * FROM `rice_garage` WHERE  `start` = "1" ORDER BY `trassa` DESC LIMIT 1')); // Вычесляем 1 место
$two=mysql_fetch_assoc(mysql_query('SELECT * FROM `rice_garage` WHERE `trassa` < "'.$one['trassa'].'" AND `start` = "1" ORDER BY `trassa` DESC LIMIT 1')); // Вычесляем 2 место
$three=mysql_fetch_assoc(mysql_query('SELECT * FROM `rice_garage` WHERE `trassa` < "'.$two['trassa'].'" AND `start` = "1" ORDER BY `trassa` DESC LIMIT 1'));
$date=''.date('d.m.Y').'';
mysql_query("UPDATE `rice_start` SET `date` = '$date' WHERE `id` = '$nado[id]'");
mysql_query("UPDATE `rice_start` SET `one` = '$one[id_user]' WHERE `id` = '$nado[id]'");
mysql_query("INSERT INTO `kolhoz_mail` SET `msg` = 'Поздравляем! Вы заняли 1 место в гонках. Вам начислен приз: 150 фермани', `id_kont` = '701', `id_user` = '$one[id_user]', `time` = '$time'");
mysql_query("UPDATE `kolhoz_user` SET `fermoney` = `fermoney` + '150' WHERE `id` = '$one[id_user]'");
mysql_query("UPDATE `rice_bet` SET `status` =  '1' WHERE `driver` = '$one[id_user]' AND `history` = '$nado[id]'");
mysql_query("UPDATE `rice_start` SET `two` = '$two[id_user]' WHERE `id` = '$nado[id]'");
mysql_query("INSERT INTO `kolhoz_mail` SET `msg` = 'Поздравляем! Вы заняли 2 место в гонках. Вам начислен приз: 75 фермани', `id_kont` = '701', `id_user` = '$two[id_user]', `time` = '$time'");
mysql_query("UPDATE `kolhoz_user` SET `fermoney` = `fermoney` + '75' WHERE `id` = '$two[id_user]'");
mysql_query("UPDATE `rice_bet` SET `status` =  '2' WHERE `driver` = '$two[id_user]' AND `history` = '$nado[id]'");
mysql_query("UPDATE `rice_start` SET `three` = '$three[id_user]' WHERE `id` = '$nado[id]'");
mysql_query("INSERT INTO `kolhoz_mail` SET `msg` = 'Поздравляем! Вы заняли 3 место в гонках. Вам начислен приз: 25 фермани', `id_kont` = '701', `id_user` = '$three[id_user]', `time` = '$time'");
mysql_query("UPDATE `kolhoz_user` SET `fermoney` = `fermoney` + '25' WHERE `id` = '$three[id_user]'");
mysql_query("UPDATE `rice_bet` SET `status` =  '3' WHERE `driver` = '$three[id_user]' AND `history` = '$nado[id]'");
mysql_query("UPDATE `rice_garage` SET `trassa` = '0', `start` = '0'");
mysql_query("INSERT INTO `rice_start` SET `time` = '$timeStart', `timeEnd` = '$timeEnd'");
}
if(isset($_GET['bet']) && ($rice['timeEnd']>=$time && $rice['time']>$time) && intval($_GET['bet'])!=NULL && mysql_result(mysql_query("SELECT COUNT(*) FROM `rice_garage` WHERE `id_user` = '".intval($_GET['bet'])."'"),0)!=0)
{
$bet=mysql_fetch_array(mysql_query("SELECT * FROM `rice_garage` WHERE `id_user` = '".intval($_GET['bet'])."'"));
if(isset($_POST['ok']) && $_POST['mdp']==$ku['mdp'] && intval($_POST['sum'])>0){
$sum=floor($_POST['sum']);
$summ=$sum-($sum/100*10);
if($ku['money']>=$summ){
	if($summ>0){
		mysql_query("UPDATE `kolhoz_user` SET `money` = '".($ku['money']-$summ)."' WHERE `id` = '$ku[id]'");
		mysql_query("INSERT INTO `rice_bet` SET `driver` = '".($bet['id_user'])."', `time` = '$time', `summa` = '".($summ)."', `status` = '5', `history` = '$rice[id]', `id_user` = '$ku[id]'");
$_SESSION['msg']='Ставка '.$summ.'  монет сделана!';
header("location:/?rice");
	}
}
else 
$_SESSION['msg']='Недостаточно ресурсов';
header("location:/?rice");

} 
echo'<div class="event"><h1><a href="/?rice">Сделать ставку</a></h1></div>';
echo'<div class="block"> Сделать ставку на гонщика: '.nc($bet['id_user']).'</div>';
echo'<div class="block"> Комиссия: <span class="title">10%</span></div>';

echo "<div class='block'>";
echo "Введите ставку (в монетах):</span><br/><form method='post'>";
echo "<input type='text' name='sum' value=''><input type='hidden' name='mdp' value='$ku[mdp]'><br/>";
echo "<input type='submit' name='ok' value='Ставка'>";
echo "</form></div>";
include_once'inc/foot.php';
}
if(isset($_GET['autoshop']) && ($auto['id']<1))
{
include_once'rice/buy.php';
}
if(isset($_GET['garage']) && ($auto['id']>0))
{
include_once'rice/garage.php';
}
if(isset($_GET['history']))
{
include_once'rice/history.php';
}
if(isset($_GET['mybet']))
{
include_once'rice/mybet.php';
}
echo'<div class="event"><h1>Гонки</h1></div>';
echo'<div class="block"><center><img style="border-radius: 6px;" width="220" src="/rice/images/index.jpeg"></center></div>';
if($auto['id']<1)echo'<div class="block"><img src="/rice/images/buy.png"> <a href="/?rice&autoshop">Купить автомобиль</a></div>';
if($auto['id']>0)echo'<div class="block"><img src="/rice/images/garage.png"> <a href="/?rice&garage">Гараж</a></div>';
echo'<div class="block"><img src="/rice/images/mybet.png"> <a href="/?rice&mybet">Мои ставки</a></div>';
echo'<div class="block"><img src="/rice/images/history.png"> <a href="/?rice&history">История заездов</a></div>';
if($auto['id']>0 && ($rice['timeEnd']>=$time && $rice['time']>$time) && $auto['start']<1 && $ku['rubies']>=50000)
{
if(isset($_GET['start']))
{
mysql_query("UPDATE `kolhoz_user` SET `rubies` = `rubies` - '50000' WHERE `id` = '$ku[id]'");
mysql_query("UPDATE `rice_garage` SET `start` =  '1', `rice` = `rice` + '1' WHERE `id_user` = '$ku[id]'");
$_SESSION['msg']='Вы стали участником текущего заезда!';
header("location:/?rice");
}
echo'<div class="block"><img src="/rice/images/start.png"> <a href="/?rice&start">Принять участие</a> (<span class="title"><img src="/images/icons/ruby.png"> 50 000</span>)</div>';
}

if($rice['timeEnd']>$time && $rice['time']>$time)
{
echo'<div class="block"><span class="title">Участники заезда:</span></div>';
$k_post = mysql_result(mysql_query("SELECT COUNT(*) FROM `rice_garage` WHERE `start` = '1'"),0);
if ($k_post==0)
{
echo "<div class='block'>Список участников пуст</div>";
}
$set['p_str']=7;
$k_page=k_page($k_post,$set['p_str']);
$page=page($k_page);
$start=$set['p_str']*$page-$set['p_str'];
$q=mysql_query("SELECT * FROM `rice_garage` WHERE `start` = '1' ORDER BY `id` DESC LIMIT $start, $set[p_str]");
while($post=mysql_fetch_array($q))
{
$num++;
echo "<div class='block minor'>";
echo "".on_k($post['id_user'])."";

echo" <a href='?user=$post[id_user]'><a href='?user=$post[id_user]'>".nc($post['id_user'])."</a> | <img src='/images/icons/money.png'> <a href='/?rice&bet=$post[id_user]'>ставка</a></div>";
}
if ($k_page>1)
{
    echo'<div class="block">';
    str("?rice&",$k_page,$page);
    echo'</div>';
}

echo'<div class="block"><img src="/rice/images/timeindex.png"> Начало заезда <span class="title">'.vremja($rice['time']).'</span></div>';
}
else
{
echo'<div class="block"><span class="title">Участники заезда</span></div>';
$k_post = mysql_result(mysql_query("SELECT COUNT(*) FROM `rice_garage` WHERE `start` = '1'"),0);
if ($k_post==0)
{
echo "<div class='block'>Список участников пуст</div>";
}
$set['p_str']=7;
$k_page=k_page($k_post,$set['p_str']);
$page=page($k_page);
$start=$set['p_str']*$page-$set['p_str'];
$q=mysql_query("SELECT * FROM `rice_garage` WHERE `start` = '1' ORDER BY `trassa` DESC LIMIT $start, $set[p_str]");echo "<ul>";
while($post=mysql_fetch_array($q))
{
$num++;
echo "<div class='block'>$num.";
echo "".on_k($post['id_user'])."";

echo" <a href='?user=$post[id_user]'>".nc($post['id_user'])."</a> | <span class='title'>".($post['trassa'])."</a> км</div>";
}
if ($k_page>1)
{
    echo'<div class="block">';
    str("?rice&",$k_page,$page);
    echo'</div>';
}

if($auto['start']==1)
{
if(isset($_GET['free']))
{
if($auto['rice']<50)
{
$second=0;
}
if($auto['rice']>50 && $auto['rice']<100)
{
$second=10;
}
if($auto['rice']>100)
{
$second=20;
}
$timefree=($time+$auto['sec']+$seсond);
mysql_query("UPDATE `rice_garage` SET `trassa` =  `trassa` + '$auto[speed]' WHERE `id_user` = '$ku[id]'");
mysql_query("UPDATE `rice_garage` SET `time_trassa` =  '$timefree' WHERE `id_user` = '$ku[id]'");
$_SESSION['msg']='Успешно!';
header("location:/?rice");
}
if(isset($_GET['donate1']) && ($ku['fermoney']>=20))
{
mysql_query("UPDATE `rice_garage` SET `trassa` =  `trassa` + '30' WHERE `id_user` = '$ku[id]'");
mysql_query("UPDATE `kolhoz_user` SET `fermoney` =  `fermoney` - '20' WHERE `id` = '$ku[id]'");
$_SESSION['msg']='Успешно!';
header("location:/?rice");
}
if(isset($_GET['donate2']) && ($ku['fermoney']>=50))
{
mysql_query("UPDATE `rice_garage` SET `trassa` =  `trassa` + '70' WHERE `id_user` = '$ku[id]'");
mysql_query("UPDATE `kolhoz_user` SET `fermoney` =  `fermoney` - '50' WHERE `id` = '$ku[id]'");
$_SESSION['msg']='Успешно!';
header("location:/?rice");
}
echo'<div class="block">Меню гонщика:</div>';
if($auto['time_trassa']<$time)echo'<div class="block"> <img src="/rice/images/speed.png"> <a href="/?rice&free">Ехать + <span class="title">'.$auto['speed'].' км</a></div>';
if($auto['time_trassa']>$time)echo'<div class="block"> <img src="/rice/images/speed.png"> Бесплатный разгон будет доступен через <span class="title">'.time_left($auto['time_trassa']-$time).'</span> </a></div>';
echo'<div class="block"> <img src="/rice/images/speed.png"> <a href="/?rice&donate1">Ехать + <span class="title">30 км</span></a> (цена <span class="title"><img src="/worldkassa/fermoney.png"> 20</span>)</div>';
echo'<div class="block"> <img src="/rice/images/speed.png"> <a href="/?rice&donate2">Ехать + <span class="title">70 км</span></a> (цена <span class="title"><img src="/worldkassa/fermoney.png"> 50</span>)</div>';
}
echo'<div class="block"><img src="/rice/images/timeindex.png"> Гонка началась <span class="title">('.time_left($rice['timeEnd']-$time).')</span></div>';
}
?>