<?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 `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'/var/www/test/data/www/vferme.com/inc/functions.php';
include'/var/www/test/data/www/vferme.com/inc/tehnika.php';
if($ku['expdouble']==date("D"))$dexp=1;

////////////////////////////Собрать все////////////
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_yastreb2_act=0;
$count_cellar_act=0;
if(isset($harvester2_act))$count_harvester2_act=floor(($count/100)*$harvester2_act);
//if(isset($yastreb2_act))$count_yastreb2_act=(($count/100)*300);
if(isset($cellar_act))$count_cellar_act=(($count/100)*$cellar_ps);
//$count=$count_harvester2_act+$count_yastreb2_act+$count+$count_greenhouse_act;
$count=$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(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(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+$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).' сек.';

?>