FANDOM


-- <pre>
local p = {}
 
local params = require('Module:Paramtest')
local lang = mw.language.getContentLanguage()
local commas = function (n) return lang:formatNum(n) end
 
local ref = require('Module:Reftag')
 
--bg, txt, sort
local chances = {
	always = {'#AFEEEE', '#000000', 1},
	common = {'#56E156', '#000000', 2},
	uncommon = {'#FFED4C', '#000000', 3},
	rare = {'#FF863C', '#000000', 4},
	['very rare'] = {'#FF6262', '#000000', 5},
	random = {'#FFA3FF', '#000000', 6},
	varies = {'#FFA3FF', '#000000', 6},
	discontinued = {'#DBFF4C', '#000000', 7}
}
 
function p.main(frame)
	local args = frame:getParent().args
	-- Params and defaults
	local name,namenotes,
		quantity,quantitynotes,
		chance,chancenotes = params.defaults{
					{args.Name,'Item'},
					{args.Namenotes,''},
					{args.Quantity,'unknown'},
					{args.Quantitynotes,''},
					{args.Chance,'unknown'},
					{args.Chancenotes,''}
				}
	local altname = params.default_to(args.Alt,name)
	local chancenotes = (args.Chancenotes or args.chancenotes) or ''
	chance = params.ucflc(chance)
	quantity = quantity:lower()
	local price
	local alt = false
 
 
	-- Use 'File:<name>.png' if no image param
	-- Use 'File:<image>' if image param; image param will include extension
	-- Special catch for coins
	local image,image_n
	if name:lower() == 'coins' then
		image_n = coins_image(quantity)
	else
		image_n = params.default_to(args.Image, name .. '.png')
	end
	if image_n:lower() == 'no' or params.is_empty(args.Name) then
		image = ''
	else
		image = '[[File:' .. image_n .. '|link=' .. name .. ']]'
	end
	-- Table row
	local ret =  p._main{ name,
			altname,
			namenotes,
			quantity,
			quantitynotes,
			chance,
			chancenotes,
			price,
			alt,
			image }
 
	-- categories for mainspace
	local cats = ''
	local ns = mw.title.getCurrentTitle().nsText
	if ns == '' then
		cats = categories{name,quantity,chance}
	end
	return ret..cats
end
 
function p._main(...)
	local name,altname,namenotes,
		quantity,quantitynotes,
		chance,chancenotes,
		price,alt,image,gemw,hasgemw = unpack(...)
	local chance_bg, chance_txt, chance_sort = unpack(chances[chance:lower()] or {'#FFFFFF', '#000000', 8})
	local total
	quantity, total = qty(price,quantity)
 
	if #quantitynotes > 3 then
		quantity = quantity..' '..quantitynotes
	end
	if #chancenotes > 3 then
		chance = chance..' '..chancenotes
	end
 
	-- Table row creation
	local ret = '|- style="text-align:center;"' ..
			'\n| ' .. image ..
			'\n| style="text-align:left;" | [[' .. name .. ' | ' .. altname .. ']]' ..
			(#namenotes > 3 and namenotes or '') ..
			'\n| ' .. quantity ..
			'\n| data-sort-value ="' .. rare_sort .. '" style="background:' .. rare_bg .. '; color:' .. rare_txt .. ';" | ' .. chance
 
	return ret
end
 
 
function qty(price,quantity)
	-- if no quantity is given, return unknown and the price
	if not quantity or quantity == 'unknown' then
		return 'Unknown', price
	end
	-- en dashes are the proper dash for number ranges
	-- replace all hyphens and em dashes with en
	-- strip *all* whitespace
	-- change '(noted)' to '$n' for parsing
	quantity = mw.ustring.gsub(quantity,'[-—]','–')
		:gsub('%s','')
		:gsub('%(noted%)','$n')
	-- split list into table
	local vals = mw.text.split(quantity,'[,;]')
	-- All prices ranges will be a range
	-- e.g. if items valued at 100 coins are dropped in quantities of 1, 3, 5
	-- the price will be 100–500 rather than 100; 300; 500
	-- If low and high vars are the same in the end, only 1 price is displayed
	local low = 2147483648
	local high = 0
	-- recreate the quantity string to ensure consistent formatting
	local numstr = {}
	for i, v in ipairs(vals) do
		local clean = v:gsub('$n','')
		-- if list element contains an en dash (indicating range)
		-- Find the smaller/larger number (just in case)
		-- Compare them to the current min/max
		-- put them in order with desired format
		if mw.ustring.find(v,'–') then
			local splitvals = mw.text.split(clean,'–')
			-- assume a is smaller, b is larger
			local a = tonumber(splitvals[1])
			local b = tonumber(splitvals[2])
			-- Just in case
			if a > b then
				a,b = b,a
			end
			if a < low then
				low = a
			end
			if b > high then
				high = b
			end
			local addx = commas(a)..'–'..commas(b)
			if v:find('$n') then
				addx = addx..' (noted)'
			end
			table.insert(numstr,addx)
		else
			local a = tonumber(clean)
			if a < low then
				low = a
			end
			if a > high then 
				high = a
			end
			local addx = commas(a)
			if v:find('$n') then
				addx = addx..' (noted)'
			end
			table.insert(numstr,addx)
		end
	end
	-- Add a line break if there are too many elements
	-- To keep the tables thin
	if #numstr > 11 then
		local mid = math.floor(#numstr/2)
		numstr[mid] = '<br/>'..numstr[mid]
	end
	-- To prevent any possible confusion with formatted numbers
	-- elements should be separated with semicolons followed by a space
	numstr = table.concat(numstr,'; ')
	-- If no numbers are found in the string, return unknown
	if not numstr:find('%d') then
		return 'Unknown', price
	end
 
	local qtys
 
	if high == low then
		qtys = { high = high }
	else
		qtys = { low = low, high = high }
	end
 
	local priceret = get_price(price,qtys)
	return numstr, priceret
end
 
-- function to parse the quantity ranges and give a price range
-- also returns the desired format
function get_price(price,quantity)
	local ttl
	if not price then
		ttl = 'Not sold'
	elseif not quantity.low then
		ttl = price * quantity.high
		ttl = commas(ttl)
	else
		local lower = price * quantity.low
		local higher = price * quantity.high
		ttl = commas(lower) .. '–' .. commas(higher)
	end
	return ttl
end
 
return p

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.