<?
if($ku['level']<20)header("Location: /arena");
if(!empty($conf['battle_arena']) AND mysql_result(mysql_query("SELECT COUNT(*) FROM `kolhoz_arena_batle` WHERE `id` = '$conf[battle_arena]' AND `status`='1'"),0)!=0){header("Location: /arena/list");exit;}
if(empty($conf['battle_arena']) AND mysql_result(mysql_query("SELECT COUNT(*) FROM `kolhoz_arena_batle` WHERE `id_user` = '$ku[id]'"),0)!=0){
$aren = mysql_fetch_array(mysql_query("SELECT * FROM `kolhoz_arena_batle` WHERE `id_user`='$ku[id]'"));
mysql_query("DELETE FROM `kolhoz_arena_batle` WHERE `id`='$aren[id]'");
mysql_query("DELETE FROM `kolhoz_arena_log` WHERE `id_arena`='$aren[id]'");
}
if(isset($_GET['no_rate'])){
if(!empty($conf['battle_result'])){
////Велосипед
$aren = mysql_fetch_array(mysql_query("SELECT `id_arena` FROM `kolhoz_arena_log` WHERE `id_user`='$ku[id]' ORDER by `time` DESC LIMIT 1"));
$uaren = mysql_fetch_array(mysql_query("SELECT * FROM `kolhoz_arena_batle` WHERE `id`='$aren[0]'"));
if($uaren['locked']==0){
mysql_query("UPDATE `kolhoz_arena_batle` SET  `locked`='1' WHERE `id`='$aren[0]' LIMIT 1");
}else{$delete=1;}
mysql_query("UPDATE `kolhoz_setting` SET `battle_exit_rate`='NULL',`battle_result`='NULL' WHERE `id_user`='$ku[id]' LIMIT 1");
if(isset($delete)){
mysql_query("DELETE FROM `kolhoz_arena_batle` WHERE `id`='$aren[0]'");
mysql_query("DELETE FROM `kolhoz_arena_log` WHERE `id_arena`='$aren[0]'");
}
header("Location: /arena/bitva");exit;
}else{header("Location: /arena");}
}

if(isset($_GET['delet_bt']) && mysql_result(mysql_query("SELECT COUNT(*) FROM `kolhoz_arena_batle` WHERE `id` = '$conf[battle_arena]' AND `status`='0'"),0)!=0){
mysql_query("DELETE FROM `kolhoz_arena_batle` WHERE `id`='$conf[battle_arena]'");
mysql_query("DELETE FROM `kolhoz_arena_invite` WHERE `id_arena`='$conf[battle_arena]'");
$fruts=unserialize($conf['battle_plant_atak']);
$fruts_kol=unserialize($conf['count_plant_atak']);
///Возвращение овощей
for($i=0; $i<count($fruts); $i++){
$inkol=mysql_fetch_array(mysql_query("SELECT `tachka` FROM `kolhoz_warehouse` WHERE `id_plant`='$fruts[$i]' AND `id_user`='$ku[id]'"));
mysql_query("UPDATE `kolhoz_warehouse` SET `tachka`='$inkol[0]'+'$fruts_kol[$i]' WHERE `id_plant`='$fruts[$i]' AND `id_user`='$ku[id]'") or die (mysql_error());
}
///
mysql_query("UPDATE `kolhoz_setting` SET `battle_tomato`='NULL',`count_plant_atak`='',`battle_plant_atak`='',`battle_hp`='NULL',`battle_arena`='NULL' WHERE `id_user`='$ku[id]' LIMIT 1");
header("Location: /arena");
}

