<?php
$title = 'Сокровищница ';
include_once 'protected/sys.php';
include_once 'protected/headermain.php';

$df1=  round(rand($user['def']/15,$user['def']/17));
$df2 = round(rand($user['agi']/10,$user['agi']/9));
$aluk_def = round(rand($df1,$df2));
$my_atk = round(rand($user['str']/6,$user['str']/4)-$aluk_def);

if ($my_atk<1)
{
    $my_atk = $user['level']+rand($user['level'],$user['level']/2);
}

$aluko = $db->fetch("SELECT * FROM `aluko` ORDER BY `id` LIMIT 1");




    if($aluko['health']==0){
?>


    <div class="menu">
        <div class="mob">
            <span class="icon drag6"></span>
            <div class="info"><span class="high">Костяной дракон</span><br />
                <small>
                    <img src="http://mdrag.ru/style/images/icons/icon_str.png"> 8000                    <img src="http://mdrag.ru/style/images/icons/icon_hp.png"> 20400000                    <img src="http://mdrag.ru/style/images/icons/icon_def.png"> 4500                </small>
            </div>
        </div>
    </div>
    <div class="wrapper center lh1">
                    <span class="high">Доступно <?=$user['artem_aluka']?> боя.</span>
            <div class="wrapper-btn">
                <a class="btn _red w40p" href="?fight">Сразиться</a>
            </div>
            </div>
    <div class="wrapper lh1">
        <span class="high">Дракона повреждён
                   
        </div>
<?
    include_once './protected/footermain.php';
    exit;
    }
    
