<?php
define('_CONSTANT_', 1);
require_once '../../core/start.php';
checkAuth();
head('Профиль', 'Профиль');

$userId = (!empty($_GET['userId']) ? $Filter->clearInt($_GET['userId']) : NULL); 

#$UserInfo = $User->userInfo($userId);

if ($userId == $user['id']) 
{
    # Param count; 
    $userDefend      = ($user['defend'] <= 1000 ? round($user['defend'] / 1000 * 100,2) : 100);
    $userHealth      = round($user['health'] / $user['max_health'] * 100,2);
    $nextLvlExp      = DB::$dbs->queryFetch("SELECT * FROM " . USER_EXP . " WHERE `lvl` = ?", [$user['lvl']]);
    $userExp         = ($user['exp'] <= $nextLvlExp['exp'] ? round($user['exp'] / $nextLvlExp['exp'] * 100,2) : 100);
    $userCrit        = round($user['lvl'] / 3.75);
    $userCritPercent = round($userCrit / 100 * 100,2);
    ?> 
<?=DIV_CONTENT?>
    <div class="head">
        <?=$User->getQuery('userLogin', $userId)?>(<?=$User->sex($user['id'], 'icon') . $User->sex($user['id'])?>)
    </div>
    <!-- User Background and Photo -->
    <div class="userBackground" style="background: url(&#39/img/user/background/<?=$Filter->clearInt($user['background'])?>.png&#39;) no-repeat center;">
        <div style="text-align: center;"><img src="<?=(!empty($user['photo']) && $user['is_moderate_photo'] == 1 ?  HOME .'/img/user/photo/mini/'.$user['photo'] : HOME . '/img/user/photo/mini/empty.gif')?>" width="120px" height="160px" alt="[user-photo]">
        </div>
    </div>
    <div class="head">
        Информация
    </div>
    <div class="wrap"> 
        <?=ico('user.png')?>Класс: <?=$User->userClass($user['id'])?><br/>
        <?=ico('exp.png')?>Уровень: <?=$user['lvl']?><br/>
        <?=ico('strike.png')?>Сила: <?=$user['strike']?><br/>
        <?=ico('strike.png')?>Крит: <?=$userCrit?>%<br/>
        <div style="height: 2px; background: #252525;">
            <div style="height: 2px;background: #DA872F;width:<?=$userCritPercent?>%;">
            </div>
        </div>
        <?=ico('defend.png')?>Защита: <?=$user['defend']?>/1000<br/>
        <div style="height: 2px; background: #252525;">
            <div style="height: 2px;background: #DA872F;width:<?=$userDefend?>%;">
            </div>
        </div>
        <?=ico('health.png')?>Здоровье: <?=$user['health']?>/<?=$user['max_health']?><br/>
        <div style="height: 2px; background: #252525;">
            <div style="height: 2px;background: #FF4040;width:<?=$userHealth?>%;">
            </div>
        </div>
        <?=ico('exp.png')?>Опыт: <?=$user['exp']?>/<?=$nextLvlExp['exp']?><br/>
        <div style="height: 2px; background: #252525;">
            <div style="height: 2px;background: #006400;width:<?=$userExp?>%;">
            </div>
        </div>
    </div>
    <div class="title">
        Умения
    </div>
    <div style="text-align: center;">
        <?php 
        if ($user['class'] == 'warior') 
        {
            ?> 
            <img src="/img/user/skill/1/<?=$user['skill_1_lvl']?>.png" width="25px">
            <?php
        }
        ?>
    </div>

    <?php
    # Bonus list;
    $all = DB::$dbs->querySingle("SELECT COUNT(`id`) FROM ". USER_BONUS ." WHERE `user_id` = ?", [$user['id']]);

    if (!empty($all)) 
    {
        ?> 
        <div class="head">
            Эффекты
        </div>
        <div class="wrap">
            <?php
            $sql = DB::$dbs->query("SELECT * FROM ". USER_BONUS ." WHERE `user_id` = ?", [$user['id']]);

            while ($bonus = $sql -> fetch()) 
            {
                ?> 
                <?=ico('effect.png', 'ico', 14, 14) . $Filter->output($bonus['text'])?>: <?=($bonus['type'] == 'param' ? '<span style="color: green;">+ '. ico('strike.png', 'ico', 14, 14) . ico('health.png', 'ico', 14, 14) . ico('defend.png', 'ico', 14, 14) . $bonus['param'] .'</span>' : null)?> <span style="color: #808080;"> осталось <?=countTime($bonus['time'] - time())?></span><br/>
                <?php
            }
            ?> 
        </div>
        <?php
    }
    ?>

    <hr>
    <a href="<?=HOME?>/modules/user/settings/index.php" class="link-touch"><?=ico('settings.png')?> Настройки</a> 
    <a href="<?=HOME?>/modules/user/achievements/index.php" class="link-touch"><?=ico('rating.png')?> Достижения</a> 
    <a href="<?=HOME?>/modules/mail/index.php" class="link-touch"><?=ico('mail.png')?> Почта</a>
    <a href="<?=HOME?>/modules/user/train/index.php" class="link-touch"><?=ico('train.png')?> Тренировка <span class="count"><?=ico('train.png') . $Filter->clearInt($user['train'])?></span></a>
    <a href="<?=HOME?>/modules/user/skill/index.php" class="link-touch"><?=ico('strike.png')?> Умения</a>
    <div class="wrap">
        <?=ico('journal.png')?>История: Глава <?=($user['history'] + 1)?>
        <?php 
        if (!empty($user['clan_id'])) 
        {
            $clan = DB::$dbs->queryFetch("SELECT `name` FROM ". CLANS ." WHERE `id` = ?", [$user['clan_id']]);
            ?> 
            <br/>
            <?=ico('clan.png')?>Вы состоите в клане: <a href="<?=HOME?>/modules/clan/clans.php?do=info&amp;clanId=<?=$user['clan_id']?>"> <?=$Filter->output($clan['name'])?></a>
            <?php
        }
    ?> 
    </div>
    <?php
    if ($user['vip'] > time()) 
    { 
        $countVip = $user['vip'] - time();
        ?> 
        <span style="color: #fff000;"><?=ico('vip.png')?>VIP: <?=countTime($countVip)?></span><br/>
        <?php
    }
    if ($user['bonus_time'] > time()) 
    {
        $timeToBonus = countTime($user['bonus_time'] - time());
    }  
    else 
    {
        $timeToBonus = '<span style="color: darkorange;">Можно забрать!</span>';
    }
    ?>
    <div class="wrap">
        <?=ico('gift.png')?>Ежедневный бонус: <?=$timeToBonus?>
        <br/>
        <?=(!empty($user['ref']) ? ico('user.png') .'Пригласил:'. userLink($user['ref']) : null)?> 
    </div>
    <span style="font-size: 10px;"><?=ico('info.png')?>Id персонажа: <?=$user['id']?></span>
<?=CLOSE_DIV?>
<?php

# Ban check;
$ban = DB::$dbs->queryFetch("SELECT * FROM ".BAN." WHERE `user_id` = ? && `time_ban` > ?", array($userId, time()));

if ($ban != NULL) 
{
    echo DIV_ERROR;
        
    echo 'Вы заблокированы!<br/> Причина: ' . $Filter->output($ban['text']) . ' <br/>Дата окончания: ' . dataTime($Filter->clearInt($ban['time_ban'])) . '<br/> Заблокировал: ' . userLink($ban['moderator_id']);
        
    echo CLOSE_DIV;
}

} 
elseif ($userId != $user['id'] && $userId != null) 
{
    $profile = DB::$dbs->queryFetch("SELECT * FROM ".USERS." WHERE `id` = ?", [$userId]); 
    if ($profile) 
    {
        # Param count; 
        $userDefend = ($profile['defend'] <= 1000 ? round($profile['defend'] / 1000 * 100,2) : 100);
        $userHealth = round($profile['health'] / $profile['max_health'] * 100,2);
        ?> 
        <?=DIV_CONTENT?>
            <div class="head">
                <?=$User->getQuery('userLogin', $userId)?>(<?=$User->sex($profile['id'], 'icon') . $User->sex($profile['id'])?>) - <?=($profile['last_time'] > time() - 600 ? '<span style="color: green;">[онлайн]</span>' : '<span style="color: silver;">[нет в игре]</span>')?>
            </div>
            <!-- User Background and Photo -->
            <div class="userBackground" style="border-left:1px #284440 solid;border-right:1px #284440 solid;background: url(&#39/img/user/background/<?=$Filter->clearInt($profile['background'])?>.png&#39;) no-repeat center;">
                <div style="text-align: center;"><img src="<?=(!empty($profile['photo']) && $profile['is_moderate_photo'] == 1 ?  HOME .'/img/user/photo/mini/'.$profile['photo'] : HOME . '/img/user/photo/mini/empty.gif')?>" width="120px" height="160px" alt="[user-photo]">
                </div>
            </div>

            <div class="head">
                Информация
            </div>
            <div class="wrap"> 
                <?=ico('user.png')?>Класс: <?=$User->userClass($userId)?><br/>
                <?=ico('exp.png')?>Уровень: <?=$profile['lvl']?><br/>
                <?=ico('strike.png')?>Сила: <?=$profile['strike']?><br/>
                <?=ico('defend.png')?>Защита: <?=$profile['defend']?>/1000<br/>
                <div style="height: 2px; background: #252525;">
                    <div style="height: 2px;background: #DA872F;width:<?=$userDefend?>%;">
                    </div>
                </div>
                <?=ico('health.png')?>Здоровье: <?=$profile['health']?>/<?=$profile['max_health']?><br/>
                <div style="height: 2px; background: #252525;">
                    <div style="height: 2px;background: #FF4040;width:<?=$userHealth?>%;">
                    </div>
                </div>
            </div>
            <div class="title">
                Умения
            </div>
            <div style="text-align: center;">
                <?php 
                if ($profile['class'] == 'warior') 
                {
                    ?> 
                    <img src="/img/user/skill/1/<?=$profile['skill_1_lvl']?>.png" width="25px">
                    <?php
                }
                ?>
            </div>
        <hr>
        <div class="title">
            Снаряжение
        </div>
        <div style="text-align: center;">
            <?php
            # Helmet
            $helmet = DB::$dbs->query("SELECT * FROM " . USERITEM . " WHERE `status` = ? and `type` = ? and `user_id` = ?", [1, 'helmet', $profile['id']]);
            $helmetS = DB::$dbs->querySingle("SELECT * FROM " . USERITEM . " WHERE `status` = ? and `type` = ? and `user_id` = ?", [1, 'helmet', $profile['id']]);

            if ($helmetS == null) 
            {
                echo '<img src="/img/item/empty.png" alt="*">';
            } 
            else 
            { 
                while ($hel = $helmet -> fetch()) 
                {
                    echo '<img src="/img/item/helmet/' . $hel['img'] . '.png" alt="*" width="25px">';
                }
            }

            # Weapon 
            $weapon = DB::$dbs->query("SELECT * FROM " . USERITEM . " WHERE `status` = ? and `type` = ? and `user_id` = ?", [1, 'weapon', $profile['id']]);
            $weaponS = DB::$dbs->querySingle("SELECT * FROM " . USERITEM . " WHERE `status` = ? and `type` = ? and `user_id` = ?", [1, 'weapon', $profile['id']]);

            if ($weaponS == null) 
            {
                echo '<img src="/img/item/empty.png" alt="*">';
            } 
            else 
            { 
                while ($wea = $weapon -> fetch()) 
                {
                    echo '<img src="/img/item/weapon/' . $wea['img'] . '.png" alt="*" width="25px">';
                }
            }

            # Defend(защита); 
            $defend = DB::$dbs->query("SELECT * FROM " . USERITEM . " WHERE `status` = ? and `type` = ? and `user_id` = ?", [1, 'defend', $profile['id']]);
            $defendS = DB::$dbs->querySingle("SELECT * FROM " . USERITEM . " WHERE `status` = ? and `type` = ? and `user_id` = ?", [1, 'defend', $profile['id']]);

            if ($defendS == null) 
            {
                echo '<img src="/img/item/empty.png" alt="*">';
            } 
            else 
            { 
                while ($def = $defend -> fetch()) 
                {
                    echo '<img src="/img/item/defend/' . $wea['img'] . '.png" alt="*" width="25px">';
                }
            }

            # Armor(броня);
            $armor = DB::$dbs->query("SELECT * FROM " . USERITEM . " WHERE `status` = ? and `type` = ? and `user_id` = ?", [1, 'armor', $profile['id']]);
            $armorS = DB::$dbs->querySingle("SELECT * FROM " . USERITEM . " WHERE `status` = ? and `type` = ? and `user_id` = ?", [1, 'armor', $profile['id']]);

            if ($armorS == null) 
            {
                echo '<img src="/img/item/empty.png" alt="*">';
            } 
            else 
            { 
                while ($arm = $armor -> fetch()) 
                {
                    echo '<img src="/img/item/armor/' . $arm['img'] . '.png" alt="*" width="25px">';
                }
            }

            # Soulder(плечи);
            $shoulder = DB::$dbs->query("SELECT * FROM " . USERITEM . " WHERE `status` = ? and `type` = ? and `user_id` = ?", [1, 'shoulder', $profile['id']]);
            $shoulderS = DB::$dbs->querySingle("SELECT * FROM " . USERITEM . " WHERE `status` = ? and `type` = ? and `user_id` = ?", [1, 'shoulder', $profile['id']]);

            if ($shoulderS == null) 
            {
                echo '<img src="/img/item/empty.png" alt="*">';
            } 
            else 
            { 
                while ($sho = $shoulder -> fetch()) 
                {
                    echo '<img src="/img/item/shoulder/' . $sho['img'] . '.png" alt="*" width="25px">';
                }
            }

            # Ring(кольцо);
            $ring = DB::$dbs->query("SELECT * FROM " . USERITEM . " WHERE `status` = ? and `type` = ? and `user_id` = ?", [1, 'ring', $profile['id']]);
            $ringS = DB::$dbs->querySingle("SELECT * FROM " . USERITEM . " WHERE `status` = ? and `type` = ? and `user_id` = ?", [1, 'ring', $profile['id']]);

            if ($ringS == null) 
            {
                echo '<img src="/img/item/empty.png" alt="*">';
            } 
            else 
            { 
                while ($rin = $ring -> fetch()) 
                {
                    echo '<img src="/img/item/ring/' . $rin['img'] . '.png" alt="*" width="25px">';
                }
            }
            ?>
            </div>
            <hr>
            <a href="<?=HOME?>/modules/mail/index.php?userId=<?=$Filter->clearInt($userId)?>" class="link-touch"><?=ico('mail.png')?> Написать сообщение</a>
            <?php 
            if ($User->clanAccess($user['id']) == 5) 
            {
                $clanForInvite = DB::$dbs->queryFetch("SELECT * FROM " . CLANS . " WHERE `id` = ?", [$user['clan_id']]);
                    if ($profile['clan_id'] == 0 && $profile['clan_invite'] == 0 && $clanForInvite['users'] != $clanForInvite['max_users']) 
                    {
                        ?>
                        <a href="<?=HOME?>/modules/clan/index.php?do=inviteUser&amp;userId=<?=$Filter->clearInt($profile['id'])?>" class="link-touch"> Пригласить в клан</a>
                        <?php
                    }
            }

            $partyMembers = DB::$dbs->querySingle("SELECT COUNT(`id`) FROM " . PARTY . " WHERE `id` = ?", [$user['party']]);
            $party = DB::$dbs->queryFetch("SELECT * FROM " . PARTY . " WHERE `id` = ?", [$user['party']]);

            if ($profile['party'] == 0 && $user['party'] != NULL && $user['party_access'] == 1 && $partyMembers != $party['max_members'] && $profile['party_invite'] == 0 && $profile['dungeon_cooldown'] < time() && $user['dungeon_cooldown'] < time()) 
            {
                if (isset($_GET['addInviteParty'])) 
                {
                    DB::$dbs->query("UPDATE " . USERS . " SET `party_invite` = ? WHERE `id` = ?", [$user['party'], $profile['id']]);
                    success('Приглашение отправлено!', '/game/index.php', 'На главную');
                } 
                ?> 
                <a href="<?=HOME?>/modules/user/index.php?userId=<?=$userId?>&amp;addInviteParty" class="link-touch">Пригласить в банду</a>
                <?php
            }

            ?>
            <div class="wrap">
                <?=ico('journal.png')?>История: Глава <?=($profile['history'] + 1)?>
            <?php 
            if (!empty($profile['clan_id'])) 
            {
                $clan = DB::$dbs->queryFetch("SELECT `name` FROM ". CLANS ." WHERE `id` = ?", [$profile['clan_id']]);
                ?> 
                <br/>
                <?=ico('clan.png')?>Состоит в клане: <a href="<?=HOME?>/modules/clan/clans.php?do=info&amp;clanId=<?=$profile['clan_id']?>"> <?=$Filter->output($clan['name'])?></a>
                <?php
            } 
            else 
            {
                ?> 
                <br/>
                <?=ico('clan.png')?>Не состоит в клане
                <?php
            }
            ?> 
            </div>
            <?php

            if ($profile['vip'] > time()) 
            { 
                $countVip = $profile['vip'] - time();
                ?> 
                <div class="wrap">
                    <?=ico('vip.png')?>VIP: <?=countTime($countVip)?>
                </div>
                <?php
            } 
            ?>
            <div class="wrap">
                <?=(!empty($profile['ref']) ? ico('user.png') .'Пригласил:'. userLink($profile['ref']) : null)?> 
            </div>
            <span style="font-size: 10px;"><?=ico('info.png')?>Id персонажа: <?=$userId?></span>
<?=CLOSE_DIV?>

<?php 
$ban = DB::$dbs->queryFetch("SELECT * FROM ".BAN." WHERE `user_id` = ? && `time_ban` > ?", array($profile['id'], time()));

if ($ban != NULL) 
{
    echo DIV_ERROR;
        
    echo 'Пользователь заблокирован!<br/> Причина: ' . $Filter->output($ban['text']) . ' <br/>Дата окончания: ' . dataTime($Filter->clearInt($ban['time_ban'])) . '<br/> Заблокировал: ' . userLink($ban['moderator_id']);
        
    echo CLOSE_DIV;
}

if ($user['access'] == 2) 
{
    ?>
    <div class="head">
        Для администратора
    </div>
    <div class="wrap"> 
        Сейчас находится: <?=$Filter->output($profile['where'])?>
        <br/><hr>
        Точное местонахождение: <?=$Filter->output($profile['where_link'])?>
        <br/><hr>
        IP: <?=$profile['ip']?>
        <br/><hr>
        Браузер: <?=$Filter->output($profile['browser'])?>
        <br/><hr> 
        Последнее действие: <?= dataTime($Filter->clearInt($profile['last_time']))?>
        <br/><hr> 
        Провел на сайте: <?= countTime($Filter->clearInt($profile['online_time']))?>
        <br/><hr> 
        Баны: (<a href="<?=HOME?>/modules/administrative/log.php?do=banUser&amp;userId=<?=$Filter->clearInt($profile['id'])?>"><?=$Count->userCountBan($profile['id'])?></a>)
        <br/><hr>
        <a href="<?=HOME?>/modules/administrative/index.php?do=editUser&amp;userId=<?=$Filter->clearInt($profile['id'])?>" class="link-touch"> Редактировать игрока</a>
        <a href="<?=HOME?>/modules/administrative/index.php?do=banedUser&amp;userId=<?=$Filter->clearInt($profile['id'])?>" class="link-touch"> Заблокировать игрока</a>
        <a href="<?=HOME?>/modules/administrative/index.php?do=deleteUser&amp;userId=<?=$Filter->clearInt($profile['id'])?>" class="link-touch"> Удалить игрока</a>
    </div>

    <?php
}
    } 
    else 
    {
        error('Пользователь не найден!', '/game/');
    }
}  
else 
{
    error('Пользователь не найден!', '/game/');
}
require_once '../../core/foot.php';
?>
