
<?php
require_once ('system/func.php');
$title = 'Сражения';
require_once ('system/header.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]."'"));
$user['max_health']=$user['max_health']+($clan['clan_hel']);
$batle = mysql_query("SELECT * FROM `duel` WHERE `id_user` = '".$myID."' ORDER BY `id` LIMIT 1");
$batle = mysql_fetch_array($batle);
if(!$batle){
mysql_query("INSERT INTO `duel` SET `id_user` = '$user[id]', `last` = '".time()."'");
header('Location: /duel');
exit();
}

//Атака
if(isset($_GET['attack']) == true and $batle['last'] <= time()){
$_hp = ceil($user['max_health']*10/100);
//Если нету енергии или жизней
if($user['energy'] < 5 or $user['health'] < $_hp){
$_SESSION['msg'] = "Для нападения необходимо минимум ".ico('icons','health.png')." 10% здоровья и ".ico('icons','energy.png')." 10 запаса сил!<br><a href='/rinok?koldun2&url=/duel' class='btn2' data-ajax>Восстановить ".ico('icons','gold.png')." ".($user[level]*5)."</a>";
header('Location: /duel/');
exit();
}
//Параметры
$opponent = mysql_fetch_assoc(mysql_query("SELECT * FROM `users` WHERE `id` = '".num($_GET['attack'])."'"));

mysql_query("update `users` set `energy` = '".($user['energy']-10)."' where (`id` = '".$user['id']."')");
mysql_query("update `duel` set `last` = '".(time()+1)."' where (`id` = '".$batle['id']."')");
$_SESSION['msg']  =  "gj";
header('Location: /duel/');
exit();
}

$opponent = mysql_query("SELECT * FROM `users` WHERE `level` != '$user[level]'  AND `str`+`def`+`max_health` >= '".ceil($user[str]+$user[def]+$user[max_health]*40/100)."' AND `str`+`def`+`max_health` <= '".ceil($user[str]+$user[def]+$user[max_health]*70/100)."' ORDER BY RAND() LIMIT 1");
$opponent = mysql_fetch_array($opponent);
if(!$opponent){
$opponent = mysql_query("SELECT * FROM `users` WHERE `level` != '$user[level]'  AND `str`+`def`+`max_health` <= '".($user[str]+$user[def]+$user[max_health])."' ORDER BY RAND() LIMIT 1");
$opponent = mysql_fetch_array($opponent);


}

echo div('block_c');
echo span('ribbon red  w75').' '.span('r_end').' '.span('r_cntr').' '.$opponent[login].' </span></span></span>';
echo  ico('icons', 'str.png').''.$opponent[str].' '.ico('icons', 'health.png').''.$opponent[health].' '.ico('icons', 'def.png').''.$opponent[def].'<br>';
echo maneken($opponent).'<br>';


if($user['energy'] >=5) $status='green'; else $status='red';

echo  div('cbtn '.$status.' center w40').' '.span('cb_end').' '.span('cb_ttl').'   <a href="/duel/'.$opponent[id].'/?attack=true">'.span('white').' Атаковать</div></span></span></a>';
echo  div('cbtn blue center w35').' '.span('cb_end').' '.span('cb_ttl').'   <a href="/duel/?'.$LinkKey.'"> '.span('white').'Другой противник </div></span></span></a>';

echo '</div>';

require_once ('system/footer.php');
?>
