Модуль:Prototypes/Роль/Лодаут: различия между версиями

Материал из Space Station 14 Вики
мНет описания правки
мНет описания правки
 
(не показаны 22 промежуточные версии этого же участника)
Строка 1: Строка 1:
-- Загрузка данных
local gearRoleLoadout  = mw.loadData("Модуль:IanComradeBot/roleLoadout.json/data")
local gearloadoutGroup = mw.loadData("Модуль:IanComradeBot/loadoutGroup.json/data")
local gearloadout      = mw.loadData("Модуль:IanComradeBot/loadout.json/data")
local p = {}
local p = {}


local gearRoleLoadout   = mw.loadData("Модуль:IanComradeBot/roleLoadout.json/data")
-- Построение кэш-таблиц для быстрого доступа по id
local gearloadoutGroup  = mw.loadData("Модуль:IanComradeBot/loadoutGroup.json/data")
local rolesById = {}
local gearloadout      = mw.loadData("Модуль:IanComradeBot/loadout.json/data")
for _, role in ipairs(gearRoleLoadout) do
    rolesById[role.id] = role
end
 
local groupsById = {}
for _, group in ipairs(gearloadoutGroup) do
    groupsById[group.id] = group
end
 
local loadoutsById = {}
for _, loadout in ipairs(gearloadout) do
    loadoutsById[loadout.id] = loadout
end
 
-- Функция для форматирования дополнительной информации (время требования)
local function formatExtraInfo(info)
    return (info ~= "" and '<div class="роль-лодаут__time">{{AltTooltip|[[Файл:Small_watch.png|link=]]|' .. info .. '}}</div>') or ""
end
 
-- Функция для получения дополнительной информации по эффектам лодаута
local function getExtraInfo(loadout)
    local extraInfo = ""
    if loadout.effects then
        for _, effect in ipairs(loadout.effects) do
            if effect["!type"] == "GroupLoadoutEffect" then
                local proto = effect.proto
                if proto then
                    local candidate = loadoutsById[proto]
                    if candidate and candidate.effects then
                        for _, eff in ipairs(candidate.effects) do
                            if eff["!type"] == "JobRequirementLoadoutEffect" and eff.requirement then
                                local req = eff.requirement
                                if req["!type"] == "DepartmentTimeRequirement" and req.department and req.time then
                                    extraInfo = extraInfo .. "<b>{{ucfirst:{{#invoke:Ftl|main|translation|department-" .. req.department .. "}}}}</b>: {{#invoke:Code/Формат/Время|main|seconds|" .. req.time .. "}}<br>"
                                elseif req["!type"] == "RoleTimeRequirement" and req.role and req.time then
                                    extraInfo = extraInfo .. "<b>{{ucfirst:{{#invoke:Ftl|main|translation|" .. req.role .. "}}}}</b>: {{#invoke:Code/Формат/Время|main|seconds|" .. req.time .. "}}<br>"
                                end
                            end
                        end
                    end
                end
            end
        end
    end
    return extraInfo
end


