<?php

$t=microtime(1);
$time=time();
require_once'/var/www/test/data/www/vferme.com/inc/start_sess.php';
$user_select=mysql_query("SELECT `id` FROM `kolhoz_user` WHERE `garden_time` > '$time' AND `gardener`='3'");
WHILE($ku=mysql_fetch_array($user_select)){
$ku=mysql_fetch_array(mysql_query("SELECT * FROM `kolhoz_user` WHERE `id` = '$ku[id]'"));
if(mysql_result(mysql_query("SELECT COUNT(*) FROM `kolhoz_collective_user` WHERE `id_user` = '$ku[id]'"),0)!=0)
{
$mcu=mysql_fetch_array(mysql_query("SELECT * FROM `kolhoz_collective_user` WHERE `id_user` = '$ku[id]'"));
$mc=mysql_fetch_array(mysql_query("SELECT * FROM `kolhoz_collective` WHERE `id` = '$mcu[id_collective]'"));
$mc['users']=mysql_result(mysql_query("SELECT COUNT(*) FROM `kolhoz_collective_user` WHERE `id_collective` = '$mc[id]'"),0);
}
require_once'/var/www/test/data/www/vferme.com/inc/functions.php';

if(isset($mc)){
$smelhel=mysql_fetch_array(mysql_query("SELECT * FROM `kolhoz_collective_heli` WHERE `id_collective` = '$mc[id]' AND `heli` = '1' AND `gaz` >= '10'"));
$arendsmel=mysql_fetch_array(mysql_query("SELECT * FROM `kolhoz_collective_ahlist` WHERE `id_user` = '$ku[id]' AND `heli` = '1'"));
if($smelhel['id'] AND (!$arendsmel || $arendsmel['time']<$time) AND $ku['time_shmel']<$time)
{//Вызов шмеля
$heli=mysql_fetch_array(mysql_query("SELECT * FROM `kolhoz_collective_heli` WHERE `id_collective` = '$mc[id]' AND `heli` != '0' AND `id` = '$smelhel[id]'"));
$type=7;$gaz=10;
$arend=mysql_fetch_array(mysql_query("SELECT * FROM `kolhoz_collective_ahlist` WHERE `id_user` = '$ku[id]' AND `heli` = '1'"));
$count_tt=mysql_result(mysql_query("SELECT COUNT(*) FROM `kolhoz_collective_heli` WHERE `id_collective` = '$mc[id]' AND `heli` = '1'"),0);
$tl=round($mc['users']/($type*$count_tt),2);
$t2=0;
if($tl2=explode('.',$tl))
{
if(!isset($tl2[1]))$tl2[1]='0';
if(strlen2($tl2[1])<2)$tl2[1]=$tl2[1]."0";
$t1=$tl2[0];
$tl2=$tl2[1];
$dil=100/60;
$t2=floor($tl2/$dil);
}
$nt=$time+($t1*3600)+($t2*60);
if(!$arend)mysql_query("INSERT INTO `kolhoz_collective_ahlist` SET `id_user` = '$ku[id]', `heli` = '1', `time` = '$nt'");
else mysql_query("UPDATE `kolhoz_collective_ahlist` SET `time` = '$nt' WHERE `id` = '$arend[id]'");

$timee=3600;
if($ku['time_shmel']>time())$ku['time_shmel'] = $ku['time_shmel']+$timee; else $ku['time_shmel'] = time()+$timee;
mysql_query("UPDATE `kolhoz_user` SET `time_shmel` = '$ku[time_shmel]' WHERE `id` = '$ku[id]'");

mysql_query("UPDATE `kolhoz_collective_heli` SET `gaz` = '".($heli['gaz']-$gaz)."' WHERE `id` = '$heli[id]'");
}

$smelhel=mysql_fetch_array(mysql_query("SELECT * FROM `kolhoz_collective_heli` WHERE `id_collective` = '$mc[id]' AND `heli` = '2' AND `gaz` >= '10'"));
$arendsmel=mysql_fetch_array(mysql_query("SELECT * FROM `kolhoz_collective_ahlist` WHERE `id_user` = '$ku[id]' AND `heli` = '2'"));
if($smelhel['id'] AND (!$arendsmel || $arendsmel['time']<$time) AND $ku['time_yastreb']<$time)
{//Вызов ястреба
$heli=mysql_fetch_array(mysql_query("SELECT * FROM `kolhoz_collective_heli` WHERE `id_collective` = '$mc[id]' AND `heli` != '0' AND `id` = '$smelhel[id]'"));
$type=7;$gaz=10;
$arend=mysql_fetch_array(mysql_query("SELECT * FROM `kolhoz_collective_ahlist` WHERE `id_user` = '$ku[id]' AND `heli` = '2'"));
$count_tt=mysql_result(mysql_query("SELECT COUNT(*) FROM `kolhoz_collective_heli` WHERE `id_collective` = '$mc[id]' AND `heli` = '2'"),0);
$tl=round($mc['users']/($type*$count_tt),2);
$t2=0;
if($tl2=explode('.',$tl))
{
if(!isset($tl2[1]))$tl2[1]='0';
if(strlen2($tl2[1])<2)$tl2[1]=$tl2[1]."0";
$t1=$tl2[0];
$tl2=$tl2[1];
$dil=100/60;
$t2=floor($tl2/$dil);
}
$nt=$time+($t1*3600)+($t2*60);
if(!$arend)mysql_query("INSERT INTO `kolhoz_collective_ahlist` SET `id_user` = '$ku[id]', `heli` = '2', `time` = '$nt'");
else mysql_query("UPDATE `kolhoz_collective_ahlist` SET `time` = '$nt' WHERE `id` = '$arend[id]'");

$timee=3600;
if($ku['time_yastreb']>time())$ku['time_yastreb'] = $ku['time_yastreb']+$timee; else $ku['time_yastreb'] = time()+$timee;
mysql_query("UPDATE `kolhoz_user` SET `time_yastreb` = '$ku[time_yastreb]' WHERE `id` = '$ku[id]'");

mysql_query("UPDATE `kolhoz_collective_heli` SET `gaz` = '".($heli['gaz']-$gaz)."' WHERE `id` = '$heli[id]'");
}

$smelhel=mysql_fetch_array(mysql_query("SELECT * FROM `kolhoz_collective_heli` WHERE `id_collective` = '$mc[id]' AND `heli` = '3' AND `gaz` >= '10'"));
$arendsmel=mysql_fetch_array(mysql_query("SELECT * FROM `kolhoz_collective_ahlist` WHERE `id_user` = '$ku[id]' AND `heli` = '3'"));
if($smelhel['id'] AND (!$arendsmel || $arendsmel['time']<$time) AND $ku['time_drakon']<$time)
{//Вызов дракона
$heli=mysql_fetch_array(mysql_query("SELECT * FROM `kolhoz_collective_heli` WHERE `id_collective` = '$mc[id]' AND `heli` != '0' AND `id` = '$smelhel[id]'"));
$type=7;$gaz=10;
$arend=mysql_fetch_array(mysql_query("SELECT * FROM `kolhoz_collective_ahlist` WHERE `id_user` = '$ku[id]' AND `heli` = '3'"));
$count_tt=mysql_result(mysql_query("SELECT COUNT(*) FROM `kolhoz_collective_heli` WHERE `id_collective` = '$mc[id]' AND `heli` = '3'"),0);
$tl=round($mc['users']/($type*$count_tt),2);
$t2=0;
if($tl2=explode('.',$tl))
{
if(!isset($tl2[1]))$tl2[1]='0';
if(strlen2($tl2[1])<2)$tl2[1]=$tl2[1]."0";
$t1=$tl2[0];
$tl2=$tl2[1];
$dil=100/60;
$t2=floor($tl2/$dil);
}
$nt=$time+($t1*3600)+($t2*60);
if(!$arend)mysql_query("INSERT INTO `kolhoz_collective_ahlist` SET `id_user` = '$ku[id]', `heli` = '3', `time` = '$nt'");
else mysql_query("UPDATE `kolhoz_collective_ahlist` SET `time` = '$nt' WHERE `id` = '$arend[id]'");

$timee=3600;
if($ku['time_drakon']>time())$ku['time_drakon'] = $ku['time_drakon']+$timee; else $ku['time_drakon'] = time()+$timee;
mysql_query("UPDATE `kolhoz_user` SET `time_drakon` = '$ku[time_drakon]' WHERE `id` = '$ku[id]'");
mysql_query("UPDATE `kolhoz_collective_heli` SET `gaz` = '".($heli['gaz']-$gaz)."' WHERE `id` = '$heli[id]'");
}

$smelhel=mysql_fetch_array(mysql_query("SELECT * FROM `kolhoz_collective_heli` WHERE `id_collective` = '$mc[id]' AND `heli` = '4' AND `gaz` >= '25'"));
$arendsmel=mysql_fetch_array(mysql_query("SELECT * FROM `kolhoz_collective_ahlist` WHERE `id_user` = '$ku[id]' AND `heli` = '4'"));
if($smelhel['id'] AND (!$arendsmel || $arendsmel['time']<$time)  AND ($ku['time_shmel']<$time OR $ku['time_yastreb']<$time OR $ku['time_drakon']<$time))
{//Вызов грома
$heli=mysql_fetch_array(mysql_query("SELECT * FROM `kolhoz_collective_heli` WHERE `id_collective` = '$mc[id]' AND `heli` != '0' AND `id` = '$smelhel[id]'"));
$type=9;$gaz=25;
$arend=mysql_fetch_array(mysql_query("SELECT * FROM `kolhoz_collective_ahlist` WHERE `id_user` = '$ku[id]' AND `heli` = '4'"));
$count_tt=mysql_result(mysql_query("SELECT COUNT(*) FROM `kolhoz_collective_heli` WHERE `id_collective` = '$mc[id]' AND `heli` = '4'"),0);
$tl=round($mc['users']/($type*$count_tt),2);
$t2=0;
if($tl2=explode('.',$tl))
{
if(!isset($tl2[1]))$tl2[1]='0';
if(strlen2($tl2[1])<2)$tl2[1]=$tl2[1]."0";
$t1=$tl2[0];
$tl2=$tl2[1];
$dil=100/60;
$t2=floor($tl2/$dil);
}
$nt=$time+($t1*3600)+($t2*60);
if(!$arend)mysql_query("INSERT INTO `kolhoz_collective_ahlist` SET `id_user` = '$ku[id]', `heli` = '4', `time` = '$nt'");
else mysql_query("UPDATE `kolhoz_collective_ahlist` SET `time` = '$nt' WHERE `id` = '$arend[id]'");

$timee=3600;
mysql_query("UPDATE `kolhoz_user` SET `time_shmel` = '".($ku["time_shmel"]>$time?"".($ku["time_shmel"]+$timee)."":"".($time+$timee)."")."' WHERE `id` = '$ku[id]'");
mysql_query("UPDATE `kolhoz_user` SET `time_yastreb` = '".($ku["time_yastreb"]>$time?"".($ku["time_yastreb"]+$timee)."":"".($time+$timee)."")."' WHERE `id` = '$ku[id]'");
mysql_query("UPDATE `kolhoz_user` SET `time_drakon` = '".($ku["time_drakon"]>$time?"".($ku["time_drakon"]+$timee)."":"".($time+$timee)."")."' WHERE `id` = '$ku[id]'");
mysql_query("UPDATE `kolhoz_collective_heli` SET `gaz` = '".($heli['gaz']-$gaz)."' WHERE `id` = '$heli[id]'");
}
}
}
echo '<br/> '.number_format(microtime(1)-$t,3).' сек.';

?>