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

if($user['level']<'5'){
    header('Location: main.php');
    exit;
}

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

switch($sd){
default:

if($game['opp']=='0'){
    
    if(isset($_GET['aigree'])){
        $e = mysql_num_rows(mysql_query("SELECT * FROM `opp` WHERE `user`='$uid'"));
        if($e=='0'){
            mysql_query("INSERT INTO `opp` SET `user`='$uid',`storona`='$user[storona]'");
            header("Location: opp.php");
        }
    }
    
    echo "<font size=2 color=grey>Великая Битва <img src='img/white.png' width='14'> <font color=lightblue>Света</font> и <img src='img/dark.png' width='14'> <font color=darkred>Тьмы</font> проходит каждый день, начинается в 18:00 и продолжается 30 минут.</font><br><br><br>";
    
    $alldark = mysql_num_rows(mysql_query("SELECT * FROM `opp` WHERE `storona`='dark'"));
    $allwhite = mysql_num_rows(mysql_query("SELECT * FROM `opp` WHERE `storona`='white'"));
    $all = mysql_num_rows(mysql_query("SELECT * FROM `opp`"));
    
    
    echo "<center>
    <font color=darkred>$alldark</font> <img src='img/opp.png' width='20'> <font color=lightblue>$allwhite</font><br>
    <font size=2 color=grey>Всего участников: $all</font><br>";
        
    $o = mysql_query("SELECT * FROM `opp` WHERE `user`='$uid'");
    $oe = mysql_num_rows($o);
    if($oe=='0'){
        echo "<a class='mybutt_no' href='?aigree'>Принять участие</a><br><br>";
    }else{
        echo "<a class='mybutt_no' href=''>Ожидайте...</a><br><br>";
    }
    
   
$date = strtotime('16:00:00');
$sec=$date - time();  
$days=floor(($date - time()) /86400);  
$h1=floor(($date - time()) /3600);  
$m1=floor(($date - time()) /60);  
$hour=floor($sec/60/60 - $days*24);  
$hours=floor($sec/60/60);  
$min=floor($sec/60 - $hours*60);  
 
switch(substr($days, -1)){  
case 1: $o='остался';  
break;  
case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: case 0: $o='осталось';  
break;}  
 
switch(substr($days, -2)){  
case 1: $d='день';  
break;  
case 2: case 3: case 4: $d='дня';  
break;  
default: $d='дней';  
}  
 
switch(substr($hour, -2)) {  
case 1: $h='час';  
break;  
case 2: case 3: case 4: $h='часа';  
break;  
default: $h='часов';  
}  
 
switch(substr($min, -2)) {  
case 1: $m='минута';  
break;  
case 2: case 3: case 4: $m='минуты';  
break;
default:$m='минут';
}   

if ($sec>0) echo '<small>Битва начнётся через:';
if ($days>0) echo $days.'&nbsp;'.$d;
if ($h1>0) echo '&nbsp;'.$hour.'&nbsp;'.$h;
if ($m1>0) echo '&nbsp;'.$min.'&nbsp;'.$m;
     
     
    echo "</small></center>";
    
    
    echo "<hr><a href='?sd=last'>Прошлые битвы</a>";
    
}else{
    
    $p = mysql_num_rows(mysql_query("SELECT * FROM `opp` WHERE `user`='$uid'"));
    if($p=='0'){
        echo "Битва ужё идёт,вы не успели принят в ней участие<br>";
        include ('tmp/foot.php');
        exit;
    }
    
    
    $date = strtotime('16:30:00');
$sec=$date - time();  
$days=floor(($date - time()) /86400);  
$h1=floor(($date - time()) /3600);  
$m1=floor(($date - time()) /60);  
$hour=floor($sec/60/60 - $days*24);  
$hours=floor($sec/60/60);  
$min=floor($sec/60 - $hours*60);  
 
switch(substr($days, -1)){  
case 1: $o='остался';  
break;  
case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: case 0: $o='осталось';  
break;}  
 
switch(substr($days, -2)){  
case 1: $d='день';  
break;  
case 2: case 3: case 4: $d='дня';  
break;  
default: $d='дней';  
}  
 
switch(substr($hour, -2)) {  
case 1: $h='час';  
break;  
case 2: case 3: case 4: $h='часа';  
break;  
default: $h='часов';  
}  
 
switch(substr($min, -2)) {  
case 1: $m='минута';  
break;  
case 2: case 3: case 4: $m='минуты';  
break;
default:$m='минут';
}   

if ($sec>0) echo '<small>Битва закончится через:';
if ($days>0) echo $days.'&nbsp;'.$d;
if ($h1>0) echo '&nbsp;'.$hour.'&nbsp;'.$h;
if ($m1>0) echo '&nbsp;и&nbsp;'.$min.'&nbsp;'.$m;

$dark = mysql_num_rows(mysql_query("SELECT * FROM `opp` WHERE `storona`='dark' and `mertv`='0'"));
$white = mysql_num_rows(mysql_query("SELECT * FROM `opp` WHERE `storona`='white' and `mertv`='0'"));

echo "<br>Живых: <font color=darkred>$dark</font> <img src='img/opp.png' width='20'> <font color=lightblue>$white</font>";
    
    echo "<hr><center>";
    
    if(isset($_GET['restore'])){
        $mp = mysql_fetch_assoc(mysql_query("SELECT * FROM `opp` WHERE `user`='$uid'"));
        if($mp['mertv']=='1' or $user['hp']<='0'){
            mysql_query("UPDATE `opp` SET `mertv`='0' WHERE `user`='$uid'");
            mysql_query("UPDATE `users` SET `hp`='$user[hpall]' WHERE `id`='$uid'");
            exit;
        }
    }
    
    $mp = mysql_fetch_assoc(mysql_query("SELECT * FROM `opp` WHERE `user`='$uid'"));
    if($mp['mertv']=='1' or $user['hp']<='0'){
        echo "Вас убили.<br><a href='?restore'>Воскреснуть</a>";
        include ('tmp/foot.php');
        exit;
    }
    

    if($user['storona']=='white'){$sta='dark';}else{$sta='white';}
    
    $q = mysql_query("SELECT * FROM `opp` WHERE `storona`='$sta' and `mertv`='0' ORDER BY RAND() LIMIT 1");
    $e = mysql_num_rows($q);
    if($e=='0'){
        echo "Противников нет, ожидайте...<br>";
        include ('tmp/foot.php');
        exit;
    }else{
        $w = mysql_fetch_assoc($q);
        
        if($use=='' or $user=='0'){$idu=$w['user'];}else{$idu=$use;}
        
        $us = mysql_fetch_assoc(mysql_query("SELECT * FROM `users` WHERE `id`='$idu'"));
        
        if($us['hp']<='0'){
            header('Location: opp.php');
            exit;
        }
        
        echo "<img src='img/$us[pol].png' width='38'><br><small>$us[login] $us[level] ур.<br>";
        $hpm = ($us['hp']/$us['hpall'])*100;

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

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

        echo "<li><a style='color:red' href='?sd=ataka&use=$us[id]'>АТАКА (+25% к урону)</a></li>";
        echo "<li><a style='color:lightblue' href='?sd=mana&use=$us[id]'>МАНА (+30% к мане)</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='?sd=mag&mag=$m2[mag]&use=$us[id]'><img src='img/mag/$m2[img]' width='40'></a>&nbsp;&nbsp;&nbsp;";
        
    }
    echo "<div class='linem2'></div>";
}



$mp = ($user['mp']/$user['mpall'])*100;
   
echo "<font color=lightblue>Мана: $user[mp]</font><br></center>";
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><center>';
}

