<?php
require_once ('system/func.php');
$duel = mysql_fetch_array(mysql_query('SELECT * FROM `duel` WHERE `id_user` = "'.$myID.'"'));
if($duel[start] != 1){
header('Location: /duel/');
exit();
}

$_opponent = mysql_fetch_array(mysql_query("SELECT * FROM `users` WHERE `side` != '$user[side]' AND `str`+`def`+`max_health` >= '".ceil($user[str]+$user[def]+$user[max_health]*40/100)."' ORDER BY RAND() LIMIT 1"));
$opponent = mysql_fetch_array(mysql_query('SELECT * FROM `users` WHERE `id` = "'.$duel[opponent].'"'));
$l_ch = mysql_query("SELECT * FROM `l_ch` WHERE `id_1` = '".$myID."' AND `id_2` = '$opponent[id]' OR `id_1` = '".$opponent['id']."' AND `id_2` = '$myID' ORDER BY `id` LIMIT 1");
//$l_ch = mysql_fetch_array($l_ch);
if(!$l_ch){
mysql_query("INSERT INTO `l_ch` SET `id_1` = '$myID', `id_2` = '$opponent[id]', `schet_1` = '0', `schet_2` = '0'");
}
$user[health] = $duel[user_health];
if($user[health] < 0)$user[health] = 0;
if($duel[opp_health] <= 0 and $user[health] <= 0){
$exp = exp_koll(rand($opponent[level]*5,$opponent[level]*10));
if($effect_2[on] == 1)$exp = $exp+ceil($exp*25/100);
$crystals = rand($opponent[level]*5,$opponent[level]*7);
$liga = rand($opponent[level]-$opponent[level]-0,$opponent[level]-$opponent[level]-0);
$win = '<font color="red">Поражение</font>';
if($l_ch['id_1'] == $myID){
mysql_query("update `l_ch` set `schet_2` = '".($l_ch['schet_2']+1)."' where (`id` = '".$l_ch['id']."')");
}else{
mysql_query("update `l_ch` set `schet_1` = '".($l_ch['schet_1']+1)."' where (`id` = '".$l_ch['id']."')");
}
mysql_query("update `duel` set `start` = '0', `opponent` = '$_opponent[id]' where (`id` = '".$duel['id']."')");
mysql_query("update `users` set `exp` = '".($user[exp]+$exp)."', `crystals` = '".($user[crystals]+$crystals)."', `liga` = '".($user[liga]+$liga)."' where (`id` = '".$myID."')");
if($user[id_clan] > 0){
mysql_query("UPDATE `users` SET `clan_exp` = '".($user[clan_exp]+ceil($exp*15/100))."' WHERE `id` = '".$myID."' LIMIT 1");
mysql_query("UPDATE `clans` SET `exp` = `exp` + '".ceil($exp*15/100)."' WHERE `id` = '".$user[id_clan]."' LIMIT 1");
}
mysql_query("DELETE FROM `duel_log` WHERE `id_batle` = '".$duel[id]."'");
$_SESSION[msg] = "<b>$win</b><br>Награда: ".ico('icons','exp.png')." + $exp ".ico('icons','crystal.png')." + $crystals  ".ico('icons','liga.png')." $liga";
header('Location: /duel/');
exit();
}elseif($user[health] > 0 and $duel[opp_health] <= 0){
$exp = exp_koll(rand($opponent[level]*10,$opponent[level]*20));
if($effect_2[on] == 1)$exp = $exp+ceil($exp*25/100);
$crystals = rand($opponent[level]*10,$opponent[level]*15);
$liga = rand($opponent[level]*1-2,$opponent[level]*1-2);
$win = '<font color="green">Победа!</font>';
if($l_ch['id_1'] == $myID){
mysql_query("update `l_ch` set `schet_1` = '".($l_ch['schet_1']+1)."' where (`id` = '".$l_ch['id']."')");
}else{
mysql_query("update `l_ch` set `schet_2` = '".($l_ch['schet_2']+1)."' where (`id` = '".$l_ch['id']."')");
}
mysql_query("update `duel` set `start` = '0', `opponent` = '$_opponent[id]' where (`id` = '".$duel['id']."')");
mysql_query("update `users` set `exp` = '".($user[exp]+$exp)."', `crystals` = '".($user[crystals]+$crystals)."', `liga` = '".($user[liga]+$liga)."' where (`id` = '".$myID."')");
if($user[id_clan] > 0){
mysql_query("UPDATE `users` SET `clan_exp` = '".($user[clan_exp]+ceil($exp*15/100))."' WHERE `id` = '".$myID."' LIMIT 1");
mysql_query("UPDATE `clans` SET `exp` = `exp` + '".ceil($exp*15/100)."' WHERE `id` = '".$user[id_clan]."' LIMIT 1");
}
mysql_query("DELETE FROM `duel_log` WHERE `id_batle` = '".$duel[id]."'");
$_SESSION[msg] = "<b>$win</b><br>Награда: ".ico('icons','exp.png')." + $exp ".ico('icons','crystal.png')." + $crystals ".ico('icons','liga.png')." + $liga ";
header('Location: /duel/');
exit();
}elseif($duel[opp_health] > 0 and $user[health] == 0){
$exp = exp_koll(rand($opponent[level]*10,$opponent[level]*20));
if($effect_2[on] == 1)$exp = $exp+ceil($exp*25/100);
$crystals = rand($opponent[level]*5,$opponent[level]*7);
$liga = rand($opponent[level]-$opponent[level]-0,$opponent[level]-$opponent[level]-0);
$win = '<font color="red">Поражение</font>';
if($l_ch['id_1'] == $myID){
mysql_query("update `l_ch` set `schet_2` = '".($l_ch['schet_2']+1)."' where (`id` = '".$l_ch['id']."')");
}else{
mysql_query("update `l_ch` set `schet_1` = '".($l_ch['schet_1']+1)."' where (`id` = '".$l_ch['id']."')");
}
mysql_query("update `duel` set `start` = '0', `opponent` = '$_opponent[id]' where (`id` = '".$duel['id']."')");
mysql_query("update `users` set `exp` = '".($user[exp]+$exp)."', `crystals` = '".($user[crystals]+$crystals)."', `liga` = '".($user[liga]+$liga)."' where (`id` = '".$myID."')");
if($user[id_clan] > 0){
mysql_query("UPDATE `users` SET `clan_exp` = '".($user[clan_exp]+ceil($exp*15/100))."' WHERE `id` = '".$myID."' LIMIT 1");
mysql_query("UPDATE `clans` SET `exp` = `exp` + '".ceil($exp*15/100)."' WHERE `id` = '".$user[id_clan]."' LIMIT 1");
}
mysql_query("DELETE FROM `duel_log` WHERE `id_batle` = '".$duel[id]."'");
$_SESSION[msg] = "<b>$win</b><br>Награда: ".ico('icons','exp.png')." + $exp ".ico('icons','crystal.png')." + $crystals ".ico('icons','liga.png')." $liga ";
header('Location: /duel/');
exit();
}
$title = 'Поединок с '.$opponent['login'];
require_once ('system/head.php');
auth(); // Закроем от гостей
# Настройки #
$id = abs(intval($_GET['id']));
if($id)$profile = mysql_fetch_assoc(mysql_query("SELECT * FROM `users` WHERE `id` = '".$id."'"));
else $profile = $user;
$clan = mysql_fetch_assoc(mysql_query("SELECT * FROM `clans` WHERE `id` = '".$profile[id_clan]."'"));

