Модуль:Prototypes/Объект/Торгомат: различия между версиями

Материал из Space Station 14 Вики
мНет описания правки
мНет описания правки
 
(не показано 46 промежуточных версий этого же участника)
Строка 1: Строка 1:
-- Загрузка данных
local vendingMachinesData = mw.loadData("Модуль:IanComradeBot/prototypes/vending machines.json/data")
local restockData = mw.loadData("Модуль:IanComradeBot/prototypes/vending machines/restock.json/data")
local inventoriesData = mw.loadData("Модуль:IanComradeBot/prototypes/vending machines/inventories.json/data")
local p = {}
local p = {}


-- Загрузка данных
-- Поиск данных по ID
local function loadData(filePath)
local function findDataById(data, id)
    local page = mw.title.new(filePath)
if not data then return nil end
    local content = page:getContent()
for _, item in ipairs(data) do
    return content and mw.text.jsonDecode(content) or nil
if item.id == id then
return item
end
end
return nil
end
end


-- Поиск данных по ID
-- Формирование вывода инвентаря
local function findDataById(data, id)
local function formatInventoryOutput(inventoryData)
    if not data then return nil end
local result = ""
    for _, item in ipairs(data) do
for itemId, count in pairs(inventoryData) do
        if item.id == id then
result = result .. mw.getCurrentFrame():preprocess("{{#invoke:Предмет|main|" .. itemId .. "|[" .. count .. "]|repository=|wrapper=}}")
            return item
end
        end
return result ~= "" and result or "Инвентарь пуст."
    end
    return nil
end
end


-- Получение инвентаря автомата
-- Получение инвентаря автомата
local function getInventoryOutput(id)
local function getInventoryOutput(id, mode)
    -- Загрузка данных
local vendingMachine = findDataById(vendingMachinesData, id)
    local vendingMachines = loadData("User:IanComradeBot/prototypes/vending machines.json")
if not vendingMachine then return "Автомат с таким ID не найден." end
    local inventories = loadData("User:IanComradeBot/prototypes/vending machines/inventories.json")


    -- Поиск автомата по ID
local inventoryId = vendingMachine.VendingMachine and vendingMachine.VendingMachine.pack
    local vendingMachine = findDataById(vendingMachines, id)
if not inventoryId then return "Инвентарь не найден." end
    if not vendingMachine then return "" end


    -- Получение инвентаря автомата
local inventory = findDataById(inventoriesData, inventoryId)
    local inventoryId = vendingMachine.pack
if not inventory then return "Данные об инвентаре отсутствуют." end
    local inventory = findDataById(inventories, inventoryId)
    if not inventory or not inventory.startingInventory then return "" end


    -- Формирование вывода
if mode == "inventory" and inventory.startingInventory then
    local result = "== Содержимое автомата ==\n"
return formatInventoryOutput(inventory.startingInventory)
    for itemId, count in pairs(inventory.startingInventory) do
elseif mode == "contraband" and inventory.contrabandInventory then
        result = result .. "* " .. itemId .. " " .. count .. "\n"
return formatInventoryOutput(inventory.contrabandInventory)
    end
elseif mode == "emag" and inventory.emaggedInventory then
return formatInventoryOutput(inventory.emaggedInventory)
end


    return result
return "Не имеет дополнительного ассортимента."
end
end


-- Получение информации о пополнителе автомата
-- Получение информации о пополнителе автомата
local function getRestockOutput(id)
local function getRestockOutput(id)
    -- Загрузка данных
local vendingMachine = findDataById(vendingMachinesData, id)
    local vendingMachines = loadData("User:IanComradeBot/prototypes/vending machines.json")
if not vendingMachine then return "" end
    local restockData = loadData("User:IanComradeBot/prototypes/vending machines/restock.json")


    -- Поиск автомата по ID
local packId = vendingMachine.VendingMachine and vendingMachine.VendingMachine.pack
    local vendingMachine = findDataById(vendingMachines, id)
    if not vendingMachine then return "" end


    -- Получение ID пополнителя
for _, restock in ipairs(restockData) do
    local packId = vendingMachine.pack
    local restockInfo = restock.VendingMachineRestock
    for _, restock in ipairs(restockData) do
    if restockInfo and restockInfo.canRestock then
        if restock.canRestock and table.concat(restock.canRestock):find(packId) then
        for _, restockPack in ipairs(restockInfo.canRestock) do
            return "== Пополнитель ==\n* ID пополнителя: " .. restock.id
            if restockPack == packId then
        end
                return mw.getCurrentFrame():preprocess("{{Предмет|" .. restock.id .. "|size=64px|link=|repository=|wrapper=}}")
    end
            end
        end
    end
end


    return ""
return "Не имеет пополнителя."
end
 
