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

Материал из МК14 | Space Station 14 Wiki
(Попытка 11 блять)
(перечисление нескольких результатов с новой строки, а не через запятую)
 
(не показано 12 промежуточных версий этого же участника)
Строка 1: Строка 1:
local p = {}
local p = {}


Строка 6: Строка 7:
-- Кэш для хранения данных о химических веществах
-- Кэш для хранения данных о химических веществах
local chemCache = nil
local chemCache = nil
-- Кэш для перевода идентификаторов
local translationCache = {}
-- Функция для логирования
local function log(message)
    mw.log("DEBUG: " .. message)
end


-- Функция для загрузки данных о рецептах из JSON-файла
-- Функция для загрузки данных о рецептах из JSON-файла
Строка 17: Строка 26:
     end)
     end)
      
      
     if success then
     if success and type(data) == "table" then
         recipeCache = data
         recipeCache = data
         return data
         return data
     else
     else
         mw.log("Ошибка при загрузке JSON (mealrecipes_prototypes.json): " .. data)
         log("Ошибка при загрузке JSON (mealrecipes_prototypes.json): " .. tostring(data))
         return {}
         return {}
     end
     end
Строка 36: Строка 45:
     end)
     end)
      
      
     if success then
     if success and type(data) == "table" then
         chemCache = data
         chemCache = data
         return data
         return data
     else
     else
         mw.log("Ошибка при загрузке JSON (chem_prototypes.json): " .. data)
         log("Ошибка при загрузке JSON (chem_prototypes.json): " .. tostring(data))
         return {}
         return {}
     end
     end
Строка 47: Строка 56:
-- Функция для перевода 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|link=|32пкс]]", translatedName, id)
         translationCache[id] = {
            name = translatedName,
            image = string.format("[[Файл:%s.png|32px]]", id)
        }
     else
     else
         return id  -- Если перевод не найден, возвращаем исходный ID
         translationCache[id] = {
            name = id, -- Если перевод не найден, возвращаем исходный ID
            image = ""
        }
     end
     end
   
    return translationCache[id]
end
end


Строка 65: Строка 86:
end
end


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


-- Функция для создания ссылки на рецепт, если ингредиент можно приготовить
-- Функция для создания ссылки на строку таблицы
local function createIngredientLink(frame, ingredientId, ingredientName)
local function createLinkToRow(ingredientId, ingredientName)
     local anchor = string.gsub(ingredientId, " ", "_")  -- Заменяем пробелы на подчеркивания
     return string.format("[[#%s|%s]]", ingredientId, ingredientName)
    if isIngredientCookable(ingredientId) then
        return string.format('[[#%s|%s]]', anchor, ingredientName)
    else
        return ingredientName
    end
end
end


Строка 91: Строка 107:
p.fillRecipeTable = function(frame)
p.fillRecipeTable = function(frame)
     local args = frame.args
     local args = frame.args
     local recipeType = args.recipeType or "microwaveRecipes" -- Тип рецептов по умолчанию
     local recipeType = args.recipeType or "microwaveRecipes"
     local templateName = args.template or "RecipeRow" -- Шаблон по умолчанию
     local templateName = args.template or "RecipeRow"


     local out = ""
     local out = ""
Строка 103: Строка 119:
     end
     end
      
      
     -- Создаем таблицу для сортировки
     -- Создаем хэш-таблицу для быстрой проверки результатов рецептов
     local sortedRecipes = {}
     local recipeResultLookup = createRecipeResultLookup(recipes)
   
    -- Перебираем рецепты без сортировки
     for recipeId, recipe in pairs(recipes[recipeType]) do
     for recipeId, recipe in pairs(recipes[recipeType]) do
         table.insert(sortedRecipes, { id = recipeId, data = recipe })
         local templateArgs = {}
    end


    -- Сортировка рецептов с учетом перевода
        -- Определяем resultId для якоря
    if recipeType == "grindableRecipes" then
         local resultId = recipe.result or recipeId  -- Используем result, если он есть, иначе recipeId
         table.sort(sortedRecipes, function(a, b)
            local aName = translateID(frame, a.data.input or "")
            local bName = translateID(frame, b.data.input or "")
            return aName < bName
        end)
    else
        table.sort(sortedRecipes, function(a, b)
            local aName = translateID(frame, a.data.result or "")
            local bName = translateID(frame, b.data.result or "")
            return aName < bName
        end)
    end


    -- Перебираем отсортированные рецепты
        -- Добавляем id в аргументы шаблона (кроме grindableRecipes)
    for _, recipeData in ipairs(sortedRecipes) do
         if recipeType ~= "grindableRecipes" then
         local recipe = recipeData.data
            templateArgs.id = resultId
         local templateArgs = {}
         end


         -- Обработка для microwaveRecipes
         -- Обработка для microwaveRecipes
         if recipeType == "microwaveRecipes" then
         if recipeType == "microwaveRecipes" then
             -- Переводим результат
             -- Переводим результат
             local result = translateID(frame, recipe.result) or "Нет результата"
             local resultData = translateID(frame, recipe.result)
            local result = resultData.name or "Нет результата"
            local resultImage = resultData.image or ""
              
              
             -- Формируем список ингредиентов (solids)
             -- Формируем список ингредиентов (solids)
