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

Материал из МК14 | Space Station 14 Wiki
м (Теперь выдает нормальное название...)
(Вроде должно работать, сделал перевод)
Строка 1: Строка 1:
local p = {}
local p = {}
-- Загрузка модуля для перевода
local entityLookup = require("Module:Entity Lookup")


-- Загрузка данных о рецептах из JSON-файла
-- Загрузка данных о рецептах из JSON-файла
Строка 16: Строка 19:


p.recipes = loadRecipes()
p.recipes = loadRecipes()
-- Функция для перевода ID с использованием Module:Entity Lookup
local function translateID(id)
    return entityLookup.getname(id) or id
end


-- Функция для генерации таблицы с рецептами
-- Функция для генерации таблицы с рецептами
Строка 26: Строка 34:
      
      
     for _, recipe in pairs(p.recipes.microwaveRecipes) do
     for _, recipe in pairs(p.recipes.microwaveRecipes) do
        -- Переводим результат
        local result = translateID(recipe.result) or "Нет результата"
       
         -- Формируем список ингредиентов (solids)
         -- Формируем список ингредиентов (solids)
         local solidsList = {}
         local solidsList = {}
         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
                 table.insert(solidsList, string.format("%s (%d)", solidId, amount))
                local ingredientName = translateID(solidId)
                 table.insert(solidsList, string.format("%s (%d)", ingredientName, amount))
             end
             end
         end
         end
Строка 38: Строка 50:
         if recipe.reagents and type(recipe.reagents) == "table" then
         if recipe.reagents and type(recipe.reagents) == "table" then
             for reagentId, amount in pairs(recipe.reagents) do
             for reagentId, amount in pairs(recipe.reagents) do
                 table.insert(reagentsList, string.format("%s (%d)", reagentId, amount))
                local reagentName = translateID(reagentId)
                 table.insert(reagentsList, string.format("%s (%d)", reagentName, amount))
             end
             end
         end
         end
Строка 44: Строка 57:
         -- Формируем аргументы для шаблона
         -- Формируем аргументы для шаблона
         local templateArgs = {
         local templateArgs = {
             name = recipe.id or "Нет названия",
             result = result, -- Результат становится названием строки
             solids = table.concat(solidsList, ", ") or "Нет ингредиентов",
             solids = table.concat(solidsList, ", ") or "Нет ингредиентов",
             reagents = table.concat(reagentsList, ", ") or "Нет реагентов",
             reagents = table.concat(reagentsList, ", ") or "Нет реагентов",
             time = recipe.time or "Нет данных",
             time = recipe.time or "Нет данных"
            result = recipe.result or "Нет результата"
         }
         }
          
          

Версия от 04:28, 7 марта 2025

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

local p = {}

-- Загрузка модуля для перевода
local entityLookup = require("Module:Entity Lookup")

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

p.recipes = loadRecipes()

-- Функция для перевода ID с использованием Module:Entity Lookup
local function translateID(id)
    return entityLookup.getname(id) or id
end

-- Функция для генерации таблицы с рецептами
p.fillRecipeTable = function(frame)
    local out = ""
    
    if not p.recipes or not p.recipes.microwaveRecipes then
        return "Ошибка: данные о рецептах не загружены."
    end
    
    for _, recipe in pairs(p.recipes.microwaveRecipes) do
        -- Переводим результат
        local result = translateID(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(solidId)
                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 = translateID(reagentId)
                table.insert(reagentsList, string.format("%s (%d)", reagentName, amount))
            end
        end
        
        -- Формируем аргументы для шаблона
        local templateArgs = {
            result = result,  -- Результат становится названием строки
            solids = table.concat(solidsList, ", ") or "Нет ингредиентов",
            reagents = table.concat(reagentsList, ", ") or "Нет реагентов",
            time = recipe.time or "Нет данных"
        }
        
        -- Генерация строки таблицы с использованием шаблона
        out = out .. frame:expandTemplate{ title = "RecipeRow", args = templateArgs }
    end
    
    return out
end

return p