Module:CharacterSupportSkill

From Granblue Fantasy Wiki
Revision as of 08:38, 18 April 2019 by AdlaiT (talk | contribs) (Protected "Module:CharacterSupportSkill" ([Edit=Allow only autoconfirmed users] (indefinite) [Move=Allow only autoconfirmed users] (indefinite)))
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Documentation for this module may be created at Module:CharacterSupportSkill/doc

local p = {}

local function stripRefTags(wikitext)
	local w = wikitext:gsub("<ref%s[^>]*/>", "")
	w = w:gsub("<ref[^>]*>.-</ref>", "")
	return w
end

function findArg(needle, haystack)
	local pattern = '|'..needle..'='
	
	local startIndex = haystack:find(pattern, 0, true)
	if startIndex == nil then
		return 'Not Found'
	end
	startIndex = startIndex + pattern:len()
	
	local endIndex = haystack:find('\n|', startIndex, true)
	endIndex = endIndex - 1
	
	local result = haystack:sub(startIndex,endIndex)
	return mw.text.trim(result)
end

function p.renderSupportSkill(frame)
	mw.log('render start: ' .. os.clock())
	local template = frame.args.template
	local characterName = frame.args.characterName
	local skillIndex = frame.args.skillIndex
	skillIndex = (skillIndex == "1") and "" or skillIndex
	local additionalArgs = frame.args.additionalArgs

	mw.log('load data: ' .. os.clock())
	local title = mw.title.new(characterName)
	local rawPage = title:getContent()
	
	mw.log('find args:' .. os.clock())
	local name, desc
	if skillIndex:lower() == "emp" then
		name = 'Extended Mastery Support Skill'
		desc = findArg('sa_emp_desc', rawPage)
	else
		local nameArg = 'sa'..skillIndex..'_name'
		local descArg = 'sa'..skillIndex..'_desc'
		name = findArg(nameArg, rawPage)
		desc = findArg(descArg, rawPage)
	end

	mw.log('expandTemplate')
	local args = {}
	args['link'] = characterName
	args['name'] = name
	args['desc'] = frame:preprocess(stripRefTags(desc))
	args['emp']  = skillIndex:lower() == "emp"

	-- Pass `frame.args.additionalArgs` to `args`
	if additionalArgs ~= nil then
		for arg in (additionalArgs):gmatch('[^;]+') do
			local i = arg:find('=')
			args[arg:sub(0, i-1)] = arg:sub(i+1)
		end
	end

	mw.log('done: ' .. os.clock())
	return frame:expandTemplate{ title = template, args = args }
end

function p.testRender()
	local frame = mw.getCurrentFrame()
	frame.args.template = 'Template:CharacterSupportSkill/Template'
	frame.args.characterName = 'Threo'
	frame.args.skillIndex = '2'
	frame.args.additionalArgs = 'large=false;noicon=false'
	return p.renderSupportSkill(frame)
end

function p.testRenderEMP()
	local frame = mw.getCurrentFrame()
	frame.args.template = 'Template:CharacterSupportSkill/Template'
	frame.args.characterName = 'Threo'
	frame.args.skillIndex = 'eMp'
	frame.args.additionalArgs = 'large=false;noicon=false'
	return p.renderSupportSkill(frame)
end

return p