function p.main(frame)
function p.main(frame)
Строка 13: Строка 63:
     end
     end
     local roleId = "Job" .. roleParam
     local roleId = "Job" .. roleParam
    local modeTrinkets = (searchKey == "trinkets")
    local modeBackStorage = (searchKey == "backStorage")
     local results = {}
     local results = {}
     local role = nil
     local printedTrinkets = {}
     for _, r in ipairs(gearRoleLoadout) do
 
        if r.id == roleId then
     local role = rolesById[roleId]
            role = r
            break
        end
    end
     if not role then
     if not role then
         return 'Роль с id "' .. roleId .. '" не найдена'
         return 'Роль с id "' .. roleId .. '" не найдена'
     end
     end
     for _, groupId in ipairs(role.groups or {}) do
     for _, groupId in ipairs(role.groups or {}) do
         local group = nil
         if (modeTrinkets and groupId == "Trinkets") or ((not modeTrinkets) and groupId ~= "Trinkets") then
        for _, g in ipairs(gearloadoutGroup) do
            local group = groupsById[groupId]
            if g.id == groupId then
             if group then
                group = g
                local loadouts = group.loadouts or {}
                break
                 if type(loadouts) == "string" then
             end
                    local success, decoded = pcall(mw.text.jsonDecode, loadouts)
        end
                     loadouts = (success and type(decoded) == "table") and decoded or {}
        if group then
            for _, loadoutId in ipairs(group.loadouts or {}) do
                 local loadout = nil
                for _, l in ipairs(gearloadout) do
                     if l.id == loadoutId then
                        loadout = l
                        break
                    end
                 end
                 end
                 if loadout and loadout.equipment then
                 for _, loadoutId in ipairs(loadouts) do
                     local eqValue = loadout.equipment[searchKey]
                     local loadout = loadoutsById[loadoutId]
                     if eqValue then
                     if loadout then
                         local extraInfo = ""
                         local skipLoadout = false
                         if loadout.effects then
                         if loadout.effects then
                             for _, effect in ipairs(loadout.effects) do
                             for _, effect in ipairs(loadout.effects) do
                                 if effect["!type"] == "GroupLoadoutEffect" then
                                 if effect.proto == "EffectSpeciesVox" then
                                     local proto = effect.proto
                                     skipLoadout = true
                                    if proto then
                                    break
                                        for _, candidate in ipairs(gearloadout) do
                                end
                                            if candidate.id == proto then
                            end
                                                if candidate.effects then
                        end
                                                    for _, eff in ipairs(candidate.effects) do
                        if not skipLoadout then
                                                        if eff["!type"] == "JobRequirementLoadoutEffect" and eff.requirement then
                            if modeTrinkets then
                                                            local req = eff.requirement
                                local items = {}
                                                            if req["!type"] == "DepartmentTimeRequirement" and req.department and req.time then
                                if loadout.storage and loadout.storage.back then
                                                                extraInfo = extraInfo .. "{{#invoke:Ftl|main|translation|department-" .. req.department .. "}}: " .. req.time .. "<br>"
                                    for _, item in ipairs(loadout.storage.back) do
                                                            elseif req["!type"] == "RoleTimeRequirement" and req.role and req.time then
                                        table.insert(items, item)
                                                                extraInfo = extraInfo .. "{{#invoke:Ftl|main|translation|".. req.role .. "}}: " .. req.time .. "<br>"
                                    end
                                                            end
                                elseif loadout.equipment then
                                                        end
                                    for _, eqValue in pairs(loadout.equipment) do
                                                    end
                                        table.insert(items, eqValue)
                                                end
                                        break
                                                break
                                    end
                                            end
                                end
                                        end
                                for _, trinket in ipairs(items) do
                                    if not printedTrinkets[trinket] then
                                        printedTrinkets[trinket] = true
                                        table.insert(results, '{{Предмет|' .. trinket .. '|wrapper=|repository=|label=|imageTooltip=}}')
                                    end
                                end
                            elseif modeBackStorage then
                                if loadout.storage and loadout.storage.back then
                                    for _, eqValue in ipairs(loadout.storage.back) do
                                        local extraInfo = getExtraInfo(loadout)
                                        local infoOutput = formatExtraInfo(extraInfo)
                                        table.insert(results, infoOutput .. '{{Предмет|' .. eqValue .. '|size=64px|repository=|label=|imageTooltip=}} [[File:Slot.png|64px|link=]]')
                                    end
                                end
                            else
                                if loadout.equipment then
                                    local eqValue = loadout.equipment[searchKey]
                                    if eqValue then
                                        local extraInfo = getExtraInfo(loadout)
                                        local infoOutput = formatExtraInfo(extraInfo)
                                        table.insert(results, infoOutput .. '{{Предмет|' .. eqValue .. '|size=64px|repository=|label=|imageTooltip=}} [[File:Slot.png|64px|link=]]')
                                     end
                                     end
                                 end
                                 end
                             end
                             end
                         end
                         end
                        table.insert(results, '<div class="time">{{AltTooltip|[[Файл:Small_watch.png|link=]]|' .. extraInfo .. '}}</div>' .. '<div class="id">{{#invoke:Prototypes/Предмет/Содержание|image|' .. eqValue .. '}} [[File:Slot.png|64px|link=]]</div>')
                     end
                     end
                 end
                 end
Строка 77: Строка 138:
         end
         end
     end
     end
     local output = {}
     local output = {}
     for _, eq in ipairs(results) do
     if modeTrinkets then
         table.insert(output, '<div class="role">' .. eq .. '</div>')
        for _, item in ipairs(results) do
            table.insert(output, item)
         end
    else
        for _, item in ipairs(results) do
            table.insert(output, '<div class="роль-лодаут__item">' .. item .. '</div>')
        end
     end
     end
     return frame:preprocess(table.concat(output))
     return frame:preprocess(table.concat(output))

Текущая версия от 10:17, 4 апреля 2025

Для документации этого модуля может быть создана страница Модуль:Prototypes/Роль/Лодаут/doc

-- Загрузка данных
local gearRoleLoadout  = mw.loadData("Модуль:IanComradeBot/roleLoadout.json/data")
local gearloadoutGroup = mw.loadData("Модуль:IanComradeBot/loadoutGroup.json/data")
local gearloadout      = mw.loadData("Модуль:IanComradeBot/loadout.json/data")

local p = {}

-- Построение кэш-таблиц для быстрого доступа по id
local rolesById = {}
for _, role in ipairs(gearRoleLoadout) do
    rolesById[role.id] = role
end

local groupsById = {}
for _, group in ipairs(gearloadoutGroup) do
    groupsById[group.id] = group
end

local loadoutsById = {}
for _, loadout in ipairs(gearloadout) do
    loadoutsById[loadout.id] = loadout
end

