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

Материал из Space Station 14 Вики
мНет описания правки
мНет описания правки
Строка 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)
Строка 17: Строка 67:
     local modeBackStorage = (searchKey == "backStorage")
     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
    local printedTrinkets = {}
 
     for _, groupId in ipairs(role.groups or {}) do
     for _, groupId in ipairs(role.groups or {}) do
         if (modeTrinkets and groupId == "Trinkets") or ((not modeTrinkets) and groupId ~= "Trinkets") then
         if (modeTrinkets and groupId == "Trinkets") or ((not modeTrinkets) and groupId ~= "Trinkets") then
             local group = nil
             local group = groupsById[groupId]
            for _, g in ipairs(gearloadoutGroup) do
                if g.id == groupId then
                    group = g
                    break
                end
            end
             if group then
             if group then
                 local loadouts = group.loadouts or {}
                 local loadouts = group.loadouts or {}
                 if type(loadouts) == "string" then
                 if type(loadouts) == "string" then
                     local success, decoded = pcall(mw.text.jsonDecode, loadouts)
                     local success, decoded = pcall(mw.text.jsonDecode, loadouts)
                     if success and type(decoded) == "table" then
                     loadouts = (success and type(decoded) == "table") and decoded or {}
                        loadouts = decoded
                    else
                        loadouts = {}
                    end
                 end
                 end
                 for _, loadoutId in ipairs(loadouts) do
                 for _, loadoutId in ipairs(loadouts) do
                     local loadout = nil
                     local loadout = loadoutsById[loadoutId]
                    for _, l in ipairs(gearloadout) do
                        if l.id == loadoutId then
                            loadout = l
                            break
                        end
                    end
                     if loadout then
                     if loadout then
                         local skipLoadout = false
                         local skipLoadout = false
Строка 87: Строка 117:
                                 if loadout.storage and loadout.storage.back then
                                 if loadout.storage and loadout.storage.back then
                                     for _, eqValue in ipairs(loadout.storage.back) do
                                     for _, eqValue in ipairs(loadout.storage.back) do
                                         local extraInfo = ""
                                         local extraInfo = getExtraInfo(loadout)
                                        if loadout.effects then
                                         local infoOutput = formatExtraInfo(extraInfo)
                                            for _, effect in ipairs(loadout.effects) do
                                                if effect["!type"] == "GroupLoadoutEffect" then
                                                    local proto = effect.proto
                                                    if proto then
                                                        for _, candidate in ipairs(gearloadout) do
                                                            if candidate.id == proto then
                                                                if 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
                                                                break
                                                            end
                                                        end
                                                    end
                                                end
                                            end
                                        end
                                         local infoOutput = ""
                                        if extraInfo ~= "" then
                                            infoOutput = '<div class="роль-лодаут__time">{{AltTooltip|[[Файл:Small_watch.png|link=]]|' .. extraInfo .. '}}</div>'
                                        end
                                         table.insert(results, infoOutput .. '{{#invoke:Prototypes/Предмет/Содержание|image|' .. eqValue .. '}} [[File:Slot.png|64px|link=]]')
                                         table.insert(results, infoOutput .. '{{#invoke:Prototypes/Предмет/Содержание|image|' .. eqValue .. '}} [[File:Slot.png|64px|link=]]')
                                     end
                                     end
Строка 125: Строка 126:
                                     local eqValue = loadout.equipment[searchKey]
                                     local eqValue = loadout.equipment[searchKey]
                                     if eqValue then
                                     if eqValue then
                                         local extraInfo = ""
                                         local extraInfo = getExtraInfo(loadout)
                                        if loadout.effects then
                                         local infoOutput = formatExtraInfo(extraInfo)
                                            for _, effect in ipairs(loadout.effects) do
                                                if effect["!type"] == "GroupLoadoutEffect" then
                                                    local proto = effect.proto
                                                    if proto then
                                                        for _, candidate in ipairs(gearloadout) do
                                                            if candidate.id == proto then
                                                                if 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
                                                                break
                                                            end
                                                        end
                                                    end
                                                end
                                            end
                                        end
                                         local infoOutput = ""
                                        if extraInfo ~= "" then
                                            infoOutput = '<div class="роль-лодаут__time">{{AltTooltip|[[Файл:Small_watch.png|link=]]|' .. extraInfo .. '}}</div>'
                                        end
                                         table.insert(results, infoOutput .. '{{#invoke:Prototypes/Предмет/Содержание|image|' .. eqValue .. '}} [[File:Slot.png|64px|link=]]')
                                         table.insert(results, infoOutput .. '{{#invoke:Prototypes/Предмет/Содержание|image|' .. eqValue .. '}} [[File:Slot.png|64px|link=]]')
                                     end
                                     end
Строка 166: Строка 138:
         end
         end
     end
     end
     local output = {}
     local output = {}
     if modeTrinkets then
     if modeTrinkets then
         for _, eq in ipairs(results) do
         for _, item in ipairs(results) do
             table.insert(output, eq)
             table.insert(output, item)
         end
         end
     else
     else
         for _, eq in ipairs(results) do
         for _, item in ipairs(results) do
             table.insert(output, '<div class="роль-лодаут__item">' .. eq .. '</div>')
             table.insert(output, '<div class="роль-лодаут__item">' .. item .. '</div>')
         end
         end
     end
     end

Версия от 21:41, 15 февраля 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, '{{#invoke:Prototypes/Предмет/Содержание|frame|' .. trinket .. '}}')
                                    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 .. '{{#invoke:Prototypes/Предмет/Содержание|image|' .. eqValue .. '}} [[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 .. '{{#invoke:Prototypes/Предмет/Содержание|image|' .. eqValue .. '}} [[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