echo ' <div class="line1 mb2"><div><div></div></div></div>';
echo "<div class='title'>$title</div><div class='line'></div>";
if(isset($_GET[s_poison]) and $user[s_poison] >= 1 and $duel[s_poison] < time()){
mysql_query("UPDATE `users` SET `s_poison` = `s_poison` - '1' WHERE `id` = '".$myID."' LIMIT 1");
mysql_query("UPDATE `duel` SET `s_poison` = '".(time()+30)."' WHERE `id` = '".$duel[id]."' LIMIT 1");
header('Location: ?attack');
exit();
}
if(isset($_GET[poison]) and $user[poison] >= 1 and $duel[poison] < time()){
mysql_query("UPDATE `users` SET `poison` = `poison` - '1' WHERE `id` = '".$myID."' LIMIT 1");
mysql_query("UPDATE `duel` SET `poison` = '".(time()+30)."' WHERE `id` = '".$duel[id]."' LIMIT 1");
header('Location: ?attack');
exit();
}
if(isset($_GET[ss_poison]) and $user[ss_poison] >= 1 and $duel[ss_poison] < time()){
mysql_query("UPDATE `users` SET `ss_poison` = `ss_poison` - '1' WHERE `id` = '".$myID."' LIMIT 1");
mysql_query("UPDATE `duel` SET `ss_poison` = '".(time()+30)."' WHERE `id` = '".$duel[id]."' LIMIT 1");
header('Location: ?attack');
exit();
}
if(isset($_GET[k_poison]) and $user[k_poison] >= 1 and $duel[k_poison] < time()){
mysql_query("UPDATE `users` SET `k_poison` = `k_poison` - '1' WHERE `id` = '".$myID."' LIMIT 1");
mysql_query("UPDATE `duel` SET `k_poison` = '".(time()+30)."' WHERE `id` = '".$duel[id]."' LIMIT 1");
header('Location: ?attack');
exit();
}
if(isset($_GET[k_potion]) and $user[k_potion] >= 1 and $duel[k_potion] < time()){
mysql_query("UPDATE `users` SET `k_potion` = `k_potion` - '1' WHERE `id` = '".$myID."' LIMIT 1");
mysql_query("UPDATE `duel` SET `k_potion` = '".(time()+30)."' WHERE `id` = '".$duel[id]."' LIMIT 1");
header('Location: ?attack');
exit();
}
if(isset($_GET[s_potion]) and $user[s_potion] >= 1 and $duel[s_potion] < time()){
mysql_query("UPDATE `users` SET `s_potion` = `s_potion` - '1' WHERE `id` = '".$myID."' LIMIT 1");
mysql_query("UPDATE `duel` SET `s_potion` = '".(time()+30)."' WHERE `id` = '".$duel[id]."' LIMIT 1");
header('Location: ?attack');
exit();
}
if(isset($_GET[potion]) and $user[potion] >= 1 and $duel[potion] < time()){
mysql_query("UPDATE `users` SET `potion` = `potion` - '1' WHERE `id` = '".$myID."' LIMIT 1");
mysql_query("UPDATE `duel` SET `potion` = '".(time()+30)."' WHERE `id` = '".$duel[id]."' LIMIT 1");
header('Location: ?attack');
exit();
}
if(isset($_GET[ss_potion]) and $user[ss_potion] >= 1 and $duel[ss_potion] < time()){
mysql_query("UPDATE `users` SET `ss_potion` = `ss_potion` - '1' WHERE `id` = '".$myID."' LIMIT 1");
mysql_query("UPDATE `duel` SET `ss_potion` = '".(time()+30)."' WHERE `id` = '".$duel[id]."' LIMIT 1");
header('Location: ?attack');
exit();
}
if(isset($_GET[attack]) and $user[health] > 0 and $duel[opp_health] > 0) {
$_str_opp = rand(round($opponent['str']/6), round($opponent['str']/4));
$_def_opp = rand(round($opponent['def']/12), round($opponent['def']/7));
if($duel[s_poison] > time())$_str += ceil($_str*10/100);
if($duel[poison] > time())$_str += ceil($_str*15/100);
if($duel[ss_poison] > time())$_str += ceil($_str*25/100);
if($duel[k_poison] > time())$_str += ceil($_str*40/100);

if($duel[s_potion] > time())$_str_opp -= ceil($_str_opp*10/100);
if($duel[potion] > time())$_str_opp -= ceil($_str_opp*15/100);
if($duel[ss_potion] > time())$_str_opp -= ceil($_str_opp*25/100);
if($duel[k_potion] > time())$_str_opp -= ceil($_str_opp*40/100);
$_str_opp = $_str_opp - $_def;
if($_str_opp < 0)$_str_opp = 0;
$_str = $_str - $_def_opp;
if($_str < 0)$_str = 0;
mysql_query("INSERT INTO `duel_log` SET `uron_user` = '$_str', `tip` = 'user', `id_batle` = '$duel[id]'");
mysql_query("INSERT INTO `duel_log` SET `uron_opp` = '$_str_opp', `tip` = 'opp', `id_batle` = '$duel[id]'");
mysql_query("update `duel` set `opp_health` = '".($duel[opp_health]-$_str)."', `user_health` = '".($duel[user_health]-$_str_opp)."' where (`id` = '".$duel['id']."')");
header('Location: /duel/batle/');
exit();
}
echo "<div class='block'>";
echo icons_user($opponent[id])." $opponent[login] <br>      ".ico('icons','str.png')." $opponent[str] ".ico('icons','health.png')." $duel[opp_health] ".ico('icons','def.png')." $opponent[def]<br>";
echo "<div  class='stat_bar'><div class='progress' style='width: ".round($duel[opp_health]/$opponent[max_health]*100,1)."%;'></div></div>";
echo "<a href='?attack' class='btn2'>Атаковать</a><hr>";

