<?
include './system/common.php';   
include './system/functions.php';
include './system/user.php';    
if(!$user) {header('location: /');exit;}
$title = 'Схватка питомцев';    
include './system/h.php';  
$pet = mysql_query('SELECT * FROM `pet_u` WHERE `user_id` = "'.$user['id'].'"');
$pet = mysql_fetch_array($pet);
if($user['level']<10){
?>

<div class='content' align='center'>
Для участии в <img src='/images/pet/icon.gif' alt='*'/>  схватке питомцев нужен <img src='/images/icon/level.png' alt='*'/> 10 уровень
</div>
<div class="list"><a href="/arena/"><img src="/images/icon/arena.png"> Арена</a></div>
<?
 
  
include './system/f.php';
exit;
}
?>
<div class='title'><?=$title?></div>
<?
 
if($pet!=0) {

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

  if($member) {

  $battle = mysql_query('SELECT * FROM `pets_sxvatki` WHERE `id` = "'.$member['battle'].'"');
  $battle = mysql_fetch_array($battle);

  }

if($battle['start']==1 and $member['skr']==0){
mysql_query("UPDATE `pets_sxvatki_member` SET `skr` = '1' WHERE `pet` = '".$pet['id']."'");
header('location: /pets_sxvatki/');exit;}
  if($member['exit'] == 0 && $battle['start'] == 1 && $battle['end'] == 0) {

  if(mysql_result(mysql_query('SELECT COUNT(*) FROM `pets_sxvatki_member` WHERE `battle` = "'.$battle['id'].'" AND `dead` = "0"'),0) == 1) {
     
    mysql_query('UPDATE `pets_sxvatki` SET `end` = "1" WHERE `id` = "'.$battle['id'].'"');
  
      header('location: /pets_sxvatki/');
  
  exit;
  
  }
  
?>
  
  

<?

    if($member['dead'] == 1) {
    
?>

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

<div class='content' align='center'>
<a href='/pets_sxvatki/?' class='button'>Обновить</a>

<br/><br/>
<font color='#999'>Вы были убиты во время сражения, ожидайте окончания боя</font>
</div>

<?

    

    }
    else
    {
  
  if($_GET['exit'] == true) {

    $log = '<img src=\'/images/pet/icon.gif\' alt=\'\'/> <b>'.$pet['name'].'</b> покидает бой';    
    mysql_query('INSERT INTO `pets_sxvatki_log` (`battle`,
                                               `pet`,
                                             `object`,
                                               `text`) VALUES ("'.$battle['id'].'",
                                                                 "'.$pet['id'].'",
                                                               "'.$object['id'].'",
                                                                        "'.$log.'")');

    mysql_query('UPDATE `pets_sxvatki_member` SET `dead` = "1" WHERE `battle` = "'.$battle['id'].'" AND `pet` = "'.$pet['id'].'"');
  
      header('location: /pets_sxvatki.php');
  
  exit;
  
  }
  
  if($member['object'] == 0) {
  
    $rand_object = mysql_query('SELECT * FROM `pets_sxvatki_member` WHERE `battle` = "'.$battle['id'].'" AND `dead` = "0" AND `pet` != "'.$pet['id'].'" ORDER BY RAND() LIMIT 1');
    $rand_object = mysql_fetch_array($rand_object);


    mysql_query('UPDATE `pets_sxvatki_member` SET `object` = "'.$rand_object['id'].'" WHERE `battle` = "'.$battle['id'].'" AND `pet` = "'.$pet['id'].'"');

  }

if($member['object']) {

  if($_GET['last'] == true) {
  
    $rand_object = mysql_query('SELECT * FROM `pets_sxvatki_member` WHERE `battle` = "'.$battle['id'].'" AND `dead` = "0" AND `pet` != "'.$pet['id'].'" ORDER BY RAND() LIMIT 1');
    $rand_object = mysql_fetch_array($rand_object);


    mysql_query('UPDATE `pets_sxvatki_member` SET `object` = "'.$rand_object['id'].'" WHERE `battle` = "'.$battle['id'].'" AND `pet` = "'.$pet['id'].'"');

  header('location: /pets_sxvatki/');

  }

    $member_object = mysql_query('SELECT * FROM `pets_sxvatki_member` WHERE `battle` = "'.$battle['id'].'" AND `id` = "'.$member['object'].'"');
    $member_object = mysql_fetch_array($member_object);

    $object = mysql_query('SELECT * FROM `pet_u` WHERE `id` = "'.$member_object['pet'].'"');
    $object = mysql_fetch_array($object);


  
  if($_GET['attack'] == true && $member_object['dead'] == 0) {
  

  $dmg +=round(rand(($pet['str']/6),($pet['str']/4)));


$dmg -= round(rand(($object['def']/12),($object['def']/7)));


    if($dmg < 0) {
    
      $dmg = 0;
    
    }
    


    if($dmg > $member_object['hp']) {
    
      $dmg = $member_object['hp'];
    
    }
$crit = ( (rand(1,2) * ($pet['agi'] / 100) ) - (rand(1,2) * ($object['agi'] / 100)));
 if(mt_rand(0, 100) <= $crit) { $dmg *= 2;
 $log_crit = true; } $dodge = ( (rand(1,2) * ($object['agi'] / 100) ) - (rand(1,2) * ($pet['agi'] / 100)));
 if(mt_rand(0, 100) <= $dodge) {$dmg =0; } 
$dmg_time = time() - $member['time'];
if($dmg_time < 2) { $dmg = 0;
$_SESSION['err']='Не бей так часто'; }

    if($dmg == 0) {
      $log = 'Ваш питомец промахнулся';
    }
    else
    {
      $log = 'Ваш питомец ударил   <img src=\'/images/pet/icon.gif\' alt=\'*\'/> <b>'.$object['name'].'</b> на <b>'.$dmg.'</b> '.($log_crit == true ? '(крит)':'');    
    }
    
    mysql_query('INSERT INTO `pets_sxvatki_log` (`battle`,
                                               `pet`,
                                             `object`,
                                               `text`,
                                               `show`) VALUES ("'.$battle['id'].'",
                                                                 "'.$pet['id'].'",
                                                               "'.($dmg > 0 ? $object['id']:0).'",
                                                                        "'.$log.'",
                                                                 "'.$pet['id'].'")');
    
    if($dmg > 0) {
      $log = '<img src=\'/images/pet/icon.gif\'/> <b>'.$pet['name'].'</b> ударил вашего питомца на <b>'.$dmg.'</b> '.($log_crit == true ? '(крит)':'');    
    mysql_query('INSERT INTO `pets_sxvatki_log` (`battle`,
                                               `pet`,
                                             `object`,
                                               `text`,
                                               `show`) VALUES ("'.$battle['id'].'",
                                                               "'.$object['id'].'",
                                                                 "'.$pet['id'].'",
                                                                        "'.$log.'",
                                                               "'.$object['id'].'")');
    }

    if($dmg > 0) {
    $log = '<img src=\'/images/pet/icon.gif\' alt=\'\'/> <b>'.$pet['name'].'</b> ударил <img src="/images/pet/icon.gif" alt=\'*\'/> <b>'.$object['name'].'</b> на <b>'.$dmg.'</b> '.($log_crit == true ? '(крит)':'');    
    mysql_query('INSERT INTO `pets_sxvatki_log` (`battle`,
                                               `pet`,
                                             `object`,
                                               `text`) VALUES ("'.$battle['id'].'",
                                                                 "'.$pet['id'].'",
                                                               "'.$object['id'].'",
                                                                        "'.$log.'")');
    }




    mysql_query('UPDATE `pets_sxvatki_member` SET `time` = "'.time().'"WHERE `battle` = "'.$battle['id'].'" AND `pet` = "'.$pet['id'].'"');
    mysql_query('UPDATE `pets_sxvatki_member` SET `hp` =`hp` - "'.$dmg.'"  WHERE `battle` = "'.$battle['id'].'" AND `pet` = "'.$object['id'].'" ');





    if($dmg >= $member_object['hp']) {

      $log = 'Ваш питомец убил питомца <img src=\'/images/pet/icon.gif\' alt=\'*\'/> <b>'.$object['name'].'</b>';        
      mysql_query('INSERT INTO `pets_sxvatki_log` (`battle`,
                                                 `pet`,
                                               `object`,
                                                 `text`,
                                                 `show`) VALUES ("'.$battle['id'].'",
                                                                   "'.$pet['id'].'",
                                                                 "'.$object['id'].'",
                                                                          "'.$log.'",
                                                                   "'.$pet['id'].'")');

      $log = '<img src=\'/images/pet/icon.gif\' alt=\'*\'/> <b>'.$pet['name'].'</b> убил вашего питомца';    
      mysql_query('INSERT INTO `pets_sxvatki_log` (`battle`,
                                                 `pet`,
                                               `object`,
                                                 `text`,
                                                 `show`) VALUES ("'.$battle['id'].'",
                                                                 "'.$object['id'].'",
                                                                   "'.$pet['id'].'",
                                                                          "'.$log.'",
                                                                 "'.$object['id'].'")');
    
    
      $log = '<img src=\'/images/icon/rip.png\' alt=\'*\'/> <img src=\'/images/pet/icon.gif\' alt=\'*\'/> <b>'.$pet['name'].'</b> убил <img src=\'/images/pet/icon.gif\' alt=\'*"\'> <b>'.$object['name'].'</b>';    
      mysql_query('INSERT INTO `pets_sxvatki_log` (`battle`,
                                                 `pet`,
                                               `object`,
                                                 `text`) VALUES ("'.$battle['id'].'", 
                                                                 "'.$pet['id'].'",
                                                               "'.$object['id'].'",
                                                                        "'.$log.'")');


      mysql_query('UPDATE `pets_sxvatki_member` SET `dead` = "1" WHERE `battle` = "'.$battle['id'].'" AND `pet` = "'.$object['id'].'"');
      mysql_query('UPDATE `pets_sxvatki_member` SET `kills` = `kills` + 1 WHERE `battle` = "'.$battle['id'].'" AND `pet` = "'.$pet['id'].'"');

      $rand_object = mysql_query('SELECT * FROM `pets_sxvatki_member` WHERE `battle` = "'.$battle['id'].'" AND `dead` = "0" AND `pet` != "'.$pet['id'].'" ORDER BY RAND() LIMIT 1');
      $rand_object = mysql_fetch_array($rand_object);

      mysql_query('UPDATE `pets_sxvatki_member` SET `object` = "'.$rand_object['id'].'" WHERE `battle` = "'.$battle['id'].'" AND `pet` = "'.$pet['id'].'"');

    }
        header('location: /pets_sxvatki/');
    
    exit;
  }
$nast = mysql_query('SELECT * FROM `users` WHERE `id` = "'.$object['user_id'].'"');
$nast = mysql_fetch_array($nast);

?>  
<div class='block'>
<table style="width:100%" cellspacing="0" cellpadding="0"> 
<tbody><tr><td style="text-align:center;width:40%;"> 
<img src="/images/pet/mini/<?=$pet['screen'];?>" width="40" height="40" alt=""> 
<br> 
<?=$pet['name'];?><br> 
<font color="gold"><b>Мой питомец</b></font><br> 
<span style='color:#6f6f6f;'><img src='/images/icon/vit.png' alt='*'/> <?=$member['hp'];?> <br/>

</span>
</td> 
<td style="text-align:center;width:20%;"> 
<h1><font color="green">Цель</font></h1>
</td><td style="text-align:center;width:40%;"><img src="/images/pet/mini/<?=$object['screen'];?>" width="40" height="40" alt=""> 
<br> 
<?=$object['name'];?><br> 
<font color="gold"><b>Наставник: <?=$nast['login'];?></b></font><br> 
<span style='color:#6f6f6f;'><img src='/images/icon/vit.png' alt='*'/> <?=$member_object['hp']?> <br/>
</td></tr></tbody></table></div>
<?

  }

?>

<div class='content' align='center'>

<a href='/pets_sxvatki/?attack=true' class='button'>Атаковать</a><br/><br/>

<a href='/pets_sxvatki/?last=true' class='button'>Сменить цель</a>

</div>

<?
if(!empty($_SESSION['err'])){?><div class="list"><span style=color:red>Не бейте так часто</span></div><?
$_SESSION['err']=NULL;
}
  }
$count = mysql_result(mysql_query('SELECT COUNT(*) FROM `pets_sxvatki_log` WHERE `battle` = "'.$battle['id'].'"'),0);
if($count > 0) {
?>
<div class='line'></div>
<div class='content'>
<?
$q = mysql_query('SELECT * FROM `pets_sxvatki_log` WHERE `battle` = "'.$battle['id'].'" ORDER BY `id` DESC LIMIT 15');
while($row = mysql_fetch_array($q)) {
if($row['pet'] == $pet['id'] && $row['show'] == $pet['id'] OR $row['object'] == $pet['id'] && $row['show'] == $pet['id']) {
  
      echo '<font color=\'#'.($row['object'] == 0 ? 'ffffff':'c06060').'\'>'.$row['text'].'</font><br/>';
  
    }
    elseif($row['show'] == 0)
    {
    
    if($row['pet'] == $pet['id']) {
        

    }
    else
    {

    if($row['object'] == $pet['id']) {
    
    }
    else
    {
    
      echo $row['text'].'<br/>';
  
    }
  
    }
  
    }

  }
  
?>

</div>

<?
  
  }

  if($member['dead'] == 0) {

?>

  <div class='line'></div>
<div class='list'>
  <li class='no_b'><a href='/pets_sxvatki/?exit=true'><img src='/images/icon/arrow.png' alt='*'/> Покинуть бой</a></li>
</div>

<?

  }
  
  }
  else
  {

  if($member['exit'] == 0 && $battle['start'] == 1 && $battle['end'] == 1) {

$q = mysql_query('SELECT * FROM `pets_sxvatki_member` WHERE `battle` = "'.$battle['id'].'" ORDER BY `kills` DESC  LIMIT 1');
$row= mysql_fetch_array($q);

    $best = mysql_query('SELECT * FROM `pet_u` WHERE `id` = "'.$row['pet'].'"');
    $best = mysql_fetch_array($best);
  


  



 
?>
<center>
  <div class='block'>
  
  <img src='/images/icon/2hit.png' alt='*'/> <font color='#90c090'><b>Бой окончен!</b></font> <img src='/images/icon/2hit.png' alt='*'/>
  
   </div><br/></center>

  <div class='block'>

  <img src='/images/icon/premium.png' alt='*'/> <b>Лучший: <img src='/images/pet/icon.gif' alt='*'/> <?=$best['name']?></b> <img src='/images/icon/premium.png' alt='*'/>
  <div class='separator'></div>
  <b>Итог боя:</b><br/>

<?

$q = mysql_query('SELECT * FROM `pets_sxvatki_member` WHERE `battle` = "'.$battle['id'].'" ORDER BY `kills` DESC LIMIT '.mysql_result(mysql_query('SELECT COUNT(*) FROM `pets_sxvatki_member` WHERE `battle` = "'.$battle['id'].'"'),0).'');

  while($row = mysql_fetch_array($q)) {
    $dres = mysql_query('SELECT * FROM `pet_u` WHERE `id` = "'.$row['pet'].'"');
    $dres = mysql_fetch_array($dres);

    $dres_rating_user = mysql_query('SELECT * FROM `users` WHERE `id` = "'.$dres['user_id'].'"');
    $dres_rating_user = mysql_fetch_array($dres_rating_user);

  $_rating = 5 * $row['kills'];

  mysql_query('UPDATE `users` SET `dres_rating` = "'.($dres_rating_user['dres_rating'] + $_rating).'" WHERE `id` = "'.$dres_rating_user['id'].'"');

?>

<img src='/images/icon/race/<?=$dres_rating_user['r']?>.png' alt='*'/> <a href='/user/<?=$dres_rating_user['id']?>'><?=$dres_rating_user['login']?></a> - <?=$_rating?> к рейтингу<br/>

<?

  }

?>

  </div>

<?

    mysql_query('UPDATE `pets_sxvatki_member` SET `exit` = "1" WHERE `battle` = "'.$battle['id'].'" AND `pet` = "'.$pet['id'].'"');
    
  }
  else
  {

?>

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

<?
  
  }


if(mysql_result(mysql_query('SELECT COUNT(*) FROM `pets_sxvatki` WHERE `start` = "0"'),0) == 0) {

  mysql_query('INSERT INTO `pets_sxvatki` (`start`,
                                         `end`,
                                        `time`) VALUES ("0",
                                                        "0",
                                        "'.(time() + 60).'")');

}

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

?>

<div class='content' align='center'>
<img src='/images/town/pets.png' alt='*'/><p>
<img src='/images/pet/icon.gif' alt='*'/> Питомцев в очереди: <b><?=mysql_result(mysql_query('SELECT COUNT(*) FROM `pets_sxvatki_member` WHERE `battle` = "'.$battle['id'].'"'),0)?></b> из <b>5</b><br/>
<img src='/images/icon/rage.png' alt='*'/> Ваш рейтинг: <b><?=$user['dres_rating']?></b><br/><br/>

<?

if(mysql_result(mysql_query('SELECT COUNT(*) FROM `pets_sxvatki_member` WHERE `battle` = "'.$battle['id'].'"'),0) < 5 && mysql_result(mysql_query('SELECT COUNT(*) FROM `pets_sxvatki_member` WHERE `battle` = "'.$battle['id'].'" AND `pet` = "'.$pet['id'].'"'),0) == 0) {

    if($_GET['enter'] == true && mysql_result(mysql_query('SELECT COUNT(*) FROM `pets_sxvatki_member` WHERE `battle` = "'.$battle['id'].'"'),0) < 5) {
    
      mysql_query('INSERT INTO `pets_sxvatki_member` (`battle`,`pet`, `time`, `hp`) VALUES ("'.$battle['id'].'", "'.$pet['id'].'",    "'.time().'",    "'.$pet['vit'].'")');
    
        header('location: /pets_sxvatki/');
    
    exit;
    
   }

?>

<a href='?enter=true' class='button'>Встать в очередь</a>

<?

}
else
{

  if($_GET['exit'] == true && mysql_result(mysql_query('SELECT COUNT(*) FROM `pets_sxvatki_member` WHERE `battle` = "'.$battle['id'].'"'),0) < 5) {

    mysql_query('DELETE FROM `pets_sxvatki_member` WHERE `battle` = "'.$battle['id'].'" AND `pet` = "'.$pet['id'].'"');
   
        header('location: /pets_sxvatki/');
    
    exit;

  }
  
  if(mysql_result(mysql_query('SELECT COUNT(*) FROM `pets_sxvatki_member` WHERE `battle` = "'.$battle['id'].'"'),0) > 1) {
  
  if($battle['time'] > time()) {
  
?>

<font color='#909090'>До начала боя: <?=($battle['time'] - time())?> секунд</font><br/><br/>

<?
  
  }
  else
  {

    mysql_query('UPDATE `pets_sxvatki` SET `start` = "1" WHERE `id` = "'.$battle['id'].'"');
  
  header('location: /pets_sxvatki/');
  
  }
  
  }
  else
  {
  
  if($battle['time'] < time()) {
  
    mysql_query('UPDATE `pets_sxvatki` SET `time` = "'.(time() + 30).'" WHERE `id` = "'.$battle['id'].'"');
  
  }
  
  }

?>

<a href='?' class='button'>Обновить</a>

<br/><br/>

<a href='/pets_sxvatki/?exit=true' class='button'>Выйти из очереди</a>


<?

}

?>

</div>

<?
  

  }

?>

<?

 }
 else
 {
 
?>

<div class='content' align='center'>
Для участии в <img src='/images/pet/icon.gif' alt='*'/> схватке питомцев у вас должен быть питомец 
</div>
<div class="list"><a href="/petshop/"><img src="/images/pet/icon.gif">Магазин питомцев</a></div>
<?
 
 }
  
include './system/f.php';

?>