$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='?sd=potion&p=$lw[id]&use=$us[id]'><img src='img/elixir/$lw[img]' width='40'></a>&nbsp;&nbsp;&nbsp;";
        
    }
    
}
        

        echo "</small>";
        
    }
    
    echo "<a href='opp.php?'>Другой противник</a>";
    
    echo "</center>";
}


break;


case 'potion':
$p2 = mysql_num_rows(mysql_query("SELECT * FROM `opp` WHERE `user`='$uid'"));
    if($p2=='0'){
        echo "Битва ужё идёт,вы не успели принят в ней участие<br>";
        include ('tmp/foot.php');
        exit;
    }
    $mp = mysql_fetch_assoc(mysql_query("SELECT * FROM `opp` WHERE `user`='$uid'"));
   
$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: opp.php?use=$use");
    exit;
}
$w = mysql_fetch_assoc($q);

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

if($w['kol']<='0'){
    header("Location: opp.php?use=$use");
    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'");


header("Location: opp.php?use=$use");
exit;

break;


case 'mag':
$p = mysql_num_rows(mysql_query("SELECT * FROM `opp` WHERE `user`='$uid'"));
    if($p=='0'){
        echo "Битва ужё идёт,вы не успели принят в ней участие<br>";
        include ('tmp/foot.php');
        exit;
    }
   
$us = mysql_fetch_assoc(mysql_query("SELECT * FROM `users` WHERE `id`='$use'"));
$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: opp.php?use='.$use.'');
    exit;
}
$w = mysql_fetch_assoc($q);

if($user['mp']<$w['mp']){
    header('Location: opp.php?use='.$use.'');
    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'");
}
}


    
    if($us['hp']<='0' or $us['hp']<=$uron){
        mysql_query("UPDATE `opp` SET `mertv`='1' WHERE `user`='$us[id]'");
        mysql_query("UPDATE `opp` SET `kill`=`kill`+'1' WHERE `user`='$uid'");
        header('Location: opp.php');
        exit;
    }
    

mysql_query("UPDATE `users` SET `mp`=`mp`-'$w[mp]' WHERE `id`='$uid'");
    
mysql_query("UPDATE `opp` SET `uron`=`uron`+'$uron' WHERE `user`='$uid'");
mysql_query("UPDATE `users` SET `hp`=`hp`-'$uron' WHERE `id`='$us[id]'");
    
