Модуль:Prototypes/Объект/Торгомат
Материал из Space Station 14 Вики
Для документации этого модуля может быть создана страница Модуль:Prototypes/Объект/Торгомат/doc
local p = {}
-- Загрузка данных
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
-- Получение инвентаря автомата
local function getInventoryOutput(id)
-- Загрузка данных
local vendingMachines = loadData("User:IanComradeBot/prototypes/vending machines.json")
local inventories = loadData("User:IanComradeBot/prototypes/vending machines/inventories.json")
-- Поиск автомата по ID
local vendingMachine = findDataById(vendingMachines, id)
if not vendingMachine then return "" end
-- Получение инвентаря автомата
local inventoryId = vendingMachine.pack
local inventory = findDataById(inventories, inventoryId)
if not inventory or not inventory.startingInventory then return "" end
-- Формирование вывода содержимого автомата
local result = "== Содержимое автомата ==\n"
for itemId, count in pairs(inventory.startingInventory) do
result = result .. "* " .. itemId .. " — " .. count .. "\n"
end
return result
end
-- Получение информации о пополнителе автомата
local function getRestockOutput(id)
-- Загрузка данных
local vendingMachines = loadData("User:IanComradeBot/prototypes/vending machines.json")
local restockData = loadData("User:IanComradeBot/prototypes/vending machines/restock.json")
-- Поиск автомата по ID
local vendingMachine = findDataById(vendingMachines, id)
if not vendingMachine then return "" end
-- Получение ID пополнителя
local packId = vendingMachine.pack
for _, restock in ipairs(restockData) do
if restock.canRestock and table.concat(restock.canRestock):find(packId) then
return "== Пополнитель ==\n* ID пополнителя: " .. restock.id
end
end
return ""
end
-- Получение данных о доступах
local function getAccessOutput(id)
-- Загрузка данных
local vendingMachines = loadData("User:IanComradeBot/prototypes/vending machines.json")
-- Поиск автомата по ID
local vendingMachine = findDataById(vendingMachines, id)
if not vendingMachine then return "" end
-- Получение данных доступа
local accessData = vendingMachine.AccessReader and vendingMachine.AccessReader.access
if not accessData then return "" end
-- Преобразуем данные в формат, подходящий для вызова "Модуль:Prototypes/Механика/Доступ"
local jsonAccessData = mw.text.jsonEncode(accessData)
-- Формируем вывод с использованием "Модуль:Prototypes/Механика/Доступ"
return '{{#invoke:Prototypes/Механика/Доступ|parse|' .. jsonAccessData .. '}}'
end
-- Основная функция модуля
function p.main(frame)
local id = frame.args.id
if not id then return "" end
-- Определение действия на основе ID
if frame.args.mode == "inventory" then
return getInventoryOutput(id)
elseif frame.args.mode == "restock" then
return getRestockOutput(id)
elseif frame.args.mode == "access" then
return getAccessOutput(id)
else
return ""
end
end
return p