<?php

require_once 'system/common.php';
require_once 'system/functions.php';
require_once 'system/user.php';

if ( !isset($user) OR $user['level'] < 3 ) {
    header( 'location: /' );
    exit;
}

$league = array( '', 'новичков', 'опытных', 'претендентов', 'мастеров', 'титанов', 'избранных' );
  $title = 'Лига ' . $league[$user['league']];
include ( './system/h.php' );
///ТИПА АВТОРАСПРЕДЕЛЕНИЕ
if($user['league_place']=='0'){
  mysql_query("UPDATE `users` SET `league_place`='".rand(1,999)."',`league`='1',`league_fights`='5' WHERE `id`='".$user['id']."'");
  header("Location:/league.php");
  exit;

}
echo '<div class=\'title\'><span style=\'float: right;\'>' . $user['league_place'] . ' место</span>Лига ' . $league[$user['league']] . '</div>
<div class=\'line\'></div>';

if ( isset($_GET['attack']) && ereg('^[0-9]', $_GET['attack']) ) {
  if($_GET['attack']==$user['league_place']){
    header("location:/league.php");
    exit;
  }
    if ( $user['league_place'] == 1 ) {
        $query = mysql_query( ' SELECT * FROM `users` WHERE `league` = \'' . $user['league'] . '\' AND `league_place` > \'' . $user['league_place'] . '\' ORDER BY `league_place`  LIMIT 3 ' );
    } else {
        $query = mysql_query( ' SELECT * FROM `users` WHERE `league` = \'' . $user['league'] . '\' AND `league_place` < \'' . $user['league_place'] . '\' ORDER BY `league_place` DESC LIMIT 3 ' );
    }
  
    if ( mysql_num_rows( $query ) != 0 ) {  
        $rows   = array();
  
    while ( $row = mysql_fetch_array( $query ) )
    {

      $rows[] = $row['league_place'];

    }
    
      
    if ( !in_array( $_GET['attack'], $rows ) )
    {
    
    
      header( 'location: /league.php');
      exit;
    
    }
  
  }
  else
  {

      header( 'location: /league.php' );
      exit;

  
  }
$att=abs(intval($_GET['attack']));
  $query    = mysql_query( 'SELECT * FROM `users` WHERE `league` = \'' . $user['league'] . '\' AND `league_place` = \'' . $att . '\' AND `league_place` != \'' . $user['league_place'] . '\' ' );
  
  if ( mysql_num_rows( $query ) == 0 OR $user['league_fights'] == 0 )
  {
     $_SESSION['light']="У вас закончились бои или враги.";
      header( 'location: /league.php' );
      exit;
  }
  $opponent = mysql_fetch_array( $query );
  if ( $user['mp'] < 50 OR $user['hp'] < ( ( ($user['vit'] * 2) / 100 ) * 10 ) )
  {
    $_SESSION['light']="Не достаточно маны или здоровья.";
    header("Location:/league.php");  
    exit;
  }
  else
  {
  
    $dmg          = 0;
    $opponent_dmg = 0;
    for ( $i = 1; $i < 6; $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 ) ) );
    }
    if ( $dmg > $opponent_dmg )
    {

      $_hp  = ceil( $opponent_dmg / 4 );
    
    }
    else
    {

      $_hp  = ceil( $opponent_dmg / 2 );

    }

    mysql_query( ' UPDATE `users` SET `hp` = `hp` - ' . $_hp . ',
    
                                      `mp` = `mp` - 50 WHERE `id` = \'' . $user['id'] . '\' ' );



    echo '<div class=\'block\'>';
    
    if ( $dmg > $opponent_dmg )
    {

      if ( $opponent['league_place'] < $user['league_place'] )
      {

        mysql_query( ' UPDATE `users` SET `league_place` = \'' . $user['league_place'] . '\' WHERE `id` = \'' . $opponent['id'] . '\' ' );      
        mysql_query( ' UPDATE `users` SET `league_place` = \'' . $opponent['league_place']  . '\' WHERE `id` = \'' . $user['id'] . '\' ' );
      
      }

      $_silver =  rand( 1, 100 ) + ( ( rand( 1, 100 ) * $opponent['level'] ) + ( rand( 0, ( 1000 - $user['league_place'] ) ) ) );          
      $_exp    =  ( rand( 1, 10 ) * $opponent['level'] ) * rand( 1, ( ( 1000 - $user['league_place'] ) / 100 ) );
      
      echo '<h2 class=\'green\' style=\'font-weight:bold;\'><img src=\'/images/icon/2hit.png\' alt=\'\'/> Победа! <img src=\'/images/icon/2hit.png\' alt=\'\'/></h2>';

    }
    else
    {

      $_silver = rand( 1,100 ) + ( ( rand( 1, 100 ) * $opponent['level'] ) );
      $_exp    = 1;  
        
      echo '<h2 class=\'dred\' style=\'font-weight:bold;\'><img src=\'/images/icon/2hit.png\' alt=\'\'/> Поражение! <img src=\'/images/icon/2hit.png\' alt=\'\'/></h2>';

    }
   
    if ( $premium )
    {
    
      $_exp += ceil( ( $_exp / 100 ) * 25 );
    
    }
    

    echo '  <div class=\'separator\'></div>
        <img src=\'/images/icon/silver.png\' alt=\'\'/> ' . n_f( $_silver ) . ' серебра <img src=\'/images/icon/exp.png\' alt=\'\'/> ' . n_f( $_exp ) . ' опыта
    </div>
    </div>
    <div class=\'line\'></div>';
        
    mysql_query( ' UPDATE `users` SET `exp`            = `exp`    + ' . $_exp . ',
                                    
                                      `s`         = `s` + ' . $_silver . ',
                                      
                                      `league_fights`  = `league_fights` - 1 WHERE `id` = \'' . $user['id'] . '\' ' );

  }

}
else
{

  echo '<div class=\'content\' align=\'center\'>
    Чем выше место в лиге, тем больше награда!
  </div>';

}

