Module:Quest Info Dynamic

From Ragnarok Plus Wiki

Documentation for this module may be created at Module:Quest Info Dynamic/doc

local p = {}

local function getArgs(frame)
	local args = {}
	local function normalize(source)
		for k, v in pairs(source) do
			if type(k) == "string" and v and v ~= "" then
				args[string.lower(k)] = v
			end
		end
	end
	normalize(frame:getParent().args or {})
	normalize(frame.args or {})
	return args
end

local function isBlank(val)
	if not val then return true end
	val = mw.text.trim(val)

	-- Remove known "empty" HTML/wikitext outputs
	val = val:gsub("<p>%s*</p>", "")
	val = val:gsub("<p><br ?/?></p>", "")
	val = val:gsub("<br ?/?>", "")
	val = mw.text.trim(val)

	return val == ""
end

local function makeRow(label, value)
	if not isBlank(value) then
		return string.format(
			'<tr class="skill-info-row"><td class="skill-info-label  skill-info-quest-label">%s</td><td class="skill-info-value">%s</td></tr>',
			label, value
		)
	end
	return ""
end

local function insertRow(out, label, value)
	local row = makeRow(label, value)
	if row ~= "" then
		table.insert(out, row)
	end
end

local function makeSection(title, class)
	return string.format(
		'<tr><th colspan="2" class="skill-info-subheader">%s</th></tr>',
		title
	)
end

local function addCategory(cat)
	if not cat or cat == "" then return "" end
	return string.format("[[Category:%s]]", cat)
end

function p.main(frame)
	local args = getArgs(frame)
	local out = {}

	local questClass = "skill-info-quest-window"
	local pagename = args.pagename or mw.title.getCurrentTitle().text

	if not args.cat then
		if args.window then table.insert(out, "[[Category:Quest Window Quests]]") end
		if args.jobexpreward then table.insert(out, "[[Category:Job Experience Reward]]") end
		if args.baseexpreward then table.insert(out, "[[Category:Base Experience Reward]]") end
		if args.classreq then table.insert(out, addCategory(args.classreq)) end
		if args.classreq2 then table.insert(out, addCategory(args.classreq2)) end
	end

	table.insert(out, '<table class="infobox skill-info-table skill-info-quest">')

	table.insert(out, string.format(
		'<tr><th colspan="2" class="skill-info-header %s">%s</th></tr>',
		questClass, pagename
	))

	-- Requirements section
	table.insert(out, makeSection("Requirements", questClass))
	insertRow(out, "Base Level:", args.levelreq)
	insertRow(out, "Job Level:", args.joblevelreq)

	if args.classreq then
		local classText = string.format("[[%s]]", args.classreq)
		if args.classreq2 then
			classText = classText .. string.format(" or [[%s]]", args.classreq2)
		end
		insertRow(out, "Job Class:", classText)
	end

	insertRow(out, "Skill Requirements:", args.skillreq)
	insertRow(out, "Party:", args.partyreq)
	insertRow(out, "Item(s) (Consumed):", args.itemreq)
	insertRow(out, "Item(s) (Not Consumed):", args.nitemreq)
	insertRow(out, "Zeny:", args.zenyreq)
	insertRow(out, "Hunting:", args.hunting)
	insertRow(out, "Quest Prerequisite(s):", args.questsreq)
	insertRow(out, "Quest Corequisite(s):", args.questscoreq)

	-- Rewards section
	table.insert(out, makeSection("Rewards", questClass))
	insertRow(out, "Base Experience:", args.baseexpreward)
	insertRow(out, "Job Experience:", args.jobexpreward)
	insertRow(out, "Item(s):", args.itemreward)
	insertRow(out, "Quest Reward(s):", args.qreward)

	-- External link row
	if args.external then
		table.insert(out, string.format(
			'<tr><td colspan="2" class="skill-info-center">\'\'\'[%s Event Notice]\'\'\'</td></tr>',
			args.external
		))
	end

	table.insert(out, '</table>')

	return table.concat(out, "\n")
end

return p