<?php
define('_CONSTANT_', 1); 

require_once '../../core/start.php';

checkAuth(); 

switch ($do) 
{
    default : 
        head('Рейтинг игроков', 'Рейтинг игроков');
        ?> 
<a href="<?=HOME?>/modules/rating/index.php?do=param" class="link-touch"><?=ico('strike.png')?> Рейтинг по сумме параметров</a> 
<a href="<?=HOME?>/modules/rating/index.php?do=level" class="link-touch"><?=ico('exp.png')?> Рейтинг по уровню</a> 
<a href="<?=HOME?>/modules/rating/index.php?do=timeOnline" class="link-touch"><?=ico('clock.png')?> Рейтинг по времени в игре</a>
<a href="<?=HOME?>/modules/rating/index.php?do=usersOnline" class="link-touch"><?=ico('users.png')?> Игроки онлайн</a> 
<a href="<?=HOME?>/modules/rating/index.php?do=search" class="link-touch"><?=ico('search.png')?> Поиск</a>
<?php 
        break; 
        
        case'level': 
            head('Рейтинг по уровню', 'Рейтинг по уровню'); 
            
            $all = DB::$dbs->querySingle("SELECT COUNT(`id`) FROM ".USERS." ORDER BY `lvl` DESC");
            $n = new Navigator($all,10,'do=level');
            $sql = DB::$dbs->query("SELECT * FROM ".USERS." ORDER BY `lvl` DESC LIMIT {$n->start()}, 10"); 
            
            $position = 0;

            while ($ank = $sql -> fetch()) 
            {

                $position++;

                ?> 
                <?=DIV_CONTENT?>

                <?php if($position==1) : ?> <span style='font-weight: bold;color:#E8C849;'><?=$position?>
                <?php elseif($position==2) : ?><span style='font-weight: bold;color:#979797;'><?=$position?>
                <?php elseif($position==3) : ?><span style='font-weight: bold;color:#6A5020;'><?=$position?>
                <?php else : ?><span style='color:#D7D7D7;'><?=$position?>
                <?php endif ?></span>
                .<span> <?=userLink($ank['id'])?></span>
                <br />
                <span>Уровень: <font color="#D7D7D7"><?=$ank['lvl']?></font></span>
               <br />
              <?=CLOSE_DIV?>
                <?php
            } 
            ?>  
                <?=$n->navi()?> 
                <?php 
                $array = ['Рейтинг игроков'];
                navPanel($array); 
                break; 
                
        case 'timeOnline': 
            head('Рейтинг по времени в игре', 'Рейтинг по времени в игре');
            
            $all = DB::$dbs->querySingle("SELECT COUNT(`id`) FROM ".USERS." ORDER BY `online_time` DESC");
            $n = new Navigator($all,10,'do=timeOnline');
            $sql = DB::$dbs->query("SELECT * FROM ".USERS." ORDER BY `online_time` DESC LIMIT {$n->start()}, 10"); 
            
            $position = 0;
            
            while ($ank = $sql -> fetch()) 
            { 
                $position ++;
                
                ?>
                <?=DIV_CONTENT?>

    <?php if($position==1) : ?> <span style='font-weight: bold;color:#E8C849;'><?=$position?>
    <?php elseif($position==2) : ?><span style='font-weight: bold;color:#979797;'><?=$position?>
    <?php elseif($position==3) : ?><span style='font-weight: bold;color:#6A5020;'><?=$position?>
    <?php else : ?><span style='color:#D7D7D7;'><?=$position?>
    <?php endif ?>

    .<?=userLink($ank['id'])?></span>
    <br/>
   <?=ico('clock.png')?>Провел в игре: <span style='color:#D7D7D7;'> <?=countTime($ank['online_time'])?> </span> 
   <!-- Провел в игре: <?=countTime($ank['online_time'])?>  -->

<?=CLOSE_DIV?>
                <?php
            }
            ?>  
                <?=$n->navi()?> 
                <?php 
                $array = ['Рейтинг игроков'];
                navPanel($array); 
                break;
                
        case 'usersOnline': 
            head('Кто онлайн', 'Кто онлайн'); 
            
            $all = DB::$dbs->querySingle("SELECT COUNT(`id`) FROM ".USERS." WHERE `last_time` > ?", [(time() - 600)]);
            $n = new Navigator($all,10,'do=usersOnline');
            $sql = DB::$dbs->query("SELECT * FROM ".USERS." WHERE `last_time` > ? ORDER BY `lvl` DESC LIMIT {$n->start()}, 10", [(time() - 600)]); 
            
            while ($ank = $sql -> fetch()) 
            { 
                $lastTime = countTime(time() - $ank['last_time']);
                if ($lastTime == NULL) 
                {
                    $lastTime = 'Только-что';
                } 
                else 
                {
                    $lastTime .= ' назад'; 
                }
                ?> 
                <?=userLink($ank['id'], 'link-touch', '<span class="count">' . $lastTime . '</span>')?> 
                <?php
            } 
            ?>  
                <?=$n->navi()?> 
                <?php  
                break; 

                case 'search': 

                head('Поиск игрока', 'Поиск игрока'); 

                if (isset($_POST['try'])) 
                {
                    $data = $_POST;
                    $data['login'] = $Filter->clearString($data['login']);

                    if (empty($data['login'])) 
                    {
                        error('Обнаружено пустое поле', '/', 'На главную');
                    }
                    ?> 
                    <div class="head">Результаты поиска:</div> 
                    <?php 
                    $all = DB::$dbs->querySingle("SELECT COUNT(`id`) FROM ".USERS." WHERE `login` LIKE '%".$data['login']."%' ORDER BY `id` DESC"); 

                    echo DIV_CONTENT;

                    if (!empty($all)) 
                    {
                        $n = new Navigator($all,10,'do=search'); 
                        $sql = DB::$dbs->query("SELECT * FROM ".USERS." WHERE `login` LIKE '%".$data['login']."%' ORDER BY `id` DESC LIMIT {$n->start()}, 10"); 

                        while ($profile = $sql -> fetch()) 
                        {
                            ?> 
                            <?=userLink($profile['id'], 'link-touch')?> 
                            <?php
                        }
                    } 
                    else 
                    {
                        ?> 
                        Пользователь не найден<br/>
                        <?php
                    }
                } 
                ?> 
                <form action="" enctype="multipart/form-data" method="POST"> 
                Логин:<br/> 
                <input type="text" name="login" value="<?=(!empty($data['login']) ? $Filter->output($data['login']) : NULL)?>"><br/> 
                <input type="submit" name="try" value="Поиск"> 
                </form> 
                <?php 

                $array = ['Рейтинг игроков'];
                navPanel($array);
                break;

                case 'param': 
                head('Рейтинг по cумме параметров', 'Рейтинг по сумме параметров');

                $all = DB::$dbs->querySingle("SELECT COUNT(`id`) FROM ".USERS." WHERE `access` = ? ORDER BY `strike` + `defend` + `max_health` DESC", [0]);
                $n = new Navigator($all,10,'do=param');
                $sql = DB::$dbs->query("SELECT * FROM ".USERS." WHERE `access` = ? ORDER BY `strike` + `defend` + `max_health` DESC LIMIT {$n->start()}, 10", [0]);

                $position = 0;

                while ($ank = $sql -> fetch()) 
                {
                    $position ++;
                    $param = $ank['strike'] + $ank['defend'] + $ank['max_health'];
                    ?> 
                    <?=DIV_CONTENT?>

                    <?php if($position==1) : ?> <span style='font-weight: bold;color:#E8C849;'><?=$position?>
                    <?php elseif($position==2) : ?><span style='font-weight: bold;color:#979797;'><?=$position?>
                    <?php elseif($position==3) : ?><span style='font-weight: bold;color:#6A5020;'><?=$position?>
                    <?php else : ?><span style='color:#D7D7D7;'><?=$position?>
                    <?php endif ?></span>
                    .<span> <?=userLink($ank['id'])?></span>
                    <br />
                    <span>Cумма: <font color="#D7D7D7"><?=$param?></font></span>
                    <br />
                    <?=CLOSE_DIV?>
                    <?php
                } 
                echo $n->navi();
                $array = ['Рейтинг игроков'];
                navPanel($array);
                break;
} 
        
require_once '../../core/foot.php';