Модуль:Meals Lookup: различия между версиями
Mhamster (обсуждение | вклад) Нет описания правки |
Pok (обсуждение | вклад) мНет описания правки |
||
| (не показано 6 промежуточных версий 2 участников) | |||
| Строка 1: | Строка 1: | ||
local prototypes = mw.loadData("Module:Meals Lookup/data") | local prototypes = mw.loadData("Module:Meals Lookup/data") | ||
local chem = mw.loadData("Module:Chemistry Lookup/data") | local chem = mw.loadData("Module:Chemistry Lookup/data") | ||
local tags = mw.loadData("Module:IanComradeBot/prototypes/entity tags.json/data") | |||
local p = {} | local p = {} | ||
| Строка 8: | Строка 9: | ||
--#region universal | --#region universal | ||
function table.containsv( | function table.containsv(t, value) -- FUCKING LUA | ||
-- containsv = contains value | -- containsv = contains value | ||
for _, v in pairs( | for _, v in pairs(t) do | ||
if v == value then | if v == value then | ||
return true | return true | ||
| Строка 18: | Строка 19: | ||
end | end | ||
function table.containsk( | function table.containsk(t, key) -- FUCKING LUA | ||
-- containsk = contains key | -- containsk = contains key | ||
for k, _ in pairs( | for k, _ in pairs(t) do | ||
if k == key then | if k == key then | ||
return true | return true | ||
| Строка 28: | Строка 29: | ||
end | end | ||
function table.length( | function table.length(t) | ||
local out = 0 | local out = 0 | ||
for _ in pairs( | for _ in pairs(t) do | ||
out = out + 1 | out = out + 1 | ||
end | end | ||
| Строка 44: | Строка 45: | ||
end | end | ||
return false | return false | ||
end | |||
-- Вспомогательная функция для обрезки пробелов | |||
local function trim(s) | |||
return (s:gsub("^%s*(.-)%s*$", "%1")) | |||
end | |||
-- Функция проверки тегов рецепта по параметрам фильтрации | |||
function p.recipePassesTags(frame, recipe) | |||
local productId = recipe["result"] or recipe["id"] | |||
local productTags = {} | |||
for _, entry in ipairs(tags) do | |||
if entry.id == productId then | |||
if entry.Tag and entry.Tag.tags then | |||
productTags = entry.Tag.tags | |||
end | |||
break | |||
end | |||
end | |||
-- Если заданы белый или чёрный списки, выполняем фильтрацию | |||
if frame.args.whiteListTags then | |||
local whitelist = {} | |||
for tag in string.gmatch(frame.args.whiteListTags, "([^,]+)") do | |||
table.insert(whitelist, trim(tag)) | |||
end | |||
local found = false | |||
for _, wtag in ipairs(whitelist) do | |||
if table.containsv(productTags, wtag) then | |||
found = true | |||
break | |||
end | |||
end | |||
if not found then return false end | |||
elseif frame.args.blackListTags then | |||
local blacklist = {} | |||
for tag in string.gmatch(frame.args.blackListTags, "([^,]+)") do | |||
table.insert(blacklist, trim(tag)) | |||
end | |||
for _, btag in ipairs(blacklist) do | |||
if table.containsv(productTags, btag) then | |||
return false | |||
end | |||
end | |||
end | |||
return true | |||
end | |||
-- Функция фильтрации набора рецептов | |||
function p.filterRecipes(recipes, frame) | |||
local filtered = {} | |||
for id, recipe in pairs(recipes) do | |||
if p.recipePassesTags(frame, recipe) then | |||
filtered[id] = recipe | |||
end | |||
end | |||
return filtered | |||
end | end | ||
| Строка 112: | Строка 170: | ||
out[type] = {} | out[type] = {} | ||
for recipeId, recipe in pairs(recipes) do | for recipeId, recipe in pairs(recipes) do | ||
if string. | if string.match(recipeId, str) then | ||
table.insert(out[type], recipe) | table.insert(out[type], recipe) | ||
end | end | ||
| Строка 124: | Строка 182: | ||
local out = {} | local out = {} | ||
for type, recipes in pairs(tabl) do | for type, recipes in pairs(tabl) do | ||
out[type] = {} | if not table.containsk(out, type) then | ||
out[type] = {} | |||
end | |||
for recipeId, recipe in pairs(recipes) do | for recipeId, recipe in pairs(recipes) do | ||
if string. | if not string.match(recipeId, str) then | ||
if not table.containsv(out[type], recipe) then | |||
table.insert(out[type], recipe) | |||
end | |||
end | |||
end | end | ||
end | end | ||
| Строка 220: | Строка 283: | ||
function p.buildmicrowaverecipes(frame) | function p.buildmicrowaverecipes(frame) | ||
local out = "" | local out = "" | ||
local recipes = p.filterRecipes(getrecipesfromtype(frame, "microwaveRecipes"), frame) | |||
for id, _ in pairs(recipes) do | |||
out = out .. frame:preprocess("{{#invoke:Meals Lookup|buildmicrowaverecipebox|" .. id .. "}}") .. "\n" | out = out .. frame:preprocess("{{#invoke:Meals Lookup|buildmicrowaverecipebox|" .. id .. "}}") .. "\n" | ||
end | end | ||
| Строка 249: | Строка 313: | ||
function p.buildslicerecipes(frame) | function p.buildslicerecipes(frame) | ||
local out = "" | local out = "" | ||
local recipes = p.filterRecipes(getrecipesfromtype(frame, "sliceableRecipes"), frame) | |||
for id, _ in pairs(recipes) do | |||
out = out .. frame:preprocess("{{#invoke:Meals Lookup|buildslicerecipebox|" .. id .. "}}") .. "\n" | out = out .. frame:preprocess("{{#invoke:Meals Lookup|buildslicerecipebox|" .. id .. "}}") .. "\n" | ||
end | end | ||
| Строка 277: | Строка 342: | ||
function p.buildgrindrecipes(frame) | function p.buildgrindrecipes(frame) | ||
local out = "" | local out = "" | ||
local recipes = p.filterRecipes(getrecipesfromtype(frame, "grindableRecipes"), frame) | |||
for id, _ in pairs(recipes) do | |||
out = out .. frame:preprocess("{{#invoke:Meals Lookup|buildgrindrecipebox|" .. id .. "}}") .. "\n" | out = out .. frame:preprocess("{{#invoke:Meals Lookup|buildgrindrecipebox|" .. id .. "}}") .. "\n" | ||
end | end | ||
| Строка 306: | Строка 372: | ||
function p.buildheatrecipes(frame) | function p.buildheatrecipes(frame) | ||
local out = "" | local out = "" | ||
local recipes = p.filterRecipes(getrecipesfromtype(frame, "heatableRecipes"), frame) | |||
for id, _ in pairs(recipes) do | |||
out = out .. frame:preprocess("{{#invoke:Meals Lookup|buildheatrecipebox|" .. id .. "}}") .. "\n" | out = out .. frame:preprocess("{{#invoke:Meals Lookup|buildheatrecipebox|" .. id .. "}}") .. "\n" | ||
end | end | ||
| Строка 336: | Строка 403: | ||
function p.buildtoolmaderecipes(frame) | function p.buildtoolmaderecipes(frame) | ||
local out = "" | local out = "" | ||
local recipes = p.filterRecipes(getrecipesfromtype(frame, "toolmadeRecipes"), frame) | |||
for id, _ in pairs(recipes) do | |||
out = out .. frame:preprocess("{{#invoke:Meals Lookup|buildtoolmaderecipebox|" .. id .. "}}") .. "\n" | out = out .. frame:preprocess("{{#invoke:Meals Lookup|buildtoolmaderecipebox|" .. id .. "}}") .. "\n" | ||
end | end | ||
| Строка 377: | Строка 445: | ||
function p.buildixablerecipes(frame) | function p.buildixablerecipes(frame) | ||
local out = "" | local out = "" | ||
local recipes = p.filterRecipes(getrecipesfromtype(frame, "mixableRecipes"), frame) | |||
for id, _ in pairs(recipes) do | |||
out = out .. frame:preprocess("{{#invoke:Meals Lookup|buildmixablerecipebox|" .. id .. "}}") .. "\n" | out = out .. frame:preprocess("{{#invoke:Meals Lookup|buildmixablerecipebox|" .. id .. "}}") .. "\n" | ||
end | end | ||
| Строка 395: | Строка 464: | ||
do | do | ||
for n, recipe in pairs(recipes) do | for n, recipe in pairs(recipes) do | ||
id = recipe["id"] | if p.recipePassesTags(frame, recipe) then | ||
id = recipe["id"] | |||
out = out .. | |||
frame:preprocess("{{#invoke:Meals Lookup|buildmicrowaverecipebox|" .. id .. "}}") .. "\n" | |||
end | |||
end | end | ||
end | end | ||
| Строка 403: | Строка 474: | ||
do | do | ||
for n, recipe in pairs(recipes) do | for n, recipe in pairs(recipes) do | ||
id = recipe["id"] | if p.recipePassesTags(frame, recipe) then | ||
id = recipe["id"] | |||
out = out .. frame:preprocess("{{#invoke:Meals Lookup|buildmixablerecipebox|" .. id .. "}}") .. "\n" | |||
end | |||
end | end | ||
end | end | ||
| Строка 410: | Строка 483: | ||
do | do | ||
for n, recipe in pairs(recipes) do | for n, recipe in pairs(recipes) do | ||
id = recipe["id"] | if p.recipePassesTags(frame, recipe) then | ||
id = recipe["id"] | |||
out = out .. frame:preprocess("{{#invoke:Meals Lookup|buildslicerecipebox|" .. id .. "}}") .. "\n" | |||
end | |||
end | end | ||
end | end | ||
| Строка 417: | Строка 492: | ||
do | do | ||
for n, recipe in pairs(recipes) do | for n, recipe in pairs(recipes) do | ||
id = recipe["id"] | if p.recipePassesTags(frame, recipe) then | ||
id = recipe["id"] | |||
out = out .. frame:preprocess("{{#invoke:Meals Lookup|buildgrindrecipebox|" .. id .. "}}") .. "\n" | |||
end | |||
end | end | ||
end | end | ||
| Строка 424: | Строка 501: | ||
do | do | ||
for n, recipe in pairs(recipes) do | for n, recipe in pairs(recipes) do | ||
id = recipe["id"] | if p.recipePassesTags(frame, recipe) then | ||
id = recipe["id"] | |||
out = out .. frame:preprocess("{{#invoke:Meals Lookup|buildheatrecipebox|" .. id .. "}}") .. "\n" | |||
end | |||
end | end | ||
end | end | ||
| Строка 431: | Строка 510: | ||
do | do | ||
for n, recipe in pairs(recipes) do | for n, recipe in pairs(recipes) do | ||
id = recipe["id"] | if p.recipePassesTags(frame, recipe) then | ||
id = recipe["id"] | |||
out = out .. frame:preprocess("{{#invoke:Meals Lookup|buildtoolmaderecipebox|" .. id .. "}}") .. "\n" | |||
end | |||
end | end | ||
end | end | ||
| Строка 493: | Строка 574: | ||
return out | return out | ||
end | end | ||
--#endregion | --#endregion | ||
| Строка 500: | Строка 580: | ||
-- returns recipes which id is matched by pattern | -- returns recipes which id is matched by pattern | ||
function getrecipesbynamenew(frame, tabl, str) -- should not be | function getrecipesbynamenew(frame, tabl, str) -- should not be invoked | ||
local out = {} | local out = {} | ||
for rtype, recipes in pairs(tabl) do | for rtype, recipes in pairs(tabl) do | ||
for recipeId, recipe in pairs(recipes) do | for recipeId, recipe in pairs(recipes) do | ||
out[rtype] = {} | out[rtype] = out[rtype] or {} | ||
if type(str) == "table" then | if type(str) == "table" then | ||
for _, patt in pairs(str) do | for _, patt in pairs(str) do | ||
if string.match(recipeId, patt) and not table.containsv(recipe) then | if string.match(recipeId, patt) and not table.containsv(recipe) then | ||
| Строка 511: | Строка 591: | ||
end | end | ||
end | end | ||
else | |||
if string.match(recipeId, str) then | if string.match(recipeId, str) then | ||
table.insert(out[rtype], recipe) | table.insert(out[rtype], recipe) | ||
end | end | ||
end | end | ||
end | end | ||
end | end | ||
| Строка 526: | Строка 605: | ||
local out = {} | local out = {} | ||
for type, recipes in pairs(tabl) do | for type, recipes in pairs(tabl) do | ||
out[type] = out[type] or {} | |||
for recipeId, recipe in pairs(recipes) do | for recipeId, recipe in pairs(recipes) do | ||
if not string.match(recipeId, str) then | if not string.match(recipeId, str) then | ||
| Строка 561: | Строка 638: | ||
return out | return out | ||
end | end | ||
--#endregion | --#endregion | ||
return p | return p | ||