Модуль:TableOfRecipes: различия между версиями

Материал из МК14 | Space Station 14 Wiki
(Сортировки больше нет)
(уауэ)
Строка 6: Строка 6:
-- Кэш для хранения данных о химических веществах
-- Кэш для хранения данных о химических веществах
local chemCache = nil
local chemCache = nil
-- Кэш для перевода идентификаторов
local translationCache = {}


-- Функция для логирования
-- Функция для логирования
Строка 52: Строка 55:
-- Функция для перевода ID с использованием Module:Entity Lookup (для твердых веществ)
-- Функция для перевода ID с использованием Module:Entity Lookup (для твердых веществ)
local function translateID(frame, id)
local function translateID(frame, id)
    if translationCache[id] then
        return translationCache[id]
    end
   
     local translatedName = frame:callParserFunction{ name = '#invoke', args = { 'Entity_Lookup', 'getname', id } }
     local translatedName = frame:callParserFunction{ name = '#invoke', args = { 'Entity_Lookup', 'getname', id } }
     if translatedName then
     if translatedName then
         return string.format("%s [[Файл:%s.png|32px]]", translatedName, id)
         translationCache[id] = string.format("%s [[Файл:%s.png|32px]]", translatedName, id)
     else
     else
         return id  -- Если перевод не найден, возвращаем исходный ID
         translationCache[id] = id  -- Если перевод не найден, возвращаем исходный ID
     end
     end
   
    return translationCache[id]
end
end


Строка 68: Строка 77:
         return reagentId  -- Если перевод не найден, возвращаем исходный ID
         return reagentId  -- Если перевод не найден, возвращаем исходный ID
     end
     end
end
-- Функция для создания хэш-таблицы с результатами рецептов
local function createRecipeResultLookup(recipes)
    local lookup = {}
    for recipeType, recipeList in pairs(recipes) do
        for recipeId, recipe in pairs(recipeList) do
            if recipe.result then
                lookup[recipe.result] = true
            end
        end
    end
    return lookup
end
-- Функция для создания ссылки на строку таблицы
local function createLinkToRow(ingredientId, ingredientName)
    return string.format("[[#%s|%s]]", ingredientId, ingredientName)
end
end


Строка 84: Строка 111:
         return "Ошибка: данные о рецептах не загружены или тип рецептов не найден."
         return "Ошибка: данные о рецептах не загружены или тип рецептов не найден."
     end
     end
   
    -- Создаем хэш-таблицу для быстрой проверки результатов рецептов
    local recipeResultLookup = createRecipeResultLookup(recipes)
      
      
     -- Перебираем рецепты без сортировки
     -- Перебираем рецепты без сортировки
Строка 99: Строка 129:
                 for solidId, amount in pairs(recipe.solids) do
                 for solidId, amount in pairs(recipe.solids) do
                     local ingredientName = translateID(frame, solidId)
                     local ingredientName = translateID(frame, solidId)
                    -- Проверяем, можно ли приготовить ингредиент
                    if recipeResultLookup[solidId] then
                        ingredientName = createLinkToRow(solidId, ingredientName)
                    end
                     table.insert(solidsList, string.format("%s (%d)", ingredientName, amount))
                     table.insert(solidsList, string.format("%s (%d)", ingredientName, amount))
                 end
                 end
Строка 123: Строка 157:
         elseif recipeType == "sliceableRecipes" then
         elseif recipeType == "sliceableRecipes" then
             local input = recipe.input and translateID(frame, recipe.input) or "Нет входного элемента"
             local input = recipe.input and translateID(frame, recipe.input) or "Нет входного элемента"
            -- Проверяем, можно ли приготовить input
            if recipeResultLookup[recipe.input] then
                input = createLinkToRow(recipe.input, input)
            end
             local result = recipe.result and translateID(frame, recipe.result) or "Нет результата"
             local result = recipe.result and translateID(frame, recipe.result) or "Нет результата"
             local count = recipe.count or "Нет данных"
             local count = recipe.count or "Нет данных"
Строка 135: Строка 174:
         elseif recipeType == "grindableRecipes" then
         elseif recipeType == "grindableRecipes" then
             local input = recipe.input and translateID(frame, recipe.input) or "Нет входного элемента"
             local input = recipe.input and translateID(frame, recipe.input) or "Нет входного элемента"
            -- Проверяем, можно ли приготовить input
            if recipeResultLookup[recipe.input] then
                input = createLinkToRow(recipe.input, input)
            end
             local resultList = {}
             local resultList = {}


