<? 
include './system/common.php';
include './system/functions.php';
include './system/user.php';
if(!$user) {
header('location: /');   
exit;}
if($user['lair_boi'] == '0'){
header('location: /');   
exit;}
$lair_mobs = mysql_fetch_assoc(mysql_query("SELECT * FROM `lair_mobs` WHERE `id`='".$user['lair']."' ")); 
$lair = mysql_fetch_assoc(mysql_query("SELECT * FROM `lair` WHERE `id`='".$lair_mobs['glava']."' ")); 
$lair_users = mysql_fetch_assoc(mysql_query("SELECT * FROM `lair_users` WHERE `user_id`='".$user['id']."' ")); 

$title = ''.$lair['name'].'';
include './system/h.php'; 
echo'<div class="title">'.$title.'</div>';

/////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////
if($lair_users['start'] == '0'){//Если бой не начат
	
	if(isset($_REQUEST['start'])){
	// Задания
$task_id=1;// Сразись 3 раза с босами
$req = mysql_query ('select * from `task_user` WHERE (`user`="'.$user['id'].'") AND (`task`="'.$task_id.'") AND (`complete`="0")');
$task = mysql_fetch_array(mysql_query ('SELECT * FROM `task` WHERE (`id`="'.$task_id.'")'));
   if (mysql_num_rows ($req) != 0) {
while ($t = mysql_fetch_array ($req)) {
   if ($t['how'] < $task['how']){                              
mysql_query ('UPDATE `task_user` SET `how`=`how`+1 WHERE (`user`="'.$user['id'].'") AND (`task`="'.$task_id.'")');
}}}
		
	mysql_query("DELETE FROM `lair_logs` WHERE `user_id` = '".$user['id']."'");
	mysql_query("UPDATE `lair_users` SET `user_str`='".$user['str']."', `user_vit`='".$user['vit']."', `user_def`='".$user['def']."', `boss_id`='".$lair_mobs['id']."', `boss_str`='".$lair_mobs['str']."', `boss_vit`='".$lair_mobs['vit']."', `boss_def`='".$lair_mobs['def']."', `start`='1', `time`='0' WHERE `user_id`='".$user['id']."' ");
	header('Location:/lair');
	exit;} 

	echo'<div class="empty_block item_center">
	Вы встретили '.$lair_mobs['name2'].' </br>
	<img src="/images/ico/lair/lair'.$lair_mobs['img'].'_nowin.jpg" alt="*"> </br>
	<img src="/images/ico/png/attack.png" alt="*"> '.$lair_mobs[str].'
	<img src="/images/ico/png/hp.png" alt="*">     '.$lair_mobs[vit].'
	<img src="/images/ico/png/def.png" alt="*">    '.$lair_mobs[def].' </br>
	<form action="/lair" method="post">
	<input class="button" type="submit" name="start"value="Начать бой"/>
	</form>
	</div>
	<div class="line"></div>';
include './system/f.php';	
exit;}	
//////////////////////////////////////////////////
/////////////////////////////////////////////////





  

  








