本站文本内容除另有声明外,转载时均必须注明出处,并遵守CC BY-NC-SA 3.0协议。(转载须知
本站是中文Minecraft Wiki的镜像站,与Mojang Studios、Weird Gloop没有从属关系。(免责声明

全站通知:

模块:Experience

来自我的世界地下城WIKI_BWIKI_哔哩哔哩
跳到导航 跳到搜索

此模块用于实现{{Experience}}。 它不接受任何直接传递的参数,仅接受父参数,因此建议不要直接从模板中调用它。

local p = {}

local i18n = {
	error_exp_arg_not_a_number = "模块:Experience: %s不是一个数字", -- %1 = the description of the parameter
	error_exp_arg_too_small = "模块:Experience: %s小于-32768(int16的最小值)", -- %1 = the description of the parameter
	error_exp_arg_too_large = "模块:Experience: %s大于32767(int16的最大值)", -- %1 = the description of the parameter
	arg_desc_min_exp = "最小经验值", -- %1 = the description of the parameter
	arg_desc_max_exp = "最大经验值", -- %1 = the description of the parameter
	error_exp_min_gt_exp_max = "Module:Experience: %s (%d) > %s (%d)", -- %1 = min exp description, %2 = min exp, %3 = max exp description, %4 = max exp
	link = "经验#经验球"
}

local EXP_VALUES = {
	{"xp-2", -32768, 2},
	{"xp-6", 3, 6},
	{"xp-16", 7, 16},
	{"xp-36", 17, 36},
	{"xp-72", 37, 72},
	{"xp-148", 73, 148},
	{"xp-306", 149, 306},
	{"xp-616", 307, 616},
	{"xp-1236", 617, 1236},
	{"xp-2476", 1237, 2476},
	{"xp-32767", 2477, 32767}
}

local function validate_xp_arg(arg, desc)
	local arg_n = assert(tonumber(arg), i18n.error_exp_arg_not_a_number:format(desc))
	assert(arg_n >= -32768, i18n.error_exp_arg_too_small:format(desc))
	assert(arg_n <= 32767, i18n.error_exp_arg_too_large:format(desc))
	return arg_n
end

local function produce_exp_sprite(class, is_first)
	local sprite = mw.html.create('span'):addClass("pixel-image")
	if is_first then
		sprite:addClass("animated-active")
	end
	sprite:css({
		["padding-left"] = "19px",
		["background-size"] = "16px 16px",
		["background-position"] = "left center",
		["background-repeat"] = "no-repeat",
	}):addClass(class)
	return sprite
end

local function produce_exp_anim(exp_min, exp_max)
	local wrap = mw.html.create("span"):addClass("animated")
	
	local lower_bound = 1
	local upper_bound = #EXP_VALUES
	for i, v in ipairs(EXP_VALUES) do
		if exp_min >= v[2] and exp_min <= v[3] then
			lower_bound = i
		end
		if exp_max >= v[2] and exp_max <= v[3] then
			upper_bound = i
		end	
	end
	--mw.log(lower_bound)
	--mw.log(upper_bound)
	
	local first = true
	for i = lower_bound, upper_bound do
		if first then
			wrap:node(produce_exp_sprite(EXP_VALUES[i][1], true))
			first = false
		else
			wrap:node(produce_exp_sprite(EXP_VALUES[i][1]))
		end
	end
	
	return tostring(wrap)
end

function p.main(exp_min, exp_max)
	local anim = produce_exp_anim(exp_min, exp_max)
	local text
	if exp_min == exp_max then
		text = exp_min
	else
		text = exp_min .. "–" .. exp_max
	end
	return text .. ' ' .. anim .. '[[经验值]]'
end

function p.exp_range(f)
	local args = require("Module:ProcessArgs").merge(true)
	local exp_min = validate_xp_arg(args[1], i18n.arg_desc_min_exp)
	local exp_max = validate_xp_arg(args[2], i18n.arg_desc_max_exp)
	
	assert(exp_max >= exp_min, i18n.error_exp_min_gt_exp_max:format(
		i18n.arg_desc_min_exp,
		exp_min,
		i18n.arg_desc_max_exp,
		exp_max
	))

	return p.main(exp_min, exp_max)
end

return p