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

мНет описания правки
мНет описания правки
Строка 7: Строка 7:
local p = {}
local p = {}


-- Функция для получения элемента экипировки по ключу, учитывая возможность разного регистра первой буквы
-- Вспомогательная функция для поиска в gearloadout по id
local function getEquipment(equipment, slot)
function p.findInLoadoutById(id, slot)
     if not equipment then return nil end
     for _, v in pairs(gearloadout) do
    local item = equipment[slot]
        if string.lower(v.id) == string.lower(id) then
    if item then return item end
            if v.equipment then
     local capitalized = slot:sub(1,1):upper() .. slot:sub(2)
                return v.equipment[slot], v
     return equipment[capitalized]
            end
            break
        end
     end
     return nil, nil
end
end


Строка 24: Строка 28:
         return "Ошибка: Не заданы необходимые параметры. Используйте: {{#invoke:IanComradeBot/gear|main|<slot>|<jobId>}}"
         return "Ошибка: Не заданы необходимые параметры. Используйте: {{#invoke:IanComradeBot/gear|main|<slot>|<jobId>}}"
     end
     end
   
 
     -- Поиск должности по id
     -- Поиск должности по id
     local job = nil
     local job = nil
Строка 54: Строка 58:
         return "Ошибка: не найдена экипировка с id " .. gearId
         return "Ошибка: не найдена экипировка с id " .. gearId
     end
     end
     if not gear.equipment then
     if not gear.equipment then
         return "Ошибка: в экипировке с id " .. gearId .. " отсутствует раздел equipment"
         return "Ошибка: в экипировке с id " .. gearId .. " отсутствует раздел equipment"
     end
     end


    -- 1. Попытка получить экипировку из gearData
    local itemId = gear.equipment[slot]
     local sourceGear = gear
     local sourceGear = gear
    local itemId = getEquipment(gear.equipment, slot)


     -- 1. Если в gearData для запрошенного слота ничего нет,
     -- 2. Если в gearData ничего не найдено, ищем в gearloadout по id (jobId .. slot)
    -- ищем в gearloadout по составленному id (jobId .. slot)
     if not itemId then
     if not itemId then
         local combinedId = jobId .. slot
         local combinedId = jobId .. slot -- например, "Quartermaster" + "shoes" → "Quartermastershoes"
         local loadoutGear = nil
         itemId, sourceGear = p.findInLoadoutById(combinedId, slot)
        for _, v in pairs(gearloadout) do
            if string.lower(v.id) == string.lower(combinedId) then
                loadoutGear = v
                break
            end
        end
        if loadoutGear and loadoutGear.equipment then
            itemId = getEquipment(loadoutGear.equipment, slot)
            sourceGear = loadoutGear
        end
     end
     end


     -- 2. Если всё ещё не нашли – ищем в gearloadoutGroup
     -- 3. Если и там ничего не найдено, ищем в gearloadoutGroup
     if not itemId then
     if not itemId then
         local combinedId = jobId .. slot
         local combinedId = jobId .. slot
         local groupGear = nil
         local groupEntry = nil
         for _, v in pairs(gearloadoutGroup) do
         for _, v in pairs(gearloadoutGroup) do
             if string.lower(v.id) == string.lower(combinedId) then
             if string.lower(v.id) == string.lower(combinedId) then
                 groupGear = v
                 groupEntry = v
                 break
                 break
             end
             end
         end
         end
         if groupGear and groupGear.loadouts and #groupGear.loadouts > 0 then
         if groupEntry and groupEntry.loadouts and #groupEntry.loadouts > 0 then
             local firstLoadoutId = groupGear.loadouts[1]
             local firstLoadoutId = groupEntry.loadouts[1]
             local foundLoadout = nil
             itemId, sourceGear = p.findInLoadoutById(firstLoadoutId, slot)
            for _, v in pairs(gearloadout) do
                if string.lower(v.id) == string.lower(firstLoadoutId) then
                    foundLoadout = v
                    break
                end
            end
            if foundLoadout and foundLoadout.equipment then
                itemId = getEquipment(foundLoadout.equipment, slot)
                sourceGear = foundLoadout
            end
         end
         end
     end
     end


    -- 4. Если экипировка так и не найдена, возвращаем пустую строку
     if not itemId then
     if not itemId then
         return ""
         return ""
     end
     end


     -- Дополнительная обработка для слота "back"
     -- Обработка для слота "back"
     if slot == "back" then
     if slot == "back" then
         local storageBack = ""
         local storageBack = ""