<?

$fruts=unserialize($conf['battle_plant_atak']);
$fruts_kol=unserialize($conf['count_plant_atak']);
if(empty($conf['battle_tomato'])){$rand_keys = array_rand($fruts, 1);$conf['battle_tomato']=$fruts[$rand_keys]; mysql_query("UPDATE `kolhoz_setting` SET `battle_tomato`='$fruts[$rand_keys]' WHERE `id_user`='$ku[id]' LIMIT 1");
}
$ukey = array_search(''.$conf['battle_tomato'].'', $fruts);
if(!isset($conf['battle_tomato']))echo 'error';
////////////Переменная
$aren=mysql_fetch_array(mysql_query("SELECT * FROM `kolhoz_arena_batle` WHERE `id` = '$conf[battle_arena]'"));
$oponi=mysql_fetch_array(mysql_query("SELECT * FROM `kolhoz_setting` WHERE `id_user` != '$ku[id]' AND `battle_arena`='$conf[battle_arena]'"));
if(!isset($oponi['id_user'])){echo 'error';exit;}
$ofruts=unserialize($oponi['battle_plant_atak']);
$ofruts_kol=unserialize($oponi['count_plant_atak']);
if(empty($oponi['battle_tomato'])){$rand_keys = array_rand($ofruts, 1);$oponi['battle_tomato']=$ofruts[$rand_keys]; mysql_query("UPDATE `kolhoz_setting` SET `battle_tomato`='$ofruts[$rand_keys]' WHERE `id_user`='$oponi[id_user]' LIMIT 1");
}
$okey = array_search(''.$oponi['battle_tomato'].'', $ofruts);
$cunt_log = mysql_result(mysql_query("SELECT COUNT(*) FROM `kolhoz_arena_log` WHERE `id_arena` = '$conf[battle_arena]'"),0);
if($cunt_log==0)$ktime=5; else $ktime=0;
$kol_sob=floor(($time-($aren['time']+$ktime))/10);
if($kol_sob>30)$kol_sob=30;
if($kol_sob>0){
if($cunt_log==0)$aren['time']=$aren['time']+15;
$i=0;
$uplant=mysql_fetch_array(mysql_query("SELECT * FROM `kolhoz_plant` WHERE `id` = '$conf[battle_tomato]'"));
$oplant=mysql_fetch_array(mysql_query("SELECT * FROM `kolhoz_plant` WHERE `id` = '$oponi[battle_tomato]'"));$oshark=mysql_fetch_array(mysql_query("SELECT `time_shark` FROM `kolhoz_user` WHERE `id`='$oponi[id_user]'"));
if($uplant['sila']==0)$uplant['sila']=100;
if($oplant['sila']==0)$oplant['sila']=100;
if($ku['time_shark']>$time)$uplant['sila']=floor($uplant['sila']+($uplant['sila']/10));
if($oshark[0]>$time)$oplant['sila']=floor($oplant['sila']+($oplant['sila']/10));

While($i<$kol_sob && count($fruts)>0 && count($ofruts)>0 && $oponi['battle_hp']>0 && $conf['battle_hp']>0){
if($cunt_log==0)$aren['time']=$aren['time']; else $aren['time']=$aren['time']+10;
$count_rate=$conf['battle_count_rate'];
$count_rate_opini=$oponi['battle_count_rate'];
//Мой действие  
$fr=$fruts_kol[$ukey];
$fruts_kol[''.$ukey.'']--; 
if($fr<=0){unset($fruts_kol[$ukey],$fruts[$ukey]);sort($fruts); sort($fruts_kol);
if(count($fruts)>=1){
$rand_keys = array_rand($fruts, 1);
$conf['battle_tomato']=$fruts[$rand_keys];
$uplant=mysql_fetch_array(mysql_query("SELECT * FROM `kolhoz_plant` WHERE `id` = '$conf[battle_tomato]'"));
$ukey = array_search(''.$conf['battle_tomato'].'', $fruts); 
mysql_query("UPDATE `kolhoz_setting` SET `battle_tomato`='$fruts[$rand_keys]' WHERE `id_user`='$ku[id]' LIMIT 1");}
}
unset($fr);
//

////Sila
if($uplant['type']==1 || $uplant['type']==2){
$kr_rand=rand(0,100);
$krit=0;
if($conf['arena_kr']*3>=$kr_rand){$krit=floor($uplant['sila']/2);}
$sila_uron=floor(($uplant['sila']/100)*$conf['arena_sila']*8);
$bonus_uron=floor(($uplant['sila']/100)*rand(-5,20));
$body=$oponi['arena_body']*4;
$uron=$uplant['sila']+$sila_uron+$krit+$bonus_uron-$body;
if($uron<0)$uron=0;
$conf['battle_count_rate']++;
$oponi['battle_hp']=$oponi['battle_hp']-$uron;
mysql_query("UPDATE `kolhoz_setting` SET `battle_hp`='$oponi[battle_hp]' WHERE `id_user`='$oponi[id_user]' LIMIT 1");
mysql_query("UPDATE `kolhoz_setting` SET `battle_count_rate`='$conf[battle_count_rate]' WHERE `id_user`='$ku[id]' LIMIT 1");

mysql_query("INSERT INTO `kolhoz_arena_log` SET `time` = '$aren[time]', `type` = '1',`krit`='".($krit>0?"1":"0")."',`plant`='$uplant[id]',`id_user`='$ku[id]',`id_who`='$oponi[id_user]]',`id_arena`='$aren[id]',`sila`='$uron'");
//////////Контр атака
if(rand(0,100)<$oponi['arena_body']+10){
$curon=$oponi['arena_body']*4+floor($uron/10);
if($curon>$uron/2)$curon=floor($uron/2);
$conf['battle_hp']=$conf['battle_hp']-$curon;
mysql_query("UPDATE `kolhoz_setting` SET `battle_hp`='$conf[battle_hp]' WHERE `id_user`='$ku[id]' LIMIT 1");
mysql_query("INSERT INTO `kolhoz_arena_log` SET `time` = '$aren[time]', `type` = '2',`krit`='0',`plant`='$uplant[id]',`id_user`='$oponi[id_user]',`id_who`='$ku[id]',`id_arena`='$aren[id]',`sila`='$curon'");
}
}//
if($uplant['type']==3 || $uplant['type']==4){
$kr_rand=rand(0,100);
$krit=0;
if($conf['arena_kr']*3>=$kr_rand){$krit=floor($uplant['sila']/2);}
$sila_uron=floor(($uplant['sila']/100)*$conf['arena_sila']*8);
$bonus_uron=floor(($uplant['sila']/100)*rand(-5,20));
$uron=$uplant['sila']+$sila_uron+$krit+$bonus_uron;
if($uron<0)$uron=0;
$conf['battle_count_rate']++;
$conf['battle_hp']=$conf['battle_hp']+$uron;
mysql_query("UPDATE `kolhoz_setting` SET `battle_hp`='$conf[battle_hp]',`battle_count_rate`='$conf[battle_count_rate]' WHERE `id_user`='$ku[id]' LIMIT 1");
mysql_query("INSERT INTO `kolhoz_arena_log` SET `time` = '$aren[time]', `type` = '3',`krit`='".($krit>0?"1":"0")."',`plant`='$uplant[id]',`id_user`='$ku[id]',`id_who`='$ku[id]',`id_arena`='$aren[id]',`sila`='$uron'");
}
$count_rate++;
//Действие врага
$fr=$ofruts_kol[$okey];
$ofruts_kol[''.$okey.'']--; 
if($fr<=0){unset($ofruts_kol[$okey],$ofruts[$okey]);sort($ofruts); sort($ofruts_kol);
if(count($ofruts)>=1){
$rand_keys = array_rand($ofruts, 1);
$oponi['battle_tomato']=$ofruts[$rand_keys];
$oplant=mysql_fetch_array(mysql_query("SELECT * FROM `kolhoz_plant` WHERE `id` = '$oponi[battle_tomato]'"));
$okey = array_search(''.$oponi['battle_tomato'].'', $ofruts); 
mysql_query("UPDATE `kolhoz_setting` SET `battle_tomato`='$ofruts[$rand_keys]' WHERE `id_user`='$oponi[id_user]' LIMIT 1");}
}
unset($fr);
////Sila
if($oplant['type']==1 || $oplant['type']==2){
$kr_rand=rand(0,100);
$krit=0;
if($oponi['arena_kr']*3>=$kr_rand){$krit=floor($oplant['sila']/2);}
$sila_uron=floor(($oplant['sila']/100)*$oponi['arena_sila']*8);
$bonus_uron=floor(($oplant['sila']/100)*rand(-5,20));
$body=$conf['arena_body']*4;
$uron=$oplant['sila']+$sila_uron+$krit+$bonus_uron-$body;
if($uron<0)$uron=0;
$oponi['battle_count_rate']++;
$conf['battle_hp']=$conf['battle_hp']-$uron;
mysql_query("UPDATE `kolhoz_setting` SET `battle_hp`='$conf[battle_hp]' WHERE `id_user`='$ku[id]' LIMIT 1");
mysql_query("UPDATE `kolhoz_setting` SET `battle_count_rate`='$oponi[battle_count_rate]' WHERE `id_user`='$oponi[id_user]' LIMIT 1");
mysql_query("INSERT INTO `kolhoz_arena_log` SET `time` = '$aren[time]', `type` = '1',`krit`='".($krit>0?"1":"0")."',`plant`='$oplant[id]',`id_user`='$oponi[id_user]',`id_who`='$ku[id]',`id_arena`='$aren[id]',`sila`='$uron'");
//////////Контр атака
if(rand(0,100)<$conf['arena_body']+10){
$curon=$conf['arena_body']*4+floor($uron/10);
if($curon>$uron/2)$curon=floor($uron/2);
$oponi['battle_hp']=$oponi['battle_hp']-$curon;
mysql_query("UPDATE `kolhoz_setting` SET `battle_hp`='$oponi[battle_hp]' WHERE `id_user`='$oponi[id_user]' LIMIT 1");
mysql_query("INSERT INTO `kolhoz_arena_log` SET `time` = '$aren[time]', `type` = '2',`krit`='0',`plant`='$oplant[id]',`id_user`='$ku[id]',`id_who`='$oponi[id_user]',`id_arena`='$aren[id]',`sila`='$curon'");
}
}//
if($oplant['type']==3 || $oplant['type']==4){
$kr_rand=rand(0,100);
$krit=0;
if($oponi['arena_kr']*3>=$kr_rand){$krit=floor($oplant['sila']/2);}
$sila_uron=floor(($oplant['sila']/100)*$oponi['arena_sila']*8);
$bonus_uron=floor(($oplant['sila']/100)*rand(-5,20));
$uron=$oplant['sila']+$sila_uron+$krit+$bonus_uron;
if($uron<0)$uron=0;
$oponi['battle_count_rate']++;
$oponi['battle_hp']=$oponi['battle_hp']+$uron;
mysql_query("UPDATE `kolhoz_setting` SET `battle_hp`='$oponi[battle_hp]',`battle_count_rate`='$oponi[battle_count_rate]' WHERE `id_user`='$oponi[id_user]' LIMIT 1");
mysql_query("INSERT INTO `kolhoz_arena_log` SET `time` = '$aren[time]', `type` = '3',`krit`='".($krit>0?"1":"0")."',`plant`='$oplant[id]',`id_user`='$oponi[id_user]',`id_who`='$oponi[id_user]',`id_arena`='$aren[id]',`sila`='$uron'");

}

//

mysql_query("UPDATE `kolhoz_arena_batle` SET `time`='$aren[time]' WHERE `id`='$aren[id]' LIMIT 1");
$i++;
}
}
mysql_query("UPDATE `kolhoz_setting` SET `battle_plant_atak`='".(serialize($fruts))."',`count_plant_atak`='".(serialize($fruts_kol))."' WHERE `id_user`='$ku[id]' LIMIT 1");
mysql_query("UPDATE `kolhoz_setting` SET `battle_plant_atak`='".(serialize($ofruts))."',`count_plant_atak`='".(serialize($ofruts_kol))."' WHERE `id_user`='$oponi[id_user]' LIMIT 1");

