 <?php

$t=microtime(1);
$time=time();
require_once'inc/start_sess.php';
$user_select=mysql_query("SELECT `id` FROM `kolhoz_user` WHERE `darja_time` > '$time'");
WHILE($ku=mysql_fetch_array($user_select)){
if(mysql_result(mysql_query("SELECT COUNT(*) FROM `kolhoz_polka2` WHERE `id_user` = '$ku[id]'
AND (`id_plant2` = '0' OR `time` < '$time' )"),0)>0){
$ku=mysql_fetch_array(mysql_query("SELECT * FROM `kolhoz_user` WHERE `id` = '$ku[id]'"));
$conf = mysql_fetch_array(mysql_query("SELECT * FROM `kolhoz_setting` WHERE `id_user`='$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]'"));
}
//require_once'/var/www/test/data/www/vferme.com/inc/start.php';
require_once'inc/functions.php';
include'inc/tehnika.php';
if($ku['expdouble']==date("D"))$dexp=1;
$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]'"));
  if($mc['grom']>0)
  {
      $ogran=time()+3600;

    if($ku['time_shmel']<$ogran)mysql_query("UPDATE `kolhoz_user` SET `time_shmel`='".($ku['time_shmel']>time()?"".($ku['time_shmel']+3600)."":"".(time()+3600)."")."' WHERE `id`='$ku[id]'");
    if($ku['time_yastreb']<$ogran)mysql_query("UPDATE `kolhoz_user` SET `time_yastreb`='".($ku['time_yastreb']>time()?"".($ku['time_yastreb']+3600)."":"".(time()+3600)."")."' WHERE `id`='$ku[id]'");
    if($ku['time_drakon']<$ogran)mysql_query("UPDATE `kolhoz_user` SET `time_drakon`='".($ku['time_drakon']>time()?"".($ku['time_drakon']+3600)."":"".(time()+3600)."")."' WHERE `id`='$ku[id]'");

  }
        $ogran=time()+3600;
   if($mc['shmel']>0 && $ku['time_shmel']<$ogran)
  {
    mysql_query("UPDATE `kolhoz_user` SET `time_shmel`='".($ku['time_shmel']>time()?"".($ku['time_shmel']+3600)."":"".(time()+3600)."")."' WHERE `id`='$ku[id]'");
  }
        $ogran=time()+3600;
   if($mc['hawk']>0 && $ku['time_yastreb']<$ogran)
  {
    mysql_query("UPDATE `kolhoz_user` SET `time_yastreb`='".($ku['time_yastreb']>time()?"".($ku['time_yastreb']+3600)."":"".(time()+3600)."")."' WHERE `id`='$ku[id]'");
  }
        $ogran=time()+3600;

   if($mc['dragon']>0 && $ku['time_drakon']<$ogran)
  {
    mysql_query("UPDATE `kolhoz_user` SET `time_drakon`='".($ku['time_drakon']>time()?"".($ku['time_drakon']+3600)."":"".(time()+3600)."")."' WHERE `id`='$ku[id]'");
  }
////////////////////////////Собрать все////////////
if(mysql_result(mysql_query("SELECT COUNT(*) FROM `kolhoz_polka2` WHERE `id_user` = '$ku[id]' AND (`time` < '$time' OR `time` = '$time') AND `id_plant2` != '0'"),0)!=0)
{
$q=mysql_query("SELECT * FROM `kolhoz_polka2` WHERE `id_user` = '$ku[id]' AND (`time` < '$time' OR `time` = '$time') AND `id_plant2` != '0'");
$ohv=0;
while($post=mysql_fetch_array($q))
{
$plant2=mysql_fetch_array(mysql_query("SELECT * FROM `kolhoz_plant2` WHERE `id` = '$post[id_plant2]'"));

$count=$plant2['count'];
$count_harvester2_act=0;
$count_cellar_act=0;
if($ku['harvester2']>0)
{
$hv=$ku['harvester2'];
if($hv<=10)$harvester2_act=$hv*10;
else if($hv>10)$harvester2_act=($hv-10)*30+100;
}
$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]'"));

if(isset($mc) && $mc['cellar']>0) // кх склад заготовок
{
$cellar_act=1;
if($mc['cellar']<=5){$cellar_ps=100;}else{
$cellar_ps=$mc['cellar']*20;}
// увеличивает урожай с погреба на (уровень * 20%)
}
if($ku['talent_3']>0)$talent3=(($count/100)*($ku['talent_3']*30)); else $talent3=0;
if(isset($harvester2_act))$count_harvester2_act=floor(($count/100)*$harvester2_act);
if(isset($cellar_act))$count_cellar_act=(($count/100)*$cellar_ps);
$count=$talent3+$count_harvester2_act+$count_cellar_act+$count;
$count=floor($count);
if($pw=mysql_fetch_array(mysql_query("SELECT * FROM `kolhoz_market` WHERE `id_user` = '$ku[id]' AND `id_plant2` = '$plant2[id]' AND `arena` = '0'")))
{
mysql_query("UPDATE `kolhoz_market` SET `count` = '".($pw['count']+$count)."' WHERE `id` = '$pw[id]'");
}
else mysql_query("INSERT INTO `kolhoz_market` SET `id_plant2` = '$plant2[id]', `id_user`  = '$ku[id]', `count` = '$count', `arena` = '0'");
mysql_query("UPDATE `kolhoz_polka2` SET `id_plant2` = '0', `soil2` = '0', `cultivator2` = '0' WHERE `id` = '$post[id]' LIMIT 1");
$ohv=$ohv+$count;
}
//$_SESSION['harvest2']=$ohv;
mysql_query("UPDATE `kolhoz_setting` SET `recip_dost_count` = '".($conf['recip_dost_count']+$ohv)."' WHERE `id_user` = '$ku[id]'");
mysql_query("INSERT INTO `kolhoz_joirnal` SET `worker` = '1', `id_user`  = '$ku[id]', `time` = '$time', `count` = '$ohv'");
$ohv = 0;
}
////if($ku['id']==8)mysql_query("INSERT INTO `kolhoz_mail` SET `id_kont` = '0', `id_user`  = '$ku[id]', `time` = '$time', `msg` = 'Общий доход: $count, доход с таланта: $talent3, доход с комбайна: $count_harvester2_act, доход с постройки: $count_cellar_act'");


////////////////////////////Протереть полки
if(mysql_result(mysql_query("SELECT COUNT(*) FROM `kolhoz_polka2` WHERE `id_user` = '$ku[id]' AND `id_plant2` = '0' AND (`time` = '$time' OR `time` < '$time') AND `cultivator2` = '0'"),0)!=0)
{

$rat=c2r($ku['level']);
$rat_drakon_act=0;
$rat_sc_act=0;
$rat_dog_act=0;
$rat_lib_act=0;
$rat_cultivator2_act=0;
$arena_rat=0;
if(isset($sc_act))$rat_sc_act=$rat*2;
if(isset($dog_type))$rat_dog_act=(($rat/100)*$dog_type);
if(isset($cultivator2_act))$rat_cultivator2_act=(($rat/100)*$cultivator2_act);
if(isset($drakon_act))$rat_drakon_act=((2/100)*300);
if(isset($lib_act))$rat_lib_act=$rat/2;
if(isset($shmel2_act))$rat_cultivator2_act=$rat_cultivator2_act*2;
if($ku['arena_rating']>1000)$arena_rat=floor(($rat/100)*(($ku['arena_rating']-1000)/20));
$ratt=$rat_drakon_act+$rat_sc_act+$rat_cultivator2_act+$rat_lib_act+$rat_dog_act+$arena_rat;
if($ratt==0)$rat=$rat;
else $rat=$ratt;
if(isset($dexp) AND !isset($dexp3))$rat=$rat*2; elseif(isset($dexp3))$rat=$rat*3;
$orat=0;
$q=mysql_query("SELECT * FROM `kolhoz_polka2` WHERE `id_user` = '$ku[id]' AND `id_plant2` = '0' AND (`time` = '$time' OR `time` < '$time') AND `cultivator2` = '0'");
while($post=mysql_fetch_array($q))
{
mysql_query("UPDATE `kolhoz_polka2` SET `cultivator2` = '1' WHERE `id` = '$post[id]' LIMIT 1");
mysql_query("UPDATE `kolhoz_user` SET `rating` = '".($ku['rating']+$rat)."' WHERE `id` = '$ku[id]'");
$orat=$rat+$orat;
}
//$_SESSION['rating']=$orat;
update_mc($orat);

}

///////////////////////////Поставить все

if(mysql_result(mysql_query("SELECT COUNT(*) FROM `kolhoz_polka2` WHERE `id_user` = '$ku[id]' AND `id_plant2` = '0' AND `cultivator2` = '1'"),0)!=0 && $ku['last_plant2']!=0)
{
$plant2=mysql_fetch_array(mysql_query("SELECT * FROM `kolhoz_plant2` WHERE `id` = '$ku[last_plant2]'"));
$cg=mysql_result(mysql_query("SELECT COUNT(*) FROM `kolhoz_polka2` WHERE `id_user` = '$ku[id]' AND `id_plant2` = '0'"),0);
$rubies=floor($plant2['cena']);
$sc=$rubies*$cg;
$rat=$plant2['rating'];
$rat_sc_act=0;
$rat_dog_act=0;
$rat_drakon_act=0;
$rat_seeder2_act=0;
$rat_school_act=0;
$rat_cultivator2_exp=0;
$arena_rat=0;
if(isset($sc_act))$rat_sc_act=$rat*2;
if($ku['talent_3']>0)$talent3=(($rat/100)*($ku['talent_3']*10));

if(isset($dog_type))$rat_dog_act=(($rat/100)*$dog_type);
if(isset($drakon_act))$rat_drakon_act=(($plant2['rating']/100)*300);
if(isset($seeder2_act))$rat_seeder2_act=(($rat/100)*$seeder2_act);
if(isset($cultivator2_exp))$rat_cultivator2_exp=(($rat/100)*$cultivator2_exp);
if(isset($school_act))$rat_school_act=$rat/2;
if(isset($shmel_act))$rat_seeder2_act=$rat_seeder2_act*2;
if($ku['arena_rating']>1000)$arena_rat=floor(($rat/100)*(($ku['arena_rating']-1000)/20));
$ratt=$rat_drakon_act+$talent3+$rat_sc_act+$rat_seeder2_act+$rat_cultivator2_exp+$rat_school_act+$rat_dog_act+$arena_rat;
if($ratt==0)$rat=$rat;
else $rat=$ratt;
if(isset($dexp) AND !isset($dexp3))$rat=$rat*2; elseif(isset($dexp3))$rat=$rat*3;

if($ku['rubies']>=$sc)
{
$q=mysql_query("SELECT * FROM `kolhoz_polka2` WHERE `id_user` = '$ku[id]' AND `id_plant2` = '0' AND `cultivator2` = '1'");
$orat=0;
while($post=mysql_fetch_array($q))
{
$orat=$rat+$orat;
mysql_query("UPDATE `kolhoz_polka2` SET `id_plant2` = '$plant2[id]', `time_soil2` = '".($time+$plant2['time_soil2'])."', `time_water2` = '".($time+120)."', `soil2` = '0', `time` = '".($plant2['time']+$time)."' WHERE `id` = '$post[id]' LIMIT 1");
}
mysql_query("UPDATE `kolhoz_user` SET `rubies` = '".($ku['rubies']-$sc)."' WHERE `id` = '$ku[id]'");
mysql_query("UPDATE `kolhoz_user` SET `rating` = '".($ku['rating']+$orat)."' WHERE `id` = '$ku[id]'");
update_mc($orat);
$_SESSION['rating']=$orat;
}
}
}

}
echo '<br/> '.number_format(microtime(1)-$t,3).' сек.';

?> 