<? 
include './system/common.php';
include './system/functions.php';       
include './system/user.php';
    
if(!$user) {
header('location: /');
exit;}


$title = 'Вторжение';    
include './system/h.php';  
 
if($user['str'] >= '400') {//Если достаточно силы


  
  $member = mysql_query('SELECT * FROM `invasion_member` WHERE `user` = "'.$user['id'].'" ORDER BY `id` DESC LIMIT 1');
  $member = mysql_fetch_array($member);

  
  $battle = mysql_query('SELECT * FROM `invasion` WHERE `id` = "'.$member['battle'].'"');
  $battle = mysql_fetch_array($battle);  
  
  if($member['exit'] == 0 && $battle['start'] == 1 && $battle['end'] == 0) {
  $titans = mysql_result(mysql_query('SELECT COUNT(*) FROM `invasion_member` WHERE `dead` = "0" AND `battle` = "'.$battle['id'].'"'),0);  
  $titans2 = mysql_result(mysql_query('SELECT COUNT(*) FROM `invasion_bot` WHERE `dead` = "0" AND `battle` = "'.$battle['id'].'"'),0);  
  
  if($titans == 0 OR $titans2 == 0) {//Если нет вбою игроков или ботов
mysql_query('UPDATE `invasion` SET `end` = "1" WHERE `id` = "'.$battle['id'].'"');
header('location: ?');
exit;}
  
  if($battle['time'] < time()) {//Если закончилось время
mysql_query('UPDATE `invasion` SET `end` = "1" WHERE `id` = "'.$battle['id'].'"');   
header('location: ?');
exit;}










echo'
<div class="title"><span style="float: right;">'._time($battle['time'] - time()).'</span> '.$title.' </div>';



 
  
  if($member['dead'] == 0) {
  
  $oponent = mysql_query('SELECT * FROM `invasion_bot` WHERE `user` = "'.$member[oponent].'" AND `battle` = "'.$battle['id'].'" AND `dead` = "0" ');
  $oponent = mysql_fetch_array($oponent); 
  
  if($_GET['new_oponent'] == true) {
  
      $new_oponent = mysql_query('SELECT * FROM `invasion_bot` WHERE `battle` = "'.$battle['id'].'" AND `dead` = "0" ORDER BY RAND() LIMIT 1');
      $new_oponent = mysql_fetch_array($new_oponent);
mysql_query("update `invasion_member` set    `oponent` = '".$new_oponent[user]."' where `user` = '".$user[id]."' AND `battle` = '".$battle['id']."' AND `dead` = '0' ");
header("Location: /invasion.php");
exit; 
 }   
      
      
    if($_GET['attack'] == true) {    

       if($oponent['vit'] <= '0' AND $oponent['dead'] == '0') {
mysql_query('UPDATE `invasion_bot` SET `dead` = "1" WHERE `user` = "'.$oponent['user'].'" AND `battle` = "'.$battle['id'].'" AND `dead` = "0" ');
$msg = "Вы убили Врага";
mysql_query("INSERT INTO `invasion_log` SET
            `user_id`='".$user[id]."',
            `boss_id`='".$oponent[user]."',
            `msg`='".$msg."'");
header("Location: ?new_oponent=true");
exit;} 
    
    
$my_attack = _string(mt_rand($user[str] / 3.5,$user[str] / 3) - mt_rand($oponent[def] / 12,$oponent[def] / 9));
if($my_attack <= '0'){ $my_attack = 2;}
$oponent_attack = _string(mt_rand($oponent[str] / 3.5,$oponent[str] / 3) - mt_rand($user[def] / 12,$user[def] / 9));
if($oponent_attack <= '0'){ $oponent_attack = 2;}

mysql_query("update `invasion_bot` set    `vit` = `vit` - '".$my_attack."' where (`id` = '".$oponent[id]."')"); 
$my = "Вы нанесли ".$my_attack." урона";
mysql_query("INSERT INTO `invasion_log` SET
            `user_id`='".$user[id]."',
            `boss_id`='".$oponent[user]."',
            `msg`='".$my."'");

  $chanse = rand(1,100);
  if($chanse < '15') {  
mysql_query("update `invasion_member` set    `vit` = `vit` - '".$oponent_attack."' where (`user` = '".$user[id]."')");  
$boss = "$oponent[name] Враг нанес ".$oponent_attack." урона";   
mysql_query("INSERT INTO `invasion_log` SET
            `user_id`='".$user[id]."',
            `boss_id`='".$oponent[user]."',
            `msg`='".$boss."'");    
}
            
    header("Location: /invasion.php");
    exit;
   }

    if($member['vit'] <= '0') {
mysql_query('UPDATE `invasion_member` SET `dead` = "1" WHERE `user` = "'.$user['id'].'" AND `battle` = "'.$battle['id'].'"');
}
  
  
  echo' <div class="empty_block">
<table align="center"> <tbody><tr>
 <td style="width:33%;">
 <span style="float:right;"> <b>Вы</b> </span> <br>
 <span style="float:right;"> <img src="/images/ico/png/hp.png" alt="hp" width="15"> '._string(_num($member[vit])).' </span>               
</td><td> ';
  if($user['sex'] == '0'){
     echo' <img src="/images/user/man.png" alt="user" width="48">';}
  if($user['sex'] == '1'){
     echo' <img src="/images/user/woman.png" alt="user" width="48">';}
     echo'<td>
<td> <img src="'.$HOME.'/images/invasion/bot'.$oponent[img].'.png" alt="*" width="48"> </td>
<td style="width:33%;">
    <span style="float:left;"> <b>Враг</b> id'.$member[oponent].' </span> <br>
    <span style="float:left;"> <img src="/images/ico/png/hp.png" alt="hp" width="15"> '._string(_num($oponent[vit])).'  </span> 
</td></tr>
</tbody></table>
</div> ';
echo' <div class="block_link">
 <a href="?attack=true"><img src="/images/ico/png/attack.png" width="15"> Атаковать </a> </div>
 <div class="line"></div>';
echo' <div class="block_link">
 <a href="?new_oponent=true"><img src="/images/ico/png/attack.png" width="15"> Сменить противника </a> </div>
 <div class="line"></div>';

  $ca=mysql_result(mysql_query("SELECT COUNT(*) FROM `invasion_log`"),0);
$req = mysql_query("SELECT * FROM `invasion_log` where `user_id` = '".$user[id]."' ORDER by `id` DESC LIMIT 10  ");
$avto=mysql_num_rows($req);
if($avto==0){
echo'<div class="empty_block item_center"> Нет лога </div> ';
}else{
echo' <div class="empty_block"> <center>История боя</center>';
while($log = mysql_fetch_array($req)){
echo' '.$log[msg].' </br> ';
  }
echo'</div>'; 
}
echo'<div class="line"></div>';
}else{
  

echo'
<div class="empty_block item_center">
  <img src="/images/invasion/logo.png" alt="*"/></br>
  Вы были убиты во время сражения, ожидайте окончания боя
  <a href="?"><div class="button">Обновить</div></a>
  
  
  Героев: <b>'.$titans.'</b> </br>
  Скелетов: <b>'.$battle['opponents'].'</b>
  
</div>
<div class="line"></div>';



  }

  }
  else
  {



echo'<div class="title">'.$title.'</div>';






echo'
<div class="empty_block item_center">
  <img src="/images/invasion/logo.png" alt="*"/></br>
  <font color="#9bc">Бой начнется через: '._time($battle['time'] - time()).'</font>
</div><div class="line"></div>';








  
  $battle = mysql_query('SELECT * FROM `invasion` WHERE `start` = "0"');
  $battle = mysql_fetch_array($battle);  

  if(!$battle) {
  $h = date('H',time());   
  if($h > 22 && $h < 6){
  $time = 3600 * 8;
  }else{
  $time = 3600 * 3;
  }

  mysql_query('INSERT INTO `invasion` (`time`) VALUES ("'.(time() + $time).'")');
}
  
  if($battle['time'] <= time()) {//Начинаем битву и пишнм сколько она длиться
mysql_query('UPDATE `invasion` SET `start` = "1", `time` = "'.(time() + (60 * 150)).'" WHERE `id` = "'.$battle['id'].'"');
header('location: ?');
}






echo'<div class="empty_block item_center">';

  



  if($member['battle'] != $battle['id']) {//Если не подали заявку


    if($_GET['enter'] == true) {//подаем заявку
      
      mysql_query('INSERT INTO `invasion_member` (`battle`,
                                                   `user`,
                                                   `time`,
                                                   `oponent`,
                                                   `str`,
                                                   `vit`,
                                                   `def`) VALUES ("'.$battle['id'].'",
                                                                     "'.$user['id'].'",
                                                                          "'.time().'",
                                                                          "'.$user['id'].'",
                                                                          "'.$user['str'].'",
                                                                          "'.$user['vit'].'",
                                                                          "'.$user['def'].'"  )');//Добавляем юзера
                                                   
$bot_img_ = rand(1, 3);                                                   
      mysql_query('INSERT INTO `invasion_bot` (`battle`,
                                                   `user`,
                                                   `hp`,
                                                   `str`,
                                                   `vit`,
                                                   `def`,
                                                   `img`) VALUES ("'.$battle['id'].'",
                                                                  "'.$user['id'].'",
                                                                  "'.$user['vit'].'",
                                                                  "'.$user['str'].'",
                                                                  "'.$user['vit'].'",
                                                                  "'.$user['def'].'",
                                                                  "'.$bot_img_.'")'); //Добавляем ботов
                                                                          
header('location: ?');
}
  

echo'<a href="?enter=true"><div class="button">Подать заявку</div></a>';
}else{//Если подали заявку
echo'<a href="?"><div class="button">Обновить</div></a>';
}

echo'
Героев в отряде: '.mysql_result(mysql_query('SELECT COUNT(*) FROM `invasion_member` WHERE `battle` = "'.$battle['id'].'"'),0).'
</div>
<div class="line"></div>';

echo'<div class="empty_block item_center">
Вражеское войско на подходе! Вступите в отряд и разгромите врага! </div>
<div class="line"></div>';
}

}else{//Если недостаточно силы
 
echo'
<div class="title">'.$title.'</div>

<div class="empty_block item_center">
  <img src="/images/invasion/logo.png" alt="*"/></br>
  Для участия требуется 400 силы
<center> '.($user['str'] >= 400 ? '<input class="button" type="submit" value="В бой"/>':'<div class="button2">В бой</div>').'</center>

</div>
<div class="line"></div>';

echo'<div class="empty_block item_center">
Вражеское войско на подходе! Вступите в отряд и разгромите врага! </div>
<div class="line"></div>';
}
  
include './system/f.php';

?>