<?
    
    include './system/common.php';
    
 include './system/functions.php';
        
      include './system/user.php';
    
if(!$user) {

  header('location: /');
    
exit;

}


    $title = 'Пещера';    

include './system/h.php';

    $sack = mysql_query('SELECT * FROM `sack` WHERE `user` = "'.$user['id'].'"');      
    $sack = mysql_fetch_array($sack);

if(!$sack) {

  mysql_query('INSERT INTO `sack` (`user`) VALUES ("'.$user['id'].'")');

}


  $cave = mysql_query('SELECT * FROM `cave` WHERE `user` = "'.$user['id'].'"');
  $cave = mysql_fetch_array($cave);

       $res_1 = rand(1,9);
$res_1_chanse = rand(0,25);
  
       $res_2 = rand(1,9);
$res_2_chanse = rand(0,25);

       $res_3 = rand(1,9);
$res_3_chanse = rand(0,25);

  if($cave['dawn'] == 1 && $cave['gather'] == 0 && $cave['time'] <= time()) {

  if(!$cave['res_1'] && !$cave['res_2'] && !$cave['res_3']) {
  
    mysql_query('UPDATE `cave` SET `res_1` = "'.$res_1.'",
                            `res_1_chanse` = "'.$res_1_chanse.'",
                                   `res_2` = "'.$res_2.'",
                            `res_2_chanse` = "'.$res_2.'",
                                   `res_3` = "'.$res_3.'",
                            `res_3_chanse` = "'.$res_3_chanse.'" WHERE `user` = "'.$user['id'].'"');
  
  }
  
  }


  $cave = mysql_query('SELECT * FROM `cave` WHERE `user` = "'.$user['id'].'"');
  $cave = mysql_fetch_array($cave);

  if(!$cave) {
  
    mysql_query('INSERT INTO `cave` (`user`) VALUES ("'.$user['id'].'")');
  
  }

   function res($i) {
  
    switch($i) {
    case 1:
    $name = 'Алмаз';
     break;
    case 2:
    $name = 'Корунд';
     break;
    case 3:
    $name = 'Обсидиан';
     break;
    case 4:
    $name = 'Графит';
     break;
    case 5:
    $name = 'Оникс';
     break;
    case 6:
    $name = 'Амброзия';
     break;
    case 7:
    $name = 'Мята';
     break;
    case 8:
    $name = 'Аир';
     break;
    case 9:
    $name = 'Рябина';
     break;
  }

    return $name;
  
  }

  
?>



<?

  if($cave['dawn'] == 1 && $cave['time'] <= time()) {
  
if($cave['gather'] == 0) {

if ($cave['res_1_chanse'] < 100 || $cave['res_2_chanse'] < 100 || $cave['res_3_chanse'] < 100)
{
  if ($_GET['up'] ==  true)
  {
    if ($user['g'] < 2)
    {
      ?>
      <div class ='title'/>
        <center>
          Не хватает золота.
        </center>
      </div>
      <?
    } else{
      mysql_query("UPDATE `cave` SET `res_1_chanse`=`res_1_chanse`+'10',
                                      `res_2_chanse`=`res_2_chanse`+'10',
                                      `res_3_chanse`=`res_3_chanse`+'10'
                                      WHERE `user`='".$user['id']."'");
      mysql_query("UPDATE `users` SET `g`=`g`-'2' WHERE `id`='".$user['id']."'");
      header("Location:/cave/?upt");
      exit;
    }
  }

  if ($_GET['up2'] == true)
  {
    if ($user['g'] < 20)
    {
      ?>
      <div class ='title'/>
        <center>
          Не хватает золота.
        </center>
      </div>
      <?
    } else{
      mysql_query("UPDATE `cave` SET `res_1_chanse`='100',
                                      `res_2_chanse`='100',
                                      `res_3_chanse`='100'
                                      WHERE `user`='".$user['id']."'");
      mysql_query("UPDATE `users` SET `g`=`g`-'20' WHERE `id`='".$user['id']."'");
      header("Location:/cave/?upt");
      exit;
    }
  }


}
?>


<div class='main' style='word-wrap:break-word;'><span style='text-shadow:none;'></span>
<div class='center'><div class='block_zero'><span class='blue'>Осмотр пещеры завершен<br/>Вы нашли место с ресурсами:</span></div></div>
<div class='mini-line'></div>

<div class='block_zero'>
<img src='/images/res/<?=$cave['res_1']?>.png' alt='' width='50' height='50' style='float:left;margin-right:3px;margin-top:3px;'/> <?=res($cave['res_1'])?>
<br/>
<span class='small'>Шанс добыть: <?=(isset($_GET['upt']) ? '<font color =\'#30c030\'/>' : null );?><?=$cave['res_1_chanse']?>%</span>
<div style='clear:both;'></div></div>

<div class='block_zero'>
<img src='/images/res/<?=$cave['res_2']?>.png' alt='' width='50' height='50' style='float:left;margin-right:3px;margin-top:3px;'/> <?=res($cave['res_2'])?>
<br/>
<span class='small'>Шанс добыть: <?=(isset($_GET['upt']) ? '<font color =\'#30c030\'/>' : null );?><?=$cave['res_2_chanse']?>%</span>
<div style='clear:both;'></div></div>

<div class='block_zero'>
<img src='/images/res/<?=$cave['res_3']?>.png' alt='' width='50' height='50' style='float:left;margin-right:3px;margin-top:3px;'/> <?=res($cave['res_3'])?>
<br/>
<span class='small'>Шанс добыть: <?=(isset($_GET['upt']) ? '<font color =\'#30c030\'/>' : null );?><?=$cave['res_3_chanse']?>%</span>
<div style='clear:both;'></div></div>

<div class='mini-line'></div>
<div class='menuList'>
<li><a href='?up2=true'><img src='/images/icon/arrow.png' alt=''/>Увеличить шанс до 
<span class='white'>100% за <img src='/images/icon/gold.png' alt='g'/>20

</span></a></li></div>
<div class='mini-line'></div>
<div class='block_zero center'>
<a class='btn' href='/cave/?gather=true'><span class='end'><span class='label'>Начать добычу</span></span></a><br/>
<a class='grey' href='/cave/?dawn=true'>Новый поиск</a><div style='clear:both;'></div></div></div>


<?

if($_GET['gather'] == true) {

$time = (60 * 50);

               if($premium1) {

$time-= round( $time / 100 ) * 10;
    
             }

    
    mysql_query('UPDATE `cave` SET `gather` = "1",
                                     `time` = "'.(time() + $time).'" WHERE `user` = "'.$user['id'].'"');
  
  header('location: /cave/');
  
  }


if($_GET['dawn'] == true) {
  
$time = (60 * 25);
  
             if($premium) {

$time-= round( $time / 100 ) * 10;
    
             }


    mysql_query('UPDATE `cave` SET `dawn` = "1",
                                   `time` = "'.(time() + $time).'",
                                   `res_1`= "0",
                                   `res_2`= "0",
                                   `res_3`= "0"  WHERE `user` = "'.$user['id'].'"');
  
  header('location: /cave/');
  
  }


  }
  else
  {

?>

<div class='content' align='center'><font color='#90b0c0'>Работа завершена<br/>
Вы пытались добыть следующие ресурсы:</font>
</div>
 <div class='line'></div>

<?
  
  $res_1 = rand(0,100);
  $res_2 = rand(0,100);
  $res_2 = rand(0,100);
  
?>

<div class='content'>

  <table cellpadding='0' cellspacing='0'>
<tr>
  

  <td><img src='/images/res/<?=$cave['res_1']?>.png' alt='*'/></td>
  <td valign='top' style='padding-left: 5px;'><?=res($cave['res_1'])?><br/><small>
  <?=($res_1 <= $cave['res_1_chanse'] ? '<font color=\'#30c030\'>Ресурс добыт!</font>':'<font color=\'#c06060\'>Ресурс не добыт!</font>')?>
  </small></td></tr></table><br/>

  <table cellpadding='0' cellspacing='0'>
<tr>
  

  <td><img src='/images/res/<?=$cave['res_2']?>.png' alt='*'/></td>
  <td valign='top' style='padding-left: 5px;'><?=res($cave['res_2'])?><br/><small>
  <?=($res_2 <= $cave['res_2_chanse'] ? '<font color=\'#30c030\'>Ресурс добыт!</font>':'<font color=\'#c06060\'>Ресурс не добыт!</font>')?>
  </small></td></tr></table><br/>


  <table cellpadding='0' cellspacing='0'>
<tr>
  

  <td><img src='/images/res/<?=$cave['res_3']?>.png' alt='*'/></td>
  <td valign='top' style='padding-left: 5px;'><?=res($cave['res_3'])?><br/><small>
  <?=($res_3 <= $cave['res_3_chanse'] ? '<font color=\'#30c030\'>Ресурс добыт!</font>':'<font color=\'#c06060\'>Ресурс не добыт!</font>')?>
  </small></td></tr></table>



<div class='mini-line'></div>
<div class='block_zero center'>
<a class='btn' href='/cave/'>
<span class='end'><span class='label'>Новый поиск</span></span></a>
<div style='clear:both;'></div></div>
<?
  
  $c = 0;
  if($res_1 <= $cave['res_1_chanse']) {

    mysql_query('UPDATE `sack` SET `'.$cave['res_1'].'` = `'.$cave['res_1'].'` + 1 WHERE `user` = "'.$user['id'].'"');  
      $c++;
  }

  if($res_2 <= $cave['res_2_chanse']) {

    mysql_query('UPDATE `sack` SET `'.$cave['res_2'].'` = `'.$cave['res_2'].'` + 1 WHERE `user` = "'.$user['id'].'"');  
      $c++;
  }

  if($res_3 <= $cave['res_3_chanse']) {

    mysql_query('UPDATE `sack` SET `'.$cave['res_3'].'` = `'.$cave['res_3'].'` + 1 WHERE `user` = "'.$user['id'].'"');  
      $c++;
  }

      // Place
    // 5 - Пещера
    // Type
    // 0 - Спуститься в пещеру
    // 1 - Найти n-е кол-во ресурсов
    $q = mysql_query ('select * from `user_q` WHERE (`user`="' . $user['id'] . '") AND (`complete`="0")');
    if (mysql_num_rows ($q) != 0) {
        
        while ($user_q = mysql_fetch_array ($q)) {
                
            // 
            $q_ = mysql_query ('SELECT * FROM `quest` WHERE (`id`="' . $user_q['q'] . '")');
            $quest = mysql_fetch_array ($q_);
            
            
            if ($quest['place']=='5') {
            
            
                if ($quest['type']=='1') {
                    if ($c>= $quest['c']) {
                        mysql_query ('UPDATE `user_q` SET `c`=`c`+1 WHERE (`user`="' . $user['id'] . '") AND (`q`="' . $quest['id'] . '")');
                    }
                }
            
            
            
            
            }

        }

    }



    mysql_query('UPDATE `cave` SET `dawn` = "0",
                                 `gather` = "0",
                                   `time` = "0",
                                   `res_1`= "0",
                                   `res_2`= "0",
                                   `res_3`= "0"  WHERE `user` = "'.$user['id'].'"');


  }

  }
  else
  {

?>



<?

    if($cave['dawn'] == 1) {
    
  if($cave['gather'] == 0) {

if($cave['time'] > time()) {
  
?>
<?
    if(isset($_GET['speed'])){
      if($user['s']>=1500){
        mysql_query("UPDATE `cave` SET `time`='".($cave['time']-1500)."' WHERE `user`='".$user['id']."'");
        mysql_query("UPDATE `users` SET `s`='".($user['s']-1500)."' WHERE `id`='".$user['id']."'");
        $_SESSION['light']="Вы ускорили время похода на 1 час.";
        header("location:/cave/");
        exit;
      }elseif($user['s']<1500){
        $_SESSION['light']="Не хватает серебра.";
        header("location:/cave/");
        exit;
      }
    }
    ?>

<div class='main' style='word-wrap:break-word;'><span style='text-shadow:none;'></span>
<div class='center'>
<div class='block_zero'><img src='/images/town/hd/cave.jpg' width='100%' alt=''/></div>
<div class='dot-line'></div>
<div class='block_zero'><span class='blue'>Вы осматриваете пещеру</span><br/>До конца: <?=_time($cave['time'] - time())?></div></div>
<div class='mini-line'></div>
<div class='block_zero center'>
<a class='btn'  href='?speed'>
<span class='end'><span class='label'>Ускорить за <img src='/images/icon/silver.png' alt=''/> 1500</span></span></a></div>
<div class='mini-line'></div><ul class='hint'>
<li><span class='bold'>1</span> минута ускорения стоит 
<img src='/images/icon/silver.png' alt='s'/> <span class='bold'>59</span> серебра</li></ul>


<?
  
  }
  else
  {
  
  
  }
  
  }
  else
  {

?>
<?
    if(isset($_GET['speed'])){
      if($user['s']>=3000){
        mysql_query("UPDATE `cave` SET `time`='".($cave['time']-3000)."' WHERE `user`='".$user['id']."'");
        mysql_query("UPDATE `users` SET `s`='".($user['s']-3000)."' WHERE `id`='".$user['id']."'");
        $_SESSION['light']="Вы ускорили время похода на 1 час.";
        header("location:/cave/");
        exit;
      }elseif($user['s']<3000){
        $_SESSION['light']="Не хватает серебра.";
        header("location:/cave/");
        exit;
      }
    }
    ?>

<div class='main' style='word-wrap:break-word;'><span style='text-shadow:none;'></span>
<div class='center'><div class='block_zero'>
<img src='/images/town/hd/cave.jpg' width='100%' alt=''/></div>
<div class='dot-line'></div><div class='block_zero'>
<span class='blue'>Вы занимаетесь добычей ресурсов..</span><br/>
До конца: <?=_time($cave['time'] - time())?></div>
<div class='mini-line'></div>
<div class='block_zero center'>
<a class='btn'  href='?speed'>
<span class='end'><span class='label'>Ускорить за <img src='/images/icon/silver.png' alt=''/> 3000</span>
</span></a></div></div>
<div class='mini-line'></div><ul class='hint'><li><span class='bold'>1</span> минута ускорения стоит <img src='/images/icon/silver.png' alt='s'/> <span class='bold'>150</span> серебра</li></ul>



<?
  
  }
  
  }
  else
  {
  
if($_GET['dawn'] == true) {
  
$time = (60 * 25);
    
             if($premium) {

$time-= round( $time / 100 ) * 10;
    
             }
  
    mysql_query('UPDATE `cave` SET `dawn` = "1",
                                   `time` = "'.(time() + $time).'" WHERE `user` = "'.$user['id'].'"');
  
  header('location: /cave/');
  
      // Place
    // 5 - Пещера
    // Type
    // 0 - Спуститься в пещеру
    // 1 - Найти n-е кол-во ресурсов
    $q = mysql_query ('select * from `user_q` WHERE (`user`="' . $user['id'] . '") AND (`complete`="0")');
    if (mysql_num_rows ($q) != 0) {
        
        while ($user_q = mysql_fetch_array ($q)) {
                
            // 
            $q_ = mysql_query ('SELECT * FROM `quest` WHERE (`id`="' . $user_q['q'] . '")');
            $quest = mysql_fetch_array ($q_);
            
            
            if ($quest['place']=='5') {
            
            
                if ($quest['type']=='0') {
                    mysql_query ('UPDATE `user_q` SET `c`=`c`+1 WHERE (`user`="' . $user['id'] . '") AND (`q`="' . $quest['id'] . '")');
                }
            
            
            
            
            }

        }

    }
  
  }

?>


<div class='main' style='word-wrap:break-word;'><span style='text-shadow:none;'></span>
<div class='block_zero center blue'>Ищи ресурсы и используй их для улучшения вещей<br/></div>
<div class='mini-line'></div><div class='center'>
<div class='block_zero'><img src='/images/town/hd/cave.jpg' width='100%' alt=''/></div>
<div class='dot-line'></div>
<div class='block_zero'>
<a class='btn' href='/cave/?dawn=true/'><span class='end'><span class='label'>Спуститься в пещеру</span></span></a></div></div>
<div class='mini-line'></div><ul class='hint'><li>Используй ресурсы в <img src='/images/icon/smith.png' alt=''/> <a href='/smith/smith/'>Кузнице</a> для улучшения вещей</li></ul>


 

<?
  
  }

?>

  </li>
</div>

<?
  
  }
    
include './system/f.php';

?>