header('Location: opp.php?use='.$use.'');
exit;

break;


case 'mana':
$p = mysql_num_rows(mysql_query("SELECT * FROM `opp` WHERE `user`='$uid'"));
    if($p=='0'){
        echo "Битва ужё идёт,вы не успели принят в ней участие<br>";
        include ('tmp/foot.php');
        exit;
    }
    $mp = mysql_fetch_assoc(mysql_query("SELECT * FROM `opp` WHERE `user`='$uid'"));
    
$us = mysql_fetch_assoc(mysql_query("SELECT * FROM `users` WHERE `id`='$use'"));
    if($user['time_udar']>=time()){
        $uron2 = rand($user['min_u'],$user['max_u']);
        $uron = $uron2/10;
    }else{
        $uron = rand($user['min_u'],$user['max_u']);
    }
    
    $tu = time()+1;
    mysql_query("UPDATE `users` SET `time_udar`='$tu' WHERE `id`='$uid'");
    
  
    $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;
    }
    
    
    
    mysql_query("UPDATE `users` SET `mp`='$mana' WHERE `id`='$uid'");
    
    if($us['hp']<='0' or $us['hp']<=$uron){
        mysql_query("UPDATE `opp` SET `mertv`='1' WHERE `user`='$us[id]'");
        mysql_query("UPDATE `opp` SET `kill`=`kill`+'1' WHERE `user`='$uid'");
        header('Location: opp.php?');
        exit;
    }
    

    
mysql_query("UPDATE `opp` SET `uron`=`uron`+'$uron' WHERE `user`='$uid'");
mysql_query("UPDATE `users` SET `hp`=`hp`-'$uron' WHERE `id`='$us[id]'");
    
    header('Location: opp.php?use='.$use.'');
    exit;

break;



case 'ataka':
$p = mysql_num_rows(mysql_query("SELECT * FROM `opp` WHERE `user`='$uid'"));
    if($p=='0'){
        echo "Битва ужё идёт,вы не успели принят в ней участие<br>";
        include ('tmp/foot.php');
        exit;
    }
    $mp = mysql_fetch_assoc(mysql_query("SELECT * FROM `opp` WHERE `user`='$uid'"));
    
    
$us = mysql_fetch_assoc(mysql_query("SELECT * FROM `users` WHERE `id`='$use'"));
    $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($user['krit']>='50'){
        $kr = rand(2,4);
        if($kr=='3'){
            $krit=round(($uron1/100)*$user['int']);
            $uron=$uron1+$krit;
        }else{
            $uron=$uron1;
        }
    }else{
        $uron=$uron1;
    }
    
    
    $tu = time()+1;
    mysql_query("UPDATE `users` SET `time_udar`='$tu' WHERE `id`='$uid'");
    
    
    $mans = $user['mp']+$user['mana'];
    
    if($mans>=$user['mpall']){
        $mana = $user['mpall'];
    }else{
        $mana = $user['mp']+$user['mana'];
    }
    
    
    
    mysql_query("UPDATE `users` SET `mp`='$mana' WHERE `id`='$uid'");
    
    
    if($us['hp']<='0' or $us['hp']<=$uron){
        mysql_query("UPDATE `opp` SET `mertv`='1' WHERE `user`='$us[id]'");
        mysql_query("UPDATE `opp` SET `kill`=`kill`+'1' WHERE `user`='$uid'");
        header('Location: opp.php');
        exit;
    }
    

    
mysql_query("UPDATE `opp` SET `uron`=`uron`+'$uron' WHERE `user`='$uid'");
mysql_query("UPDATE `users` SET `hp`=`hp`-'$uron' WHERE `id`='$us[id]'");
    
    header('Location: opp.php?use='.$use.'');
    exit;

break;


case 'last':

$q = mysql_query("SELECT * FROM `opp_last` ORDER BY `id` DESC LIMIT 5");
$e = mysql_num_rows($q);
if($e=='0'){
    echo "Великих битв ещё не было.";
}else{
    echo "</div>";
    While($w = mysql_fetch_assoc($q)){
        
        
        
        echo "<div class='elem'><small><img src='img/opp.png' width='16'> $w[date]<br>
        Победили: <img src='img/$w[win].png' width='16'> ";
        if($w['win']=='dark'){echo "Тёмные";}else{echo "Светлые";}
        $us1 = mysql_fetch_assoc(mysql_query("SELECT * FROM `users` WHERE `id`='$w[uron]'"));
        $us2 = mysql_fetch_assoc(mysql_query("SELECT * FROM `users` WHERE `id`='$w[kill]'"));
        echo "<br>Лучшие:<br>По урону: <a href='user.php?id=$us1[id]'>$us1[login]</a><br>По убийствам: <a href='user.php?id=$us2[id]'>$us2[login]</a><br>Всего участников: $w[all]</small></div>";
        
    }
    echo "<div class='main'>";
    
}

break;


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