if ( $user['league_place'] < 4 )
{

  echo '<div class=\'block\'>
    <img src=\'/images//' . $user['league_place'] . '.png\' width=\'50\' height=\'50\' style=\'float:left; margin-right:3px; margin-top:3px;\' alt=\'\'/> <span class=\'yellow\'>Поздравляем!</span>
    <br/>';
  
  if     ( $user['league_place'] == 1 ) echo 'Вы лучший в лиге!';
  elseif ( $user['league_place'] == 2 ) echo 'Вы второй в лиге!';
  elseif ( $user['league_place'] == 3 ) echo 'Вы третий в лиге!';
 
  echo '<div style=\'clear: both;\'></div>
  </div>';

}

echo '<div class=\'line\'></div>
<div class=\'content\'>
  <img src=\'/images/icon/2hit.png\' alt=\'\'/> Осталось боев: <b>' . $user['league_fights'] . '</b>
</div>';


if ( $user['league_place'] == 1 )
{

  $query = mysql_query( ' SELECT * FROM `users` WHERE `league` = \'' . $user['league'] . '\' AND `league_place` < \'' . $user['league_place'] . '\'  ORDER BY `league_place` DESC LIMIT 3 ' );

}
else
{

  $query = mysql_query( ' SELECT * FROM `users` WHERE `league` = \'' . $user['league'] . '\' AND `league_place` < \'' . $user['league_place'] . '\' ORDER BY `league_place` DESC LIMIT 3 ' );

}

