Модуль:Prototypes/Объект/Торгомат

Материал из Space Station 14 Вики

Для документации этого модуля может быть создана страница Модуль: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 getInventoryOutput(id, mode)
	-- Поиск автомата по ID
	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

	-- Формирование вывода
	local result = ""

	-- Основной инвентарь
	if mode == "inventory" and inventory.startingInventory then
		for itemId, count in pairs(inventory.startingInventory) do
			result = result .. mw.getCurrentFrame():preprocess("{{LinkСard|SideStyle=1|background-color=#cbcbff0a|image=" .. itemId .. ".png|name={{#invoke:Entity Lookup|getname|" .. itemId .. "}} [" .. count .. "] {{#invoke:Prototypes/Хранилище/Предмет|main|framing|contained|" .. itemId .. "}} }}")
		end
		if result == "" then
			return "Основной инвентарь пуст."
		end

	-- Содержания после провода
	elseif mode == "contraband" then
		if inventory.contrabandInventory then
			for itemId, count in pairs(inventory.contrabandInventory) do
				result = result .. mw.getCurrentFrame():preprocess("{{LinkСard|SideStyle=1|background-color=#cbcbff0a|image=" .. itemId .. ".png|name={{#invoke:Entity Lookup|getname|" .. itemId .. "}} [" .. count .. "] {{#invoke:Prototypes/Хранилище/Предмет|main|framing|contained|" .. itemId .. "}} }}")
			end
		end
		if result == "" then
			return "Не имеет дополнительного ассортимента."
		end

	-- Содержания после EMAG
	elseif mode == "emag" then
		if inventory.emaggedInventory then
			for itemId, count in pairs(inventory.emaggedInventory) do
				result = result .. mw.getCurrentFrame():preprocess("{{LinkСard|SideStyle=1|background-color=#cbcbff0a|image=" .. itemId .. ".png|name={{#invoke:Entity Lookup|getname|" .. itemId .. "}} [" .. count .. "] {{#invoke:Prototypes/Хранилище/Предмет|main|framing|contained|" .. itemId .. "}} }}")
			end
		end
		if result == "" then
			return "Не имеет дополнительного ассортимента."
		end
	end

	return result
end

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

	-- Получение ID пополнителя
	local packId = vendingMachine.VendingMachine and vendingMachine.VendingMachine.pack

	for _, restock in ipairs(restockData) do
	    if restock.canRestock then
	        for _, restockPack in ipairs(restock.canRestock) do
	            if restockPack == packId then
	                return mw.getCurrentFrame():preprocess("{{LinkСard|background-color=#cbcbff0a|image=" .. restock.id .. ".png|name={{#invoke:Entity Lookup|getname|" .. restock.id .. "}}|link=Торговые автоматы#{{#invoke:Entity Lookup|getname|" .. restock.id .. "}}}}")
	            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

	-- Определение действия на основе ID и выбранного режима
	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