//////////////////////////////////////////////////
/////////////////////////////////////////////////
if($lair_users['boss_vit'] <= '0'){//Если босс мертв
$_gold = $lair_mobs['gold'];           
$_exp = $lair_mobs['exp'] + rand(1,10) + (2 * $user['level']);  
  if($clan_memb && $clan_memb['v'] > 0) {  //Верность клану
$_exp += round($_exp/100) * $clan_memb['v']; }
  if($premium) {$_exp+= round($_exp/ 100) * 100;} //Если премик 
	echo'<div class="empty_block"><center>
	<font color="lime">Вы победили '.$lair_mobs['name2'].'</font> </br>
	<font color="lime">Награда: </font> 
    <img src="/images/ico/png/gold.png" alt="*"> '.n_f($_gold).'
	<img src="/images/ico/png/exp.png" alt="*"> '.n_f($_exp).'</center>';
	
$w = mysql_fetch_array(mysql_query('SELECT * FROM `shop` WHERE `quality` = "'.$lair_mobs['item'].'" ORDER BY RAND() LIMIT 1'));
if(mysql_num_rows(mysql_query('SELECT * FROM `inv` WHERE `place` = \'0\' AND `user` = \''.$user['id'].'\'')) + 1 < 200) { //Если в рюкзакуменьше 20-ти вещей
mysql_query('INSERT INTO `inv` (`user`,
                                    `item`,
                                    `quality`,
                                    `_str`,
                                    `_vit`,
                                    `_def`) VALUES (\''.$user['id'].'\',
                                                    \''.$w['id'].'\',
                                                    \''.$w['quality'].'\',
                                                    \''.$w['_str'].'\',
                                                    \''.$w['_vit'].'\',
                                                    \''.$w['_def'].'\')');
$w_id = mysql_insert_id();
$w = mysql_fetch_array(mysql_query('SELECT * FROM `inv` WHERE `id` = \''.$w_id.'\''));
$item = mysql_fetch_array(mysql_query('SELECT * FROM `items` WHERE `id` = \''.$w['item'].'\''));
  


  switch($w['quality']) {
case 1:
$quality = 'Обычный';
$quality_color = '#999999';
break;
case 2:
$quality = 'Необычный';
$quality_color = '#B1D689';
break;
case 3:
$quality = 'Редкий';
$quality_color = '#6BA0E7';
break;
case 4:
$quality = 'Эпический';
$quality_color = '#C780DB';
break;
case 5:
$quality = 'Легендарный';
$quality_color = '#FF8E94';
break;
case 6:
$quality = 'Мифический';
$quality_color = '#FE7E01';
break;
}
  

$equip_item = mysql_query('SELECT * FROM `inv` WHERE `id` = "'.$user['w_'.$item['w']].'"');
$equip_item = mysql_fetch_array($equip_item);
    $diff = 0;
  if($w['_str'] > $equip_item['_str']) { 
$diff += $w['_str'] - $equip_item['_str'];
}
    if($w['_vit'] > $equip_item['_vit']) {
$diff += $w['_vit'] - $equip_item['_vit'];
}
    if($w['_def'] > $equip_item['_def']) {
      $diff += $w['_def'] - $equip_item['_def'];
}


	
echo'
<table cellpadding="0" cellspacing="0">
<tr>
<td width="15%"><img src="/images/items/'.$w['item'].'.png" alt="*"/></td>
<td>
<img src="/images/ico/quality/'.$w['quality'].'.png" alt="*"/> <a href="/item/'.$w['id'].'/"> '.$item['name'].' </a>
<br/><small>
<font color="#'.(($user['level'] < $item['level']) ? 'c06060':'ffffff').'"><img src="/images/ico/png/up.png" alt="*" width="12"/>'.$item['level'].' ур, </font>
<font color="'.$quality_color.'"> '.$quality.' </font>';
  if(($w['_str'] + $w['_vit'] + $w['_def']) - ($equip_item['_str'] + $equip_item['_vit'] + $equip_item['_def']) > 0) {
echo'<font color="#3c3">+'.$diff.'</font> </small>';
  }
  echo'</td></tr></table>';
  }
	
	echo'<center>
	<a href="/"><input class="button" type="submit" name="start"value="Забрать награду"/></a> </br>
	<img src="/images/ico/lair/lair'.$lair_mobs['img'].'_win.jpg" alt="*">
	</center></div>
	<div class="line"></div>';
	
	


////////////////////////////  
if($user['lair'] >= '80'){//eсли максимальный босс
mysql_query("UPDATE `users` SET `g`=`g`+'".$_gold."', `exp`=`exp`+'".$_exp."', `lair_boi`=`lair_boi`-'1', `lair`='80' WHERE `id`='".$user['id']."' ");
}else{
mysql_query("UPDATE `users` SET `g`=`g`+'".$_gold."', `exp`=`exp`+'".$_exp."', `lair_boi`=`lair_boi`-'1', `lair`=`lair`+'1' WHERE `id`='".$user['id']."' ");
}
mysql_query("UPDATE `lair_users` SET `user_str`='0', `user_vit`='0', `user_def`='0', `boss_id`='0', `boss_str`='0', `boss_vit`='0', `boss_def`='0', `start`='0', `time`='0' WHERE `user_id`='".$user['id']."' ");	
  if($clan) {//Если в клане 
mysql_query('UPDATE `clans`     SET `exp` = `exp` + '.$_exp.' WHERE `id` = "'.$clan['id'].'"'); //Плюсуем в клан
mysql_query('UPDATE `clan_memb` SET `exp` = `exp` + '.$_exp.' WHERE `clan` = "'.$clan['id'].'" AND `user` = "'.$user['id'].'"'); //Плюсуем учаснику клана
}
include './system/f.php';	
exit;}	
//////////////////////////////////////////////////
/////////////////////////////////////////////////