///Победа

if($conf['battle_hp']>0 AND $oponi['battle_hp']<=0 AND empty($conf['battle_result'])){
if($ku['level']>=20)$exp_kk=1;
if($ku['level']>=35)$exp_kk=2;
if($ku['level']>=45)$exp_kk=3;
$rat=$conf['battle_count_rate']*2;
//mysql_query("UPDATE `kolhoz_arena_batle` SET `status` = '2', `winlock`='$ku[id]' WHERE `id`='$conf[battle_arena]' LIMIT 1");
mysql_query("UPDATE `kolhoz_setting` SET `battle_count_rate`='0',`battle_tomato`='NULL',`count_plant_atak`='',`battle_plant_atak`='',`battle_hp`='NULL',`battle_arena`='NULL',`arena_exp`='".($conf['arena_exp']+$exp_kk)."',`arena_win`='".($conf['arena_win']+1)."',`battle_exit_rate`='$rat',`battle_result`='win' WHERE `id_user`='$ku[id]' LIMIT 1");
mysql_query("UPDATE `kolhoz_user` SET `arena_rating` = '".($ku['arena_rating']+$rat)."' WHERE `id`='$ku[id]' LIMIT 1");
///Возвращение овощей
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());
}
///
$oponirat=mysql_fetch_array(mysql_query("SELECT `arena_rating` FROM `kolhoz_user` WHERE `id`='$oponi[id_user]'"));

