Module:Perk link

Revision as of 21:22, 4 November 2021 by Admin (talk | contribs) (1 revision imported)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

local perks = mw.loadData('Module:Perks/data')
local yn = require('Module:Yesno')
local hc = require('Module:Paramtest').has_content

local allowed_ranks = {
	[1] = 1,
	[2] = 2,
	[3] = 3,
	[4] = 4,
	[5] = 5,
	[6] = 6,
	['1'] = 1,
	['2'] = 2,
	['3'] = 3,
	['4'] = 4,
	['5'] = 5,
	['6'] = 6
}


local p = {}

function p.main(frame)
	return p._main(frame:getParent().args)
end

function p._main(args)
	local perk = args[1]
	if not hc(perk) then
		return 'Please specify a perk'
	end
	
	perk = mw.text.trim(perk:lower():gsub(' %(perk%)', ''))
	local perkinfo = perks[perk]
	if not perkinfo then
		return args[1]..' is an invalid perk'
	end
	local ret = mw.html.create('')
	local span = ret:tag('span')
	span:addClass('perk-link')
	if yn(args.bg) == false then
		span:addClass('perk-link-nobg')
	end
	
	local img = perkinfo.name
	if hc(args.pic) then
		img = args.pic
	end
	local page = perkinfo.name
	
	span:wikitext(string.format('[[File:%s.png|25x25px|link=%s]]', img, page))
	
	local rank = allowed_ranks[args.rank] or allowed_ranks[args[2]]
	if rank then
		span:tag('span')
				:addClass('perk-link-rank')
				:wikitext(string.format('[[File:Perk rank %s.png|25x25px|link=%s]]', rank, page))
	end
	
	if yn(args.link) ~= false then
		local txt
		if hc(args.txt) then
			txt = args.txt
		else
			local nicename = perkinfo.name:gsub(' %(perk%)', '')
			if hc(args[2]) then
				txt = nicename .. ' ' .. args[2]
			else
				txt = nicename
			end
		end
		
		ret:wikitext(string.format('[[%s|%s]]', page, txt))
	end
	return tostring(ret)
end

return p