 <?php

$t=microtime(1);
$time=time();
require_once'inc/start_sess.php';
$user_select=mysql_query("SELECT `id` FROM `kolhoz_user` WHERE `trofim_time` > '$time'");
WHILE($ku=mysql_fetch_array($user_select)){

if(mysql_result(mysql_query("SELECT COUNT(*) FROM `kolhoz_pool` WHERE `id_user` = '$ku[id]' AND (`id_fish` = '0' OR `time` = '$time' OR `time` < '$time' OR `time_soil` = '$time' OR `time_soil` < '$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'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_pool` WHERE `id_user` = '$ku[id]' AND (`time_soil` = '$time' OR `time_soil` < '$time' ) AND `time`>'$time'"),0)!=0)
{
if($ku['fishfooder']>0)$proc=$ku['fishfooder']*10;    else $proc=0;
$q = mysql_query("SELECT * FROM `kolhoz_pool` WHERE `id_user` = '$ku[id]' AND (`time_soil` = '$time' OR `time_soil` < '$time' ) AND `time`>'$time'");
$orat=0;
While($ground=mysql_fetch_array($q))
{
$plant=mysql_fetch_array(mysql_query("SELECT * FROM `kolhoz_pool_plant` WHERE `id` = '".intval($ground['id_fish'])."'"));

$rat=$plant['soil_rating'];
$rat_sc_act=0;
$rat_dog_act=0;
$rat_drakon_act=0;
$rat_school_act=0;
$rat_lib_act=0;
$arena_rat=0;
$rat_fishfooder_act=0;

if(isset($sc_act))$rat_sc_act=$rat*2;
if(isset($dog_type))$rat_dog_act=(($rat/100)*$dog_type);
if(isset($drakon_act))$rat_drakon_act=(($rat/100)*300);
if(isset($school_act))$rat_school_act=$rat*2;
if(isset($lib_act))$rat_lib_act=$rat/2;
if($proc>0)$rat_fishfooder_act=($rat/100)*$proc;
if($ku['arena_rating']>1000){$arena_rat=floor(($rat/100)*(($ku['arena_rating']-1000)/20));}
if($ku['labspices']>=1)$labspices=($rat/100)*($ku['labspices']*10); 
if($ku['talent_5']>=1)$talent_51=($rat/100)*($ku['talent_5']*15); 
$ratt=$labspices+$rat+$talent_51+$rat_drakon_act+$rat_school_act+$rat_lib_act+$rat_sc_act+$rat_fishfooder_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 = $orat + $rat;
mysql_query("UPDATE `kolhoz_pool` SET `time_soil` = '".($plant['time_soil']*$incubator_timesoil_act+$time)."', `time` = '".($ground['time']-$plant['time_soil_minus'])."' WHERE `id` = '$ground[id]' LIMIT 1")or die (mysql_error());
}
mysql_query("UPDATE `kolhoz_user` SET `rating` = '".($ku['rating']+$orat)."' WHERE `id` = '$ku[id]'");

update_mc($orat);
}

////////////////////////////Продать всё
if(mysql_result(mysql_query("SELECT COUNT(*) FROM `kolhoz_pool` WHERE `id_user` = '$ku[id]' AND `id_fish`>'0' AND (`time` = '$time' OR `time` < '$time' )"),0)!=0)
{

$q = mysql_query("SELECT * FROM `kolhoz_pool` WHERE `id_user` = '$ku[id]' AND `id_fish`>'0' AND (`time` = '$time' OR `time` < '$time' )");
$orat=0;
$odohod=0;
$c_fish=0;
While($ground=mysql_fetch_array($q))
{

$plant=mysql_fetch_array(mysql_query("SELECT * FROM `kolhoz_pool_plant` WHERE `id` = '$ground[id_fish]'"));
if($ku['autocrane']>0)$proc=$ku['autocrane']*10;    else $proc=0;
$rat=$plant['rating'];
$rat_sc_act=0;
$rat_dog_act=0;
$rat_drakon_act=0;
$rat_school_act=0;
$rat_lib_act=0;
$arena_rat=0;
$rat_autocrane_act=0;
$rat_trofim_act=$rat/2;

if(isset($sc_act))$rat_sc_act=$rat*2;
if(isset($dog_type))$rat_dog_act=(($rat/100)*$dog_type);
if(isset($drakon_act))$rat_drakon_act=(($rat/100)*300);
if(isset($school_act))$rat_school_act=$rat*2;
if(isset($lib_act))$rat_lib_act=$rat/2;
if($ku['arena_rating']>1000){$arena_rat=floor(($rat/100)*(($ku['arena_rating']-1000)/20));}
if($proc>0)$rat_autocrane_act=($rat/100)*$proc;
if($ku['labspices']>=1)$labspices=($rat/100)*($ku['labspices']*10); 
if($ku['talent_5']>=1)$talent_51=($rat/100)*($ku['talent_5']*15); 
$ratt=$labspices+$rat+$talent_51+$rat_drakon_act+$rat_school_act+$rat_lib_act+$rat_sc_act+$rat_trofim_act+$rat_autocrane_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['money2x_time']>=$time)$money2x=($plant['dohod']/100)*100; 
if($ku['labsweets']>=1)$labsweets=($plant['dohod']/100)*($ku['labsweets']*10); 
if($ku['talent_5']>=1)$talent_5=($plant['dohod']/100)*($ku['talent_5']*15); 
$dohod=floor($talent_5+$labsweets+$money2x+$plant['dohod']+(($plant['dohod']/100)*$proc));
$odohod=$odohod+$dohod;
$orat=$orat+$rat;
$c_fish++;
mysql_query("UPDATE `kolhoz_pool` SET `time_soil` = '0', `time` = '0',`id_fish`='0' WHERE `id` = '$ground[id]' LIMIT 1")or die (mysql_error());
}
mysql_query("UPDATE `kolhoz_setting` SET `pool_fish_count` = '".($conf['pool_fish_count']+$c_fish)."' WHERE `id_user` = '$ku[id]'");
mysql_query("UPDATE `kolhoz_user` SET `rating` = '".($ku['rating']+$orat)."',`money`='".($ku['money']+$odohod)."' WHERE `id` = '$ku[id]'");
mysql_query("INSERT INTO `kolhoz_joirnal` SET `worker` = '2', `id_user`  = '$ku[id]', `time` = '$time', `count` = '$c_fish'");
$ku['money'] = round($ku['money']+$odohod);
if(isset($mc) && $mc['nalog']>0)
{
$cm = $odohod;
$nmoney=floor(($cm/100)*$mc['nalog']);
mysql_query("UPDATE `kolhoz_user` SET `money` = '".($ku['money']+$cm-$nmoney)."' WHERE `id` = '$ku[id]'");
mysql_query("UPDATE `kolhoz_collective_user` SET `money` = '".($mcu['money']+$nmoney)."' WHERE `id_user` = '$ku[id]'");
mysql_query("UPDATE `kolhoz_collective` SET `money` = '".($mc['money']+$nmoney)."' WHERE `id` = '$mc[id]'");
}

update_mc($orat);

}

