Модуль:Prototypes/Объект/Торгомат
Материал из Space Station 14 Вики
Для документации этого модуля может быть создана страница Модуль:Prototypes/Объект/Торгомат/doc
local p = {}
-- Функция для загрузки данных из JSON-файла
local function loadData(filePath)
local page = mw.title.new(filePath)
local content = page:getContent()
return content and mw.text.jsonDecode(content) or nil
end
-- Функция для поиска данных по 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
-- Основная функция модуля
function p.main(frame)
-- Получение ID автомата
local vendingId = frame.args.id or ""
if vendingId == "" then
return "Ошибка: ID автомата не указан."
end
-- Загрузка данных
local vendingMachines = loadData("User:IanComradeBot/prototypes/vending machines.json")
local inventories = loadData("User:IanComradeBot/prototypes/vending machines/inventories.json")
local restockData = loadData("User:IanComradeBot/prototypes/vending machines/restock.json")
-- Поиск автомата по ID
local vendingMachine = findDataById(vendingMachines, vendingId)
if not vendingMachine then
return "Ошибка: автомат с ID \"" .. vendingId .. "\" не найден."
end
-- Получение инвентаря автомата
local inventoryId = vendingMachine.pack
local inventory = findDataById(inventories, inventoryId)
if not inventory or not inventory.startingInventory then
return "Ошибка: инвентарь для автомата \"" .. vendingId .. "\" не найден."
end
-- Формирование вывода содержимого автомата
local result = "== Содержимое автомата ==\n"
for itemId, count in pairs(inventory.startingInventory) do
result = result .. "* " .. itemId .. " — " .. count .. "\n"
end
-- Поиск пополнителя для автомата
local restockId
for _, restock in ipairs(restockData) do
if restock.canRestock then
for _, packId in ipairs(restock.canRestock) do
if packId == inventoryId then
restockId = restock.id
break
end
end
end
if restockId then break end
end
if restockId then
result = result .. "\n== Пополнитель автомата ==\n"
result = result .. "* ID пополнителя: " .. restockId .. "\n"
else
result = result .. "\n== Пополнитель автомата ==\n"
result = result .. "* Пополнитель не найден.\n"
end
return result
end
return p