Module:DropsLine

From RuneScape Classic Wiki
Jump to: navigation, search

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

local params = require('Module:Paramtest')
local lang = mw.language.getContentLanguage()

local p = {}

local rarityClasses = {
	always = 'table-bg-blue',
	common = 'table-bg-green',
	uncommon = 'table-bg-yellow',
	rare = 'table-bg-orange',
	['very rare'] = 'table-bg-red',
	['extremely rare'] = 'table-bg-purple'
}

local raritySorts = {
	always = 1,
	common = 2,
	uncommon = 3,
	rare = 4,
	['very rare'] = 5,
	['extremely rare'] = 6
}

function p.main(frame)
	local args = frame:getParent().args
	return p._main(args.Name, args.Quantity, args.Rarity, args.RarityNotes)
end

function rateToRarity(num)
	if num == 1 then
		return 'always'
	elseif num > 1/25 then
		return 'common'
	elseif num > 1/100 then
		return 'uncommon'
	elseif num > 1/1000 then
		return 'rare'
	elseif num > 1/10000 then
		return 'very rare'
	else
		return 'extremely rare'
	end
end

function p.gcd(a, b)
  if b == 0 then
    return a
  else
    return p.gcd(b, a % b)
  end
end

function p._main(name, quantity, rarity, rarity_notes)
	local rarity_sort = 7
	local rarity_text = 'Unknown'
	local rarity_class = ''
	if rarity ~= nil then
		if raritySorts[rarity:lower()] then
			rarity_sort = raritySorts[rarity:lower()]
			rarity_text = params.ucflc(rarity)
			rarity_class = rarityClasses[rarity:lower()]
		else
			local rv1, rv2 = string.match(rarity, '([%d%.]+)/([%d%.]+)')
			if rv1 and rv2 then
				local rarity_value = rv1 / rv2
				local rarity_level = rateToRarity(rarity_value)
				rarity_text = string.format("%s <small>(~%s/%s)</small>", params.ucflc(rarity_level), lang:formatNum(tonumber(rv1)), lang:formatNum(tonumber(rv2)))
				rarity_class = rarityClasses[rarity_level]
				rarity_sort = raritySorts[rarity_level]
			end
		end
	end

	rarity_text_final = string.format('<span style="display:none;">%d</span>%s%s', rarity_sort, rarity_text, (rarity_notes or ''))
	local quantity_text = (quantity or "''Unknown''"):gsub("-", "–")
	local ret = mw.html.create('tr')
		:tag('td')
			:wikitext(string.format('[[File:%s.png|link=%s]]', name, name))
		:done()
		:tag('td')
			:wikitext(string.format('[[%s]]', name))
		:done()
		:tag('td')
			:wikitext(quantity_text)
		:done()
		:tag('td')
			:wikitext(rarity_text_final)
			:addClass(rarity_class)
		:done()
	mw.smw.subobject({
		['Dropped item'] = name,
		['Drop from'] = mw.title.getCurrentTitle().text,
		['Drop Quantity'] = quantity,
		['Rarity'] = rarity_text,
		['Rarity Notes'] = rarity_notes
	})
	return ret
end

function p.herb(frame)
	local args = frame:getParent().args
	local quantity = args.Quantity or '1'
	local rarity = args[1] or '1/1'
	local rv1, rv2 = string.match(rarity, '([%d%.]+)/([%d%.]+)')
	local herbLines = {}
	local herbData = {
		{'Guam leaf', 32},
		{'Marrentill', 24},
		{'Tarromin', 18},
		{'Harralander', 14},
		{'Ranarr Weed', 11},
		{'Irit Leaf', 8},
		{'Avantoe', 6},
		{'Kwuarm', 5},
		{'Cadantine', 4},
		{'Dwarf Weed', 3}
	}
	for _, v in pairs(herbData) do
		local numer = v[2] * rv1
		local denom = 125 * rv2
		local gcd = p.gcd(numer, denom)
		numer = numer / gcd
		denom = denom / gcd
		local fraction = string.format("%d/%d", numer, denom)
		local line = p._main("Herb (" .. v[1] .. ")", quantity, fraction, '')
		table.insert(herbLines, tostring(line))
	end
	return table.concat(herbLines)
end

return p