Строка 152: Строка 196:
         elseif recipeType == "heatableRecipes" then
         elseif recipeType == "heatableRecipes" then
             local input = recipe.input and translateID(frame, recipe.input) or "Нет входного элемента"
             local input = recipe.input and translateID(frame, recipe.input) or "Нет входного элемента"
            -- Проверяем, можно ли приготовить input
            if recipeResultLookup[recipe.input] then
                input = createLinkToRow(recipe.input, input)
            end
             local result = recipe.result and translateID(frame, recipe.result) or "Нет результата"
             local result = recipe.result and translateID(frame, recipe.result) or "Нет результата"
             local minTemp = recipe.minTemp or "Нет данных"
             local minTemp = recipe.minTemp or "Нет данных"
Строка 164: Строка 213:
         elseif recipeType == "toolmadeRecipes" then
         elseif recipeType == "toolmadeRecipes" then
             local input = recipe.input and translateID(frame, recipe.input) or "Нет входного элемента"
             local input = recipe.input and translateID(frame, recipe.input) or "Нет входного элемента"
            -- Проверяем, можно ли приготовить input
            if recipeResultLookup[recipe.input] then
                input = createLinkToRow(recipe.input, input)
            end
             local result = recipe.result and translateID(frame, recipe.result) or "Нет результата"
             local result = recipe.result and translateID(frame, recipe.result) or "Нет результата"
             local tool = recipe.tool and translateID(frame, recipe.tool) or "Нет инструмента"
             local tool = recipe.tool and translateID(frame, recipe.tool) or "Нет инструмента"
Строка 174: Строка 228:
         end
         end


         -- Добавляем якорь для строки таблицы
         -- Добавляем якорь для строки таблицы на основе result
         out = out .. string.format('|- id="%s"\n', recipeId)
        local resultId = recipe.result or recipeId  -- Используем result, если он есть, иначе recipeId
         out = out .. string.format('|- id="%s"\n', resultId)


         -- Генерация строки таблицы с использованием шаблона
         -- Генерация строки таблицы с использованием шаблона

Версия от 09:35, 9 марта 2025

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

local p = {}

-- Кэш для хранения данных о рецептах
local recipeCache = nil

-- Кэш для хранения данных о химических веществах
local chemCache = nil

-- Кэш для перевода идентификаторов
local translationCache = {}

-- Функция для логирования
local function log(message)
    mw.log("DEBUG: " .. message)
end

-- Функция для загрузки данных о рецептах из JSON-файла
local function loadRecipes()
    if recipeCache then
        return recipeCache
    end
    
    local success, data = pcall(function()
        return mw.text.jsonDecode(mw.title.new("User:CapybaraBot/mealrecipes_prototypes.json"):getContent())
    end)
    
    if success and type(data) == "table" then
        recipeCache = data
        return data
    else
        log("Ошибка при загрузке JSON (mealrecipes_prototypes.json): " .. tostring(data))
        return {}
    end
end

-- Функция для загрузки данных о химических веществах из JSON-файла
local function loadChemPrototypes()
    if chemCache then
        return chemCache
    end
    
    local success, data = pcall(function()
        return mw.text.jsonDecode(mw.title.new("User:CapybaraBot/chem_prototypes.json"):getContent())
    end)
    
    if success and type(data) == "table" then
        chemCache = data
        return data
    else
        log("Ошибка при загрузке JSON (chem_prototypes.json): " .. tostring(data))
        return {}
    end
end

-- Функция для перевода ID с использованием Module:Entity Lookup (для твердых веществ)
local function translateID(frame, id)
    if translationCache[id] then
        return translationCache[id]
    end
    
    local translatedName = frame:callParserFunction{ name = '#invoke', args = { 'Entity_Lookup', 'getname', id } }
    if translatedName then
        translationCache[id] = string.format("%s [[Файл:%s.png|32px]]", translatedName, id)
    else
        translationCache[id] = id  -- Если перевод не найден, возвращаем исходный ID
    end
    
    return translationCache[id]
end

-- Функция для перевода реагента (жидкого вещества) из chem_prototypes.json
local function translateReagent(reagentId)
    local chemData = loadChemPrototypes()
    if chemData[reagentId] and chemData[reagentId].name then
        return chemData[reagentId].name
    else
        return reagentId  -- Если перевод не найден, возвращаем исходный ID
    end
end

-- Функция для создания хэш-таблицы с результатами рецептов
local function createRecipeResultLookup(recipes)
    local lookup = {}
    for recipeType, recipeList in pairs(recipes) do
        for recipeId, recipe in pairs(recipeList) do
            if recipe.result then
                lookup[recipe.result] = true
            end
        end
    end
    return lookup
end

-- Функция для создания ссылки на строку таблицы
local function createLinkToRow(ingredientId, ingredientName)
    return string.format("[[#%s|%s]]", ingredientId, ingredientName)
end