-- Функция для форматирования дополнительной информации (время требования)
local function formatExtraInfo(info)
    return (info ~= "" and '<div class="роль-лодаут__time">{{AltTooltip|[[Файл:Small_watch.png|link=]]|' .. info .. '}}</div>') or ""
end

-- Функция для получения дополнительной информации по эффектам лодаута
local function getExtraInfo(loadout)
    local extraInfo = ""
    if loadout.effects then
        for _, effect in ipairs(loadout.effects) do
            if effect["!type"] == "GroupLoadoutEffect" then
                local proto = effect.proto
                if proto then
                    local candidate = loadoutsById[proto]
                    if candidate and candidate.effects then
                        for _, eff in ipairs(candidate.effects) do
                            if eff["!type"] == "JobRequirementLoadoutEffect" and eff.requirement then
                                local req = eff.requirement
                                if req["!type"] == "DepartmentTimeRequirement" and req.department and req.time then
                                    extraInfo = extraInfo .. "<b>{{ucfirst:{{#invoke:Ftl|main|translation|department-" .. req.department .. "}}}}</b>: {{#invoke:Code/Формат/Время|main|seconds|" .. req.time .. "}}<br>"
                                elseif req["!type"] == "RoleTimeRequirement" and req.role and req.time then
                                    extraInfo = extraInfo .. "<b>{{ucfirst:{{#invoke:Ftl|main|translation|" .. req.role .. "}}}}</b>: {{#invoke:Code/Формат/Время|main|seconds|" .. req.time .. "}}<br>"
                                end
                            end
                        end
                    end
                end
            end
        end
    end
    return extraInfo
end

function p.main(frame)
    local args = frame.args
    local searchKey = args[1]
    local roleParam = args[2]
    if not roleParam then
        return 'Параметр roleId не указан'
    end
    local roleId = "Job" .. roleParam

    local modeTrinkets = (searchKey == "trinkets")
    local modeBackStorage = (searchKey == "backStorage")
    local results = {}
    local printedTrinkets = {}

    local role = rolesById[roleId]
    if not role then
        return 'Роль с id "' .. roleId .. '" не найдена'
    end

    for _, groupId in ipairs(role.groups or {}) do
        if (modeTrinkets and groupId == "Trinkets") or ((not modeTrinkets) and groupId ~= "Trinkets") then
            local group = groupsById[groupId]
            if group then
                local loadouts = group.loadouts or {}
                if type(loadouts) == "string" then
                    local success, decoded = pcall(mw.text.jsonDecode, loadouts)
                    loadouts = (success and type(decoded) == "table") and decoded or {}
                end
                for _, loadoutId in ipairs(loadouts) do
                    local loadout = loadoutsById[loadoutId]
                    if loadout then
                        local skipLoadout = false
                        if loadout.effects then
                            for _, effect in ipairs(loadout.effects) do
                                if effect.proto == "EffectSpeciesVox" then
                                    skipLoadout = true
                                    break
                                end
                            end
                        end
                        if not skipLoadout then
                            if modeTrinkets then
                                local items = {}
                                if loadout.storage and loadout.storage.back then
                                    for _, item in ipairs(loadout.storage.back) do
                                        table.insert(items, item)
                                    end
                                elseif loadout.equipment then
                                    for _, eqValue in pairs(loadout.equipment) do
                                        table.insert(items, eqValue)
                                        break
                                    end
                                end
                                for _, trinket in ipairs(items) do
                                    if not printedTrinkets[trinket] then
                                        printedTrinkets[trinket] = true
                                        table.insert(results, '{{Предмет|' .. trinket .. '|wrapper=|repository=|label=|imageTooltip=}}')
                                    end
                                end
                            elseif modeBackStorage then
                                if loadout.storage and loadout.storage.back then
                                    for _, eqValue in ipairs(loadout.storage.back) do
                                        local extraInfo = getExtraInfo(loadout)
                                        local infoOutput = formatExtraInfo(extraInfo)
                                        table.insert(results, infoOutput .. '{{Предмет|' .. eqValue .. '|size=64px|repository=|label=|imageTooltip=}} [[File:Slot.png|64px|link=]]')
                                    end
                                end
                            else
                                if loadout.equipment then
                                    local eqValue = loadout.equipment[searchKey]
                                    if eqValue then
                                        local extraInfo = getExtraInfo(loadout)
                                        local infoOutput = formatExtraInfo(extraInfo)
                                        table.insert(results, infoOutput .. '{{Предмет|' .. eqValue .. '|size=64px|repository=|label=|imageTooltip=}} [[File:Slot.png|64px|link=]]')
                                    end
                                end
                            end
                        end
                    end
                end
            end
        end
    end

    local output = {}
    if modeTrinkets then
        for _, item in ipairs(results) do
            table.insert(output, item)
        end
    else
        for _, item in ipairs(results) do
            table.insert(output, '<div class="роль-лодаут__item">' .. item .. '</div>')
        end
    end
    return frame:preprocess(table.concat(output))
end

return p