<?
$gos=mysql_fetch_array(mysql_query("SELECT * FROM `kolhoz_collective_gos` WHERE `active` = '1' ORDER BY `time` DESC LIMIT 1"));

if($gos['time']<=$time)
{
$q=mysql_query("SELECT * FROM `kolhoz_collective` WHERE (`rating_gos` > '5000000' OR `rating_gos` = '5000000') ORDER by `rating_gos` DESC");
$num=0;
while($post=mysql_fetch_array($q))
{
$num++;
if($num==1)$prize=2000;elseif($num==2)$prize=1000;elseif($num==3)$prize=500;else $prize=0;
$oth=floor(($post['rating_gos']-5000000)/1000000);
$other=100+$oth;
if($other>500)$other=500;
$rubies=$prize+$other;
mysql_query("UPDATE `kolhoz_collective` SET `rubies` = '".($post['rubies']+$rubies)."', `rating_gos` = '0' WHERE `id` = '$post[id]'");
mysql_query("INSERT INTO `kolhoz_collective_gos_ct` SET `id_collective` = '$post[id]', `id_gos` = '$gos[id]', `count` = '$rubies'");
mysql_query("UPDATE `kolhoz_collective_user` SET `rating_gos` = '0' WHERE `id_collective` = '$post[id]'");
}
mysql_query("UPDATE `kolhoz_collective_gos` SET `active` = '0' WHERE `id` = '$gos[id]'");
mysql_query("DELETE FROM `kolhoz_collective_rating_gos`");
mysql_query("UPDATE `kolhoz_collective` SET `rating_gos` = '0'");
mysql_query("UPDATE `kolhoz_collective_user` SET `rating_gos` = '0'");
mysql_query("UPDATE `kolhoz_setting` SET `gos_look` = '0'");
//////////////////////Создание нового госа /////////////////////////////
$last_gos=mysql_fetch_array(mysql_query("SELECT * FROM `kolhoz_collective_gos` WHERE `active` = '0' ORDER BY `time` DESC LIMIT 1"));
$plant_gos=mysql_fetch_array(mysql_query("SELECT * FROM `kolhoz_plant` WHERE `id` != '$last_gos[id_plant]' ORDER BY rand() LIMIT 1"));
$type='rating';
$rand=rand(1,9);
if($rand==3 || $rand==6 || $rand==9)$type='harvest';
mysql_query("INSERT INTO `kolhoz_collective_gos` SET `time` = '".($time+((3600*24*3)+(rand(1,9)*60)))."', `id_plant` = '$plant_gos[id]', `type` = '$type', `active` = '1'");
$gos=mysql_fetch_array(mysql_query("SELECT * FROM `kolhoz_collective_gos` WHERE `active` = '1' ORDER BY `time` DESC LIMIT 1"));
mysql_query("INSERT INTO `kolhoz_collective_comp` SET `time` = '$time', `id_gos` = '$gos[id]'");
$comp=mysql_fetch_array(mysql_query("SELECT * FROM `kolhoz_collective_comp` WHERE `id` = '".mysql_insert_id()."'"));
$q=mysql_query("SELECT * FROM `kolhoz_user` WHERE `goscomp` = '1' ORDER BY `goscomp_time` ASC");
$num=0;
$num2=0;
while($post=mysql_fetch_array($q))
{
if($post['goscomp_plant']==$gos['id_plant'])
{
$num2++;
if($num2==1)$prize=500;elseif($num2==2)$prize=250;elseif($num2==3)$prize=100;else $prize=50;
}
else
{
$prize=10;
}
$rubies=$prize;
mysql_query("UPDATE `kolhoz_user` SET `rubies` = '".($post['rubies']+$rubies)."', `goscomp` = '0' WHERE `id` = '$post[id]'");
mysql_query("INSERT INTO `kolhoz_collective_comp_user` SET `id_user` = '$post[id]', `id_comp` = '$comp[id]', `count` = '$rubies'");
}
}

?>