打开/关闭菜单
281
2939
143
1.6万
星露谷物语扩展百科
打开/关闭外观设置菜单
打开/关闭个人菜单
未登录
未登录用户的IP地址会在进行任意编辑后公开展示。

模块:Gift Cooking Exception

来自星露谷物语扩展百科
[ 创建 | 刷新 ]文档页面
当前模块文档缺失,需要扩充。
--[[
	Module:Gift Cooking Exception —— 星露谷 SVE 料理喜好表(例外情况)
	配合 Template:Gift Cooking Exception 使用:
		{{Gift Cooking Exception|索菲亚|维克多}}  中英文名均可、可叠加
		{{Gift Cooking Exception|main}}           渲染数据文件中的全部角色

	渲染范围: 分类 -7(料理)物品 + 写入通用清单的 5 个烹饪食物 + 格兰普顿香橙鸡。
	渲染逻辑: 
	  1. 每个栏目只列出该角色与“普遍反应”不同的物品(例外);
	     普遍反应 = 通用清单内的口味: 料理默认为“喜欢”; 香橙鸡不属于料理分类但为通用“喜欢”。
	  2. 若角色个人清单里有整类 -7 条目, 且类别反应与“料理=喜欢”不同,
	     则在该类别栏目显示「所有菜肴」链接(链至“烹饪”页), 并附「除了……」,
	     除了后为不受该类别反应约束的料理。
	数据文件后续维护(增删条目/改名字)会自动跟随: 本模块运行时从数据文件采集名称,
	数据文件中不存在的物品会被自动跳过。
	--]]

local p = {}

-- wiki 的依赖数据模块
local DATA_PAGE = 'Module:Gifts/Data'

-- 向 {{Name}} 传的名字:true = 英文名(数据文件的 e 字段),false = 中文名(n 字段)
local USE_ENGLISH_NAME = false

local PREFIX = 'FlashShifter.StardewValleyExpandedCP_'
local CAT_COOKING = '-7'

-- 物品全集(SVE 26 道料理 + 香橙鸡)。数据文件中不存在某条目时自动跳过。
local DISH_IDS = {
	'Baked_Berry_Oatmeal', 'Big_Bark_Burger', 'Flower_Cookie', 'Glazed_Butterfish',
	'Mixed_Berry_Pie', 'Seaweed_Salad', 'Birch_Syrup', 'Candy', 'Chocolate_Truffle_Bar',
	'Vegan_Cone', 'Ice_Cream_Sundae', 'Fish_Dumpling', 'Gingerbread_Man', 'Ramen',
	'Baked_Potato', 'Grilled_Cheese_Sandwich', 'Pineapple_Custard_Crepe',
	'Nectarine_Fruit_Bread', 'Glazed_Pear', 'Stuffed_Persimmon', 'Cheese_Charcuterie',
	'Mushroom_Berry_Rice', 'Void_Delight', 'Void_Salmon_Sushi', 'Frog_Legs',
	'Prismatic_Pop', 'Grampleton_Orange_Chicken',
}

local data
local items = {}      -- { id, name, cooking }
local npcKeys = {}

