<?php
session_start();
include ('private/mysql.php');
include ('tmp/head.php');


$q = mysql_query("SELECT * FROM `mobs` WHERE `id`='$id'");
$e = mysql_num_rows($q);
if($e == 0){
    header('Location: hunter.php');
    exit;
}

$mob = mysql_fetch_assoc($q);

if($mob['user']!==$uid and $mob['user']!=='' and $mob['user']!=='0'){
    header('Location: hunter.php');
    exit;
}

$q1 = mysql_query("SELECT * FROM `mobs` WHERE `user`='$uid'");
$e1 = mysql_num_rows($q1);
$w1 = mysql_fetch_assoc($q1);
if($e1>='1'){
if($w1['id']!==$mob['id']){
    header('Location: battle.php?id='.$w1['id'].'');
    exit;
}
}

switch($sd){
default:

$tb=time()+600;
mysql_query("UPDATE `mobs` SET `time`='$tb' WHERE `id`='$mob[id]'");

mysql_query("UPDATE `mobs` SET `user`='$uid' WHERE `id`='$mob[id]'");

if($mob['hp']<='0'){
	header('Location: battle.php?sd=win&id='.$mob['id'].'');
    exit;
}

echo "<center><b><a style='color:red' href='info.php?sd=mob&id=$mob[id]'>$mob[name]</a></b><br>$mob[level] уровень<br><font size=1 color=red>$mob[hp]</font></center><hr>";

$hpm = ($mob['hp']/$mob['hpall'])*100;

echo '<span style="float:left">0</span><span style="float:right">'.$mob['hpall'].'</span><br><div style="background:#666666;height:6px;width:100%;padding:0;margin:4px 0;">
<div style="background:red; height:6px; width:'.$hpm.'%;"></div></div><hr>';

if($_SESSION['potion']!==''){
    echo $_SESSION['potion'];
    $_SESSION['potion']="";
}

echo "<ul class='list_page'><div class='linem'></div>";

echo "<li><a style='color:red' href='battle.php?sd=ataka&id=$mob[id]'>АТАКА (+25% к урону)</a></li>";
echo "<li><a style='color:lightblue' href='battle.php?sd=mana&id=$mob[id]'>МАНА (+30% к мане)</a></li>";
echo "<li><a style='color:lightgreen' href='battle.php?sd=def&id=$mob[id]'>ЗАЩИТА (+15% к защите)</a></li>";

echo "</ul><div class='linem2'></div>";



if($user['level']<'3'){
    echo "<font size=2 color=grey>Магия доступна с 3-го уровня.</font><hr>";
    }else{

$m = mysql_query("SELECT * FROM `us_mag` WHERE `user`='$uid'");
$m1 = mysql_num_rows($m);
if($m1=='0'){
    echo "<font color=grey><i>У вас нет изученных магических заклинаний</i></font><div class='linem2'></div>";
}else{
    
    While($m2 = mysql_fetch_assoc($m)){
        
        echo " <a href='battle.php?sd=mag&mag=$m2[mag]&id=$mob[id]'><img src='img/mag/$m2[img]' width='50'></a> ";
        
    }
    echo "<div class='linem2'></div>";
}



$mp = ($user['mp']/$user['mpall'])*100;
   
echo "<font color=lightblue>Мана: $user[mp]</font><br>";
echo '<div style="background:#666666;height:6px;width:100%;padding:0;margin:4px 0;">
<div style="background:lightblue; height:6px; width:'.$mp.'%;"></div></div><hr>';
}

$el = mysql_query("SELECT * FROM `us_hp_elixir` WHERE `user`='$uid' and `battle`='1' and `kol`>'0'");
$le = mysql_num_rows($el);
if($le>='1'){
    
    While($lw = mysql_fetch_assoc($el)){
        
        echo "<a href='battle.php?sd=potion&p=$lw[id]&id=$mob[id]'><img src='img/elixir/$lw[img]' width='50'></a> ";
        
    }
    
}

break;


case 'potion':

$p = (int)mysql_real_escape_string(htmlspecialchars(strip_tags($_GET['p'])));

$q = mysql_query("SELECT * FROM `us_hp_elixir` WHERE `user`='$uid' and `id`='$p'");
$e = mysql_num_rows($q);
if($e=='0'){
    header("Location: battle.php?id=$id");
    exit;
}
$w = mysql_fetch_assoc($q);

if($w['battle']=='0'){
    header("Location: battle.php?id=$id");
    exit;
}

if($w['kol']<='0'){
    header("Location: battle.php?id=$id");
    exit;
}

$hp1 = round(($user['hpall']/100)*$w['hp']);

$hpp = $user['hp']+$hp1;
if($hpp>=$user['hpall']){
    mysql_query("UPDATE `users` SET `hp`=`hpall` WHERE `id`='$uid'");
}else{
    mysql_query("UPDATE `users` SET `hp`=`hp`+'$hp1' WHERE `id`='$uid'");
}

mysql_query("UPDATE `us_hp_elixir` SET `kol`=`kol`-'1' WHERE `user`='$uid' and `id`='$p'");

$_SESSION['potion']="<font size=2 color=lightgreen>Вы восстановили <font color=red>$hp1</font> здоровья</font>";

header("Location: battle.php?id=$id");
exit;

break;


case 'mag':
$mag = mysql_real_escape_string(htmlspecialchars($_GET['mag']));
$q = mysql_query("SELECT * FROM `us_mag` WHERE `user`='$uid' and `mag`='$mag'");
$e = mysql_num_rows($q);
if($e=='0'){
    header('Location: battle.php?id='.$id.'');
    exit;
}
$w = mysql_fetch_assoc($q);

if($user['mp']<$w['mp']){
    header('Location: battle.php?id='.$id.'');
    exit;
}



$myuron = rand($user['min_u'],$user['max_u']);
$maguron = rand($w['min_u'],$w['max_u']);
if($w['tip']=='fire'){
    $elementsuron = ($maguron/100)*$user['fire'];
}
if($w['tip']=='volna'){
    $elementsuron = ($maguron/100)*$user['voda'];
}
if($w['tip']=='haos'){
    $elementsuron = ($maguron/100)*$user['haos'];
}
$uron = round($myuron+$maguron+$elementsuron);


if($w['mag']=='volna'){
$hp1 = ($uron/100)*$w['hp'];
$hp2 = $user['hp']+$hp1;
if($hp2>=$user['hpall']){
$hp = $user['hpall'];
}else{
$hp = $user['hp']+$hp1;
}
mysql_query("UPDATE `users` SET `hp`='$hp' WHERE `id`='$uid'");
}



if($w['mag']=='haos'){
$hr = rand(1,2);
if($hr=='2'){
$hp1 = $uron;
$hp2 = $user['hp']+$hp1;
if($hp2>=$user['hpall']){
    $hp = $user['hpall'];
}else{
    $hp = $user['hp']+$hp1;
}
mysql_query("UPDATE `users` SET `hp`='$hp' WHERE `id`='$uid'");
}
}

$mobu = rand($mob['u_min'],$mob['u_max']);

if($user['hp']<='0' or $user['hp']<=$mobu){
        $_SESSION['rez'] = "<font color=red></center>Вы проиграли бой</center></font><hr>";
        mysql_query("UPDATE `users` SET `lose`=`lose`+'1' WHERE `id`='$uid'");
        mysql_query("UPDATE `mobs` SET `user`='' WHERE `id`='$id'");
        header('Location: hunter.php');
        exit;
    }
    
    if($mob['hp']<='0' or $mob['hp']<=$uron){
        mysql_query("UPDATE `mobs` SET `hp`='0' WHERE `id`='$mob[id]'");
        header('Location: battle.php?sd=win&id='.$mob['id'].'');
        exit;
    }
    

mysql_query("UPDATE `users` SET `hp`=`hp`-'$mobu',`mp`=`mp`-'$w[mp]' WHERE `id`='$uid'");
    
    
mysql_query("UPDATE `mobs` SET `hp`=`hp`-'$uron' WHERE `id`='$id'");
    
header('Location: battle.php?id='.$id.'');
exit;

break;


case 'mana':

    if($user['time_udar']>=time()){
        $uron2 = rand($user['min_u'],$user['max_u']);
        if($game['winopp']==$user['storona']){$oppbonus=($uron2/100)*25;}else{$oppbonus=0;}
        $uron = ($uron2/10)+$oppbonus;
    }else{
        $uron1 = rand($user['min_u'],$user['max_u']);
        if($game['winopp']==$user['storona']){$oppbonus=($uron1/100)*25;}else{$oppbonus=0;}
        $uron = $uron1+$oppbonus;
    }
    
    $tu = time()+1;
    mysql_query("UPDATE `users` SET `time_udar`='$tu' WHERE `id`='$uid'");
    
    $mobu = rand($mob['u_min'],$mob['u_max']);
  
    $man = round(($user['mana']/100)*30);
    
    $man2 = $user['mana']+$man;
    
    $mans = $user['mp']+$man2;
    
    if($mans>=$user['mpall']){
        $mana = $user['mpall'];
    }else{
        $mana = $user['mp']+$man2;
    }
    
    
    if($user['hp']<='0' or $user['hp']<=$mobu){
        $_SESSION['rez'] = "<font color=red></center>Вы проиграли бой</center></font><hr>";
        mysql_query("UPDATE `users` SET `lose`=`lose`+'1' WHERE `id`='$uid'");
        mysql_query("UPDATE `mobs` SET `user`='',`time`='0' WHERE `id`='$id'");
        header('Location: hunter.php');
        exit;
    }
    
    if($mob['hp']<='0' or $mob['hp']<=$uron){
        mysql_query("UPDATE `mobs` SET `hp`='0' WHERE `id`='$mob[id]'");
        header('Location: battle.php?sd=win&id='.$mob['id'].'');
        exit;
    }
    
    
    mysql_query("UPDATE `users` SET `hp`=`hp`-'$mobu',`mp`='$mana' WHERE `id`='$uid'");
    
    
    mysql_query("UPDATE `mobs` SET `hp`=`hp`-'$uron' WHERE `id`='$id'");
    
    header('Location: battle.php?id='.$id.'');
    exit;

break;


case 'def':

    if($user['time_udar']>=time()){
        $uron2 = rand($user['min_u'],$user['max_u']);
        if($game['winopp']==$user['storona']){$oppbonus=($uron2['uron']/100)*25;}else{$oppbonus=0;}
        $uron = ($uron2/10)+$oppbonus;
    }else{
        $uron1 = rand($user['min_u'],$user['max_u']);
        if($game['winopp']==$user['storona']){$oppbonus=($uron['uron']/100)*25;}else{$oppbonus=0;}
        $uron = $uron1+$oppbonus;
    }
    
    $tu = time()+1;
    mysql_query("UPDATE `users` SET `time_udar`='$tu' WHERE `id`='$uid'");
    
    $rnd = rand(0,1);
    $mobb = rand($mob['u_min'],$mob['u_max']);
    $mobi = round(($mobb/100)*15);
    $mobu = $mobb-$mobi;
    
    $mans = $user['mp']+$user['mana'];
    
    if($mans>=$user['mpall']){
        $mana = $user['mpall'];
    }else{
        $mana = $user['mp']+$user['mana'];
    }
    
    if($user['hp']<='0' or $user['hp']<=$mobu){
        $_SESSION['rez'] = "<font color=red></center>Вы проиграли бой</center></font><hr>";
        mysql_query("UPDATE `users` SET `lose`=`lose`+'1' WHERE `id`='$uid'");
        mysql_query("UPDATE `mobs` SET `user`='',`time`='0' WHERE `id`='$id'");
        header('Location: hunter.php');
        exit;
    }
    
    if($mob['hp']<='0' or $mob['hp']<=$uron){
        mysql_query("UPDATE `mobs` SET `hp`='0' WHERE `id`='$mob[id]'");
        header('Location: battle.php?sd=win&id='.$mob['id'].'');
        exit;
    }
    
    
    mysql_query("UPDATE `users` SET `hp`=`hp`-'$mobu',`mp`='$mana' WHERE `id`='$uid'");
    
    
    mysql_query("UPDATE `mobs` SET `hp`=`hp`-'$uron' WHERE `id`='$id'");
    
    header('Location: battle.php?id='.$id.'');
    exit;

break;


case 'ataka':

    $muron = rand($user['min_u'],$user['max_u']);
    $uron1 = round($muron+(($muron/100)*25));
    
    if($user['time_udar']>=time()){
        $muron = rand($user['min_u'],$user['max_u']);
        $uron2 = round($muron+(($muron/100)*25));
        $uron1 = $uron2/10;
    }else{
        $muron = rand($user['min_u'],$user['max_u']);
        $uron1 = round($muron+(($muron/100)*25));
    }
    if($game['winopp']==$user['storona']){$oppbonus=($uron1['uron']/100)*25;}else{$oppbonus=0;}
    
          
    if($user['krit']>='50'){
        $kr = rand(2,4);
        if($kr=='3'){
            $krit=round(($uron1/100)*$user['int']);
            $uron=$uron1+$krit+$oppbonus;
        }else{
            $uron=$uron1+$oppbonus;
        }
    }else{
        $uron=$uron1+$oppbonus;
    }
    
    
    $tu = time()+1;
    mysql_query("UPDATE `users` SET `time_udar`='$tu' WHERE `id`='$uid'");
    
    $mobu = rand($mob['u_min'],$mob['u_max']);
    
    $mans = $user['mp']+$user['mana'];
    
    if($mans>=$user['mpall']){
        $mana = $user['mpall'];
    }else{
        $mana = $user['mp']+$user['mana'];
    }
    
    if($user['hp']<='0' or $user['hp']<=$mobu){
        $_SESSION['rez'] = "<font color=red></center>Вы проиграли бой</center></font><hr>";
        mysql_query("UPDATE `users` SET `lose`=`lose`+'1' WHERE `id`='$uid'");
        mysql_query("UPDATE `mobs` SET `user`='',`time`='0' WHERE `id`='$id'");
        header('Location: hunter.php');
        exit;
    }
    
    if($mob['hp']<='0' or $mob['hp']<=$uron){
        mysql_query("UPDATE `mobs` SET `hp`='0' WHERE `id`='$mob[id]'");
        header('Location: battle.php?sd=win&id='.$mob['id'].'');
        exit;
    }
    
    
    mysql_query("UPDATE `users` SET `hp`=`hp`-'$mobu',`mp`='$mana' WHERE `id`='$uid'");
    
    
    mysql_query("UPDATE `mobs` SET `hp`=`hp`-'$uron' WHERE `id`='$id'");
    
    header('Location: battle.php?id='.$id.'');
    exit;

break;


case 'win':
if($mob['hp']>'0'){
    header('Location: battle.php?id='.$mob['id'].'');
    exit;
}

/** КВЕСТ **/
$q = mysql_query("SELECT * FROM `us_quest` WHERE `user`='$uid' and `mob`='$mob[lat]'") or die(mysql_error());
$e = mysql_num_rows($q);
if($e>='1'){
    mysql_query("UPDATE `us_quest` SET `kill`=`kill`+'1' WHERE `user`='$uid' and `mob`='$mob[lat]'") or die(mysql_error());
}



if($user['premium']=='1'){
    if($game['winopp']==$user['storona']){$oppexp=($mob['exp']/100)*15;$oppsilver=($mob['silver']/100)*15;}else{$oppsilver=0;$oppexp=0;}
    $silver=($mob['silver']+$oppsilver)*2;
    $exp=round(($mob['exp']+$oppexp)*2);
}else{
    if($game['winopp']==$user['storona']){$oppexp=($mob['exp']/100)*15;$oppsilver=($mob['silver']/100)*15;}else{$oppsilver=0;$oppexp=0;}   
    $silver=$mob['silver']+$oppsilver;
    $exp=round($mob['exp']+$oppexp);
}

if($user['refer']!==''){
    $msilver=round(($silver/100)*20);
    $mexp=round(($exp/100)*25);
    mysql_query("UPDATE `users` SET `silver`=`silver`+'$msilver',`exp`=`exp`+'$mexp' WHERE `id`='$user[refer]'");
}




$q = mysql_query("SELECT * FROM `baff` WHERE `user`='$uid' and `time`>'".time()."'");
$e = mysql_num_rows($q);
if($e>='1'){
    
    $w = mysql_fetch_assoc($q);
    
    if($w['exp']>='1'){
        $exp = ($exp/100)*$w['exp'];
    }
    if($w['silver']>='1'){
        $silver = ($silver/100)*$w['silver'];
    }
    
}

if($user['orden']!==''){
    $ordenexp = $exp/80;
    
    mysql_query("UPDATE `orden` SET `exp`=`exp`+'$ordenexp' WHERE `lider`='$user[orden]'");
	mysql_query("UPDATE `users` SET `orden_exp`=`orden_exp`+'$ordenexp' WHERE `id`='$uid'");
}


/** профа **/
if($user['profa']=='hunter'){
$p = explode(',', $mob['part']);
$p1 = count($p);
$p2 = rand(0,$p1-1);

$pr = $p[$p2];


$rdn2 = mt_rand(1,2);

    
    $hk = mysql_fetch_array(mysql_query("SELECT * FROM `hunter_knife` WHERE `id`='$user[hunter_knife]'"));
    $q3 = mysql_query("SELECT * FROM `hunter_res` WHERE `id`='$pr' and `need`<='$hk[need]'");
    
    $pt = mysql_fetch_array($q3);
    
    if($pt['id']>='1' and $pt['id']!==''){
    $q1 = mysql_query("SELECT * FROM `a_hunter` WHERE `user`='$uid' and `tip`='$pt[id]'");
    if(mysql_num_rows($q1)=='0'){
        mysql_query("INSERT INTO `a_hunter` SET `user`='$uid',`name`='$pt[name]',`img`='$pt[img]',`tip`='$pt[id]',`kol`='1'");
        mysql_query("UPDATE `users` SET `p_hunter`=`p_hunter`+'1' WHERE `id`='$uid'");
    
        set('profpart', "Вы извлекли трофей <img src='/img/profa/$pt[img]' width='24'><br>Репутация охотника: <img src='/img/profa/hunter.png' width='16'> +1<br>");
    }else{
        mysql_query("UPDATE `a_hunter` SET `kol`=`kol`+'1' WHERE `user`='$uid' and `tip`='$pt[id]'");
        mysql_query("UPDATE `users` SET `p_hunter`=`p_hunter`+'1' WHERE `id`='$uid'");
    
        set('profpart', "Вы извлекли трофей <img src='/img/profa/$pt[img]' width='24'><br>Репутация охотника: <img src='/img/profa/hunter.png' width='16'> +1<br>");
    }
    }
    

}



/** **/


/** ДРОП **/
$a = explode(",", $mob['drop']);

$a1 = count($a);
$a2 = rand(0,$a1-1);
$drid = $a[$a2];

$drop = mysql_query("SELECT * FROM `drop` WHERE `id`='$drid' LIMIT 1") or die(mysql_error());
$dr = mysql_fetch_assoc($drop);
$rnd=rand($dr['rand'],100);
if($rnd==$dr['rand']){

// ключ
if($dr['tip']=='key'){
    mysql_query("UPDATE `users` SET `key`=`key`+'1' WHERE `id`='$uid'");
	$_SESSION['dropkey']='yes';
    $drops="<br><img src='img/key.png'> 1 $dr[name]";
}

// капсула опыта
if($dr['tip']=='exp'){
    mysql_query("INSERT INTO `cap_exp` SET 
    `user`='$uid',
    `name`='$dr[name]',
    `img`='exp.png',
    `exp`='$dr[exp]',
    `kol`='1'") or die(mysql_error());
   	$_SESSION['dropcap']='yes';
    $drops = "<br><img src='/img/res/$dr[img]' width='16'> $dr[name]";
}
//
    
// вещи
if($dr['tip']=='shmot'){
    
mysql_query("INSERT INTO `item` SET 
`user`='$uid',
`name`='$dr[name]',
`tip`='$dr[tip2]',
`level`='$dr[level]',
`img`='$dr[img]',
`hp`='$dr[hp]',
`mp`='$dr[mp]',
`min_u`='$dr[min_u]',
`max_u`='$dr[max_u]',
`status`='not'");
$drops = "<br><img src='/img/shmot/$dr[img]' width='32'> $dr[name]";
}
//

// ресурсы
if($dr['tip']=='res'){
$sd22 = mysql_num_rows(mysql_query("SELECT * FROM `res` WHERE `user`='$uid' and `res`='$dr[res]'"));
if($sd22 == '0'){
    mysql_query("INSERT INTO `res` SET `user`='$uid',`name`='$dr[name]',`res`='$dr[res]',`kol`='1',`img`='$dr[img]'") or die(mysql_error());
}else{
    mysql_query("UPDATE `res` SET `kol`=`kol`+'1' WHERE `user`='$uid' and `res`='$dr[res]'") or die(mysql_error());
}
$drops = "<br><img src='/img/res/$dr[img]' width='16'> $dr[name]";
}
//
}

mysql_query("UPDATE `users` SET `win`=`win`+'1',`mobkill`=`mobkill`+'1',
`silver`=`silver`+'$silver',
`exp`=`exp`+'$exp',`mp`='0' WHERE `id`='$uid'");
mysql_query("UPDATE `mobs` SET `user`='',`hp`=`hpall`,`time`='0' WHERE `id`='$id'") or die(mysql_error());


$_SESSION['winq']='yes';
$_SESSION['rez'] = "<font color=lightgreen>Вы выиграли бой</font><br>Серебро: <img src='img/silver.png'> $silver, Опыт: <img src='img/exp.png'>$exp $drops";
header('Location: hunter.php');
exit;
break;


}
include ('tmp/foot.php');
?>