//////////////////////////////////////////////////
/////////////////////////////////////////////////
if($lair_users['user_vit'] <= '0'){//Если вы мертвы
$_gold = 0;           
$_exp = ($lair_mobs['exp'] + rand(1,10) + (2 * $user['level'])) / 2;    
  if($clan_memb && $clan_memb['v'] > 0) {  //Верность клану
$_exp += round($_exp/100) * $clan_memb['v']; }
  if($premium) {$_exp+= round($_exp/ 100) * 100;} //Если премик 	
  
 if($user['lair_gold'] < $lair_mobs['gold']){
	$_gold = round($lair_mobs['gold'] / 5); 
	$lair_gold=$user['lair_gold']+$_gold;
	if($lair_gold > $lair_mobs['gold']){
	$_gold = $lair_mobs['gold']-$user['lair_gold'];
	$lair_gold=$user['lair_gold']+$_gold;
	}
 }elseif($user['lair_gold'] == $lair_mobs['gold']){	 
    $_gold = 0;
	$lair_gold=$user['lair_gold'];
 }
 
 
 
	echo'<div class="empty_block">
	<center> Вы отступили. </br>
	Возвращайтесь когда станете сильнее </br>
	<font color="lime">Награда: </font> ';
	if($_gold){echo'<img src="/images/ico/png/gold.png" alt="*"> '.n_f($_gold).',';}
    echo'<img src="/images/ico/png/exp.png" alt="*"> '.n_f($_exp).' </center>';

  $proc = rand(1,100);
  if($proc < '20') { //Шанс выпадания шмоток 20% только при победе	
$w = mysql_fetch_array(mysql_query('SELECT * FROM `shop` WHERE `quality` <= "3" ORDER BY RAND() LIMIT 1'));
if(mysql_num_rows(mysql_query('SELECT * FROM `inv` WHERE `place` = \'0\' AND `user` = \''.$user['id'].'\'')) + 1 < 200) { //Если в рюкзакуменьше 20-ти вещей
mysql_query('INSERT INTO `inv` (`user`,
                                    `item`,
                                    `quality`,
                                    `_str`,
                                    `_vit`,
                                    `_def`) VALUES (\''.$user['id'].'\',
                                                    \''.$w['id'].'\',
                                                    \''.$w['quality'].'\',
                                                    \''.$w['_str'].'\',
                                                    \''.$w['_vit'].'\',
                                                    \''.$w['_def'].'\')');
$w_id = mysql_insert_id();
$w = mysql_fetch_array(mysql_query('SELECT * FROM `inv` WHERE `id` = \''.$w_id.'\''));
$item = mysql_fetch_array(mysql_query('SELECT * FROM `items` WHERE `id` = \''.$w['item'].'\''));
  


  switch($w['quality']) {
case 1:
$quality = 'Обычный';
$quality_color = '#999999';
break;
case 2:
$quality = 'Необычный';
$quality_color = '#B1D689';
break;
case 3:
$quality = 'Редкий';
$quality_color = '#6BA0E7';
break;
case 4:
$quality = 'Эпический';
$quality_color = '#C780DB';
break;
case 5:
$quality = 'Легендарный';
$quality_color = '#FF8E94';
break;
case 6:
$quality = 'Мифический';
$quality_color = '#FE7E01';
break;
}
  

$equip_item = mysql_query('SELECT * FROM `inv` WHERE `id` = "'.$user['w_'.$item['w']].'"');
$equip_item = mysql_fetch_array($equip_item);
    $diff = 0;
  if($w['_str'] > $equip_item['_str']) { 
$diff += $w['_str'] - $equip_item['_str'];
}
    if($w['_vit'] > $equip_item['_vit']) {
$diff += $w['_vit'] - $equip_item['_vit'];
}
    if($w['_def'] > $equip_item['_def']) {
      $diff += $w['_def'] - $equip_item['_def'];
}



echo'
<table cellpadding="0" cellspacing="0">
<tr>
<td width="15%"><img src="/images/items/'.$w['item'].'.png" alt="*"/></td>
<td>
<img src="/images/ico/quality/'.$w['quality'].'.png" alt="*"/> <a href="/item/'.$w['id'].'/"> '.$item['name'].' </a>
<br/><small>
<font color="#'.(($user['level'] < $item['level']) ? 'c06060':'ffffff').'"><img src="/images/ico/png/up.png" alt="*" width="12"/>'.$item['level'].' ур, </font>
<font color="'.$quality_color.'"> '.$quality.' </font>';
  if(($w['_str'] + $w['_vit'] + $w['_def']) - ($equip_item['_str'] + $equip_item['_vit'] + $equip_item['_def']) > 0) {
echo'<font color="#3c3">+'.$diff.'</font> </small>';
  }
  echo'</td></tr></table>';
  }
  
  }
	echo'<center>
	<a href="/"><input class="button" type="submit" name="start"value="Забрать награду"/></a> </br>
	<img src="/images/ico/lair/lair'.$lair_mobs['img'].'_nowin.jpg" alt="*"> </br>
	Найдено сегодня <img src="/images/ico/png/gold.png" alt="*"> '.$lair_gold.' из '.$lair_mobs['gold'].'
	</center></div>
	<div class="line"></div>';
mysql_query("UPDATE `users` SET `g`=`g`+'".$_gold."', `exp`=`exp`+'".$_exp."', `lair_boi`=`lair_boi`-'1', `lair_gold`=`lair_gold`+'".$_gold."' WHERE `id`='".$user['id']."' ");
mysql_query("UPDATE `lair_users` SET `user_str`='0', `user_vit`='0', `user_def`='0', `boss_id`='0', `boss_str`='0', `boss_vit`='0', `boss_def`='0', `start`='0', `time`='0' WHERE `user_id`='".$user['id']."' ");
  if($clan) {//Если в клане 
mysql_query('UPDATE `clans`     SET `exp` = `exp` + '.$_exp.' WHERE `id` = "'.$clan['id'].'"'); //Плюсуем в клан
mysql_query('UPDATE `clan_memb` SET `exp` = `exp` + '.$_exp.' WHERE `clan` = "'.$clan['id'].'" AND `user` = "'.$user['id'].'"'); //Плюсуем учаснику клана
}
include './system/f.php';	
exit;}	
//////////////////////////////////////////////////
/////////////////////////////////////////////////




	if($_GET['attack'] == true){//Система боя

$my_attack = round(rand($lair_users[user_str] * 2.4 * 0.38,$lair_users[user_str] * 2.5 * 0.38) - ($lair_users[boss_def] / 5 * 0.66));
if($my_attack <= '0'){ $my_attack = 5;}
$lair_attack = round(rand($lair_users[boss_str] * 2.1 * 0.37,$lair_users[boss_str] * 2.2 * 0.38) - ($lair_users[boss_def] / 5 * 0.66) );
/*if($lair_attack <= '0'){ $lair_attack = 5;}*/
	
$log = 'Вы ударили '.$lair_mobs['name'].'  на <span class="red">'.$my_attack.'</span> </br>
'.$lair_mobs['name'].' ударил вас на <span class="red">'.$lair_attack.'</span>';
mysql_query("INSERT INTO `lair_logs` SET `user_id`='".$user['id']."', `text`='".$log."', `time`='".time()."'");
mysql_query("UPDATE `lair_users` SET `user_vit`=`user_vit`-'".$lair_attack."', `boss_vit`=`boss_vit`-'".$my_attack."' WHERE `user_id`='".$user['id']."' ");
header('Location:/lair');
exit;} 

  echo' <div class="empty_block">
