Модуль:Песочница/Pok

Материал из Space Station 14 Вики

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

local p = {}

---------------------------------------------------------------------
-- Загрузка данных 
---------------------------------------------------------------------
local itemData           = mw.loadData("Модуль:IanComradeBot/prototypes/fills/Item.json/data")
local tableData          = mw.loadData("Модуль:IanComradeBot/prototypes/table.json/data")
local gearData           = mw.loadData("Модуль:IanComradeBot/startingGear.json/data")
local jobData            = mw.loadData("Модуль:IanComradeBot/job.json/data")
local gearRoleLoadout    = mw.loadData("Модуль:IanComradeBot/roleLoadout.json/data")
local loadoutData        = mw.loadData("Модуль:IanComradeBot/loadout.json/data")
local loadoutGroupData   = mw.loadData("Модуль:IanComradeBot/loadoutGroup.json/data")
local cargoData          = mw.loadData("Модуль:IanComradeBot/prototypes/сargo.json/base")
local latheData          = mw.loadData("Модуль:IanComradeBot/prototypes/lathe.json/data")
local recipeData         = mw.loadData("Модуль:IanComradeBot/prototypes/lathe/recipes.json/data")
local researchData       = mw.loadData("Модуль:IanComradeBot/prototypes/research.json/data")
local materialData       = mw.loadData("Модуль:IanComradeBot/prototypes/materials.json/data")
local chemDataLathe      = mw.loadData("Модуль:IanComradeBot/chem prototypes.json/data")
local vendingMachinesData = mw.loadData("Модуль:IanComradeBot/prototypes/vending machines.json/data")
local inventoriesData     = mw.loadData("Модуль:IanComradeBot/prototypes/vending machines/inventories.json/data")

---------------------------------------------------------------------
-- Функции для обратного поиска
---------------------------------------------------------------------

local function searchItemInStructure(struct, targetId)
    if type(struct) ~= "table" then
        return false
    end
    if struct.id and struct.id == targetId then
        return true
    end
    for _, v in pairs(struct) do
        if type(v) == "table" then
            if searchItemInStructure(v, targetId) then
                return true
            end
        end
    end
    return false
end

---------------------------------------------------------------------

---------------------------------------------------------------------
local function findRelatedTables(initialList)
    local results = {} 
    local function recursiveSearch(tblId)
        if results[tblId] then 
            return 
        end
        results[tblId] = true
        for id, entry in pairs(tableData) do
            if type(entry) == "table" then
                for _, value in pairs(entry) do
                    if type(value) == "table" and value.tableId and value.tableId == tblId then
                        recursiveSearch(id)
                    end
                end
            end
        end
    end
    for _, tblId in ipairs(initialList) do
        recursiveSearch(tblId)
    end
    local res = {}
    for id in pairs(results) do 
        table.insert(res, id)
    end
    return res
end

function p.reverseContained(frame)
    local targetId = frame.args[2]
    if not targetId or targetId == "" then
        return "Ошибка: не указан id предмета для обратного поиска."
    end

    local initialTables = {}
    for tableId, tblEntry in pairs(tableData) do
        if type(tblEntry) == "table" then
            if searchItemInStructure(tblEntry, targetId) then
                table.insert(initialTables, tableId)
            end
        end
    end

    if #initialTables == 0 then
        return "Таблица, содержащая предмет с id " .. targetId .. ", не найдена."
    end

    local allRelatedTables = findRelatedTables(initialTables)

    local matchingStorages = {}
    for storageId, storage in pairs(itemData) do
        if type(storage) == "table" and storage.EntityTableContainerFill and storage.EntityTableContainerFill.containers then
            local containers = storage.EntityTableContainerFill.containers
            if containers.entity_storage and containers.entity_storage.tableId then
                for _, tblId in ipairs(allRelatedTables) do
                    if containers.entity_storage.tableId == tblId then
                        table.insert(matchingStorages, storageId)
                    end
                end
            end
            if containers.storagebase and containers.storagebase.tableId then
                for _, tblId in ipairs(allRelatedTables) do
                    if containers.storagebase.tableId == tblId then
                        table.insert(matchingStorages, storageId)
                    end
                end
            end
        end
        if storage.id and storage.id == targetId then
            table.insert(matchingStorages, storageId)
        end
    end

    if #matchingStorages == 0 then
        return "Хранилище, вызывающее найденные таблицы, не найдено."
    end

    return "Найденные хранилища: " .. table.concat(matchingStorages, ", ")
