Модуль:Prototypes/Объект/Торгомат/Пополнение: различия между версиями
Материал из Space Station 14 Вики
Pok (обсуждение | вклад) Новая страница: «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 --...» |
Pok (обсуждение | вклад) мНет описания правки |
||
| Строка 26: | Строка 26: | ||
local result = "" | local result = "" | ||
-- | -- Находим пополнителя по ID | ||
local restockItem = findDataById(restockData, id) | |||
if | if not restockItem then | ||
return "Пополнитель не найден" | |||
end | |||
-- Для каждого объекта в canRestock ищем соответствующие автоматы | |||
if restockItem.canRestock then | |||
for _, canRestockId in ipairs(restockItem.canRestock) do | |||
-- Ищем все автоматы, где pack соответствует значению canRestock | |||
for _, vendingMachine in ipairs(vendingMachines) do | |||
if vendingMachine.VendingMachine and vendingMachine.VendingMachine.pack == canRestockId then | |||
-- Добавляем ID найденного автомата в результат | |||
result = result .. mw.getCurrentFrame():preprocess("{{LinkСard|image=" .. vendingMachine.id .. "|name={{#invoke:Entity Lookup|getname|" .. vendingMachine.id .. "}}|link=Торговые автоматы#{{#invoke:Entity Lookup|getname|" .. vendingMachine.id .. "}}}}") | |||
end | end | ||
end | end | ||
Версия от 22:22, 16 января 2025
Для документации этого модуля может быть создана страница Модуль: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 getRestockOutput(id)
local vendingMachines = loadData("User:IanComradeBot/prototypes/vending machines.json")
local restockData = loadData("User:IanComradeBot/prototypes/vending machines/restock.json")
local result = ""
-- Находим пополнителя по ID
local restockItem = findDataById(restockData, id)
if not restockItem then
return "Пополнитель не найден"
end
-- Для каждого объекта в canRestock ищем соответствующие автоматы
if restockItem.canRestock then
for _, canRestockId in ipairs(restockItem.canRestock) do
-- Ищем все автоматы, где pack соответствует значению canRestock
for _, vendingMachine in ipairs(vendingMachines) do
if vendingMachine.VendingMachine and vendingMachine.VendingMachine.pack == canRestockId then
-- Добавляем ID найденного автомата в результат
result = result .. mw.getCurrentFrame():preprocess("{{LinkСard|image=" .. vendingMachine.id .. "|name={{#invoke:Entity Lookup|getname|" .. vendingMachine.id .. "}}|link=Торговые автоматы#{{#invoke:Entity Lookup|getname|" .. vendingMachine.id .. "}}}}")
end
end
end
end
return result
end
-- Основная функция модуля
function p.main(frame)
local id = frame.args.id
if not id then return "" end
return getRestockOutput(id)
end
return p