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

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


-- Функция для поиска объекта по id
-- Поиск записи по id в массиве
local function findById(tbl, id)
local function findById(tbl, id)
     for k, v in pairs(tbl) do
    if type(tbl) ~= "table" then return nil end
         if k == id or (v.id and v.id == id) then
     for _, v in ipairs(tbl) do
         if v.id == id then
             return v
             return v
         end
         end
Строка 17: Строка 18:
end
end


-- Функция для преобразования первой буквы строки в заглавную
-- Преобразование первой буквы в заглавную
local function capitalizeFirst(str)
local function capitalizeFirst(str)
     if not str or str == "" then return "" end
     if not str or str == "" then return "" end
     return str:sub(1,1):upper() .. str:sub(2)
     return str:sub(1,1):upper() .. str:sub(2)
end
-- Формирование вывода предмета
local function buildResult(itemId, sourceGear, slot)
    if slot == "back" and sourceGear.storage and sourceGear.storage.back then
        local storageBack = ""
        for _, storageItem in ipairs(sourceGear.storage.back) do
            storageBack = storageBack .. string.format("{{Предмет|wrapper=1|repository=1|%s}}", storageItem)
        end
        return string.format("<div>{{Предмет|%s|repository=1|size=64px|label=|imageTooltip=1}} {{СollapsibleMenu|%s}}</div>", itemId, storageBack)
    else
        return string.format("{{Предмет|%s|repository=1|size=64px|label=|imageTooltip=1}}", itemId)
    end
end
-- Обработка в режиме radio
local function processResult(itemId, result, radioMode, frame)
    if radioMode then
        return frame:preprocess(string.format("{{#invoke:Prototypes/Механика/Частоты|main|%s}}", itemId))
    else
        return frame:preprocess(result)
    end
end
end