end

---------------------------------------------------------------------

---------------------------------------------------------------------
function p.reverseEquipment(frame)
    local targetId = frame.args[2]
    local slotFilter = frame.args[3]
    if not targetId or targetId == "" then
        return "Ошибка: не указан id оборудования для обратного поиска."
    end

    local results = {}

    -- Обработка startingGear (gearData)
    for _, gear in pairs(gearData) do
        local currentGearId = gear.id or "Unknown"
        if gear.equipment then
            for slot, equipId in pairs(gear.equipment) do
                if equipId == targetId and (not slotFilter or slot == slotFilter) then
                    local foundJob = nil
                    for jobId, job in pairs(jobData) do
                        if job.startingGear == currentGearId then
                            foundJob = jobId
                            break
                        end
                    end
                    if foundJob then
                        table.insert(results, "Job: " .. foundJob .. " (слот: " .. slot .. ")")
                    end
                end
            end
        end
    end

    -- Обработка loadout (loadoutData)
    for _, 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
                    local foundGroupId = nil
                    -- Поиск группы в loadoutGroupData, в которой присутствует loadout.id
                    for _, group in pairs(loadoutGroupData) do
                        if group.loadouts and type(group.loadouts) == "table" then
                            for _, lId in ipairs(group.loadouts) do
                                if lId == loadout.id then
                                    foundGroupId = group.id
                                    break
                                end
                            end
                        end
                        if foundGroupId then break end
                    end
                    local foundJob = nil
                    -- Поиск роли в gearRoleLoadout, где группы содержат найденный id группы
                    if foundGroupId then
                        for _, role in pairs(gearRoleLoadout) do
                            if role.groups then
                                for _, g in ipairs(role.groups) do
                                    if g == foundGroupId then
                                        foundJob = role.id  -- предполагается, что поле id присутствует
                                        break
                                    end
                                end
                            end
                            if foundJob then break end
                        end
                    end
                    if foundJob then
                        table.insert(results, "Job: " .. foundJob .. " (слот: " .. slot .. ")")
                    end
                end
            end
        end
    end

    if #results == 0 then
        return "Оборудование с id " .. targetId .. " не найдено в наборах экипировки."
    end

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

---------------------------------------------------------------------

---------------------------------------------------------------------
function p.reverseCargo(frame)
    local searchValue = frame.args[2]
    if not searchValue or searchValue == "" then
        return "Ошибка: не указано значение для поиска в режиме cargo."
    end

    local foundIds = {}
    for _, entry in ipairs(cargoData) do
        if entry.product == searchValue then
            table.insert(foundIds, entry.id)
        end
    end

    if #foundIds == 0 then
        return "Запись для product со значением '" .. searchValue .. "' не найдена."
    end

    return "Найденные записи cargo: " .. table.concat(foundIds, ", ")
end

---------------------------------------------------------------------

---------------------------------------------------------------------
local function getRecipeById(recipeId)
    for _, recipe in ipairs(recipeData) do
        if recipe.id == recipeId then
            return recipe
        end
    end
    return nil
end

