Модуль:Prototypes/Хранилище/Предмет

Материал из Space Station 14 Вики
Версия от 19:11, 26 января 2025; 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 ite...»)

Для документации этого модуля может быть создана страница Модуль: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 getChemOutput(data, id)
    local item = findDataById(data, id)
    if not item then return "Предмет с таким ID не найден." end

    local solutions = item.SolutionContainerManager and item.SolutionContainerManager.solutions
    if not solutions then return "Веществ не найдено." end

    local result = "<ul>"
    for _, solution in pairs(solutions) do
        for _, reagent in ipairs(solution.reagents) do
            result = result .. string.format("<li>%s (%s) (%d ед.)</li>", reagent.ReagentId, reagent.ReagentId, reagent.Quantity)
        end
    end
    result = result .. "</ul>"
    return result
end

-- Формирование списка содержащихся предметов
local function getContainedOutput(data, id, wrapped)
    local item = findDataById(data, id)
    if not item then return "Предмет с таким ID не найден." end

    local contents = item.StorageFill and item.StorageFill.contents
    if not contents then return "Содержимого не найдено." end

    local result = wrapped and "<div>" or ""
    for _, content in ipairs(contents) do
        if wrapped then
            result = result .. string.format("<div>%s</div>", content.id)
        else
            result = result .. string.format("%s\n", content.id)
        end
    end
    if wrapped then
        result = result .. "</div>"
    end
    return result
end

-- Главная функция модуля
function p.main(frame)
    local mode = frame.args[1]
    local id = frame.args[2]

    if not id then return "Не указан ID." end

    local data = loadData("User:IanComradeBot/prototypes/fills/Item.json")
    if not data then return "Не удалось загрузить данные." end

    if mode == "chem" then
        return getChemOutput(data, id)
    elseif mode == "contained" then
        return getContainedOutput(data, id, false)
    elseif mode == "framing" then
        return getContainedOutput(data, id, true)
    else
        return "Неизвестный режим: " .. mode
    end
end

return p