Загрузка данных


-- Shop_kdm1eHvH.lua
-- Логика магазина

-- Сетевое сообщение: клиент просит купить
net.Receive("kdm1e_ShopBuy", function(len, ply)
    if not IsValid(ply) then return end

    local itemId = net.ReadString()

    -- Ищем товар
    local item = nil
    for _, it in ipairs(SHOP_ITEMS) do
        if it.id == itemId then
            item = it
            break
        end
    end

    if not item then
        ply:PrintMessage(HUD_PRINTTALK, "Товар не найден!")
        return
    end

    -- Проверяем токены
    local tokens = GetTokens(ply)
    if tokens < item.price then
        ply:PrintMessage(HUD_PRINTTALK, "Недостаточно токенов! Нужно: " .. item.price)
        return
    end

    -- Списываем
    RemoveTokens(ply, item.price)

    -- Выдаём
    if item.type == "weapon" then
        ply:Give(item.id)
        ply:PrintMessage(HUD_PRINTTALK, "Ты купил: " .. item.name)
    elseif item.type == "health" then
        ply:SetHealth(math.min(ply:Health() + (item.amount or 50), ply:GetMaxHealth()))
        ply:PrintMessage(HUD_PRINTTALK, "Ты купил: " .. item.name)
    elseif item.type == "armor" then
        ply:SetArmor(math.min(ply:Armor() + (item.amount or 50), 100))
        ply:PrintMessage(HUD_PRINTTALK, "Ты купил: " .. item.name)
    end
end)

-- Сетевое сообщение: клиент просит список товаров
net.Receive("kdm1e_ShopRequest", function(len, ply)
    if not IsValid(ply) then return end

    net.Start("kdm1e_ShopSend")
    net.WriteUInt(#SHOP_ITEMS, 16)
    for _, item in ipairs(SHOP_ITEMS) do
        net.WriteString(item.id)
        net.WriteString(item.name)
        net.WriteUInt(item.price, 32)
        net.WriteString(item.category)
        net.WriteString(item.type)
    end
    net.Send(ply)
end)

print("[Shop] Логика магазина загружена")