Files
VnUtest/garrysmod/gamemodes/militaryrp/plugins/shop/sv_plugin.lua
2026-03-31 10:27:04 +03:00

171 lines
4.8 KiB
Lua
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
local PLUGIN = PLUGIN
util.AddNetworkString("ixShop_OpenMenu")
util.AddNetworkString("ixShop_BuyItem")
util.AddNetworkString("ixShop_UpdateModel")
util.AddNetworkString("ixShop_UpdateType")
ix.util.Include("sh_config.lua")
function PLUGIN:SaveData()
local data = {}
for _, npc in ipairs(ents.FindByClass("ix_shop_npc")) do
if IsValid(npc) then
data[#data + 1] = {
pos = npc:GetPos(),
angles = npc:GetAngles(),
model = npc:GetModel(),
shopType = npc:GetShopType()
}
end
end
self:SetData(data)
end
function PLUGIN:LoadData()
for _, v in ipairs(self:GetData() or {}) do
local npc = ents.Create("ix_shop_npc")
if IsValid(npc) then
npc:SetPos(v.pos)
npc:SetAngles(v.angles)
npc:SetModel(v.model or "models/humans/group01/male_01.mdl")
npc:Spawn()
timer.Simple(0.1, function()
if IsValid(npc) then
npc:SetShopType(v.shopType or "general")
end
end)
end
end
end
function PLUGIN:OnUnloaded()
self:SaveData()
end
-- Обработка изменения типа магазина
net.Receive("ixShop_UpdateType", function(len, client)
if not client:IsAdmin() then return end
local npc = net.ReadEntity()
local shopType = net.ReadString()
if not IsValid(npc) or npc:GetClass() != "ix_shop_npc" then return end
if client:GetPos():Distance(npc:GetPos()) > 300 then return end
npc:SetShopType(shopType)
PLUGIN:SaveData()
client:Notify("Тип магазина изменен на: " .. shopType)
end)
-- Обработка покупки предмета
net.Receive("ixShop_BuyItem", function(len, client)
local shopType = net.ReadString()
local itemID = net.ReadString()
local shopData = PLUGIN.shops[shopType]
if not shopData or not shopData.items[itemID] then
client:Notify("Предмет не найден!")
return
end
local itemData = shopData.items[itemID]
local character = client:GetCharacter()
if not character then return end
-- Проверка денег
if not character:HasMoney(itemData.price) then
client:Notify("Недостаточно денег! Требуется: " .. itemData.price .. "")
return
end
-- Проверка наличия предмета в базе ix.item
local ixItem = ix.item.list[itemID]
if not ixItem then
client:Notify("Предмет не существует в базе данных!")
return
end
-- Попытка дать предмет
if shopType == "drones" then
local weaponClass = ixItem.class or itemID
-- Проверка, нет ли уже такого дрона у игрока
if client:HasWeapon(weaponClass) then
client:Notify("У вас уже есть этот дрон!")
return
end
-- Если это магазин дронов, даем оружие напрямую (SWEP)
client:Give(weaponClass)
client:SelectWeapon(weaponClass)
else
-- Для остальных магазинов добавляем в инвентарь
local inventory = character:GetInventory()
if not inventory or isnumber(inventory) then
client:Notify("Инвентарь еще загружается, подождите немного...")
return
end
if not inventory:Add(itemID) then
client:Notify("Недостаточно места в инвентаре!")
return
end
end
-- Списание денег
character:TakeMoney(itemData.price)
client:Notify("Вы купили " .. (ixItem.name or itemID) .. " за " .. itemData.price .. "")
-- Логирование покупки
local serverlogsPlugin = ix.plugin.list["serverlogs"]
if serverlogsPlugin then
local message = string.format("%s купил %s за %d₽ (магазин: %s)",
client:GetName(), (ixItem.name or itemID), itemData.price, shopData.name)
serverlogsPlugin:AddLog("SHOP_PURCHASE", message, client, {
itemID = itemID,
itemName = (ixItem.name or itemID),
price = itemData.price,
shopType = shopType
})
end
end)
-- Обработка изменения модели NPC
net.Receive("ixShop_UpdateModel", function(len, client)
if not client:IsAdmin() then return end
local npc = net.ReadEntity()
local model = net.ReadString()
if not IsValid(npc) or npc:GetClass() != "ix_shop_npc" then return end
if client:GetPos():Distance(npc:GetPos()) > 300 then return end
npc:SetModel(model)
PLUGIN:SaveData()
client:Notify("Модель NPC изменена на: " .. model)
end)
-- Команда для сохранения магазинов
ix.command.Add("ShopSave", {
description = "Сохранить все магазины",
superAdminOnly = true,
OnRun = function(self, client)
local plugin = ix.plugin.Get("shop")
if not plugin then
return "@commandNoExist"
end
plugin:SaveData()
local count = #ents.FindByClass("ix_shop_npc")
client:Notify("Магазины сохранены (" .. count .. " шт.)")
end
})