Module:THGem

From the RuneScape Wiki, the wiki for all things RuneScape
Jump to: navigation, search
Module documentation
This documentation is transcluded from Template:No documentation/doc. [edit] [history] [purge]
This module does not have any documentation. Please consider adding documentation at Module:THGem/doc. [edit]
Module:THGem's function main is invoked by Template:THGem.
Module:THGem requires Module:Coins image.

-- <pre>
-- CSS located at [[MediaWiki:Common.less/thgem.less]]
local p = {}
local coins_image = require('Module:Coins image')
local rarity_gems = {
	common = 'thgem-common',
	['fairly-common'] = 'thgem-fairly-common',
	uncommon = 'thgem-uncommon',
	rare = 'thgem-rare',
	['very-rare'] = 'thgem-very-rare',
	['ultra-rare'] = 'thgem-ultra-rare'
}

local rarity_map = {
	common = 'common',
	['fairly common'] = 'fairly-common',
	['fairly-common'] = 'fairly-common',
	uncommon = 'uncommon',
	rare = 'rare',
	['very rare'] = 'very-rare',
	['very-rare'] = 'very-rare',
	['ultra-rare'] = 'ultra-rare',
	['ultra rare'] = 'ultra-rare'
}

function p.main(frame)
	local rarity,item,qty,qtyraw,item_page,image
	local args = frame:getParent().args

	rarity = string.lower(args[1] or '')
	local rarity_gem = rarity_gems[rarity_map[rarity]] or 'thgem-none'
	item = args[2]
	qty = args[3] or 'no'
	-- item_page= args[4] or item
	if qty ~= 'no' then
		qty = qty:upper():gsub('[^%dKM]','')
		qtyraw = qty:gsub('K','000'):gsub('M','000000')
		-- Use raw just in case (e.g. 20K instead of 20000)
		qty = qtyraw
		-- Look for 6+ digits
		if qty:find('%d%d%d%d%d%d') then
			-- replace millions first (10m+)
			qty = qtyraw:gsub('^(%d%d%d?%d?)%d%d%d%d%d%d$','%1M')
			-- replace thousands next (100k to 9999k)
			-- this is ignored if millions are replaced
			qty = qty:gsub('^(%d%d%d%d?)%d%d%d$','%1K')
		end
	end
	if item:lower() == 'coins' then
		image = coins_image(qtyraw)
	else
		image = args['img'] or item..'.png'
	end
	local ret = mw.html.create('div')
		:addClass('thgem')
		:addClass(rarity_gem)
		:addClass('inventory-image')

	-- Item container
	    :tag('span')
	    	:addClass('thgem-image')
	    	:wikitext(string.format('[[File:%s|%s|link=%s]]',image,(args['dim'] or ''),item))
		:done()
	if qty ~= 'no' then
		qty = qty:upper():gsub('[^%dKM]','')
		-- Look for 6+ digits
		if qty:find('%d%d%d%d%d%d') then
			-- replace millions first (10m+)
			qty = qty:gsub('^(%d%d%d?%d?)%d%d%d%d%d%d$','%1M')
			-- replace thousands next (100k to 9999k)
			-- this is ignored if millions are replaced
			qty = qty:gsub('^(%d%d%d%d?)%d%d%d$','%1K')
		end
		local font_color
		if qty:find('K') then
			font_color = '#FFFFFF'
		elseif qty:find('M') then
			font_color = '#01FF80'
		else
			font_color = '#FFFF00'
		end
		ret:tag('span')
			:addClass('thgem-number')
			:css({ color = font_color })
			:wikitext(qty)
		:done()
	end

	return tostring(ret)
end

return p