Module:Command table

来自Minecraft基岩版开发Wiki
[创建 | 历史 | 清除缓存]文档页面
此模块没有文档页面。如果你知道如何使用模块,请创建它。
local p = {}
local function GetCount(args, key, value, argType)
	local i = 0
	if argType == 'cmd' then
		if value == 'any' then
			repeat
				i = i + 1
				key = key + 3
			until args[key] == nil or
				(args[key] ~= value and args[key] ~= '*' and args[key] ~= '~' and args[key] ~= '任意')
		else
			repeat
				i = i + 1
				key = key + 3
			until args[key] == nil or (args[key] ~= value and args[key] ~= '*' and args[key] ~= '~')
		end
	elseif argType == 'tc-result' then
		if value == 'succeed' then
			repeat
				i = i + 1
				key = key + 3
			until args[key] == nil or (args[key] ~= value and args[key] ~= 'yes' and args[key] ~= '成功')
		elseif value == 'fail' then
			repeat
				i = i + 1
				key = key + 3
			until args[key] == nil or (args[key] ~= value and args[key] ~= 'no' and args[key] ~= '失败')
		else
			repeat
				i = i + 1
				key = key + 3
			until args[key] == nil or args[key] ~= value
		end
	end
	if i then
		return i
	end
	return 1
end

local function GetTableChoice(str)
	if str == 'N/A' then
		return nil, 'tc-na', str
	elseif string.match(str, '^style=".*" class="tc%-%w+%-?%w-" %| .*$') then
		return string.match(str, '^style="(.*)" class="(tc%-%w+%-?%w-)" %| (.*)$')
	elseif string.match(str, '^class="tc%-%w+%-?%w-" %| .*$') then
		return nil, string.match(str, '^class="(tc%-%w+%-?%w-)" %| (.*)$')
	end
	return nil, nil, str
end

function p.OutputTable(f)
	local args = require('Module:Arguments').getArgs(f)
	local tableHtml = mw.html.create('table'):addClass('wikitable')
		:tag('tr')
			:tag('th'):wikitext('命令'):done()
			:tag('th'):wikitext('执行结果'):done()
			:tag('th'):wikitext('[[命令方块|成功次数]]增量'):allDone()
	local _cmd = '任意'
	local cmdType = ''
	local cmdCount = 1
	local isCmdChanged = true
	local _situation = ''
	local situationType = ''
	local style = {}
	local resultType
	local _result
	for i, v in ipairs(args) do
		local value = tostring(v)
		if i % 3 == 1 then
			style[1] = nil
			if value == 'any' or value == '任意' then
				if _cmd ~= '任意' then
					_cmd = '任意'
					cmdCount = GetCount(args, i, 'any', 'cmd')
					isCmdChanged = true
				elseif i == 1 then
					cmdCount = GetCount(args, i, 'any', 'cmd')
				end
			elseif _cmd ~= value and value ~= '*' and value ~= '~' then
				style[1], cmdType, _cmd = GetTableChoice(value)
				cmdCount = GetCount(args, i, value, 'cmd')
				isCmdChanged = true
			elseif i == 1 and (value == '*' or value == '~') then
				cmdCount = GetCount(args, i, 'any', 'cmd')
			end
		elseif i % 3 == 2 then
			style[2] = nil
			if value == 'succeed' or value == 'yes' or value == '成功' then
				_situation = '成功'
				situationType = 'tc-yes'
			elseif value == 'fail' or value == 'no' or value == '失败' then
				_situation = '失败'
				situationType = 'tc-no'
			else
				style[2], situationType, _situation = GetTableChoice(value)
			end
		elseif i % 3 == 0 then
			style[3] = nil
			style[3], resultType, _result = GetTableChoice(value)
			local tableRow = tableHtml:tag('tr'):css('text-align', 'center')
			if isCmdChanged then
				isCmdChanged = false
				tableRow:tag('td'):cssText(style[1]):attr({rowspan = cmdCount}):addClass(cmdType):wikitext(_cmd)
			end
			tableRow
				:tag('td'):cssText(style[2]):addClass(situationType):wikitext(_situation):done()
				:tag('td'):cssText(style[3]):addClass(resultType):wikitext(_result):done()
		end
	end
	return tostring(tableHtml)
end

function p.ResultTable(f)
	local args = require('Module:Arguments').getArgs(f)
	local tableHtml = mw.html.create('table'):addClass('wikitable')
		:tag('tr')
			:tag('th'):wikitext('命令'):done()
			:tag('th'):wikitext('触发条件'):done()
			:tag('th'):wikitext('执行结果'):allDone()
	local _cmd = '任意'
	local cmdType = ''
	local cmdCount = 1
	local isCmdChanged = true
	local _situation = ''
	local isSituationSucceeded = false
	local situationType = ''
	local _result = ''
	local isResultChanged = true
	local resultCount = 1
	local resultType = ''
	local style = {}
	for i, v in ipairs(args) do
		local value = tostring(v)
		if i % 3 == 1 then
			style[1] = nil
			if value == 'any' or value == '任意' then
				if _cmd ~= '任意' then
					_cmd = '任意'
					cmdCount = GetCount(args, i, 'any', 'cmd')
					isCmdChanged = true
				elseif i == 1 then
					cmdCount = GetCount(args, i, 'any', 'cmd')
				end
			elseif _cmd ~= value and value ~= '*' and value ~= '~' then
				style[1], cmdType, _cmd = GetTableChoice(value)
				cmdCount = GetCount(args, i, value, 'cmd')
				isCmdChanged = true
			elseif i == 1 and (value == '*' or value == '~') then
				cmdCount = GetCount(args, i, 'any', 'cmd')
			end
		elseif i % 3 == 2 then
			style[2] = nil
			if value == 'succeed' or value == 'yes' or value == '成功' then
				_situation = '执行成功'
				situationType = 'tc-yes'
				isSituationSucceeded = true
			else
				style[2], situationType, _situation = GetTableChoice(value)
			end
		elseif i % 3 == 0 then
			style[3] = nil
			if value == 'succeed' or value == 'yes' or value == '成功' then
				if _result ~= '成功' then
					_result = '成功'
					resultType = 'tc-yes'
					resultCount = GetCount(args, i, 'succeed', 'tc-result')
					isResultChanged = true
				end
			elseif value == 'fail' or value == 'no' or value == '失败' then
				if _result ~= '失败' then
					_result = '失败'
					resultType = 'tc-no'
					resultCount = GetCount(args, i, 'fail', 'tc-result')
					isResultChanged = true
				end
			else
				resultCount = 1
				style[3], resultType, _result = GetTableChoice(value)
				isResultChanged = true
			end
			if isSituationSucceeded and resultType == nil then
				resultType = 'tc-yes'
				isSituationSucceeded = false
			end
			local tableRow = tableHtml:tag('tr'):css('text-align', 'center')
			if isCmdChanged then
				isCmdChanged = false
				tableRow:tag('td'):cssText(style[1]):attr({rowspan = cmdCount}):addClass(cmdType):wikitext(_cmd)
			end
			tableRow:tag('td'):cssText(style[2]):addClass(situationType):wikitext(_situation)
			if isResultChanged then
				isResultChanged = false
				tableRow:tag('td'):cssText(style[3]):attr({rowspan = resultCount}):addClass(resultType):wikitext(_result)
			end
		end
	end
	return tostring(tableHtml)
end
return p