-- Получение данных о доступах
local function getAccessOutput(id)
    -- Загрузка данных
    local vendingMachines = loadData("User:IanComradeBot/prototypes/vending machines.json")
   
    -- Поиск автомата по ID
    local vendingMachine = findDataById(vendingMachines, id)
    if not vendingMachine or not vendingMachine.AccessReader then
        return "Нет данных о доступах"
    end
   
    -- Получение строки с доступами
    local access = vendingMachine.AccessReader.access
    if not access then
        return "Нет данных о доступах"
    end
   
    -- Формирование строки для шаблона
    local templateCall = "{{#invoke:Prototypes/Механика/Доступ|parse|" .. access .. "}}"
   
    local result = mw.getCurrentFrame():preprocess(templateCall)
   
    return result
end
end


-- Основная функция модуля
-- Основная функция модуля
function p.main(frame)
function p.main(frame)
    local id = frame.args.id
local mode = frame.args[1]
    if not id then return "" end
local id = frame.args[2]
 
if not id then return "" end


    -- Определение действия на основе ID
if mode == "inventory" or mode == "contraband" or mode == "emag" then
    if frame.args.mode == "inventory" then
return getInventoryOutput(id, mode)
        return getInventoryOutput(id)
elseif mode == "restock" then
    elseif frame.args.mode == "restock" then
return getRestockOutput(id)
        return getRestockOutput(id)
else
    elseif frame.args.mode == "access" then
return ""
        return getAccessOutput(id)
end
    else
        return ""
    end
end
end


return p
return p

Текущая версия от 10:38, 4 апреля 2025

Для документации этого модуля может быть создана страница Модуль:Prototypes/Объект/Торгомат/doc

-- Загрузка данных
local vendingMachinesData = mw.loadData("Модуль:IanComradeBot/prototypes/vending machines.json/data")
local restockData = mw.loadData("Модуль:IanComradeBot/prototypes/vending machines/restock.json/data")
local inventoriesData = mw.loadData("Модуль:IanComradeBot/prototypes/vending machines/inventories.json/data")

local p = {}

-- Поиск данных по ID
local function findDataById(data, id)
	if not data then return nil end
	for _, item in ipairs(data) do
		if item.id == id then
			return item
		end
	end
	return nil
end

-- Формирование вывода инвентаря
local function formatInventoryOutput(inventoryData)
	local result = ""
	for itemId, count in pairs(inventoryData) do
		result = result .. mw.getCurrentFrame():preprocess("{{#invoke:Предмет|main|" .. itemId .. "|[" .. count .. "]|repository=|wrapper=}}")
	end
	return result ~= "" and result or "Инвентарь пуст."
end

-- Получение инвентаря автомата
local function getInventoryOutput(id, mode)
	local vendingMachine = findDataById(vendingMachinesData, id)
	if not vendingMachine then return "Автомат с таким ID не найден." end

	local inventoryId = vendingMachine.VendingMachine and vendingMachine.VendingMachine.pack
	if not inventoryId then return "Инвентарь не найден." end

	local inventory = findDataById(inventoriesData, inventoryId)
	if not inventory then return "Данные об инвентаре отсутствуют." end

	if mode == "inventory" and inventory.startingInventory then
		return formatInventoryOutput(inventory.startingInventory)
	elseif mode == "contraband" and inventory.contrabandInventory then
		return formatInventoryOutput(inventory.contrabandInventory)
	elseif mode == "emag" and inventory.emaggedInventory then
		return formatInventoryOutput(inventory.emaggedInventory)
	end

	return "Не имеет дополнительного ассортимента."
end

-- Получение информации о пополнителе автомата
local function getRestockOutput(id)
	local vendingMachine = findDataById(vendingMachinesData, id)
	if not vendingMachine then return "" end

	local packId = vendingMachine.VendingMachine and vendingMachine.VendingMachine.pack

	for _, restock in ipairs(restockData) do
	    local restockInfo = restock.VendingMachineRestock
	    if restockInfo and restockInfo.canRestock then
	        for _, restockPack in ipairs(restockInfo.canRestock) do
	            if restockPack == packId then
	                return mw.getCurrentFrame():preprocess("{{Предмет|" .. restock.id .. "|size=64px|link=|repository=|wrapper=}}")
	            end
	        end
	    end
	end

	return "Не имеет пополнителя."
end

-- Основная функция модуля
function p.main(frame)
	local mode = frame.args[1]
	local id = frame.args[2]

	if not id then return "" end

	if mode == "inventory" or mode == "contraband" or mode == "emag" then
		return getInventoryOutput(id, mode)
	elseif mode == "restock" then
		return getRestockOutput(id)
	else
		return ""
	end
end

return p