View file vobog-wmzo_ru/user/equip.php

File size: 5.65Kb
<?php
require_once ('../system/core.php');
auth();

if (isset($_GET['equip'])) {
    $id = (int)$_GET['equip'];
    $item = db_query("SELECT i.id, i.item_id, it.w, it.str, it.def, it.hp, it.level, it.name 
                       FROM `inventory` i 
                       JOIN `items` it ON i.item_id = it.id 
                       WHERE i.id = ? AND i.user_id = ? AND i.equiped = '0' LIMIT 1", [$id, $myID])->fetch();
    if ($item) {
        $slot_name = 'w_' . (int)$item['w'];
        if ($user['level'] < $item['level']) {
            set_msg("Ваш уровень слишком мал!", "error");
        } else {
            if ($user[$slot_name] > 0) {
                $old = db_query("SELECT str, def, hp FROM `items` WHERE `id` = ? LIMIT 1", [$user[$slot_name]])->fetch();
                db_query("UPDATE `inventory` SET `equiped` = '0' WHERE `user_id` = ? AND `item_id` = ? LIMIT 1", [$myID, $user[$slot_name]]);
                db_query("UPDATE `users` SET `str` = `str` - ?, `def` = `def` - ?, `hp_max` = `hp_max` - ? WHERE `id` = ?", [(int)$old['str'], (int)$old['def'], (int)$old['hp'], $myID]);
            }
            db_query("UPDATE `users` SET `$slot_name` = ?, `str` = `str` + ?, `def` = `def` + ?, `hp_max` = `hp_max` + ? WHERE `id` = ?", [$item['item_id'], (int)$item['str'], (int)$item['def'], (int)$item['hp'], $myID]);
            db_query("UPDATE `inventory` SET `equiped` = '1' WHERE `id` = ?", [$id]);
            set_msg("Вы надели " . out($item['name']));
        }
    }
    header("Location: equip.php"); exit;
}

if (isset($_GET['unequip'])) {
    $slot_id = (int)$_GET['unequip'];
    $slot_name = 'w_' . $slot_id;
    if ($user[$slot_name] > 0) {
        $item = db_query("SELECT id, name, str, def, hp FROM `items` WHERE `id` = ? LIMIT 1", [$user[$slot_name]])->fetch();
        $inv_count = (int)db_query("SELECT COUNT(*) FROM `inventory` WHERE `user_id` = ? AND `equiped` = '0'", [$myID])->fetchColumn();
        if ($inv_count >= $user['inv_limit']) {
            set_msg("Сумка полна!", "error");
        } else {
            db_query("UPDATE `inventory` SET `equiped` = '0' WHERE `user_id` = ? AND `item_id` = ? LIMIT 1", [$myID, $user[$slot_name]]);
            db_query("UPDATE `users` SET `$slot_name` = '0', `str` = `str` - ?, `def` = `def` - ?, `hp_max` = `hp_max` - ? WHERE `id` = ?", [(int)$item['str'], (int)$item['def'], (int)$item['hp'], $myID]);
            set_msg("Вы сняли " . out($item['name']));
        }
    }
    header("Location: equip.php"); exit;
}

$id = isset($_GET['id']) ? (int)$_GET['id'] : $myID;
$i = ($id == $myID) ? $user : db_query("SELECT * FROM `users` WHERE `id` = ? LIMIT 1", [$id])->fetch();
if (!$i) { header("Location: inv.php"); exit; }

$title = ($id != $myID) ? 'Снаряжение ' . out($i['login']) : 'Моё снаряжение';
require_once ('../system/thead.php');
show_msg();

echo '<div style="text-align:center; margin-bottom:10px;"><div class="head-new"><div class="b-new" style="display:inline-block; width:90%;">' . mb_strtoupper($title) . '</div></div></div>';

$w_name = [1 => 'Голова', 2 => 'Плечи', 3 => 'Торс', 4 => 'Перчатки', 5 => 'Левая рука', 6 => 'Правая рука', 7 => 'Ноги', 8 => 'Обувь'];
$q_color = [1 => '#999', 2 => '#08aa00', 3 => '#49acfc', 4 => '#a335ee', 5 => '#ff8000', 6 => '#fe7e01'];

for ($w = 1; $w <= 8; $w++) {
    $item_id = (int)$i['w_' . $w];
    echo '<div class="msg-new" style="margin-bottom:2px;"><div class="wr4-new" style="padding:8px; display:flex; align-items:center; gap:12px;">';
    if ($item_id > 0) {
        $item = db_query("SELECT * FROM `items` WHERE `id` = ? LIMIT 1", [$item_id])->fetch();
        $color = $q_color[$item['quality']] ?? '#fff';
        echo '<div style="width:40px; height:40px; background:#000; border:1px solid #183543;"><img src="/style/items/'.(int)$item['id'].'.png" style="width:100%"></div>
              <div style="flex:1;">
                  <div style="color:'.$color.'; font-weight:bold; font-size:13px;">'.out($item['name']).'</div>
                  <div style="color:#477193; font-size:10px;">'.(int)$item['level'].' ур. | '.($item['str']?"Сл +$item[str] ":"").($item['def']?"Зщ +$item[def] ":"").($item['hp']?"ХП +$item[hp]":"").'</div>
              </div>';
        if ($id == $myID) echo '<a href="?unequip='.$w.'" class="bbtn-new" style="width:65px; height:24px; text-decoration:none;"><span class="lbl-new" style="color:#ff4444; font-size:9px; text-align:center; padding:4px 0;">СНЯТЬ</span></a>';
    } else {
        echo '<div style="width:40px; height:40px; background:#000; border:1px solid #183543; opacity:0.4;"><img src="/style/items/0.png" style="width:100%"></div>
              <div style="flex:1; color:#477193; font-size:13px;">'.$w_name[$w].' <span style="font-size:10px; opacity:0.6;">(пусто)</span></div>';
        if ($id == $myID) echo '<a href="/user/inv.php" class="bbtn-new" style="width:65px; height:24px; text-decoration:none;"><span class="lbl-new" style="color:#fff; font-size:9px; text-align:center; padding:4px 0;">ОДЕТЬ</span></a>';
    }
    echo '</div></div>';
}

echo '<div style="margin:10px; display:flex; justify-content:center; gap:8px;">
        <a href="/user/inv.php" class="bbtn-new" style="width:140px; text-decoration:none; height:34px;"><span class="lbl-new" style="color:#fff; font-size:11px; text-align:center; padding:8px 0;">В РЮКЗАК</span></a>
        <a href="/profile.php?id='.$id.'" class="bbtn-new" style="width:140px; text-decoration:none; height:34px;"><span class="lbl-new" style="color:#fff; font-size:11px; text-align:center; padding:8px 0;">В ПРОФИЛЬ</span></a>
      </div>';

require_once ('../system/tfoot.php');
?>