<table align="center"> <tbody><tr>
 <td style="width:33%;">
 <span style="float:right;"> <b>Вы</b> </span> <br>
 <span style="float:right;"> <img src="/images/ico/png/hp.png" alt="hp" width="15"> '._string(_num($lair_users['user_vit'])).' </span>               
</td><td> ';
  if($user['sex'] == '0'){
     echo' <img src="/images/user/man.png" alt="user" width="48">';}
  if($user['sex'] == '1'){
     echo' <img src="/images/user/woman.png" alt="user" width="48">';}
     echo'<td>
<td> <img src="/images/ico/lair/lair'.$lair_mobs['img'].'.png" alt="*" width="48"> </td>
<td style="width:33%;">
    <span style="float:left;"> <b>'.$lair_mobs['name'].'</b> </span> <br>
    <span style="float:left;"> <img src="/images/ico/png/hp.png" alt="hp" width="15"> '._string(_num($lair_users['boss_vit'])).'  </span> 
</td></tr>
</tbody></table>
</div> <div class="line"></div>';
echo' <div class="block_link">
 <a href="?attack=true"><img src="/images/ico/png/attack.png" width="15"> Атаковать </a> </div>
 <div class="line"></div>';
 
 echo' <div class="title"> История боя </div> 
<div class="empty_block">';			
$logs=mysql_query("SELECT * FROM `lair_logs` WHERE `user_id` = '".$user['id']."'  ORDER BY `id` DESC LIMIT 5");
if(mysql_num_rows($logs)=='0'){
echo'<font color="lime">Бой начался</font>';
}elseif (mysql_num_rows($logs)>0) {
while ($log=mysql_fetch_array($logs)) {
echo''.$log['text'].'</br>';
			}	
	}
echo'</div><div class="line"></div>';

include './system/f.php';
?>