local function harvest()
	data = mw.loadData(DATA_PAGE)
	local found = {}
	for _, short in ipairs(DISH_IDS) do
		found[short] = false
	end
	local function scan(list)
		if type(list) ~= 'table' then return end
		for _, e in ipairs(list) do
			if type(e) == 'table' and type(e.t) == 'string'
				and e.t:sub(1, #PREFIX) == PREFIX then
				local short = e.t:sub(#PREFIX + 1)
				if found[short] == false and e.n then
					found[short] = {
						name = e.n,
						en = e.e,
						cooking = short ~= 'Grampleton_Orange_Chicken',
					}
				end
			end
		end
	end
	local u = data.universal
	for _, group in ipairs({ u.love, u.like, u.neutral, u.dislike, u.hate }) do
		scan(group)
	end
	for key, npc in pairs(data.npcs) do
		npcKeys[#npcKeys + 1] = key
		for _, group in ipairs({ npc.love, npc.like, npc.neutral, npc.dislike, npc.hate }) do
			scan(group)
		end
	end
	table.sort(npcKeys)
	for _, short in ipairs(DISH_IDS) do
		if found[short] then
			items[#items + 1] = {
				id = PREFIX .. short,
				name = found[short].name,
				en = found[short].en,
				cooking = found[short].cooking,
			}
		end
	end
end

local function itemIn(list, item)
	if type(list) ~= 'table' then return false end
	for _, e in ipairs(list) do
		if type(e) == 'table' and e.t == item.id then return true end
	end
	return false
end

local function catIn(list, item)
	if not item.cooking or type(list) ~= 'table' then return false end
	for _, e in ipairs(list) do
		if type(e) == 'table' and e.l == 'c' and e.t == CAT_COOKING then return true end
	end
	return false
end

local function hasCat(list)
	if type(list) ~= 'table' then return false end
	for _, e in ipairs(list) do
		if type(e) == 'table' and e.l == 'c' and e.t == CAT_COOKING then return true end
	end
	return false
end

local function universalItemTaste(item)
	local u = data.universal
	if itemIn(u.love, item) then return 'love' end
	if itemIn(u.hate, item) then return 'hate' end
	if itemIn(u.like, item) then return 'like' end
	if itemIn(u.dislike, item) then return 'dislike' end
	if itemIn(u.neutral, item) then return 'neutral' end
	return nil
end

local function tasteOf(npc, item)
	local utItem = universalItemTaste(item)
	local ut = utItem
	if ut == nil and item.cooking then ut = 'like' end   -- 原版规则:料理(-7)= 通用喜欢
	local useCat = (utItem == nil)                        -- 个人类别条目仅在无通用物品级口味时参与
	if itemIn(npc.love, item) or (useCat and catIn(npc.love, item)) then return 'love' end
	if itemIn(npc.hate, item) or (useCat and catIn(npc.hate, item)) then return 'hate' end
	if itemIn(npc.like, item) or (useCat and catIn(npc.like, item)) then return 'like' end
	if itemIn(npc.dislike, item) or (useCat and catIn(npc.dislike, item)) then return 'dislike' end
	if itemIn(npc.neutral, item) or (useCat and catIn(npc.neutral, item)) then return 'neutral' end
	return ut or 'neutral'
end

-- 普遍反应:通用物品级口味,料理默认“喜欢”,其余默认“一般”
local function baseline(item)
	local ut = universalItemTaste(item)
	if ut then return ut end
	if item.cooking then return 'like' end
	return 'neutral'
end

local BUCKET_ORDER = { 'love', 'like', 'neutral', 'dislike', 'hate' }

local function link(frame, item)
	local label = USE_ENGLISH_NAME and (item.en or item.name) or item.name
	return frame:expandTemplate{ title = 'Name', args = { label } }
end

function p.main(frame)
	if not data then harvest() end
	local args = frame:getParent().args or frame.args
	local requested = {}
	local i = 1
	while args[i] do
		requested[#requested + 1] = mw.text.trim(tostring(args[i]))
		i = i + 1
	end
	local keys = {}
	if #requested == 0 then
		for _, k in ipairs(npcKeys) do
			keys[#keys + 1] = k
		end
	else
		local aliases = data.aliases or {}
		for _, name in ipairs(requested) do
			if name ~= '' then
				local key = aliases[name] or (data.npcs[name] and name) or nil
				keys[#keys + 1] = key or ('!' .. name)
			end
		end
	end

	local t = {}
	t[#t + 1] = '{|class="wikitable sortable mw-collapsible mw-collapsed" style="width: 90%; min-width: 500px;"'
	t[#t + 1] = '! style="min-width: 90px" | 村民'
	t[#t + 1] = '! style="width: 25%;" | 最爱'
	t[#t + 1] = '! style="width: 15%;" | 喜欢'
	t[#t + 1] = '! style="width: 15%;" | 一般'
	t[#t + 1] = '! style="width: 30%;" | 不喜欢'
	t[#t + 1] = '! style="width: 15%;" | 讨厌'
	t[#t + 1] = '|-'

	for _, key in ipairs(keys) do
		if key:sub(1, 1) == '!' then
			t[#t + 1] = '|-'
			t[#t + 1] = '|class="error"|未知角色:' .. key:sub(2)
			for _ = 1, 5 do t[#t + 1] = '|' end
		else
			local npc = data.npcs[key]

			-- 计算每个物品的最终反应与普遍反应
			local taste = {}
			for _, item in ipairs(items) do
				taste[item.id] = tasteOf(npc, item)
			end

			-- 个人清单中的整类 -7 条目
			local catBucket = nil
			for _, b in ipairs(BUCKET_ORDER) do
				if hasCat(npc[b]) then catBucket = b break end
			end
			if catBucket == 'like' then catBucket = nil end

			local out = { love = {}, like = {}, neutral = {}, dislike = {}, hate = {} }
			for _, item in ipairs(items) do
				local tt = taste[item.id]
				if catBucket and item.cooking and tt == catBucket then
					-- 由“所有菜肴”覆盖,不单独列出
				elseif tt ~= baseline(item) then
					out[tt][#out[tt] + 1] = link(frame, item)
				end
			end

			if catBucket then
				local except = {}
				for _, item in ipairs(items) do
					if item.cooking and taste[item.id] ~= catBucket then
						except[#except + 1] = link(frame, item)
					end
				end
				local txt = '[[烹饪|所有菜肴]]'
				if #except > 0 then
					txt = txt .. '\n除了' .. table.concat(except, '')
				end
				table.insert(out[catBucket], 1, txt)
			end

			t[#t + 1] = '|-'
			t[#t + 1] = '|[[' .. npc.name .. ']]'
			t[#t + 1] = '|<!--最爱-->' .. table.concat(out.love, '')
			t[#t + 1] = '|<!--喜欢-->' .. table.concat(out.like, '')
			t[#t + 1] = '|<!--一般-->' .. table.concat(out.neutral, '')
			t[#t + 1] = '|<!--不喜欢-->' .. table.concat(out.dislike, '')
			t[#t + 1] = '|<!--讨厌-->' .. table.concat(out.hate, '')
		end
	end
	t[#t + 1] = '|}'
	return table.concat(t, '\n')
end

return p