Модуль:Песочница/Pok: различия между версиями

мНет описания правки
мНет описания правки
Строка 96: Строка 96:
---------------------------------------------------------------------
---------------------------------------------------------------------
-- Обратный поиск для экипировки (reverseEquipment)
-- Обратный поиск для экипировки (reverseEquipment)
-- По заданному id оборудования ищем, в каких наборах экипировки (gear или loadout)
-- По заданному id оборудования ищем, в каких наборах экипировки
-- оно используется, и возвращаем id набора с указанием слота.
-- оно используется, и, если найдено в startingGear, ищем в jobData
-- по параметру startingGear, выводя id соответствующего job.
---------------------------------------------------------------------
---------------------------------------------------------------------
local function searchEquipmentInTable(equipmentTable, targetId)
    for slot, equipId in pairs(equipmentTable or {}) do
        if equipId == targetId then
            return slot
        end
    end
    return nil
end
function p.reverseEquipment(frame)
function p.reverseEquipment(frame)
     local targetId = frame.args[2]
     local targetId = frame.args[2]
    local slotFilter = frame.args[3]  -- если указан, фильтруем по конкретному слоту
     if not targetId or targetId == "" then
     if not targetId or targetId == "" then
         return "Ошибка: не указан id оборудования для обратного поиска."
         return "Ошибка: не указан id оборудования для обратного поиска."
     end
     end


     local foundJobs = {}
     local results = {}
     -- Ищем в startingGear (gearData)
     -- Поиск в startingGear (gearData)
     for gearId, gear in pairs(gearData) do
     for gearId, gear in pairs(gearData) do
         if gear.equipment then
         if gear.equipment then
             for slot, equipId in pairs(gear.equipment) do
             for slot, equipId in pairs(gear.equipment) do
                 if equipId == targetId then
                 if equipId == targetId and (not slotFilter or slot == slotFilter) then
                     -- Нашли подходящую экипировку – ищем в jobData, где startingGear равен gearId
                     -- Ищем в jobData, где startingGear равен gearId
                     for _, job in pairs(jobData) do
                    local foundJob = nil
                     for jobId, job in pairs(jobData) do
                         if job.startingGear == gearId then
                         if job.startingGear == gearId then
                             table.insert(foundJobs, job.id)
                             foundJob = jobId
                            break
                         end
                         end
                    end
                    if foundJob then
                        table.insert(results, "Job: " .. foundJob .. " (слот: " .. slot .. ")")
                    else
                        table.insert(results, "Gear: " .. gearId .. " (слот: " .. slot .. ")")
                     end
                     end
                 end
                 end
Строка 131: Строка 131:
     end
     end


     if #foundJobs == 0 then
     -- Поиск в loadout (loadoutData)
        return "Оборудование с id " .. targetId .. " не найдено в стартовых наборах экипировки."
    for loadoutId, loadout in pairs(loadoutData) do
        if loadout.equipment then
            for slot, equipId in pairs(loadout.equipment) do
                if equipId == targetId and (not slotFilter or slot == slotFilter) then
                    table.insert(results, "Loadout: " .. loadoutId .. " (слот: " .. slot .. ")")
                end
            end
        end
     end
     end


     -- Удаляем возможные дубликаты
     if #results == 0 then
    local uniqueJobs = {}
         return "Оборудование с id " .. targetId .. " не найдено в наборах экипировки."
    local seen = {}
    for _, jobId in ipairs(foundJobs) do
         if not seen[jobId] then
            table.insert(uniqueJobs, jobId)
            seen[jobId] = true
        end
     end
     end


     return "Найден job: " .. table.concat(uniqueJobs, ", ")
     return "Найдено использование оборудования: " .. table.concat(results, "; ")
end
end


---------------------------------------------------------------------
---------------------------------------------------------------------
-- Обратный поиск для груза (cargo) (reverseCargo)
-- Обратный поиск для груза (cargo) (reverseCargo)
-- По заданному режиму (product, cost, category, icon) и значению ищем запись cargo и возвращаем её id.
-- Всегда осуществляется поиск по полю product.
---------------------------------------------------------------------
---------------------------------------------------------------------
function p.reverseCargo(frame)
function p.reverseCargo(frame)
     local searchValue = frame.args[3]
     local searchValue = frame.args[2]
     if not searchValue or searchValue == "" then
     if not searchValue or searchValue == "" then
         return "Ошибка: необходимо указать значение для поиска."
         return "Ошибка: не указано значение для поиска в режиме cargo."
     end
     end