<?php
 include './system/common.php';
    
 include './system/functions.php';
        
      include './system/user.php';
    
if(!$user) {

  header('location: /');
    
exit;

}
$title = 'Карьера';

if($user['start'] ==9){
          include './system/h.php';
}else{
      include './system/head.php';
}
$mesto = ($user['place']); 
$kiek = array(4, 3, 2, 1); //на сколько двигать мест при победе
$dmg = round(rand(($user['str']/2),($user['str']/4)));
$kiek2 = array(4, 3, 2, 1); //на сколько умножаем параметры, первый бот самый сильный
if($mesto <= 1){
$mesto2 =1; //если место меньше 10 то выводим одного бота
$kiek = array(1);
}else{
$mesto2 = 4; //скольео выводить игроков, если меняем, то и сдвиг по местам добавляем, так же пораметры ботов.
}
$powet = $user['str'] + $user['agi'] + $user['vit'] + $user['def'];
 
if($user['used']==5){

?>

<div class="frame"><div class="wr1"><div class="wr2"><div class="wr3"><div class="wr4"><div class="wr5"><div class="wr6"><div class="wr7"><div class="f_ttl yellow"><div class="fl">
Глава 1</div>&nbsp;</div><div class="f_pic_l"><div class="f_pic_r"><div class="f_sep"><div class="swr1"><div class="swr2 cntr p_sep"><div class="mb5">Турнир откроется через:<br><img src="/images/icon/clock.png" alt=""><span class="orange">8 ч</span></div>
<div class="yellow">Результат в прошедшем турнире: <span class="nwr"><?=$user['place'];?> место</span></div></div></div></div></div></div></div></div></div></div></div></div></div></div>
<?
}else{
?>
<div class="cntr blue mt5">Займи 1 место в турнире, чтобы открыть новую главу!</div>
<div class="frame mt5">
<div class="wr1"><div class="wr2"><div class="wr3"><div class="wr4"><div class="wr5"><div class="wr6"><div class="wr7"><div class="f_ttl yellow">
<div class="fl">Глава <?=$user['chapter'];?></div>&nbsp;</div>


<div class="f_pic_l"><div class="f_pic_r"><div class="f_sep"><div class="swr1"><div class="swr2 cntr p_sep"><div class="yellow mb5">Ты на <?=$user['place'];?> месте</div>
<a href="/career?attack=<?=($mesto - $kiek[$y]);?>" class="nbtn1 b_red w130p"><span class="lbl glow">Атаковать <?=$user['used']?>/5</span></a>
<table class="tlist sep mb5"><tbody><tr class="grey"><td class="nwr">Место</td><td class="w100">Имя</td><td class="nwr">Мощь</td></tr>

<?
if(isset($_GET['attack']) && explode('^[0-9]', $_GET['attack'])){
if($_GET['attack']==$mesto - $kiek[$y]){
 
     $dmg = 0;
    $opponent_dmg = 0;
    for($i = 1; $i < 2; $i++){
      $dmg += ceil(rand(($user['str'] / 6 ), ($user['str'] / 4 )));
      $crit = ((rand(1, 2) * ($user['agi'] / 100)) - (rand(1,2) * ($opponent['agi'] / 100)));
      if(mt_rand( 0, 100 ) <= $crit){
        $dmg *= 2;
      }
      $dodge = ((rand(1, 2) * ($opponent['agi'] / 100)) - (rand(1,2) * ($user['agi'] / 100)));
      if(mt_rand(0,100) <= $dodge){
        $dmg = 0;
      }
      $dmg -= ceil(rand(($opponent['def'] / 12 ),($opponent['def'] / 7)));
      $opponent_dmg += ceil(rand(($opponent['str'] / 6),($opponent['str'] / 4)));
      if($opponent_dmg < 0){
        $opponent_dmg = 0;
      }
      $opponent_crit = ((rand(1,2) * ($opponent['agi'] / 100)) - (rand(1,2) * ($user['agi'] / 100)));
      if(mt_rand(0,100) <= $opponent_crit){
        $opponent_dmg *= 2;
      }    
      $opponent_dodge = ((rand(1,2) * ($user['agi'] / 100 )) - (rand(1,2) * ($opponent['agi'] / 100)));
      if ( mt_rand(0,100) <= $opponent_dodge){
        $opponent_dmg = 0;
      }
      $opponent_dmg -= ceil(rand(($user['def'] / 12),($user['def'] / 7)));
    }
    
 $rand = rand(3,6);
    $_s = (rand(7,10)  * (100 - $user['place']));         
      $_e = (rand(5,7) * (100 - $user['place']));
       $kool =  ($mesto - $kiek[$y]-$rand);
if($dmg > $opponent_dmg){
mysql_query('UPDATE `users` SET `place` = "'.($mesto - $kiek[$y]-$rand).'" WHERE `id` = "'.$user['id'] .'"');
    }

if($user['used'] + 1 ==5){
mysql_query('UPDATE `users` SET `place` = "'.$kool.'",`exp`=`exp`+"'.$_e.'",`s`=`s`+"'.$_s.'",`used`= "5" WHERE `id` = "'.$user['id'] .'"');
$if = '<div class="center block_light">
<h2 class="dgreen bold"><img src="/images/icon/2hit.png" alt=""> 
Турнир завершён <img src="/images/icon/2hit.png" alt=""></h2><div class="separ"></div>
Ты занял '.$kool.' место<div class="separ"></div>
<span class="blue">Награда:</span> <img src="/images/icon/silver.png" alt=""> '.$_s.' серебра <span class="nwr"><img src="/images/icon/exp.png" alt="exp"> '.$_e.' опыта</span></div>

<div class="mini-line"></div>';
$_SESSION['inf'] = $if;

header('location: ?');

}else{
mysql_query('UPDATE `users` SET `used`= `used` + 1 WHERE `id` = '.$user['id'].'');
header("Location:?".($mesto - $kiek[$y])."");
  }
  }
}

for($y = 0 ; $y < $mesto2; $y++){
$opponent = array("str"=>"".($dmg*$kiek2[$y])."", "vit"=>"".($dmg*$kiek2[$y])."", "agi"=>"".($dmg*$kiek2[$y])."","def"=>"".($dmg*$kiek2[$y])."");	

$power = $opponent['str'] + $opponent['agi'] + $opponent['vit'] + $opponent['def'];

if($mesto != 1 ){


?>


<tr><td class="nwr"><?=$mesto - $kiek[$y];?></td><td class="w100"><img src="/images/icon/race/1.png" alt=""> <?=NameBot($name);?></td><td class="nwr"><img src="/images/icon/2hit.png" alt=""> <?=$power;?></td></tr>

<?}
}?>
<tr class="green"><td class="nwr"><?=$user['place'];?></td><td class="w100"><img src="/images/icon/race/<?=$user['r']?>.png" alt=""> <?=$user['login']?></td><td class="nwr"><img src="/images/icon/2hit.png" alt=""> <?=($user['str']+$user['vit']+$user['agi']+$user['def']);?></td></tr>
</tbody></table></div></div></div></div></div></div></div></div></div></div></div></div></div>
<?}?>

<div class="mini-line"></div>
<ul class="hint"><li>Чем выше глава, тем больше награда!</li></ul>

<?php

if($user['start']==9){
include './system/f.php';
}else{
include './system/foot.php';

}