///////////////////////////Разводить все

if(mysql_result(mysql_query("SELECT COUNT(*) FROM `kolhoz_pool` WHERE `id_user` = '$ku[id]' AND `id_fish` = '0'"),0)!=0)
{
if(!empty($ku['last_pool'])){
$plant=mysql_fetch_array(mysql_query("SELECT * FROM `kolhoz_pool_plant` WHERE `id` = '".intval($ku['last_pool'])."'"));
$times=$plant['time']*$incubator_time_act;
$stime=$plant['time_soil']*$incubator_timesoil_act;
$count_gg = mysql_result(mysql_query("SELECT COUNT(*) FROM `kolhoz_pool` WHERE `id_user` = '$ku[id]' AND `id_fish` = '0'"),0);
if(!empty($plant['rcena'])){
if($ku['rubies']>=$plant['rcena']*$count_gg)
{
mysql_query("UPDATE `kolhoz_user` SET `rubies` = '".($ku['rubies']-$plant['rcena']*$count_gg)."' WHERE `id` = '$ku[id]'");
mysql_query("UPDATE `kolhoz_pool` SET `id_fish` = '$plant[id]', `time_soil` = '".($plant['time_soil']+$time)."', `time` = '".($times+$time)."' WHERE `id_user` = '$ku[id]' AND `id_fish` = '0' ")or die (mysql_error());

}

}elseif($ku['money']>=$plant['mcena']*$count_gg)
{
mysql_query("UPDATE `kolhoz_user` SET `money` = '".($ku['money']-$plant['mcena']*$count_gg)."' WHERE `id` = '$ku[id]'");
mysql_query("UPDATE `kolhoz_pool` SET `id_fish` = '$plant[id]', `time_soil` = '".($stime+$time)."', `time` = '".($times+$time)."' WHERE `id_user` = '$ku[id]' AND `id_fish` = '0' ")or die (mysql_error());

}


}
}
}

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

?> 