<?php

define('_CONSTANT_', 1); 

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

checkAuth();

head('Локации', 'Локации');

$costTeleport = $Filter->clearInt($settings['fight_cost_teleport']);            // Цена телепорта;

/*
 * Если получаем ID локации;
 */
if (isset($_GET['locationId']) && !empty($_GET['locationId'])) 
{
    $locationId = $Filter->clearInt($_GET['locationId']);
    
    $costTeleportPlus = $Filter->clearInt($costTeleport * $locationId);         // Умножаем цену телепорта;
    
    $location = DB::$dbs->queryFetch("SELECT * FROM " . LOCATION . " WHERE `id` = ? LIMIT 1", [$locationId]);
    
    if (!$location) 
    {
        error('Локации не существует.', '', 'На главную');
    }
    
    if ($user['money'] < $costTeleportPlus && $user['location_id'] != $locationId) 
    {
        error('Недостаточно денег для телепорта.', '', 'На главную');
    }
    
    /*
     * Если не записана локация;
     */ 
    if ($user['location_id'] != $locationId) 
    {
        echo DIV_BLOCK . 'Оплата телепортации: ' . moneyIcon() . $costTeleportPlus . CLOSE_DIV;
        
        DB::$dbs->query("UPDATE " . USERS . " SET `location_id` = ?, `money` = ? WHERE `id` = ?", [$locationId, ($user['money'] - $costTeleportPlus), $user['id']]);
    }
    
    # Recomend lvl;
    if ($locationId == 1) 
    {
        $recomendLvl = '1-5';
    } 
    else if ($locationId == 2) 
    {
        $recomendLvl = '6-10';
    } 
    else if ($locationId == 3)
    {
        $recomendLvl = '11-15';
    } 
    else if ($locationId == 4) 
    {
        $recomendLvl = '16-20';
    } 
    else if ($locationId == 5) 
    {
        $recomendLvl = '21-25';
    } 
    else 
    {
        $recomendLvl = 'Неизвестно';
    }
    ?> 
    <div class="wrap">
        Рекомендуемый уровень персонажа <?=$recomendLvl?>.
    </div>
    <?php
    
    /*
     * Вывод NPC;
     */ 
    $all = DB::$dbs->querySingle("SELECT COUNT(`id`) FROM " . NPC . " WHERE `location_id` = ? and `dead` = ?", [$locationId, 0]);
    $n = new Navigator($all,5,'locationId='. $locationId);
    $sql = DB::$dbs->query("SELECT * FROM " . NPC . " WHERE `location_id` = ? and `dead` = ? ORDER BY `lvl` LIMIT {$n->start()}, 5", [$locationId, 0]);
    
    /*
     * Если пуста локация;
     */ 
    if (empty($all)) 
    {
        ?> 
        <div class="wrap">
            Монстров нет
        </div>
        <?php
    }
    while ($npc = $sql -> fetch()) 
    {
        /*
         * Если NPC жив;
         */
        if ($npc['dead'] == 0) 
        {
            echo '<a href="'.HOME.'/modules/fight/attack.php?do=attack.location.npc&amp;npcId='.$Filter->clearInt($npc['id']).'&amp;locationId='.$locationId.'" class="link-touch"><img src="/img/NPC/'.$Filter->output($npc['img']).'" alt="[npc-img]" width="18px" height="18px">' . $Filter->output($npc['name']) . '<span class="count">'. ico('exp.png') .'Уровень: ' . $Filter->clearInt($npc['lvl']) . '</span></a>';
        } 
    } 
    
    echo $n->navi();
    ?> 
    <a href="<?=HOME?>/modules/fight/location.php" class="link-touch"><?=ico('back.png')?> Локации</a>
    <?php
} 
else 
{
    $all = DB::$dbs->querySingle("SELECT COUNT(`id`) FROM " . LOCATION . " ORDER BY `id` DESC");
    $n = new Navigator($all,10,'');
    $sql = DB::$dbs->query("SELECT * FROM " . LOCATION . " ORDER BY `id` LIMIT {$n->start()}, 10"); 
    
    while ($loc = $sql -> fetch()) 
    {
        $costTeleportPlus = $Filter->clearInt($costTeleport * $loc['id']);      // Умноженая цена на телепорт; 
        
        /*
         * Если пользователь уже в этой локации;
         */ 
        if ($user['location_id'] == $loc['id']) 
        {
            $costTeleportPlus = 0;
        }
        
        echo '<a href="' . HOME . '/modules/fight/location.php?locationId='.$Filter->clearInt($loc['id']).'" class="link-touch">'.$Filter->output($loc['name']).' <span class="count">'.moneyIcon(). $costTeleportPlus . '</span></a>';
    } 
    
    echo $n->navi();
    echo '<a href="#" class="link-touch">'. ico('info.png', '[info-icon]').' О локациях</a>';
} 

require_once '../../core/foot.php';

