URim (обсуждение | вклад) (Попытка 3) |
URim (обсуждение | вклад) (Попытка 4) |
||
| Строка 1: | Строка 1: | ||
-- Module:TableOfRecipes | |||
local p = {} | local p = {} | ||
-- Функция для загрузки данных о рецептах из JSON-файла | -- Функция для загрузки данных о рецептах из JSON-файла | ||
| Строка 19: | Строка 17: | ||
-- Функция для перевода ID с использованием Module:Entity Lookup | -- Функция для перевода ID с использованием Module:Entity Lookup | ||
local function translateID(id) | local function translateID(frame, id) | ||
return | return frame:callParserFunction{ name = '#invoke', args = { 'Entity_Lookup', 'getname', id } } | ||
end | end | ||
| Строка 36: | Строка 34: | ||
for _, recipe in pairs(recipes.microwaveRecipes) do | for _, recipe in pairs(recipes.microwaveRecipes) do | ||
-- Переводим результат | -- Переводим результат | ||
local result = translateID(recipe.result) or "Нет результата" | local result = translateID(frame, recipe.result) or "Нет результата" | ||
-- Формируем список ингредиентов (solids) | -- Формируем список ингредиентов (solids) | ||
| Строка 42: | Строка 40: | ||
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(solidId) | local ingredientName = translateID(frame, solidId) | ||
table.insert(solidsList, string.format("%s (%d)", ingredientName, amount)) | table.insert(solidsList, string.format("%s (%d)", ingredientName, amount)) | ||
end | end | ||
| Строка 51: | Строка 49: | ||
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 | ||
local reagentName = translateID(reagentId) | local reagentName = translateID(frame, reagentId) | ||
table.insert(reagentsList, string.format("%s (%d)", reagentName, amount)) | table.insert(reagentsList, string.format("%s (%d)", reagentName, amount)) | ||
end | end | ||
Версия от 04:38, 7 марта 2025
Для документации этого модуля может быть создана страница Модуль:TableOfRecipes/doc
-- Module:TableOfRecipes
local p = {}
-- Функция для загрузки данных о рецептах из 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
-- Функция для перевода ID с использованием Module:Entity Lookup
local function translateID(frame, id)
return frame:callParserFunction{ name = '#invoke', args = { 'Entity_Lookup', 'getname', id } }
end
-- Функция для генерации таблицы с рецептами
p.fillRecipeTable = function(frame)
local out = ""
-- Загрузка данных о рецептах
local recipes = loadRecipes()
if not recipes or not recipes.microwaveRecipes then
return "Ошибка: данные о рецептах не загружены."
end
for _, recipe in pairs(recipes.microwaveRecipes) do
-- Переводим результат
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)
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(frame, 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