echo icons_user($user[id])." $user[login] <br>      ".ico('icons','str.png')." $user[str] ".ico('icons','health.png')." $duel[user_health] ".ico('icons','def.png')." $user[def]<br>";
echo "<div class='stat_bar'><div class='progres' style='width: ".round($duel[user_health]/$user[max_health]*100,1)."%;'></div></div>";
echo "</div>";
echo "<div class='block2'>";
if($user[s_potion] > 0)echo "<li><a href='?s_potion'>".ico('icon','s_potion.png')."<b>Защита +10%</b>, $user[s_potion] шт.</a></li>";
if($user[potion] > 0)echo "<li><a href='?potion'>".ico('icon','potion.png')."<b>Защита +15%</b>, $user[potion] шт.</a></li>";
if($user[ss_potion] > 0)echo "<li><a href='?ss_potion'>".ico('icon','ss_potion.png')."<b>Защита +25%</b>, $user[ss_potion] шт.</a></li>";
if($user[k_potion] > 0)echo "<li><a href='?k_potion'>".ico('icon','k_potion.png')."<b>Защита +40%</b>, $user[k_potion] шт.</a></li>";

if($user[s_poison] > 0)echo "<li><a href='?s_poison'>".ico('icon','s_poison.png')."<b>Урон +10%</b>, $user[s_poison] шт.</a></li>";
if($user[poison] > 0)echo "<li><a href='?poison'>".ico('icon','poison.png')."<b>Урон +15%</b>, $user[poison] шт.</a></li>";
if($user[ss_poison] > 0)echo "<li><a href='?ss_poison'>".ico('icon','ss_poison.png')."<b>Урон +25%</b>, $user[ss_poison] шт.</a></li>";
if($user[k_poison] > 0)echo "<li><a href='?k_poison'>".ico('icon','k_poison.png')."<b>Урон +40%</b>, $user[k_poison] шт.</a></li>";
echo "</div>";
echo "<div class='block'>";
$k_post = mysql_result(mysql_query("SELECT COUNT(*) FROM `duel_log` WHERE `id_batle` = '$duel[id]'"),0);
if($k_post == 0)echo "Логов не найдено...";
$q = mysql_query("SELECT * FROM `duel_log` WHERE `id_batle` = '$duel[id]' ORDER BY `id` DESC LIMIT 15");
while($post = mysql_fetch_assoc($q)){
if($post[tip] == 'user'){
echo "Вы атаковали ".icons_user($opponent['id'])." $opponent[login] на <b>$post[uron_user]</b><br>";
}else{
echo icons_user($opponent[id])." <font color='red'>$opponent[login] атаковал".($opponent['sex'] == 'w' ? 'а' : '')." Вас на <b>$post[uron_opp]</b></font><br>";
}
}
echo "</div>";
echo "<a href='/duel/' class='link center'>".ico('icons','arrow.png')." Покинуть бой</a>";
echo "</body></html>";
?>