缺氧 wiki 编辑团队提示:注册账号并登录后体验更佳,且可通过参数设置定制优化您的浏览体验!

该站点为镜像站点,如果你想帮助这个由玩家志愿编辑的 wiki 站点,请前往原站点参与编辑,
同时欢迎加入编辑讨论群 851803695 与其他编辑者一起参与建设!

全站通知:

模块:食物导航

来自缺氧WIKI_BWIKI_哔哩哔哩
跳到导航 跳到搜索
local p = {}
local fstr = mw.ustring.format -- shortcut for formattig a string
local getArgs = require('Module:Dev/Arguments').getArgs
local utils = require("Module:Utils")


function p.foodEntries()
    local out = {}
    local ks = {}
    local fData = mw.loadData("Module:Data/Food")
    for k, _ in pairs(fData) do
        table.insert(ks, k)
    end
    table.sort(ks)

    for _, k in ipairs(ks) do
        -- mw.logObject(utils.getEntry(k))
        table.insert(out, fstr("{{物品|%s}}", utils.getEntry(k) or k))
    end
    return out
end

-- test by: = p.main()
function p.main()
	return table.concat(p.foodEntries(), "&nbsp;{{*}}&nbsp;<wbr>")
end

-- Returns the table containing formatted food entries of the specified type.
-- Parameters:
--   type_name (string): the type to be filtered from the food list using "tagFilter".
--   to_exclude (table): collection of items IDs to be excluded from the returns.
-- Returns:
--   output_text_table (table): table containing food entries corresponding to the type_name in the form of "{{物品|Item}}".
function p.getCategoryTable(type_name, to_exclude)
    local output_text_table = {}
	local key_table = {}
	local datablock = mw.loadData("Module:Data/Food")
	for key, entry in pairs(datablock) do
		if not to_exclude[key] then
			if type_name == "Edible" and entry.CaloriesPerUnit > 0.0 then
				table.insert(key_table, key)
			elseif type_name == "CookingIngredient" and entry.CaloriesPerUnit == 0.0 then
				table.insert(key_table, key)
			end
		end
	end
	table.sort(key_table)
	for _, key in ipairs(key_table) do
		-- output localized text (if exist) or the original tag (if no translation is avilable)
		table.insert(output_text_table, fstr("{{物品|%s}}", utils.getEntry(key) or key))
    end
    return output_text_table
end

-- Returns the string of the foods with the specified type.
-- Usage:
--   {{#invoke:食物导航|getItemList|ITEM_TYPE|exclude=EXCLUDE_LIST(optional)}}
--   supported ITEM_TYPE: {"Edible", "CookingIngredient"}
-- Parameters:
--   frame (table): the data sent by wiki.
-- Returns:
--   output_string (string): a formatted string of food entries corresponding to the type_name.
-- Test:
--   In the console examine with = p.getItemList({args={"Edible"}})
--   Should retrieve the list of edible foods.
function p.getItemList(frame)
	local output_table = {}
	local exclude_set = {}
	local output_string = ""
	local exclude_list = frame.args.exclude
	local query_type = frame.args[1]
	if query_type ~="Edible" and query_type~="CookingIngredient" then
		error("Unsupported food type for query!")
	end
	if exclude_list ~= nil then
		for entry in string.gmatch(exclude_list, "([^,%s]+)") do
			exclude_set[entry] = true
		end
	end
	output_table = p.getCategoryTable(query_type, exclude_set)
	output_string = table.concat(output_table, "&nbsp;{{*}}&nbsp;<wbr>")
	return output_string
end

return p