Модуль: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
-- Функция для получения списка содержимого автомата
local function getInventory(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
-- Поиск инвентаря по pack
local inventory = findDataById(inventories, vendingMachine.pack)
if not inventory then return "" end
-- Формирование строки с содержимым автомата
local inventoryString = ""
for item, quantity in pairs(inventory.startingInventory) do
inventoryString = inventoryString .. "* " .. item .. ": " .. quantity .. "\n"
end
return inventoryString
end
-- Функция для получения списка доступов
local function getAccessList(id)
-- Загрузка данных
local vendingMachines = loadData("User:IanComradeBot/prototypes/vending machines.json")
-- Поиск автомата по ID
local vendingMachine = findDataById(vendingMachines, id)
if not vendingMachine or not vendingMachine.AccessReader or not vendingMachine.AccessReader.access then
return ""
end
-- Извлечение доступа
local accessList = mw.text.jsonDecode(vendingMachine.AccessReader.access)
if not accessList then return "" end
-- Формирование строки для передачи в модуль Prototypes/Механика/Доступ
local accessString = ""
for _, access in ipairs(accessList) do
accessString = accessString .. "[[\"" .. access .. "\"]],"
end
return '{{#invoke:Prototypes/Механика/Доступ|parse|[' .. accessString .. ']}}'
end
-- Функция для получения ID пополнителя
local function getRestockId(id)
-- Загрузка данных
local vendingMachines = loadData("User:IanComradeBot/prototypes/vending machines.json")
local restocks = loadData("User:IanComradeBot/prototypes/vending machines/restock.json")
-- Поиск автомата по ID
local vendingMachine = findDataById(vendingMachines, id)
if not vendingMachine then return "" end
-- Поиск пополнителя по pack
local restock = findDataById(restocks, vendingMachine.pack)
if not restock then return "" end
-- Формирование строки с ID пополнителя
local restockString = restock.id or ""
return restockString
end
-- Основная функция модуля
function p.main(frame)
local id = frame.args.id
local mode = frame.args.mode or "" -- Определяем, что за режим: 'access', 'inventory' или 'restock'
if not id then
return ""
end
-- В зависимости от значения mode выбираем нужную функцию
if mode == "access" then
return getAccessList(id)
elseif mode == "inventory" then
return getInventory(id)
elseif mode == "restock" then
return getRestockId(id)
else
return ""
end
end
return p