Строка 138: Строка 145:
             if recipe.solids and type(recipe.solids) == "table" then
             if recipe.solids and type(recipe.solids) == "table" then
                 for solidId, amount in pairs(recipe.solids) do
                 for solidId, amount in pairs(recipe.solids) do
                     local ingredientName = translateID(frame, solidId)
                     local solidData = translateID(frame, solidId)
                     ingredientName = createIngredientLink(frame, solidId, ingredientName)
                     local ingredientName = solidData.name
                     table.insert(solidsList, string.format("%s (%d)", ingredientName, amount))
                    local ingredientImage = solidData.image
                    -- Проверяем, можно ли приготовить ингредиент
                    if recipeResultLookup[solidId] then
                        ingredientName = createLinkToRow(solidId, ingredientName)
                    end
                     table.insert(solidsList, string.format("%s %s (%d)", ingredientName, ingredientImage, amount))
                 end
                 end
             end
             end
Строка 154: Строка 166:
              
              
             -- Формируем аргументы для шаблона
             -- Формируем аргументы для шаблона
             templateArgs = {
             templateArgs.result = result .. " " .. resultImage
                result = result,
            templateArgs.solids = table.concat(solidsList, "</br>") or "Нет ингредиентов"
                solids = table.concat(solidsList, ", ") or "Нет ингредиентов",
            templateArgs.reagents = table.concat(reagentsList, "</br>") or "Нет реагентов"
                reagents = table.concat(reagentsList, ", ") or "Нет реагентов",
            templateArgs.time = recipe.time or "Нет данных"
                time = recipe.time or "Нет данных"
            }


         -- Обработка для sliceableRecipes
         -- Обработка для sliceableRecipes
         elseif recipeType == "sliceableRecipes" then
         elseif recipeType == "sliceableRecipes" then
             local input = translateID(frame, recipe.input) or "Нет входного элемента"
             local inputData = translateID(frame, recipe.input)
             input = createIngredientLink(frame, recipe.input, input)
            local input = inputData.name or "Нет входного элемента"
             local result = translateID(frame, recipe.result) or "Нет результата"
             local inputImage = inputData.image or ""
            -- Проверяем, можно ли приготовить input
            if recipeResultLookup[recipe.input] then
                input = createLinkToRow(recipe.input, input)
            end
 
             local resultData = translateID(frame, recipe.result)
            local result = resultData.name or "Нет результата"
            local resultImage = resultData.image or ""
             local count = recipe.count or "Нет данных"
             local count = recipe.count or "Нет данных"


             templateArgs = {
             templateArgs.input = input .. " " .. inputImage
                input = input,
            templateArgs.result = result .. " " .. resultImage
                result = result,
            templateArgs.count = count
                count = count
            }


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