function p.reverseLathe(frame)
    local targetResult = frame.args[2]
    if not targetResult or targetResult == "" then
        return "Ошибка: не указан результат рецепта для обратного поиска."
    end

    local matchingLathes = {}
    local function checkRecipes(recipeIds)
        for _, recipeId in ipairs(recipeIds or {}) do
            local recipe = getRecipeById(recipeId)
            if recipe and recipe.result == targetResult then
                return true
            end
        end
        return false
    end

    for _, lathe in ipairs(latheData) do
        local found = false
        if lathe.Lathe then
            if checkRecipes(lathe.Lathe.staticRecipes) or checkRecipes(lathe.Lathe.dynamicRecipes) then
                found = true
            end
        end
        if not found and lathe.EmagLatheRecipes then
            if checkRecipes(lathe.EmagLatheRecipes.emagStaticRecipes) or checkRecipes(lathe.EmagLatheRecipes.emagDynamicRecipes) then
                found = true
            end
        end
        if found and lathe.id then
            table.insert(matchingLathes, lathe.id)
        end
    end

    if #matchingLathes == 0 then
        return "Станок, производящий '" .. targetResult .. "', не найден."
    end

    return "Найденные станки: " .. table.concat(matchingLathes, ", ")
end

---------------------------------------------------------------------

---------------------------------------------------------------------
function p.reverseVending(frame)
    local invMode = frame.args[2] or ""   -- режим поиска: "inventory", "contraband", "emag" или пустая строка для всех
    local targetId = frame.args[3]
    if not targetId or targetId == "" then
        return "Ошибка: не указан id предмета для обратного поиска в торговых автоматах."
    end

    local matchingInventoryIds = {}
    -- Перебираем все инвентарные записи
    for _, inventory in pairs(inventoriesData) do
        if inventory.id then
            local found = false
            -- Если режим не указан, ищем во всех типах инвентарей
            if (invMode == "inventory" or invMode == "" or not invMode) and inventory.startingInventory and type(inventory.startingInventory) == "table" then
                if inventory.startingInventory[targetId] then
                    found = true
                end
            end
            if (invMode == "contraband" or invMode == "" or not invMode) and inventory.contrabandInventory and type(inventory.contrabandInventory) == "table" then
                if inventory.contrabandInventory[targetId] then
                    found = true
                end
            end
            if (invMode == "emag" or invMode == "" or not invMode) and inventory.emaggedInventory and type(inventory.emaggedInventory) == "table" then
                if inventory.emaggedInventory[targetId] then
                    found = true
                end
            end
            if found then
                table.insert(matchingInventoryIds, inventory.id)
            end
        end
    end

    if #matchingInventoryIds == 0 then
        return "Предмет с id " .. targetId .. " не найден ни в одном инвентаре."
    end

    local matchingVendingMachines = {}
    -- Поиск торговых автоматов, у которых поле VendingMachine.pack совпадает с id найденного инвентаря
    for _, vm in pairs(vendingMachinesData) do
        if vm.VendingMachine and vm.VendingMachine.pack then
            local packId = vm.VendingMachine.pack
            for _, invId in ipairs(matchingInventoryIds) do
                if packId == invId then
                    if vm.id then
                        table.insert(matchingVendingMachines, vm.id)
                    end
                    break
                end
            end
        end
    end

    if #matchingVendingMachines == 0 then
        return "Не найден торговый автомат, содержащий предмет с id " .. targetId .. "."
    end

    return "Торговые автоматы, содержащие предмет " .. targetId .. ": " .. table.concat(matchingVendingMachines, ", ")
end

---------------------------------------------------------------------
-- Основная функция модуля
---------------------------------------------------------------------
function p.main(frame)
    local mode = frame.args[1]
    if not mode or mode == "" then
        return "Ошибка: не указан режим обратного поиска. Доступные режимы: reverseContained, reverseEquipment, reverseCargo, reverseLathe."
    end

    if mode == "reverseContained" then
        return p.reverseContained(frame)
    elseif mode == "reverseEquipment" then
        return p.reverseEquipment(frame)
    elseif mode == "reverseCargo" then
        return p.reverseCargo(frame)
    elseif mode == "reverseLathe" then
        return p.reverseLathe(frame)
    elseif mode == "reverseVending" then
        return p.reverseVending(frame)
    else
        return "Неизвестный режим: " .. mode .. ". Доступные режимы: reverseContained, reverseEquipment, reverseCargo, reverseLathe."
    end
end

return p