function fruits($count_en,$count_sila,$fruits1,$fruits2,$kol1,$kol2,$plant1,$plant2)
{
global $ku,$conf;

if($count_en>=8){$hp_randen=8; $ataka_rn=12;}
if($count_en<8){$hp_randen=$count_en; $ataka_rn=12+8-$count_en;}

$count_ataka_vt=count($fruits1);
$count_hp_vt=count($fruits2);

$fruits_ataka=array(); $kol_ataka=array(); $fruits_ataka_plant=array();
$fruits_hp=array(); $kol_hp=array(); $fruits_hp_plant=array();

While($ataka_rn>0){
$rr=$count_ataka_vt-1;
$random=rand(0,$rr);
if($kol1[$random]>=1){$kol1[$random]--;
$fr_rr=$fruits1[$random];
if (!in_array("$fruits1[$random]", $fruits_ataka)){array_push($fruits_ataka, "$fruits1[$random]");array_push($kol_ataka, "1");array_push($fruits_ataka_plant, "$plant1[$random]");} else {
$key = array_search(''.$fr_rr.'', $fruits_ataka); $kol_ataka[$key]++;
}
$ataka_rn--;}
}

While($hp_randen>0){
$rr=$count_hp_vt-1;
$random=rand(0,$rr);
if($kol2[$random]>=1){$kol2[$random]--;
$fr_rr=$fruits2[$random];
if (!in_array("$fr_rr", $fruits_hp)){array_push($fruits_hp, "$fr_rr");array_push($kol_hp, "1");array_push($fruits_hp_plant, "$plant2[$random]");} else {
$key = array_search(''.$fr_rr.'', $fruits_hp); $kol_hp[$key]++;
}
$hp_randen--;}
}

if(count($fruits_ataka)>=1){
$i=0;
While($i<count($fruits_ataka)){
$ff_rrr="$fruits_ataka[$i]";
$ff_kkk="$kol_ataka[$i]";
$kols=mysql_fetch_array(mysql_query("SELECT `tachka` FROM `kolhoz_warehouse` WHERE `id`='$ff_rrr' AND `id_user`='$ku[id]'"));
mysql_query("UPDATE `kolhoz_warehouse` SET `tachka`='".($kols['tachka']-$ff_kkk)."' WHERE `id`='$ff_rrr' AND `id_user`='$ku[id]' ");
$i++;
}
}
if(count($fruits_hp)>=1){
$i=0;
While($i<count($fruits_hp)){
$ff_rrr="$fruits_hp[$i]";
$ff_kkk="$kol_hp[$i]";
$kols=mysql_fetch_array(mysql_query("SELECT `tachka` FROM `kolhoz_warehouse` WHERE `id`='$ff_rrr' AND `id_user`='$ku[id]'"));
mysql_query("UPDATE `kolhoz_warehouse` SET `tachka`='".($kols['tachka']-$ff_kkk)."' WHERE `id`='$ff_rrr' AND `id_user`='$ku[id]' ");
$i++;
}
}
///Экономия//
if($conf['arena_econom']>0){
$i=0;
WHILE($i<$conf['arena_econom']){
$rand_keys = array_rand($kol_ataka, 1);
$kol_ataka[$rand_keys]++;
$i++;
}
}
/////////////
$all_frut = array_merge($fruits_ataka_plant,$fruits_hp_plant);
$all_kol = array_merge($kol_ataka,$kol_hp);
mysql_query("UPDATE `kolhoz_setting` SET `battle_plant_atak`='".(serialize($all_frut))."',`count_plant_atak`='".(serialize($all_kol))."' WHERE `id_user`='$ku[id]' LIMIT 1");

return "1";

}

