Module:Dump drop info

From WIDEVERSE Wiki
Jump to navigation Jump to search

Documentation for this module may be created at Module:Dump drop info/doc

local p = {}
local price = require('Module:Exchange')._price
local yn = require('Module:Yesno')
local lang = mw.getContentLanguage()
local rarities = {
	always = { 'table-bg-blue', 1 },
	common = { 'table-bg-green', 16 },
	uncommon = { 'table-bg-yellow', 64 },
	rare = { 'table-bg-orange', 256 },
	['very rare'] = { 'table-bg-red', 1024 },
	random = { 'table-bg-pink', 4096 },
	varies = { 'table-bg-pink', 4096 },
	_default = { 'table-bg-grey', 65536 }
}

-- arbitrary numbers
local rarities2 = {
	{ 1, 'table-bg-blue' },
	{ 1/16, 'table-bg-green' },
	{ 1/64, 'table-bg-yellow' },
	{ 1/256, 'table-bg-orange' },
	{ 1/1024, 'table-bg-red' }
}

local nogemw = {
	'clue scroll', 'starved ancient effigy', 'rare drop table', 'warpriest', 'court summons'
}

function expr(t)
	local err, val = pcall(mw.ext.ParserFunctions.expr, t)
	if err then
		return tonumber(val)
	else
		return false
	end
end

function get_rarity_class(val)
	for i,v in ipairs(rarities2) do
		curr = v
		if val >= v[1] then
			break
		end
	end
	return curr[2]
end
function gep(data)
	local i = data["Dropped item text"]
	local pr = 0
	if i == 'Coins' then
		p = 1
	--  elseif data.gemw == false then
	--      p = 0
	else
		local gemw = true
		for _,v in ipairs(nogemw) do
			if string.match(string.lower(i), v) then
				gemw = false
				break
			end
		end
		if gemw then
			local p_good, p_price = pcall(price, i)
			if p_good then
				pr = p_price
			end
		end
	end
	if pr == 0 then return 0 end
	local q_min= data["Quantity Low"]
	local q_max = data["Quantity High"]
	if q_min == q_max then
		return lang:formatNum(pr * q_min)
	end
	return string.format('%s–%s', lang:formatNum(q_min*pr), lang:formatNum(q_max*pr))
end
function make_row(data, opts)
	local tr = mw.html.create('tr')
	if opts.nomerge then
		tr:tag('td')
			:wikitext(opts.mob)
	elseif opts.i == 1 then
		tr	:tag('th')
				:wikitext(opts.mob)
				:attr('rowspan', opts.num)
	end
	
	local rarity, rare_class, rare_sort, rarity_value
	rarity = data["Rarity"]
	if rarities[rarity:lower()] then
		rare_class,rarity_sort = unpack(rarities[rarity:lower()])
	else
		rarity_value = expr(rarity)
		if rarity_value == false then
			rare_class, rare_sort = unpack(rarities._default)
		else
			rare_sort = 1/ rarity_value
			rare_class = get_rarity_class(rarity_value)
		end
	end
	
	local name = data["Dropped item text"]
	local qty = data["Drop Quantity"]
	
	tr	:tag('td')
			:wikitext('[['..name..']]')
		:done()
		:tag('td')
			:wikitext(qty)
		:done()
		:tag('td')
			:wikitext(rarity)
			:addClass(rare_class)
			:attr('data-sort-value', rare_sort)
		:done()
		:tag('td')
			:wikitext(gep(data))
		:done()
	return tostring(tr)
end

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

function p._main(args)
	local mob = args[1]
	local q = {
		"[[-Has subobject::"..mob.."]]",
		"[[Drop from::+]]",
		"?Dropped item text",
		"?Drop from#-",
		-- "?Name Notes",
		"?Drop Quantity",
		"?Quantity Low",
		"?Quantity High",
		"?Rarity",
		-- "?Rarity Notes",
		-- "?RarityNote Ref",
		limit = args.limit or 100,
		sort = args.sort,
		order = args.order
	}
	local data = mw.smw.ask(q)
	local num = 1
	local ret = {}
	local nomerge = yn(tostring(args.nomerge))
	if type(data) == 'table' then
		num = #data
		for i,v in ipairs(data) do
			table.insert(ret, make_row(v, {i=i, mob=v["Drop from"], num=num, nomerge=nomerge}))
		end
	else
		return "An error occurred."
	end
	
	return table.concat(ret, '\n')
end

function p.header()
	local header = mw.html.create('tr')
	header:tag('th')
			:wikitext('Monster')
		:done()
			:tag('th')
			:wikitext('Item')
		:done()
			:tag('th')
			:wikitext('Quantity')
		:done()
			:tag('th')
			:wikitext('Rarity')
		:done()
			:tag('th')
			:wikitext('GEP')
		:done()
	return '<table class="wikitable sortable align-center-2 align-center-3 align-right-4">' .. tostring(header)
end

return p