Модуль:Prototypes/Хранилище/Предмет: различия между версиями

Материал из Space Station 14 Вики
Нет описания правки
мНет описания правки
Строка 22: Строка 22:
local function getChemOutput(data, id)
local function getChemOutput(data, id)
     local item = findDataById(data, id)
     local item = findDataById(data, id)
     if not item then return "Предмет с таким ID не найден." end
     if not item then return '' end


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


     local result = "<ul>"
     local result = '<ul class="1">'
     for _, solution in pairs(solutions) do
     for _, solution in pairs(solutions) do
         for _, reagent in ipairs(solution.reagents) do
         for _, reagent in ipairs(solution.reagents) do
             result = result .. string.format("<li>%s (%s) (%d ед.)</li>", reagent.ReagentId, reagent.ReagentId, reagent.Quantity)
             result = result .. string.format('<li>[[Химия#chem_%s|%s] (%d ед.)</li>', reagent.ReagentId, reagent.ReagentId, reagent.Quantity)
         end
         end
     end
     end
     result = result .. "</ul>"
     result = result .. '</ul>'
     return result
     return result
end
end
Строка 40: Строка 40:
local function getContainedOutput(data, id, wrapped)
local function getContainedOutput(data, id, wrapped)
     local item = findDataById(data, id)
     local item = findDataById(data, id)
     if not item then return "Предмет с таким ID не найден." end
     if not item then return '' end


     local contents = item.StorageFill and item.StorageFill.contents
     local contents = item.StorageFill and item.StorageFill.contents
     local tableContainer = item.EntityTableContainerFill and item.EntityTableContainerFill.containers
     local tableContainer = item.EntityTableContainerFill and item.EntityTableContainerFill.containers


     local result = wrapped and "<div>" or ""
     local result = wrapped and '<div>' or ''


     if contents then
     if contents then
         for _, content in ipairs(contents) do
         for _, content in ipairs(contents) do
             if wrapped then
             if wrapped then
                 result = result .. string.format("<div>%s</div>", content.id)
                 result = result .. string.format('<div class="1 2">{{LinkСard|SideStyle=1|background-color=#d7d7ff0b|image=%s.png|name={{#invoke:Entity Lookup|getname|%s}} }}</div>', content.id, content.id)
             else
             else
                 result = result .. string.format("%s\n", content.id)
                 result = result .. string.format('{{LinkСard|SideStyle=1|background-color=#d7d7ff0b|image=%s.png|name={{#invoke:Entity Lookup|getname|%s}}', content.id, content.id)
             end
             end
         end
         end
Строка 58: Строка 58:
         local tableId = tableContainer.storagebase and tableContainer.storagebase.tableId
         local tableId = tableContainer.storagebase and tableContainer.storagebase.tableId
         if tableId then
         if tableId then
             local allSelectors = loadData("User:IanComradeBot/prototypes/AllSelector.json")
             local allSelectors = loadData('User:IanComradeBot/prototypes/AllSelector.json')
             local tableData = findDataById(allSelectors, tableId)
             local tableData = findDataById(allSelectors, tableId)
             local children = tableData and tableData["!type:AllSelector"] and tableData["!type:AllSelector"].children
             local children = tableData and tableData['!type:AllSelector'] and tableData['!type:AllSelector'].children


             if children then
             if children then
                 for _, child in ipairs(children) do
                 for _, child in ipairs(children) do
                     if wrapped then
                     if wrapped then
                         result = result .. string.format("<div>%s</div>", child.id)
                         result = result .. string.format('<div>%s</div>', child.id)
                     else
                     else
                         result = result .. string.format("%s\n", child.id)
                         result = result .. string.format('%s\n', child.id)
                     end
                     end
                 end
                 end
             else
             else
                 result = result .. "Таблица не содержит элементов."
                 result = result .. 'Таблица не содержит элементов.'
             end
             end
         else
         else
             result = result .. "Таблица не найдена."
             result = result .. 'Таблица не найдена.'
         end
         end
     else
     else
         result = result .. "Содержимого не найдено."
         result = result .. 'Содержимого не найдено.'
     end
     end


     if wrapped then
     if wrapped then
         result = result .. "</div>"
         result = result .. '</div>'
     end
     end
     return result
     return result
Строка 91: Строка 91:
     local id = frame.args[2]
     local id = frame.args[2]


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


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


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


return p
return p

Версия от 19:32, 26 января 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 getChemOutput(data, id)
    local item = findDataById(data, id)
    if not item then return '' end

    local solutions = item.SolutionContainerManager and item.SolutionContainerManager.solutions
    if not solutions then return '' end

    local result = '<ul class="1">'
    for _, solution in pairs(solutions) do
        for _, reagent in ipairs(solution.reagents) do
            result = result .. string.format('<li>[[Химия#chem_%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 '' end

    local contents = item.StorageFill and item.StorageFill.contents
    local tableContainer = item.EntityTableContainerFill and item.EntityTableContainerFill.containers

    local result = wrapped and '<div>' or ''

    if contents then
        for _, content in ipairs(contents) do
            if wrapped then
                result = result .. string.format('<div class="1 2">{{LinkСard|SideStyle=1|background-color=#d7d7ff0b|image=%s.png|name={{#invoke:Entity Lookup|getname|%s}} }}</div>', content.id, content.id)
            else
                result = result .. string.format('{{LinkСard|SideStyle=1|background-color=#d7d7ff0b|image=%s.png|name={{#invoke:Entity Lookup|getname|%s}}', content.id, content.id)
            end
        end
    elseif tableContainer then
        local tableId = tableContainer.storagebase and tableContainer.storagebase.tableId
        if tableId then
            local allSelectors = loadData('User:IanComradeBot/prototypes/AllSelector.json')
            local tableData = findDataById(allSelectors, tableId)
            local children = tableData and tableData['!type:AllSelector'] and tableData['!type:AllSelector'].children

            if children then
                for _, child in ipairs(children) do
                    if wrapped then
                        result = result .. string.format('<div>%s</div>', child.id)
                    else
                        result = result .. string.format('%s\n', child.id)
                    end
                end
            else
                result = result .. 'Таблица не содержит элементов.'
            end
        else
            result = result .. 'Таблица не найдена.'
        end
    else
        result = result .. 'Содержимого не найдено.'
    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