$count_batle=mysql_result(mysql_query("SELECT COUNT(*) FROM `kolhoz_arena_batle` WHERE (`level`='$ku[level]' OR `level`='".($ku['level']-1)."' OR `level`='".($ku['level']+1)."') AND `status`='0'"),0);
if($count_batle==0){
$fruits1=array(); $kol1=array(); $plant1=array();
$fruits2=array(); $kol2=array(); $plant2=array();
$count_sila=0;
$count_en=0;
$q=mysql_query("SELECT * FROM `kolhoz_warehouse` WHERE `id_user` = '$ku[id]' AND `tachka` > '0' AND `fish` = '0'");
while($post=mysql_fetch_array($q))
{
$plant=mysql_fetch_array(mysql_query("SELECT * FROM `kolhoz_plant` WHERE `id` = '$post[id_plant]'"));
if($plant['level']<=$ku['level'] AND ($plant['type']==1 OR $plant['type']==2)){$count_sila=$count_sila+$post['tachka']; array_push($fruits1, "$post[id]");array_push($kol1, "$post[tachka]");array_push($plant1, "$post[id_plant]");}
if($plant['level']<=$ku['level'] AND ($plant['type']==3 OR $plant['type']==4)){$count_en=$count_en+$post['tachka'];array_push($fruits2, "$post[id]");array_push($kol2, "$post[tachka]");array_push($plant2, "$post[id_plant]");}
}

if($count_sila>=12 && $count_sila+$count_en>=20){
mysql_query("INSERT INTO `kolhoz_arena_batle` SET `id_user` = '$ku[id]', `level` = '$ku[level]'");
$arena_id=mysql_insert_id();
$xp = 500+(($ku['level']-20)*20)+$conf['arena_hp']*50;

//////////////////////Выбор овощей
fruits($count_en,$count_sila,$fruits1,$fruits2,$kol1,$kol2,$plant1,$plant2);
/////////////////////

mysql_query("UPDATE `kolhoz_setting` SET `battle_arena` = '$arena_id',`battle_hp`='$xp' WHERE `id_user`='$ku[id]' LIMIT 1");
}else{header("Location: /arena/many_fruit");}

}else{
if($count_batle>=1 && mysql_result(mysql_query("SELECT COUNT(*) FROM `kolhoz_arena_batle` WHERE `id_user` = '$ku[id]'"),0)==0 && empty($conf['battle_arena'])){

$fruits1=array(); $kol1=array(); $plant1=array();
$fruits2=array(); $kol2=array(); $plant2=array();

$count_sila=0;
$count_en=0;
$q=mysql_query("SELECT * FROM `kolhoz_warehouse` WHERE `id_user` = '$ku[id]' AND `tachka` > '0' AND `fish` = '0'");
while($post=mysql_fetch_array($q))
{
$plant=mysql_fetch_array(mysql_query("SELECT * FROM `kolhoz_plant` WHERE `id` = '$post[id_plant]'"));
if($plant['level']<=$ku['level'] AND ($plant['type']==1 OR $plant['type']==2)){$count_sila=$count_sila+$post['tachka']; array_push($fruits1, "$post[id]");array_push($kol1, "$post[tachka]");array_push($plant1, "$post[id_plant]");}
if($plant['level']<=$ku['level'] AND ($plant['type']==3 OR $plant['type']==4)){$count_en=$count_en+$post['tachka']; array_push($fruits2, "$post[id]");array_push($kol2, "$post[tachka]");array_push($plant2, "$post[id_plant]");}

}

if($count_sila>=12 && $count_sila+$count_en>=20){

$xp = 500+(($ku['level']-20)*20)+$conf['arena_hp']*50;

if(!isset($_GET['id'])){
$arren=mysql_fetch_array(mysql_query("SELECT * FROM `kolhoz_arena_batle` WHERE (`level`='$ku[level]' OR `level`='".($ku['level']-1)."' OR `level`='".($ku['level']+1)."') AND `status`='0' LIMIT 1"));
mysql_query("UPDATE `kolhoz_arena_batle` SET `status` = '1', `time` = '$time' WHERE `id`='$arren[id]' LIMIT 1");
} else {
$arren=mysql_fetch_array(mysql_query("SELECT * FROM `kolhoz_arena_batle` WHERE `id`='".(intval($_GET['id']))."'"));
if($arren['level']>=$ku['level']-1 AND $arren['level']<=$ku['level']+1 AND $arren['status']==0){
mysql_query("UPDATE `kolhoz_arena_batle` SET `status` = '1', `time` = '$time' WHERE `id`='".(intval($_GET['id']))."' LIMIT 1");
}else{header("Location: /arena");exit;}
}
fruits($count_en,$count_sila,$fruits1,$fruits2,$kol1,$kol2,$plant1,$plant2);
mysql_query("UPDATE `kolhoz_setting` SET `battle_arena` = '$arren[id]',`battle_hp`='$xp' WHERE `id_user`='$ku[id]' LIMIT 1");
mysql_query("DELETE FROM `kolhoz_arena_invite` WHERE `id_arena`='$arren[id]'");
}else{header("Location: /arena/many_fruit-$count_sila");}

header("Location: /arena/many_fruit");
}else{}
}
require_header ('Арена');
echo'<div class="content">
<div class="block">';
$q=mysql_query("SELECT * FROM `kolhoz_arena_batle` WHERE `id_user` = '$ku[id]' AND `status`='0'");
if(mysql_num_rows($q)>0){
if(!isset($_GET['online_bt'])){
echo'<li><img width="16" height="16" src="/images/icons/refresh.png" alt="*"/><a href="/arena/bitva/'.(rand(1000000,9999999)).'">Обновить</a></li>
<li>'.(on_k($ku['id'])).'<a href="/user/'.$ku['id'].'"> '.(nc($ku['id'])).'</a></li>
<li class="pt"><img src="/images/icons/arrow.png" alt="*"><a href="/arena/bitva/online_bt">Пригласить игроков</a></li>
<li><img src="/images/icons/cross.png" alt="*"><a href="/arena/bitva/delet_bt">Выйти</a></li>';
}else{
$tm=600;
$k_post = mysql_result(mysql_query("SELECT COUNT(*) FROM `kolhoz_user` WHERE `date_last` > '".(time()-$tm)."' AND  (`level`='$ku[level]' OR `level`='".($ku['level']-1)."' OR `level`='".($ku['level']+1)."')"),0);
if ($k_post==0)
{
echo "Список пуст...";
}
$k_page=k_page($k_post,$set['p_str']);
$page=page($k_page);
$start=$set['p_str']*$page-$set['p_str'];
$q=mysql_query("SELECT * FROM `kolhoz_user` WHERE  `date_last` > '".(time()-$tm)."' AND (`level`='$ku[level]' OR `level`='".($ku['level']-1)."' OR `level`='".($ku['level']+1)."')ORDER by `level` DESC LIMIT $start, $set[p_str]");
echo "<ul>";
if($page==1)$i=1;else$i=($page-1)*$set['p_str']+1;
while($post=mysql_fetch_array($q))
{
echo "<li><span><span class='minor'>$i. </span>";
echo "".on_k($post['id'])."";
echo " <a class='' href='/user/$post[id]'><span>".nc($post['id'])."</span></a> <span class='title'>$post[level]</span></span> <a class='epic' href='/arena_user/user-$post[id]'> пригласить</a></li>";$i++;
}
echo "</ul>";
if ($k_page>1){echo'<div class="pt"></div>'; new_str("arena/bitva/online_bt/",$k_page,$page);} // Вывод страниц

}
}

include_once 'inc/foot.php';
?>