Строка 187: Строка 209:
             end
             end


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


         -- Обработка для heatableRecipes
         -- Обработка для heatableRecipes
         elseif recipeType == "heatableRecipes" then
         elseif recipeType == "heatableRecipes" then
             local input = translateID(frame, recipe.input) or "Нет входного элемента"
             local inputData = translateID(frame, recipe.input)
             input = createIngredientLink(frame, recipe.input, input)
            local input = inputData.name or "Нет входного элемента"
             local result = translateID(frame, recipe.result) or "Нет результата"
             local inputImage = inputData.image or ""
            -- Проверяем, можно ли приготовить input
            if recipeResultLookup[recipe.input] then
                input = createLinkToRow(recipe.input, input)
            end
 
             local resultData = translateID(frame, recipe.result)
            local result = resultData.name or "Нет результата"
            local resultImage = resultData.image or ""
             local minTemp = recipe.minTemp or "Нет данных"
             local minTemp = recipe.minTemp or "Нет данных"


             templateArgs = {
             templateArgs.input = input .. " " .. inputImage
                input = input,
            templateArgs.result = result .. " " .. resultImage
                result = result,
            templateArgs.minTemp = minTemp
                minTemp = minTemp
            }


         -- Обработка для toolmadeRecipes
         -- Обработка для toolmadeRecipes
         elseif recipeType == "toolmadeRecipes" then
         elseif recipeType == "toolmadeRecipes" then
             local input = translateID(frame, recipe.input) or "Нет входного элемента"
             local inputData = translateID(frame, recipe.input)
             input = createIngredientLink(frame, recipe.input, input)
            local input = inputData.name or "Нет входного элемента"
             local result = translateID(frame, recipe.result) or "Нет результата"
             local inputImage = inputData.image or ""
             local tool = translateID(frame, recipe.tool) or "Нет инструмента"
            -- Проверяем, можно ли приготовить input
            if recipeResultLookup[recipe.input] then
                input = createLinkToRow(recipe.input, input)
            end
 
             local resultData = translateID(frame, recipe.result)
            local result = resultData.name or "Нет результата"
             local resultImage = resultData.image or ""


             templateArgs = {
             local toolData = translateID(frame, recipe.tool)
                input = input,
            local tool = toolData.name or "Нет инструмента"
                result = result,
            local toolImage = toolData.image or ""
                tool = tool
 
            }
            templateArgs.input = input .. " " .. inputImage
            templateArgs.result = result .. " " .. resultImage
            templateArgs.tool = tool .. " " .. toolImage
         end
         end
        -- Добавляем якорь для строки таблицы
        out = out .. string.format('|- id="%s"\n', recipe.id)


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

Текущая версия от 13:11, 11 марта 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] = {
            name = translatedName,
            image = string.format("[[Файл:%s.png|32px]]", id)
        }
    else
        translationCache[id] = {
            name = id,  -- Если перевод не найден, возвращаем исходный ID
            image = ""
        }
    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 = {}

        -- Определяем resultId для якоря
        local resultId = recipe.result or recipeId  -- Используем result, если он есть, иначе recipeId

        -- Добавляем id в аргументы шаблона (кроме grindableRecipes)
        if recipeType ~= "grindableRecipes" then
            templateArgs.id = resultId
        end

        -- Обработка для microwaveRecipes
        if recipeType == "microwaveRecipes" then
            -- Переводим результат
            local resultData = translateID(frame, recipe.result)
            local result = resultData.name or "Нет результата"
            local resultImage = resultData.image or ""
            
            -- Формируем список ингредиентов (solids)
            local solidsList = {}
            if recipe.solids and type(recipe.solids) == "table" then
                for solidId, amount in pairs(recipe.solids) do
                    local solidData = translateID(frame, solidId)
                    local ingredientName = solidData.name
                    local ingredientImage = solidData.image
                    -- Проверяем, можно ли приготовить ингредиент
                    if recipeResultLookup[solidId] then
                        ingredientName = createLinkToRow(solidId, ingredientName)
                    end
                    table.insert(solidsList, string.format("%s %s (%d)", ingredientName, ingredientImage, 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 .. " " .. resultImage
            templateArgs.solids = table.concat(solidsList, "</br>") or "Нет ингредиентов"
            templateArgs.reagents = table.concat(reagentsList, "</br>") or "Нет реагентов"
            templateArgs.time = recipe.time or "Нет данных"

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

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

            templateArgs.input = input .. " " .. inputImage
            templateArgs.result = result .. " " .. resultImage
            templateArgs.count = count

        -- Обработка для grindableRecipes
        elseif recipeType == "grindableRecipes" then
            local inputData = translateID(frame, recipe.input)
            local input = inputData.name or "Нет входного элемента"
            local inputImage = inputData.image 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 .. " " .. inputImage
            templateArgs.result = table.concat(resultList, "</br>") or "Нет результата"

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

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

            templateArgs.input = input .. " " .. inputImage
            templateArgs.result = result .. " " .. resultImage
            templateArgs.minTemp = minTemp

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

            local resultData = translateID(frame, recipe.result)
            local result = resultData.name or "Нет результата"
            local resultImage = resultData.image or ""

            local toolData = translateID(frame, recipe.tool)
            local tool = toolData.name or "Нет инструмента"
            local toolImage = toolData.image or ""

            templateArgs.input = input .. " " .. inputImage
            templateArgs.result = result .. " " .. resultImage
            templateArgs.tool = tool .. " " .. toolImage
        end

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

return p