<?
define('PROTECTOR', 1);


$head = 'duel';//фикс. места

if($_GET[mod]=="udar"){
$header=TRUE;
}else{
$textl='Онлайн Дуэли. Бой!';}
@include('files/db.php');
@include('files/auth.php');
@include('files/func.php');
@include('files/core.php');
@include('files/head.php');
@include('files/zag.php');
//
$time=time();
$timer=time()+300;
///Глобальные переменные///
$req = mysql_query("SELECT * FROM `userduel` WHERE `usr` = '$log' LIMIT 1");
$duel = mysql_fetch_array($req);
$redu = mysql_query("SELECT * FROM `duel` WHERE `id` = '$duel[duid]' LIMIT 1");
$dued = mysql_fetch_array($redu);
switch($_GET[mod]){

default:
if($dueli==0){echo'<div class="a"><font color="#AAAAAA">Вы не участвуете в бою</font></div>
<a href="duel.php?">Продолжить »</a>';break;
}
if($duel[storona]=="white"){$pr="black";}else{$pr="white";}
$rand=rand(1000000,10000000);
echo"<div class='a'><b>Битва</b></div>$div";
$win = mysql_num_rows(mysql_query("SELECT `duid` FROM `userduel` WHERE `storona` = '$pr' and `duid`='$duel[duid]'"));
if($win<=0){
echo'<div class="a"><font color="#AAAAAA">Дуэль не началась, так как нет противников</font></div>';
mysql_query("DELETE FROM `duel` WHERE `id` = '$duel[duid]'");
echo"<ul class='nav'>";
echo"<li><a href=\"fiting.php?rand=$rand\"> <img src='pic/game/reload.png' alt='*'/> Обновить </a></li>";
echo"<li><a href=\"fiting.php?mod=exit\"> <img src='pic/game/ext.png' alt='*'/> Покинуть бой </a></li>";break;
}
if($win>=1){
if(empty($duel[usr])){
echo"<img src='pic/game/ext.png' alt='*'/><a href=\"start.php?\"> Продолжить »</a> ";break;
}
$wins = mysql_num_rows(mysql_query("SELECT `duid` FROM `userduel` WHERE `storona` = '$pr' and `status`='die' and `duid`='$duel[duid]'"));
if($win=$wins){
if($udata[lvl]>=1 and $udata[lvl]<=10){$bon=30;}
if($udata[lvl]>10 and $udata[lvl]<=20){$bon=50;}
if($udata[lvl]>20 and $udata[lvl]<=30){$bon=100;}
if($udata[lvl]>30 and $udata[lvl]<=40){$bon=200;}
if($udata[lvl]>40 and $udata[lvl]<=50){$bon=300;}
if($udata[lvl]>50 and $udata[lvl]<=60){$bon=400;}
if($udata[lvl]>60 and $udata[lvl]<=71){$bon=500;}
if($duel[status]="active"){
$viz=round($udata[lvl]*1.5+$bon);}else{$viz=0;}
$wins=$udata[lvl]+$bon;
$die=$duel[dies]*$bon;
$itogo=$wins+$die;
echo"<div class='a'><img src='pic/game/losed.jpg' alt='*'/> <br/><img src='pic/game/wins.png' alt='*'/> Бой завершен. Ты выиграл.<br/></div>";
echo"<font color='#ffc22b'><b>Твоя награда:</font></b>";
echo"<div class='log'>
Выживание: <img src='pic/bazar.gif' alt='*'/> <b>$viz</b><br/>
Победа: <img src='pic/main/auction.png' alt='*'/> <b>$wins</b><br/>
Убийство: <img src='pic/main/auction.png' alt='*'/> <b>$die</b><br/>
Итого: <b><img src='pic/bazar.gif' alt='*'/> $viz <img src='pic/main/auction.png' alt='*'/> $itogo</b><br/></div>";
mysql_query("UPDATE `users` SET `money` = '$udata[money]'+'$viz', `exp`='$udata[exp]'+'$itogo' WHERE `usr`='$log'") or die (mysql_error());
$usr="$log x";
mysql_query("UPDATE `userduel` SET `usr` = '$usr' WHERE `usr`='$log' LIMIT 1") or die (mysql_error());
$du = mysql_num_rows(mysql_query("SELECT `duid` FROM `userduel` WHERE `storona` = '$duel[storona]' and `status`='active' and `duid`='$duel[duid]'"));
if($du<=0){mysql_query("DELETE FROM `duel` WHERE `id` = '$duel[duid]'");}
echo"<img src='pic/game/ext.png' alt='*'/><a href=\"start.php?\"> Продолжить »</a> ";break;
}
}
if($udata[hp]<=0){
echo'<div class="a"><font color="red">Ты мертв. Эта битва завершена для тебя</font></div>';
echo"<ul class='nav'>";
echo"<li><a href=\"fiting.php?rand=$rand\"> <img src='pic/game/reload.png' alt='*'/> Обновить </a></li>";
echo"<li><a href=\"fiting.php?mod=exit\"> <img src='pic/game/ext.png' alt='*'/> Покинуть бой </a></li>";
}else{
echo"<img src='pic/game/5.jpg' alt='*'/><br/>";
if(empty($_GET[oponent])){
$boy=mysql_query("SELECT * FROM `userduel` WHERE `storona`='$pr' and `duid`='$dued[id]' and `status`='active' ORDER BY RAND() LIMIT 1");
$oponent = mysql_fetch_array($boy);
$_GET[oponent]="$oponent[usr]";
}
echo"<ul class='nav'>";
echo"<li><a href=\"fiting.php?mod=udar&oponent=$_GET[oponent]\"> <img src='pic/red.png' alt='*'/> Атаковать $_GET[oponent] </a></li>";
$req=mysql_query("SELECT umen,timer FROM duellog WHERE usr = '$log' and timer>='".time()."' and umen='1' ORDER BY id DESC LIMIT 1");
$su = mysql_fetch_array($req);
if($su[timer]+8>$timer){$vr=8-($timer-$su[timer]);$vre="0:0$vr";$cvet="<font color='#AAAAAA'>";}
echo"<li><a href=\"fiting.php?mod=udar&oponent=$_GET[oponent]&umen=sudar\"> <img src='pic/game/sudar.png' alt='*'/>$cvet Сильный удар $vre </a></font></li>";
echo"<li><a href=\"fiting.php?mod=udar&oponent=$_GET[oponent]&umen=sfera\"> <img src='pic/game/sfera.png' alt='*'/> Сфера защиты </a></li>";
////Рандом игроков///
$users=mysql_num_rows(mysql_query("SELECT `duid` FROM `userduel` WHERE `storona`='$pr' and `duid`='$dued[id]' and `status`='active'"));
if($users>=2){
$users=mysql_fetch_array(mysql_query("SELECT `usr` FROM `userduel` WHERE `usr`!='$_GET[oponent]' and `storona`='$pr' and `duid`='$dued[id]' ORDER BY RAND() LIMIT 1"));
echo"<li><a href=\"fiting.php?mod=udar&oponent=$users[usr]\"> <img src='pic/red.png' alt='*'/> Бить любого </a></li>";
}
}//die
$kom=mysql_num_rows(mysql_query("SELECT `duid` FROM `userduel` WHERE `storona`='$duel[storona]' and `duid`='$dued[id]' "));
echo"<li><a href=\"fiting.php?mod=kom\"> <img src='pic/game/kom.png' alt='*'/> Моя команда ($kom) </a></li>";
echo"</ul>";
echo"$div <font color='chocolate'>Твои противники:</font> $div";
///Враги///
$boy=mysql_query("SELECT * FROM `userduel` WHERE `storona`='$pr' and `duid`='$dued[id]' ");
While($vrag=mysql_fetch_array($boy)){
if($vrag[status]=="active"){
echo"<img src='pic/main/$vrag[storona].png' alt='*'/> <a href='fiting.php?mod=udar&oponent=$vrag[usr]'>$vrag[usr]</a>
<img src='pic/up.png' alt='*'/> $vrag[lvl] [<img src='pic/game/mob/hp.gif' alt='*'/>"; 
$hp=mysql_fetch_array(mysql_query("SELECT hp FROM `users` WHERE `usr`='$vrag[usr]' LIMIT 1"));
echo"$hp[hp]],"; }
if($vrag[status]=="die"){
echo"<img src='pic/main/$vrag[storona].png' alt='*'/> <font color='gray'>$vrag[usr]</font>
<img src='pic/game/die.png' alt='*'/> $vrag[lvl], "; 
}
}
echo"$div <font color='chocolate'>Боевой журнал</font> $div";
$protiv = mysql_query("SELECT * FROM `userduel` WHERE `duid` = '$duel[duid]' and `storona`='$pr' ");
$souz = mysql_query("SELECT * FROM `userduel` WHERE `duid` = '$duel[duid]' and `storona`='$udata[storona]' ");
log_duel($tlog='duel');
break;

case 'udar':
$req=mysql_query("SELECT umen,timer FROM duellog WHERE usr = '$log' and timer>='".time()."' and umen='1' ORDER BY id DESC LIMIT 1");
$su = mysql_fetch_array($req);
if($su[timer]+8>$timer and $_GET[umen]=="sudar"){header ("Location: fiting.php?");exit;}
if(empty($_GET[oponent])){header ("Location: fiting.php?");exit;}
if($udata[hp]<=0){header ("Location: fiting.php?");exit;}
$oponent = htmlspecialchars(addslashes(stripslashes($_GET['oponent'])));
$user = mysql_query("SELECT * FROM `users` WHERE `usr`='$oponent' ");
$rest = substr("$oponent", 0, -2);
if($rest==" x"){header ("Location: fiting.php?");exit;}

$vrag = mysql_fetch_array($user);
$udar = $udata[sila]-$vrag[prot];
if($udar>=0 and $udar<=10){$bon=2;}
if($udar>10 and $udar<=30){$bon=4;}
if($udar>30 and $udar<=50){$bon=8;}
if($udar>50 and $udar<=70){$bon=12;}
if($udar>70 and $udar<=100){$bon=16;}
if($udar>100){$bon=20;}
if($udar<=0){
$shans=rand(0,100);
if($shans>=0 and $shans<=10){$udar=rand(3,5);}
if($shans>=10 and $shans<=30){$udar=rand(2,4);}
if($shans>=30 and $shans<=60){$udar=rand(1,3);}
if($shans>=60 and $shans<=100){$udar=rand(1,2);}
}else{
$shans=rand(0,100);
if($shans>=0 and $shans<=10){$udar=rand(7,10);}
if($shans>=10 and $shans<=30){$udar=rand(6,8);}
if($shans>=30 and $shans<=60){$udar=rand(5,6);}
if($shans>=60 and $shans<=100){$udar=rand(4,4);}
}
if($udata[lovk]-$vrag[lovk]<0){$uvorot=50;}else{$uvorot=25;}
$umen=mysql_query("SELECT `sfera` FROM `userduel` WHERE `usr`='$oponent' ");
$sfera = mysql_fetch_array($user);
if($_GET[umen]=="sudar" and $sfera[sfera]==0){$udar=$udar*1.5;}
$req=mysql_query("SELECT * FROM duellog WHERE usr = '$log' and timer>='".time()."' ORDER BY id DESC LIMIT 1");
$ilog=mysql_fetch_array($req);
$razn=$timer-$ilog[timer];
if($razn<=2){$uvorot=$uvorot+25;}
if($razn>=2 and $razn<=5){$uvorot=$uvorot;}
if($razn>=5 and $razn<=10){$uvorot=$uvorot-15;}
$sravn=rand(0,100);
if($sravn<$uvorot){$udar=0;}
if($udar==0){$sms="Промах";$pr=1;}
if($pr!==1){
$uron=round($vrag[hp]-(($vrag[hp]/100)*$udar)-$bon);
$udar=round(($vrag[hp]/100)*$udar+$bon);
$msg=rand(0,2);
if($msg=="0"){$sms="ударил";}
if($msg=="1"){$sms="поразил";}
if($msg=="2"){$sms="сразил";}
if($_GET[umen]=="sfera"){$sms=html_entity_decode("использовал</font> сфера защиты");$um=1;$uron=0;}
if($_GET[umen]=="sudar"){$sms=html_entity_decode("использовал</font> сильный удар");$um=1;}
if($uron<=0){$sms="убил";
$die=$duel[dies]+1;
mysql_query("UPDATE `userduel` SET `status` = 'die' WHERE `usr`='$oponent'") or die (mysql_error());
mysql_query("UPDATE `userduel` SET `dies` = '$die' WHERE `usr`='$log'") or die (mysql_error());
}

mysql_query("UPDATE `users` SET `hp` = '$uron' WHERE `usr`='$oponent'") or die (mysql_error());
mysql_query("UPDATE `userduel` SET `hp` = '$uron' WHERE `usr`='$oponent'") or die (mysql_error());
mysql_query("INSERT INTO duellog SET usr='$log',text='$sms',opon='$oponent',uron='$udar',timer='$timer', umen='$um'");}
else{
mysql_query("INSERT INTO duellog SET usr='$log',text='$sms',timer='$timer', umen='$um'");
}
$b = html_entity_decode($_POST[name]);
if($uron<=0){$oponent="";}
header ("Location: fiting.php?oponent=$oponent");exit;
break;

case 'exit':
$usr="$log x";
mysql_query("UPDATE `userduel` SET `usr` = '$usr' WHERE `usr`='$log' LIMIT 1") or die (mysql_error());
echo"<div class='a'><b>Битва</b></div>$div";
echo"<font color='red'>Вы покинули бой. </font><br/><a href=\"start.php?\">Продолжить »</a>";
break;

case 'kom':
echo"<div class='a'><b>Битва</b></div>$div";
echo"<img src='pic/game/kom.png' alt='*'/> Моя команда:$div";
if($duel[storona]=="white"){$pr="black";}else{$pr="white";}

$boy=mysql_query("SELECT * FROM `userduel` WHERE `storona`='$duel[storona]' and `duid`='$dued[id]' ");
While($vrag=mysql_fetch_array($boy)){
if($vrag[status]=="active"){
echo"<img src='pic/main/$vrag[storona].png' alt='*'/> <a href='fiting.php?mod=udar&oponent=$vrag[usr]'>$vrag[usr]</a>
<img src='pic/up.png' alt='*'/> $vrag[lvl] [<img src='pic/game/mob/hp.gif' alt='*'/>"; 
$hp=mysql_fetch_array(mysql_query("SELECT hp FROM `users` WHERE `usr`='$vrag[usr]' LIMIT 1"));
echo"$hp[hp]],"; }
if($vrag[status]=="die"){
echo"<img src='pic/main/$vrag[storona].png' alt='*'/> <font color='gray'>$vrag[usr]</font>
<img src='pic/game/die.png' alt='*'/> $vrag[lvl], "; 
}
}
echo"$div <img src='pic/game/vrag.png' alt='*'/> Противники:$div";
$boy=mysql_query("SELECT * FROM `userduel` WHERE `storona`='$pr' and `duid`='$dued[id]' ");
While($vrag=mysql_fetch_array($boy)){
if($vrag[status]=="active"){
echo"<img src='pic/main/$vrag[storona].png' alt='*'/> <a href='fiting.php?mod=udar&oponent=$vrag[usr]'>$vrag[usr]</a>
<img src='pic/up.png' alt='*'/> $vrag[lvl] [<img src='pic/game/mob/hp.gif' alt='*'/>"; 
$hp=mysql_fetch_array(mysql_query("SELECT hp FROM `users` WHERE `usr`='$vrag[usr]' LIMIT 1"));
echo"$hp[hp]],"; }
if($vrag[status]=="die"){
echo"<img src='pic/main/$vrag[storona].png' alt='*'/> <font color='gray'>$vrag[usr]</font>
<img src='pic/game/die.png' alt='*'/> $vrag[lvl], "; 
}
}
break;
}
@include('files/down.php');
?>