mysql_query("UPDATE `kolhoz_setting` SET `battle_count_rate`='0',`battle_tomato`='NULL',`count_plant_atak`='',`battle_plant_atak`='',`battle_hp`='NULL',`battle_arena`='NULL',`arena_win`='".($oponi['arena_win']+1)."',`battle_exit_rate`='".(round($oponi['battle_count_rate']*1))."',`battle_result`='lose' WHERE `id_user`='$oponi[id_user]' LIMIT 1");
$rat=round($oponirat[0]-$oponi['battle_count_rate']*1);
if($rat<0)$rat=0;
mysql_query("UPDATE `kolhoz_user` SET `arena_rating` = '$rat' WHERE `id`='$oponi[id_user]' LIMIT 1");
header("Location: /arena/list/result");
}
///Проигрыш
if($conf['battle_hp']<=0 AND $oponi['battle_hp']>0 AND empty($conf['battle_result'])){
//mysql_query("UPDATE `kolhoz_arena_batle` SET `status` = '2', `loselock`='$ku[id]' WHERE `id`='$conf[battle_arena]' LIMIT 1");
mysql_query("UPDATE `kolhoz_setting` SET `battle_count_rate`='0',`battle_tomato`='NULL',`count_plant_atak`='',`battle_plant_atak`='',`battle_hp`='NULL',`battle_arena`='NULL',`arena_win`='".($conf['arena_win']+1)."',`battle_exit_rate`='".(round($conf['battle_count_rate']*1))."',`battle_result`='lose' WHERE `id_user`='$ku[id]' LIMIT 1");
$rat=round($ku['arena_rating']-$conf['battle_count_rate']*1);
if($rat<0)$rat=0;
mysql_query("UPDATE `kolhoz_user` SET `arena_rating` = '$rat' WHERE `id`='$ku[id]' LIMIT 1");
$oponirat=mysql_fetch_array(mysql_query("SELECT `arena_rating`,`level` FROM `kolhoz_user` WHERE `id`='$oponi[id_user]'"));
if($oponirat['level']>=20)$exp_kk=1;
if($oponirat['level']>=35)$exp_kk=2;
if($oponirat['level']>=45)$exp_kk=3;
$rat=$oponi['battle_count_rate']*2;
mysql_query("UPDATE `kolhoz_setting` SET `battle_count_rate`='0',`battle_tomato`='NULL',`count_plant_atak`='',`battle_plant_atak`='',`battle_hp`='NULL',`battle_arena`='NULL',`arena_exp`='".($oponi['arena_exp']+$exp_kk)."',`arena_win`='".($oponi['arena_win']+1)."',`battle_exit_rate`='$rat',`battle_result`='win' WHERE `id_user`='$oponi[id_user]' LIMIT 1");
mysql_query("UPDATE `kolhoz_user` SET `arena_rating` = '".($oponirat['arena_rating']+$rat)."' WHERE `id`='$oponi[id_user]' LIMIT 1");
///Возвращение овощей
for($i=0; $i<count($ofruts); $i++){
$inkol=mysql_fetch_array(mysql_query("SELECT `tachka` FROM `kolhoz_warehouse` WHERE `id_plant`='$ofruts[$i]' AND `id_user`='$oponi[id_user]'"));
mysql_query("UPDATE `kolhoz_warehouse` SET `tachka`='$inkol[0]'+'$ofruts_kol[$i]' WHERE `id_plant`='$ofruts[$i]' AND `id_user`='$oponi[id_user]'") or die (mysql_error());
}
///
header("Location: /arena/list/result");
}
///Ничья
if((count($ofruts)<=0 || count($fruts)<=0) || ($conf['battle_hp']<=0 && $oponi['battle_hp']<=0) && !empty($conf['battle_count_rate'])){
//mysql_query("UPDATE `kolhoz_arena_batle` SET `status` = '3', `loselock`='$ku[id]' WHERE `id`='$conf[battle_arena]' LIMIT 1");
$rat=$conf['battle_count_rate'];
$orat=$oponi['battle_count_rate'];
$oponirat=mysql_fetch_array(mysql_query("SELECT `arena_rating` FROM `kolhoz_user` WHERE `id`='$oponi[id_user]'"));

mysql_query("UPDATE `kolhoz_setting` SET `battle_count_rate`='0',`battle_tomato`='NULL',`count_plant_atak`='',`battle_plant_atak`='',`battle_hp`='NULL',`battle_arena`='NULL',`arena_win`='".($conf['arena_win']+1)."',`battle_exit_rate`='$rat',`battle_result`='drawn' WHERE `id_user`='$ku[id]' LIMIT 1");
mysql_query("UPDATE `kolhoz_setting` SET `battle_count_rate`='0',`battle_tomato`='NULL',`count_plant_atak`='',`battle_plant_atak`='',`battle_hp`='NULL',`battle_arena`='NULL',`arena_win`='".($oponi['arena_win']+1)."',`battle_exit_rate`='$orat',`battle_result`='drawn' WHERE `id_user`='$oponi[id_user]' LIMIT 1");

mysql_query("UPDATE `kolhoz_user` SET `arena_rating` = '".($ku['arena_rating']+$rat)."' WHERE `id`='$ku[id]' LIMIT 1");
mysql_query("UPDATE `kolhoz_user` SET `arena_rating` = '".($oponirat['arena_rating']+$orat)."' WHERE `id`='$oponi[id_user]' LIMIT 1");
header("Location: /arena/list/result");}
?>