<?php

$title = "Кампания";
include_once 'protected/sys.php';

/*
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', 1);
*/
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
function vvod($vvod){GLOBAL $base; return trim(mysqli_real_escape_string($base,$vvod));}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
function vivod($vivod){return trim(htmlspecialchars(stripslashes($vivod)));}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
function num($chislo){return abs(intval($chislo));}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
function go($url){header("Location: ".$url); exit;}


$user=$u;$time=time();
$arena= mysqli_fetch_assoc(mysqli_query($base,"select * from `camp` WHERE `id_user`='".num($user['id'])."' and `status`!=0 limit 1"));
$last=mysqli_fetch_assoc(mysqli_query($base,"SELECT id,id_mob  FROM `camp` WHERE `id_user`='".num($user['id'])."' order by `id_mob` asc"));$tip=num($last['id_mob']/10)+1;
if($arena['id']==0){
if(!isset($_GET['id'])){
$kol_lair= mysqli_num_rows(mysqli_query($base,"select id from `camp` WHERE `id_user`='".num($user['id'])."' "));
if($kol_lair==0){
mysqli_query($base,"INSERT INTO `camp` (`id_user`,`id_mob`,`name`,`lvl`,`att`,`deff`,`hp_vrag`,`hp_vrag_max`)
VALUES('".num($user['id'])."','1','М-1','1','50','50','50','50')");

mysqli_query($base,"INSERT INTO `camp` (`id_user`,`id_mob`,`name`,`lvl`,`att`,`deff`,`hp`,`hp_max`)
VALUES('".num($user['id'])."','2','М-2','1','70','70','70','70')");
go("/camp");
}


$glava = array( '1' =>'ГИБЛЫЕ ТОПИ', '2' =>'В разработке', '3' =>'Г-3');
$name = array( '1' =>'Ракшас', '2' =>'Ведьма', '3' =>'Сатир', '4' =>'Древо', '5' =>'Лучник', '6' =>'Козерог', '7' =>'Водяной', '8' =>'Бурак', '9' =>'Бурый медведь', '10' =>'Королева леса', '11' =>'М-11', '12' =>'М-12');

if(isset($_SESSION['pokaz'])){echo''.$_SESSION['pokaz'].'';unset($_SESSION['pokaz']);}

echo'
<script type="text/javascript" src="/js/camp.js"></script>
<div class="bg_bosses_my">
<div class="ribbon ttu bold ibi mt5 "><div class="r_end"><div class="r_cntr">ГЛАВА '.$tip.': '.$glava[''.$tip.''] .'</div></div></div>
<div class="mlra mb10 pb10 small">Пройдено боссов '.($last['id_mob']).' из 10</div>';

$q=mysqli_query($base,"SELECT *  FROM `camp` WHERE `id_user`='".num($user['id'])."'  order by `id_mob` desc ");
while($r = mysqli_fetch_array($q))
{

echo'
<div class="h175x">
<div class="tptrn ib mlrf"><div class="p_end">'.$name[''.$r['id_mob'].''].'</div></div>
<div class="mt5 mb10"><span class="w50x ib vt right ml30"></span>
<span class="sqr_img3" style="background-image:url(/images/camp/'.$r['id_mob'].'_ico.png);"> 
<span class="si_frame_bosses"></span></span>
</div>';
if($r['time']>$time){echo'<span class="w80x ib vt"></span><span id="timer_camp_'.$r['id'].'"> '.tl($r['time']-$time).'</span>
<script type="text/javascript" language="javascript">timer('.num($r['time'] - time()).','."'timer_camp_".$r['id']."'".','.$r['time'].','.$time.')</script>';}else{echo'<br/><a class="s_a" href="/camp?id='.$r['id'].'">Напасть</a>';}

echo'</div>';

}


echo'</div>';
}else{
$id=num($_GET['id']);
$lair_p= mysqli_fetch_assoc(mysqli_query($base,"select * from `camp` WHERE `id`='$id' and `id_user`='".num($user['id'])."' and `status`=0 and `time`<$time "));
if($lair_p['id']!=0){
$stat = array( 
array('att' => 70, 'deff' =>50, 'hp' => 70),
array('att' => 100, 'deff' =>60, 'hp' => 100),
array('att' => 150, 'deff' =>80, 'hp' => 150),
array('att' => 200, 'deff' =>100, 'hp' => 200),
array('att' => 300, 'deff' =>150, 'hp' => 300),
array('att' => 400, 'deff' =>250, 'hp' => 700),
array('att' => 450, 'deff' =>350, 'hp' => 1450),
array('att' => 550, 'deff' =>450, 'hp' => 1550),
array('att' => 700, 'deff' =>600, 'hp' => 1750),
array('att' => 900, 'deff' =>750, 'hp' => 2000)
        );

if(!isset($stat[''.($lair_p['id_mob']-1).''])){$_SESSION['pokaz']='<div class="c1 center">Монстр добавляется, ожидайте.</div>';go("/camp");}
mysqli_query($base,"UPDATE `camp` SET `name`='".vvod($name[''.$lair_p['id_mob'].''])."',`hp`='".get_health($u['id'])."',`hp_max`='".get_health($u['id'])."',`att`='".num($stat[''.($lair_p['id_mob']-1).'']['att'])."',`deff`='".num($stat[''.($lair_p['id_mob']-1).'']['deff'])."',`hp_vrag`='".num($stat[''.($lair_p['id_mob']-1).'']['hp'])."',`hp_vrag_max`='".num($stat[''.($lair_p['id_mob']-1).'']['hp'])."',`status`='1',`att_user`='".get_power($u['id'])."',`deff_user`='".get_block($u['id'])."'  WHERE `id`='$id'");

}
go("/camp");
}



}else{

if($arena['hp_vrag']>0 and $arena['hp']>0){
$hp_proc_v=($arena['hp_vrag']/$arena['hp_vrag_max'])*100;$hp_proc_u=($arena['hp']/$arena['hp_max'])*100;
if($hp_proc_v<0){$hp_proc_v=0;}if($hp_proc_u<0){$hp_proc_u=0;}

/*
$arrow_cd = $myPlayer->arrow_cd-time();
$heal_cd = $myPlayer->heal_cd-time();
*/

echo'<script type="text/javascript" src="/js/camp.js"></script>
<div>

<div class="hp_proc_block_s"><div style="border-bottom:1px #191919 solid;" class="hp_proc_block"><div style="margin:0 55px 0 0;" ><div id="hp_proc_block2_v"  class="hp_proc_block2" style="width:'.$hp_proc_v.'%;"></div></div></div></div>

<div style="background-repeat: no-repeat;background-image: url(/images/camp/boi_bg.png);background-position: 50% 50% ;padding:33% 0 33% 0;position:relative;z-index:2;">
<div class="text_boi_vrag right">
<div style="margin:-17px 0 -10px 0;" class="float-right ico_small"><img  style="margin:0px;" id="img_vrag" width="60px" src="/images/camp/'.$arena['id_mob'].'_small.png" alt="" /></div>
<div class="boi_name_vrag" id="name_vrag_b">'.vivod($arena['name']).'</div>
<div id="stats_vrag">
<span class="text_boi_stats">'.$arena['att'].' <img width="22px" id="img_att_v" src="/images/str.png" alt="" /></span>
<span class="text_boi_stats">'.$arena['deff'].' <img width="22px" id="img_deff_v" src="/images/def.png" alt="" /></span>
<span class="text_boi_stats"><span id="hp_small_v">'.$arena['hp_vrag'].'</span> <img width="22px" id="img_hp_v"  src="/images/health.png" alt="" /></span>
</div>
</div>
<div style="position:absolute;bottom:15%;left:0px;width:35%;">
<div class="hp_proc_block_small"><div class="hp_proc_block2_small" id="hp_proc_block2_small_u" style="width:'.$hp_proc_u.'%;"></div></div>
<img width="100%" src="/images/camp/user_big.png" alt="" />
<div class="hp_del2" style="display:none;" id="hp_del_u"></div>
<div class="hp_isc" style="display:none;" id="hp_isc_u"></div>
</div>

<div style="position:absolute;bottom:15%;right:0px;width:35%;">
<div class="hp_proc_block_small"><div class="hp_proc_block2_small" id="hp_proc_block2_small_v"  style="width:'.$hp_proc_v.'%;"></div></div>
<img width="100%" src="/images/camp/'.$arena['id_mob'].'.png" alt="" />
<div class="hp_del2" style="display:none;" id="hp_del_v"></div>
</div>

<div class="text_boi_user">
<img  id="img_user" style="margin:0px 0 -17px 0;" width="70px" class="float-left" src="/images/camp/user.png" alt="" />
<div class="boi_name_user" id="name_user_b">Вы</div>
<div id="stats_user">
<span class="text_boi_stats">'.$arena['att_user'].' <img width="22px" id="img_att_u" src="/images/str.png" alt="" /></span>
<span class="text_boi_stats">'.$arena['deff_user'].' <img width="22px" id="img_deff_u" src="/images/def.png" alt="" /></span>
<span class="text_boi_stats"><span id="hp_small_u">'.$arena['hp'].'</span> <img width="22px" id="img_hp_u" src="/images/health.png" alt="" /></span>
</div>

</div>

</div>

<div class="hp_proc_block_s" style="padding:0px 0 1px 0;"><div class="hp_proc_block"><div style="margin:0 0 0 55px;"><div id="hp_proc_block2_u" class="hp_proc_block2" style="width:'.$hp_proc_u.'%;"></div></div></div></div>
<br/>
<div class="center" style="margin:0 0 10px 0;"><a  class="s_a" href="javascript:attack_camp('."'/js/func.php?m=camp'".');">Атаковать <span class="t_norm" id="time_att"></span></a></div>
<div class="center">
<div class  ="exam"><a href="javascript:moln('."'/js/func.php?m=moln_camp'".');"> <img width="50px" src="/images/skills/2.png"><div class="exam_text"><center><span id="time_isc">'.($arena['navik_pr2']> $time ? ''.($arena['navik_pr2']-$time).'</center>' : '').' </span></div> </a> </div> 
<div class  ="exam"><a href="javascript:isc('."'/js/func.php?m=isc_camp'".');"> <img width="50px" src="/images/skills/1.png"><div class="exam_text"><center><span id="time_moln">'.($arena['navik_pr1'] > $time ? ''.($arena['navik_pr1']-$time).'' : '').'</span></center></div></a></div>

</div>
<div class="log_boi" id="log_boi">
</div>

<script type="text/javascript" language="javascript">
var hp_user='.$arena['hp'].'; 
var hp_vrag='.$arena['hp_vrag'].';
var hp_user_max='.$arena['hp_max'].'; 
var hp_vrag_max='.$arena['hp_vrag_max'].';
var time_serv='.$time.';
var time_att='.$arena['time_att'].';
var time_log='.$time.';
var time_navik1='.$arena['navik_pr1'].';
var time_navik2='.$arena['navik_pr2'].';
if(hp_vrag==0 || hp_user==0){window.location = "/camp";}
var timer_se = window.setInterval("runServerTimeTimer_camp()", 1000);

</script>


';

}else{

if($arena['hp']==0){$opit=num($arena['id_mob']+20);$money=num($arena['id_mob']+2);$gold=0;
mysqli_query($base,"UPDATE `camp` SET `status`='0', `time`='".num($time+45*60)."'  WHERE `id`='".num($arena['id'])."'");

$result = 'Поражение! Награда: '.$opit.' опыта и '.$money.' серебра!';

}else{
$nagrada = array( 
array('opit' => 120,'money' => 500,'item' => 2),
array('opit' => 250,'money' => 1000,'item' => 2),
array('opit' => 450,'money' => 2500,'item' => 6),
array('opit' => 600,'money' => 4000,'item' => 6),
array('opit' => 750,'money' => 6000,'item' => 6),
array('opit' => 1000,'money' => 7500,'item' => 6),
array('opit' => 1250,'money' => 8500,'item' => 6),
array('opit' => 1500,'money' => 9250,'item' => 6),
array('opit' => 1750,'money' => 10000,'item' => 15),
array('opit' => 2000,'money' => 12000,'item' => 15)

        );


$randItem = $db->query("SELECT * FROM `subject` WHERE `level`< '".$nagrada[''.($arena['id_mob']-1).'']['item']."' ORDER BY RAND() LIMIT 1")->fetch_object();

 $db->query("INSERT INTO `inventory` (`user`,`item`,`type`,`buytime`) VALUES ('".num($user['id'])."', '".$randItem->id."', '".$randItem->clothing_type."', '".time()."')");
$opit=num($nagrada[''.($arena['id_mob']-1).'']['opit']);$money=num($nagrada[''.($arena['id_mob']-1).'']['money']);$gold=$arena['id_mob']*1+2;

$last=mysqli_fetch_assoc(mysqli_query($base,"SELECT id,id_mob  FROM `camp` WHERE `id_user`='".num($user['id'])."' order by `id_mob` desc"));
if($arena['id_mob']==$last['id_mob']){mysqli_query($base,"UPDATE `camp` SET `id_mob`=`id_mob`+1,`status`='0'  WHERE `id_user`='".num($user['id'])."'");}
else{mysqli_query($base,"UPDATE `camp` SET `status`='0', `time`='".num($time+45*60)."'  WHERE `id`='".num($arena['id'])."'");}
$result = 'Победа! Награда: '.$gold.' гемов, '.$opit.' опыта , '.$money.' серебра!<br/><img   src="'.$randItem->images.'" alt="" />';
}
mysqli_query($base,"UPDATE `users` SET `gold`=`gold`+'$gold',`silver`=`silver`+'$money',`exp`=`exp`+'$opit' WHERE `id`='".num($user['id'])."'  limit 1 ");

echo'<div class="b center"><div>'.$result.'</div><br/><a class="s_a" href="/camp">Далее</a>
</div>
';

}

}
include_once 'protected/footermain';
?>