function p.main(frame)
function p.main(frame)
     local args = frame.args
     local args = frame.args
     local slot  = mw.text.trim(args[1] or "")
     local param  = mw.text.trim(args[1] or "")
     local jobId = mw.text.trim(args[2] or "")
     local idParam = mw.text.trim(args[2] or "")
   
     if param == "" or idParam == "" then
     if slot == "" or jobId == "" then
         return "Ошибка: Не заданы необходимые параметры. Используйте: {{#invoke:Прототип/Роль/Экипировка|main|<slot/special/radio>|<jobId или startingGearId>}}"
         return "Ошибка: Не заданы необходимые параметры. Используйте: {{#invoke:Prototypes/Роль/Экипировка|main|<slot>|<jobId>}}"
    end
 
    jobId = capitalizeFirst(jobId)
    local job = findById(jobData, jobId)
    if not job then
        return "Ошибка: не найдена должность с id " .. jobId
     end
     end


     -- Новый режим обработки special
     -- Режим special: импланты
     if slot == "special" then
     if param == "special" then
         local implants = {}
         local job = findById(jobData, capitalizeFirst(idParam))
         if job.special then
         if not job then return "" end
            for _, special in ipairs(job.special) do
        local specials = job.special or {}
                if special["!type"] == "AddImplantSpecial" and special.implants then
        local implantsList = {}
                    for _, implant in ipairs(special.implants) do
        for _, special in ipairs(specials) do
                        table.insert(implants, string.format("{{#invoke:Entity Lookup|getname|%s}}", implant))
            if special["!type"] == "AddImplantSpecial" and special.implants then
                    end
                for _, implant in ipairs(special.implants) do
                    table.insert(implantsList, string.format("{{#invoke:Entity Lookup|getname|%s}}", implant))
                 end
                 end
             end
             end
         end
         end
         return table.concat(implants, ", ")
         if #implantsList > 0 then
            return frame:preprocess(table.concat(implantsList, ", "))
        else
            return ""
        end
     end
     end


     local gearId = job.startingGear
     local radioMode = false
     if not gearId then
    local slot = param
         return "Ошибка: у должности с id " .. jobId .. " не указан startingGear"
     if param == "radio" then
         radioMode = true
        slot = "ears"
     end
     end


     local gear = findById(gearData, gearId)
     local itemId = ""
    if not gear then
     local sourceGear = nil
        return "Ошибка: не найдена экипировка с id " .. gearId
     end


     local gearEquipment = gear.equipment or {}
     -- Пытаемся найти запись в roleLoadout
     local itemId = gearEquipment[slot] or ""
     local roleLoadoutId = "Job" .. capitalizeFirst(idParam)
     local sourceGear = gear
     local roleLoadoutData = findById(gearRoleLoadout, roleLoadoutId)


     if itemId == "" then
     if not roleLoadoutData then
         local function capitalize(s)
        -- Нет групповых лоадаутов: сначала пробуем ручной ввод gearId
             return (s:gsub("^%l", string.upper))
        local gear = findById(gearData, idParam)
        if gear then
            -- ручной режим: idParam — это startingGearId
            local gearEquipment = gear.equipment or {}
            itemId = gearEquipment[slot] or ""
            sourceGear = gear
         else
            -- fallback: берём startingGear из job
            local job = findById(jobData, capitalizeFirst(idParam))
             if not job or not job.startingGear then
                return "Ошибка: не найдена экипировка или должность с id " .. idParam
            end
            local gear2 = findById(gearData, job.startingGear)
            if not gear2 then
                return "Ошибка: не найдена экипировка с id " .. job.startingGear
            end
            local gearEquipment2 = gear2.equipment or {}
            itemId = gearEquipment2[slot] or ""
            sourceGear = gear2
        end
    else
        -- Есть групповые лоадауты: классический режим через roleLoadout
        local jobId = capitalizeFirst(idParam)
        local job = findById(jobData, jobId)
        if not job then
            return "Ошибка: не найдена должность с id " .. jobId
         end
         end
 
         local gearId = job.startingGear
         local groupID = jobId .. capitalize(slot)
         if not gearId then
   
             return "Ошибка: у должности с id " .. jobId .. " не указан startingGear"
        local groupFound = nil
         for _, group in ipairs(gearloadoutGroup) do
            if group.id == groupID then
                groupFound = group
                break
             end
         end
         end
 
        local gear = findById(gearData, gearId)
         if not groupFound then
         if not gear then
             return ""
             return "Ошибка: не найдена экипировка с id " .. gearId
         end
         end
 
        local gearEquipment = gear.equipment or {}
         for _, loadoutID in ipairs(groupFound.loadouts) do
         itemId = gearEquipment[slot] or ""
            for _, loadout in ipairs(gearloadout) do
        sourceGear = gear
                if loadout.id == loadoutID then
        if itemId == "" then
                    if loadout.equipment and loadout.equipment[slot] then
            for _, groupId in ipairs(roleLoadoutData.groups or {}) do
                        itemId = loadout.equipment[slot]
                if groupId ~= "Trinkets" then
                        sourceGear = loadout
                    local group = findById(gearloadoutGroup, groupId)
                         break
                    if group and type(group.loadouts) == "table" then
                        for _, loadoutID in ipairs(group.loadouts) do
                            local loadout = findById(gearloadout, loadoutID)
                            if loadout and not loadout.effects and loadout.equipment and loadout.equipment[slot] then
                                itemId = loadout.equipment[slot]
                                sourceGear = loadout
                                break
                            end
                         end
                     end
                     end
                 end
                 end
                if itemId ~= "" then break end
             end
             end
            if itemId ~= "" then break end
         end
         end
     end
     end
Строка 104: Строка 152:
     end
     end


     if slot == "back" and sourceGear.storage and sourceGear.storage.back then
     local result = buildResult(itemId, sourceGear, slot)
        local storageBack = ""
    return processResult(itemId, result, radioMode, frame)
        for _, storageItem in ipairs(sourceGear.storage.back) do
            storageBack = storageBack .. string.format("{{#invoke:Prototypes/Предмет/Содержание|main|%s}}", storageItem)
        end
        return frame:preprocess(string.format("{{#invoke:Prototypes/Предмет/Содержание|image|%s|%s}}", itemId, storageBack))
    else
        return frame:preprocess(string.format("{{#invoke:Prototypes/Предмет/Содержание|image|%s}}", itemId))
    end
end
end


return p
return p