if($my_atk >$aluko['health'] && $aluko['health']>0)
{
    $db->query("UPDATE `aluko` SET `health`=0 WHERE `id`='".$aluko['id']."'")or die (mysql_error());
    echo '<div class="menu">
        Сокровищница закрыта!</div>';
    include './system/f.php';
$total = $db->query("SELECT COUNT(*) FROM `aluko_log`");
        if($total>0){
$q_nagr = $db->query("SELECT * FROM `aluko_log` GROUP BY `user_id` ORDER BY RAND()");
        /*3 лучших */
$top_q  = $db->query("SELECT SUM( uron ) , `user_id`     FROM  `aluko_log` GROUP BY  `user_id` ORDER BY  SUM( uron ) DESC LIMIT 100");
    
            $topes_us = '<font color="90c0c0">Результат сражения с Драконом:</font><br> ';
while($top= $db->fetch($top_q)){
$db->query("UPDATE `users` SET `silver`=`silver`+".$aluko['s']."  WHERE `id`='".$top['user_id']."'"); 
// Рассчёт награды 
$max_uron = $db->rows("SELECT SUM( uron ) FROM `aluko_log` WHERE `user_id`='".$top['user_id']."'");
$cena_uron=1; // монет за единицу урона             
$uron_money=$max_uron/16; 
$_exp = rand(200,1000); 
$_gold = rand(20,100); 
// Рассчёт награды 
// Пишем в журнал 
if($max_uron >0){ 
$db->query("UPDATE `users` SET `exp`=`exp`+".$_exp.", `silver`=`silver`+".$uron_money.", `gold`=`gold`+ ".$_gold."  WHERE `id`='".$top['user_id']."'"); 
}           
$name_top = $db->fetch("SELECT `login` FROM `users` WHERE `id`='".$top['user_id']."' LIMIT 1");
$topes_us.= 'Ура вы победили дракона!'; 
                $aluko['g']=$aluko['g']+5;
                $aluko['exp']= round($aluko['exp']/2);
            }
            $db->query("INSERT INTO `chat` SET
            `user`='1',
            `text`='".$topes_us."',
            `time`='".time()."'");

            while($nagr = $db->fetch($q_nagr)){
                $db->query("INSERT INTO `journal` SET 
`user_id`='".$nagr['user_id']."', `text`='За победу над Алуко Вы получаете ".$aluko['g']."',
                `time`='".time()."'");    

$db->query("UPDATE `users` SET `gold`=`gold`+".$aluko['g'].",`journal`=`journal`+1 WHERE `id`='".$nagr['user_id']."'");


                $aluko['g']= round($aluko['g']/2);
            }
            $db->query("TRUNCATE TABLE  `aluko_log`");
                    }

   include 'protected/footermain.php';
    exit;
    }
    
$aluko['health'] = $aluko['health']-$my_atk;    
$aluko_sl = array('пукнул в глаз','нанес урон','порезал','ударил','царапнул');
shuffle($aluko_sl);
$rand_m = rand(1,15);

if($user['artem_aluka']<1 ){
        ?>
  <div class="menu">
        <div class="mob">
            <span class="icon drag6"></span>
            <div class="info"><span class="high">Костяной дракон</span><br />
                <small>
                    <img src="http://mdrag.ru/style/images/icons/icon_str.png"> 8000                    <img src="http://mdrag.ru/style/images/icons/icon_hp.png"> 20400000                    <img src="http://mdrag.ru/style/images/icons/icon_def.png"> 4500                </small>
            </div>
        </div>
    </div>
    <div class="wrapper center lh1">
                    <span class="high">Доступно <?=$user['artem_aluka']?> боя.</span>
            <div class="wrapper-btn">
                <a class="btn _red w40p" href="?fight">Сразиться</a>
            </div>
            </div>
    <div class="wrapper lh1">
        <span class="high">Ваш урон:</span> <?=$user['uron']?><br />                           
        </div>
<span class="high">В бою:</span> <?=$inFight;?><br />                           
        </div>
        <div class ='menu'/>
        <center>

            

        <?
    include 'protected/footermain.php';
    exit;
    }

/**
* Атака написанная не таким ГК
* @author Alex Priadko
*/

if (isset($_GET['attack']))
{
    $db->query("INSERT INTO `aluko_log` SET
    `user_id`='".$user['id']."',
    `text`='".$aluko_sl[0]." <b>Дракон</b> на <b>".$my_atk."</b>',
    `time`='".time()."',
    `uron`='".$my_atk."'");


    $db->query("UPDATE `aluko` SET `health`=`health`-".$my_atk." WHERE `id`='".$aluko['id']."'");
    $db->query("UPDATE `users` SET `artem_aluka`=`artem_aluka`-1,`exp` = `exp` + '".$expee."',`silver` = `silver` + '".$se."' WHERE `id` = '".$user[id]."'");
    
    $perc_health_aluko = round($aluko['health']/$aluko['max_health']*100,2);
    header("Location:?");
    exit;
}

   $aluko_progress = round(100/($aluko['max_health']/$aluko['health']));
    echo '    
<div class="menu">
    <div class="mob">
        <span class="icon drag6"></span>
        <div class="info lh1"><span class="high">Костяной дракон</span>
            <small>
                <span class="error">-'.$my_atk.'</span><br />
                <img src="http://mdrag.ru/style/images/icons/icon_str.png"> 8000                <img src="http://mdrag.ru/style/images/icons/icon_hp.png"> '.$aluko['health'].'                <img src="http://mdrag.ru/style/images/icons/icon_def.png"> 4500            </small>
        </div>
    </div>
    <div class="progress-bar _fight">
        <div style="width: '.$aluko_progress.'%;" class="progress _green fl"></div>
    </div>
</div>
<div class="wrapper-btn t w100p">
    <div style="padding: 0 5px" class="cell w50p">
        <a class="btn _blue w100p no-padding" href="?attack">Автобой</a>
    </div>
    <div style="padding: 0 5px" class="cell w50p">
        <a class="btn _red w100p no-padding" href="?attack">Ударить</a>
    </div>
</div>

  

    ';
    
include_once 'protected/footermain.php';


$aluko_log_q = $db->query("SELECT * FROM `aluko_log` ORDER BY `time` DESC LIMIT 5");

echo '</div>';
$self = 'Дракон';
$inFight = $db->rows("SELECT `id`,`self` FROM `users` 
                                    WHERE `self`='".($self)."' and 
                                    `online`>'".(time()-300)."'");

?>
<div class ='line'/></div>
<div class ='content'/>
<center/>
У вас бои:  <?=n_f($user['artem_aluka'])?> 
</center>
</div>
<?
echo '
<div class ="line"/></div>
<div class="content"><center>Лог боя
</center>
</div>
<div class ="line"/></div>
';

while($aluko_log = $db->fetch($aluko_log_q))
{

    $row = $db->fetch("SELECT * FROM `users` WHERE `id`='".$aluko_log['user_id']."'");


    //echo '<a href="user.php?ank='.$ank['id'].'">'.$ank['login'].'</a> '.$aluko_log['text'].'<br>';
    ?>

<div class="bdr cnr mb2"><div class="wr1"><div class="wr2"><div class="wr3"><div class="wr4"><div class="wr5"><div class="wr6"><div class="wr7"><div class="wr8">
		<div class="ml5 mt5 mb5 sh small">

	<div class="alf"><div class="art"><div class="acn"></div></div></div></div>
		<div class="mb10 mt10 mlr10 lwhite lft">
    <span class ='float:left;'/>
    <a href='/view_profile/<?=$row['id']?>'>
    <?=$row['login']?>
    </a>
    </span>
    |
    <?=$aluko_log['text'];?>
    </div>
    <div class ='line'/></div>

	</div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div>
    <?
}

/**
* Вывод лидеров в бою!
*
*@author AlexPriadko
*/
$queryLiders  = $db->rows("SELECT SUM( uron ) , `user_id`  FROM  `aluko_log`
                             GROUP BY  `user_id`
                             ORDER BY  SUM( uron ) DESC LIMIT 3");

?>
<div class ='content'/>
<center>
    Топ 3 бойцов:
</center>
</div>
<div class ='line'/>
</div>
<?

$alPos = 0;
while ($liders = $db->fetchAll($queryLiders))
{
   $alPos++;
    $row = $db->fetch("SELECT * FROM `users` 
                                        WHERE `id`='".$liders['user_id']."'");
    $damageUs = $db->rows("SELECT SUM( uron ) , `user_id`  
                            FROM  `aluko_log`
                            WHERE `user_id`='".$liders['user_id']."'");
    
    ?>  
    <div class ='menu'/>
    <span class ='float:left;'/>
    <?=$alPos;?>.<img src='/images/icon/race/<?=$row['r'].($row['online'] > (time() - 300) ? '':'-off')?>.png' alt='*'/>
    <a href='/user/<?=$row['id']?>/'>
    <?=$row['login'];?>
    </a>
    </span>
    <span class ='float:right;'/>
    <?=$damageUs;?>
    </span>
    </div>
    <div class ='dl'/></div>
    <?
}



include_once 'protected/footermain.php';
