 <?php
$t=microtime(1);

require_once'inc/start_sess.php';

$tt=600;
$k_post = mysql_query("SELECT * FROM `kolhoz_collective` WHERE `date_last` > '".(time()-$tt)."' ORDER BY `date_last` DESC");
while($mc=mysql_fetch_array($k_post))
{
include'inc/next_level_collective.php';
include'inc/station_act.php';
include'inc/count_cash.php';
echo"$mc[name] m=$meh_all<br/>";

}
//////////////////////////Турнир/Распределение призов////////////////////////////////
if(mysql_result(mysql_query("SELECT COUNT(*) FROM `kolhoz_turnir` WHERE `active` = '1' AND (`time`<'".(time())."' OR `time`='".(time())."')"),0)>0)
{
$turnir=mysql_fetch_array(mysql_query("SELECT * FROM `kolhoz_turnir` WHERE `active` = '1' ORDER by `id` DESC")); 
$q=mysql_query("SELECT `id` FROM `kolhoz_user` WHERE `active_turnir` = '1' AND `turnir_count`>'0' ORDER BY `turnir_count` DESC, `rating` DESC");
$num=1;
while($post=mysql_fetch_array($q))
{
$post=mysql_fetch_array(mysql_query("SELECT * FROM `kolhoz_user` WHERE `id` = '$post[id]' "));

if($num==1){$money=$turnir['money']; $rubies=$turnir['rubies']; $rat=$turnir['rating']; $timekey=$turnir['timekey'];}
else if($num==2){$money=round($turnir['money']/2); $rubies=round($turnir['rubies']/2); $rat=round($turnir['rating']/2); $timekey=round($turnir['timekey']/1.5);}
else if($num==3){$money=round($turnir['money']/4); $rubies=round($turnir['rubies']/4); $rat=round($turnir['rating']/4); $timekey=round($turnir['timekey']/3);}
else {$money=0; $rubies=0; $rat=round($post['level']*$turnir['cena']*$post['turnir_count']*1000); $timekey=0;}
if($num<4){mysql_query("UPDATE `kolhoz_user` SET `money` = '".($post['money']+$money)."',`rubies` = '".($post['rubies']+$rubies)."',`rating`='".($post['rating']+$rat)."' WHERE `id` = '$post[id]'")or die (mysql_error());
mysql_query("INSERT INTO `kolhoz_key` SET `id_user` = '$post[id]', `time` = '".($turnir['timekey']*86400)."' ") or die (mysql_error());
$msg='Турнир "'.$turnir['name'].'" окончен. Поздравляем вас, вы заняли '.$num.' место и выиграли [img]/images/icons/experience.png[/img] '.$rat.' [img]/images/icons/money.png[/img] '.$money.' [img]/images/icons/ruby.png[/img] '.$rubies.' [img]/images/icons/pkey.png[/img] '.$timekey.'. Посмотреть [url=turnir/st-'.$turnir['id'].']Результаты турнира[/url]';
mysql_query("INSERT INTO `kolhoz_mail` SET `id_user` = '$post[id]', `id_kont` = '0', `msg` = '$msg', `time` = '".time()."', `type` = 'to'") or die (mysql_error());
$gift=serialize(array(''.$rat.'',''.$money.'',''.$rubies.'',''.$timekey.''));
}
else {mysql_query("UPDATE `kolhoz_user` SET `rating` = '".($post['rating']+$rat)."' WHERE `id` = '$post[id]'");
$msg='Турнир "'.$turnir['name'].'" окончен. Поздравляем вас, вы заняли '.$num.' место и получили утешительный приз [img]/images/icons/experience.png[/img] '.$rat.'. Посмотреть [url=turnir/st-'.$turnir['id'].']Результаты турнира[/url]';
mysql_query("INSERT INTO `kolhoz_mail` SET `id_user` = '$post[id]', `id_kont` = '0', `msg` = '$msg', `time` = '".time()."', `type` = 'to'") or die (mysql_error());
$gift=$rat;
}
mysql_query("INSERT INTO `kolhoz_turnir_st` SET `id_user` = '$post[id]', `id_turnir` = '$turnir[id]', `mesto` = '$num', `count` = '$post[turnir_count]', `gift` = '$gift'") or die (mysql_error());
$num++;
}
mysql_query("UPDATE `kolhoz_user` SET `turnir_count` = '0' ,`active_turnir`='0'");
mysql_query("UPDATE `kolhoz_turnir` SET `active` = '0' WHERE `id`='$turnir[id]'");
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
echo '<br/> '.number_format(microtime(1)-$t,5).' сек';

$a=number_format(microtime(1)-$t,3);
$y = explode('.', $a);
echo '<br/><span> '.intval($y[1]).' ms.</span></div>';

?> 