URim (обсуждение | вклад) (скрипт (работает ли???)) |
URim (обсуждение | вклад) (Прошлое не сработало) Исправляюю.....) |
||
| Строка 2: | Строка 2: | ||
-- Загрузка данных о рецептах из JSON-файла | -- Загрузка данных о рецептах из 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() | |||
-- Функция для генерации таблицы с рецептами | -- Функция для генерации таблицы с рецептами | ||
| Строка 8: | Строка 21: | ||
local out = "" | local out = "" | ||
for _, recipe in pairs(p.recipes) do | if not p.recipes or not p.recipes.microwaveRecipes then | ||
return "Ошибка: данные о рецептах не загружены." | |||
end | |||
for _, recipe in pairs(p.recipes.microwaveRecipes) do | |||
-- Формируем список ингредиентов (solids) | -- Формируем список ингредиентов (solids) | ||
local solidsList = {} | local solidsList = {} | ||
for solidId, amount in pairs(recipe.solids) do | if recipe.solids and type(recipe.solids) == "table" then | ||
for solidId, amount in pairs(recipe.solids) do | |||
table.insert(solidsList, string.format("%s (%d)", solidId, amount)) | |||
end | |||
end | end | ||
-- Формируем список реагентов (reagents) | -- Формируем список реагентов (reagents) | ||
local reagentsList = {} | local reagentsList = {} | ||
for reagentId, amount in pairs(recipe.reagents) do | if recipe.reagents and type(recipe.reagents) == "table" then | ||
for reagentId, amount in pairs(recipe.reagents) do | |||
table.insert(reagentsList, string.format("%s (%d)", reagentId, amount)) | |||
end | |||
end | end | ||
-- Формируем аргументы для шаблона | -- Формируем аргументы для шаблона | ||
local templateArgs = { | local templateArgs = { | ||
name = recipe.name or "Нет названия", | |||
name = recipe.name | solids = table.concat(solidsList, ", ") or "Нет ингредиентов", | ||
reagents = table.concat(reagentsList, ", ") or "Нет реагентов", | |||
time = recipe.time or "Нет данных", | |||
solids = table.concat(solidsList, ", "), | result = recipe.result or "Нет результата" | ||
reagents = table.concat(reagentsList, ", "), | |||
result = recipe.result | |||
} | } | ||
Версия от 23:11, 4 марта 2025
Для документации этого модуля может быть создана страница Модуль:TableOfRecipes/doc
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
p.recipes = loadRecipes()
-- Функция для генерации таблицы с рецептами
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
-- Формируем список ингредиентов (solids)
local solidsList = {}
if recipe.solids and type(recipe.solids) == "table" then
for solidId, amount in pairs(recipe.solids) do
table.insert(solidsList, string.format("%s (%d)", solidId, amount))
end
end
-- Формируем список реагентов (reagents)
local reagentsList = {}
if recipe.reagents and type(recipe.reagents) == "table" then
for reagentId, amount in pairs(recipe.reagents) do
table.insert(reagentsList, string.format("%s (%d)", reagentId, amount))
end
end
-- Формируем аргументы для шаблона
local templateArgs = {
name = recipe.name or "Нет названия",
solids = table.concat(solidsList, ", ") or "Нет ингредиентов",
reagents = table.concat(reagentsList, ", ") or "Нет реагентов",
time = recipe.time or "Нет данных",
result = recipe.result or "Нет результата"
}
-- Генерация строки таблицы с использованием шаблона
out = out .. frame:expandTemplate{ title = "RecipeRow", args = templateArgs }
end
return out
end
return p