if ( mysql_num_rows( $query ) > 0 )
{

  $rows = array();

  while ( $row = mysql_fetch_array( $query ) )
  {

    $rows[] = $row['id'];

  }

  
  $rows = array_reverse( $rows );

  for ( $i = 0; $i < 3; $i++ )
  {
  
    $query     = mysql_query( ' SELECT * FROM `users` WHERE `id` = \'' . $rows[$i] . '\' ' );
    $opponent  = mysql_fetch_array( $query );
 
    $inv_w_1 = mysql_fetch_array( mysql_query( 'SELECT * FROM `inv` WHERE `user` = \'' . $opponent['id'] . '\' AND `id` = \'' . $opponent['w_1'] . '\'') );
    $w_1       = $opponent['w_1'] ? $inv_w_1['item'] : 0;

    $inv_w_2 = mysql_fetch_array( mysql_query( 'SELECT * FROM `inv` WHERE `user` = \'' . $opponent['id'] . '\' AND `id` = \'' . $opponent['w_2'] . '\'') );
    $w_2       = $opponent['w_2'] ? $inv_w_2['item'] : 0;

    $inv_w_3 = mysql_fetch_array( mysql_query( 'SELECT * FROM `inv` WHERE `user` = \'' . $opponent['id'] . '\' AND `id` = \'' . $opponent['w_3'] . '\'') );
    $w_3       = $opponent['w_3'] ? $inv_w_3['item'] : 0;

    $inv_w_4 = mysql_fetch_array( mysql_query( 'SELECT * FROM `inv` WHERE `user` = \'' . $opponent['id'] . '\' AND `id` = \'' . $opponent['w_4'] . '\'') );
    $w_4       = $opponent['w_4'] ? $inv_w_4['item'] : 0;

    $inv_w_5 = mysql_fetch_array( mysql_query( 'SELECT * FROM `inv` WHERE `user` = \'' . $opponent['id'] . '\' AND `id` = \'' . $opponent['w_5'] . '\'') );
    $w_5       = $opponent['w_5'] ? $inv_w_5['item'] : 0;

    $inv_w_6 = mysql_fetch_array( mysql_query( 'SELECT * FROM `inv` WHERE `user` = \'' . $opponent['id'] . '\' AND `id` = \'' . $opponent['w_6'] . '\'') );
    $w_6       = $opponent['w_6'] ? $inv_w_6['item'] : 0;

    $inv_w_7 = mysql_fetch_array( mysql_query( 'SELECT * FROM `inv` WHERE `user` = \'' . $opponent['id'] . '\' AND `id` = \'' . $opponent['w_7'] . '\'') );
    $w_7       = $opponent['w_7'] ? $inv_w_7['item'] : 0;

    $inv_w_8 = mysql_fetch_array( mysql_query( 'SELECT * FROM `inv` WHERE `user` = \'' . $opponent['id'] . '\' AND `id` = \'' . $opponent['w_8'] . '\'') );
    $w_8       = $opponent['w_8'] ? $inv_w_8['item'] : 0;

    echo '<div class=\'content\'>
      <span style=\'float: left;\'>
        <a href=\'/league.php?attack=' . $opponent['league_place'] . '\'><img src=\'/manekenImage/' . $opponent['sex'] . '/' . $w_1 . '/' . $w_2 . '/' . $w_3 . '/' . $w_4 . '/' . $w_5 . '/' . $w_6 . '/' . $w_7 . '/' . $w_8 . '/\' width=\'120\' height=\'160\' style=\'margin-right:10px; margin-top:3px;\' alt=\'\'></a>
      </span>
      <img src=\'/images/icon/race/' . $opponent['race'] . '.png\' alt=\'\'> <b>' . $opponent['league_place'] . '.</b> ' . $opponent['login'] . '
      <br/>
      <br/>
      <img src=\'/images/icon/str.png\' alt=\'\'> Сила:  ' . $opponent['str'] . '
      <br/>
      <img src=\'/images/icon/vit.png\' alt=\'\'> Жизнь: ' . $opponent['vit'] . '
      <br/>
      <img src=\'/images/icon/agi.png\' alt=\'\'> Удача: ' . $opponent['agi'] . '
      <br/>
      <img src=\'/images/icon/def.png\' alt=\'\'> Броня: ' . $opponent['def'] . '
      <br/>
      <br/>
      <a class=\'btn\' href=\'/league.php?attack=' . $opponent['league_place'] . '\'><span class=\'end\'><span class=\'label\'>Атакoвать</span></span></a>
      <div style=\'clear:both;\'></div>
    </div>';
    
    if ( $i < 2 ) echo '<div class=\'line\'></div>';

  }

}
else
{


}

echo '<div class=\'line\'></div>
<div class=\'content\'>
  Ваши параметры:
  <br/>
  <img src=\'/images/icon/str.png\' alt=\'\'/> ' . $user['str'] . ' <img src=\'/images/icon/vit.png\' alt=\'\'/> ' . $user['vit'] . ' <img src=\'/images/icon/agi.png\' alt=\'\'/> ' . $user['agi'] . ' <img src=\'/images/icon/def.png\' alt=\'\'/> ' . $user['def'] . '
</div>
<div class=\'line\'></div>';
if($user['league_fights']=='0'){
  if(isset($_GET['respawn'])){
    if($user['g']<'30'){
      $_SESSION['light']="У вас не достаточно золота.";
      header("location:/league.php");
      exit;
    }elseif ($user['g']>='30') {
      mysql_query("UPDATE `users` SET `league_fights`='5',`g`='".($user['g']-30)."' WHERE `id`='".$user['id']."'");
      $_SESSION['light']="Вы приобрели 5 боев в лиге!";
      header("location:/league.php");
      exit;
    }
  }
  ?>
  <Div class='content'/>
  <center>
 <a class='btn' href='/league.php?respawn'><span class='end'><span class='label'>Купить 5 боев за 30 <img src='/images/icon/gold.png' alt=''/> золота</span></span></a>
 </center>
</div>
<div class='line'></div>
 <?
}
echo '
<div class=\'list\'>

  <li><a href=\'/arena.php\'><img src=\'/images/icon/arrow.png\' alt=\'\'/> Арена</a></li>
</div>';

include_once 'system/f.php';


?>