-- Основная функция для генерации таблицы с рецептами
p.fillRecipeTable = function(frame)
    local args = frame.args
    local recipeType = args.recipeType or "microwaveRecipes"
    local templateName = args.template or "RecipeRow"

    local out = ""
    
    -- Загрузка данных о рецептах
    local recipes = loadRecipes()
    
    if not recipes or not recipes[recipeType] then
        return "Ошибка: данные о рецептах не загружены или тип рецептов не найден."
    end
    
    -- Создаем хэш-таблицу для быстрой проверки результатов рецептов
    local recipeResultLookup = createRecipeResultLookup(recipes)
    
    -- Перебираем рецепты без сортировки
    for recipeId, recipe in pairs(recipes[recipeType]) do
        local templateArgs = {}

        -- Обработка для microwaveRecipes
        if recipeType == "microwaveRecipes" then
            -- Переводим результат
            local result = translateID(frame, recipe.result) or "Нет результата"
            
            -- Формируем список ингредиентов (solids)
            local solidsList = {}
            if recipe.solids and type(recipe.solids) == "table" then
                for solidId, amount in pairs(recipe.solids) do
                    local ingredientName = translateID(frame, solidId)
                    -- Проверяем, можно ли приготовить ингредиент
                    if recipeResultLookup[solidId] then
                        ingredientName = createLinkToRow(solidId, ingredientName)
                    end
                    table.insert(solidsList, string.format("%s (%d)", ingredientName, amount))
                end
            end
            
            -- Формируем список реагентов (reagents)
            local reagentsList = {}
            if recipe.reagents and type(recipe.reagents) == "table" then
                for reagentId, amount in pairs(recipe.reagents) do
                    local reagentName = translateReagent(reagentId)
                    table.insert(reagentsList, string.format("%s (%d)", reagentName, amount))
                end
            end
            
            -- Формируем аргументы для шаблона
            templateArgs = {
                result = result,
                solids = table.concat(solidsList, ", ") or "Нет ингредиентов",
                reagents = table.concat(reagentsList, ", ") or "Нет реагентов",
                time = recipe.time or "Нет данных"
            }

        -- Обработка для sliceableRecipes
        elseif recipeType == "sliceableRecipes" then
            local input = recipe.input and translateID(frame, recipe.input) or "Нет входного элемента"
            -- Проверяем, можно ли приготовить input
            if recipeResultLookup[recipe.input] then
                input = createLinkToRow(recipe.input, input)
            end

            local result = recipe.result and translateID(frame, recipe.result) or "Нет результата"
            local count = recipe.count or "Нет данных"

            templateArgs = {
                input = input,
                result = result,
                count = count
            }

        -- Обработка для grindableRecipes
        elseif recipeType == "grindableRecipes" then
            local input = recipe.input and translateID(frame, recipe.input) or "Нет входного элемента"
            -- Проверяем, можно ли приготовить input
            if recipeResultLookup[recipe.input] then
                input = createLinkToRow(recipe.input, input)
            end

            local resultList = {}

            if recipe.result and type(recipe.result) == "table" then
                for reagentId, amount in pairs(recipe.result) do
                    local reagentName = translateReagent(reagentId)
                    table.insert(resultList, string.format("%s (%d)", reagentName, amount))
                end
            end

            templateArgs = {
                input = input,
                result = table.concat(resultList, ", ") or "Нет результата"
            }

        -- Обработка для heatableRecipes
        elseif recipeType == "heatableRecipes" then
            local input = recipe.input and translateID(frame, recipe.input) or "Нет входного элемента"
            -- Проверяем, можно ли приготовить input
            if recipeResultLookup[recipe.input] then
                input = createLinkToRow(recipe.input, input)
            end

            local result = recipe.result and translateID(frame, recipe.result) or "Нет результата"
            local minTemp = recipe.minTemp or "Нет данных"

            templateArgs = {
                input = input,
                result = result,
                minTemp = minTemp
            }

        -- Обработка для toolmadeRecipes
        elseif recipeType == "toolmadeRecipes" then
            local input = recipe.input and translateID(frame, recipe.input) or "Нет входного элемента"
            -- Проверяем, можно ли приготовить input
            if recipeResultLookup[recipe.input] then
                input = createLinkToRow(recipe.input, input)
            end

            local result = recipe.result and translateID(frame, recipe.result) or "Нет результата"
            local tool = recipe.tool and translateID(frame, recipe.tool) or "Нет инструмента"

            templateArgs = {
                input = input,
                result = result,
                tool = tool
            }
        end

        -- Добавляем якорь для строки таблицы на основе result
        local resultId = recipe.result or recipeId  -- Используем result, если он есть, иначе recipeId
        out = out .. string.format('|- id="%s"\n', resultId)

        -- Генерация строки таблицы с использованием шаблона
        out = out .. frame:expandTemplate{ title = templateName, args = templateArgs } .. "\n"
    end
    
    return out
end

return p