Old School RuneScape Wiki
No edit summary
Tag: sourceedit
m (Reverted edits by Spineweilder (talk | block) to last version by The Mol Man)
Line 2: Line 2:
 
local p = {}
 
local p = {}
   
  +
local yesno = require('Module:Yesno')
 
local params = require('Module:Paramtest')
 
local params = require('Module:Paramtest')
  +
local commas = require('Module:Addcommas')
local lang = mw.language.getContentLanguage()
 
local commas = function (n) return lang:formatNum(n) end
 
   
  +
local raritybg = {
local geprice = require('Module:Exchange')._price
 
  +
['always'] = '#AFEEEE',
local ref = require('Module:Reftag')
 
  +
['common'] = '#56E156',
  +
['uncommon'] = '#FFED4C',
  +
['rare'] = '#FF863C',
  +
['very rare'] = '#FF6262',
  +
['random'] = '#FFA3FF',
  +
['varies'] = '#FFA3FF',
  +
['discontinued'] = '#DBFF4C'
  +
}
   
  +
local raritysort = {
--bg, txt, sort
 
  +
['always'] = '1',
local rarities = {
 
  +
['common'] = '2',
always = {'#AFEEEE', '#000000', 1},
 
  +
['uncommon'] = '3',
common = {'#56E156', '#000000', 2},
 
  +
['rare'] = '4',
uncommon = {'#FFED4C', '#000000', 3},
 
rare = {'#FF863C', '#000000', 4},
+
['very rare'] = '5',
['very rare'] = {'#FF6262', '#000000', 5},
+
['varies'] = '6',
random = {'#FFA3FF', '#000000', 6},
+
['random'] = '6',
  +
['discontinued'] = '7'
varies = {'#FFA3FF', '#000000', 6},
 
discontinued = {'#DBFF4C', '#000000', 7}
 
 
}
 
}
   
Line 24: Line 31:
 
local args = frame:getParent().args
 
local args = frame:getParent().args
 
-- Params and defaults
 
-- Params and defaults
  +
local name = params.ucflc( params.default_to(args.Name,'item') )
local name,namenotes,
 
  +
local namenotes = args.Namenotes or args.namenotes or ''
quantity,quantitynotes,
 
  +
local quantity = params.default_to(args.Quantity,'Unknown')
rarity,raritynotes = params.defaults{
 
  +
local quantitynotes = args.Quantitynotes or args.quantitynotes or ''
{args.Name,'Item'},
 
  +
local rarity = params.default_to(args.Rarity,'Unknown')
{args.Namenotes,''},
 
  +
local raritynotes = (args.Raritynotes or args.raritynotes) or ''
{args.Quantity,'unknown'},
 
{args.Quantitynotes,''},
 
{args.Rarity,'unknown'},
 
{args.Raritynotes,''}
 
}
 
local altname = params.default_to(args.Alt,name)
 
local gemwname = params.default_to(args.gemwname,name)
 
local raritynotes = args.Raritynotes or ''
 
 
rarity = params.ucflc(rarity)
 
rarity = params.ucflc(rarity)
 
quantity = quantity:lower()
 
quantity = quantity:lower()
local gemw = string.lower(args.gemw or 'yes') == 'yes'
+
local gemw = args.gemw or 'yes'
  +
gemw = yesno(gemw)
local price
 
  +
-- Use 'File:<name>.png' if no image param
local alt = false
 
  +
-- User 'File:<image>' if image param; image param will include extension
-- Test for existence of an exchange page
 
  +
local image = 'File:' .. params.default_to(args.Image,name .. '.png')
local hasgemw
 
if gemw then
 
hasgemw, price = pcall(geprice,gemwname)
 
elseif args.AltValue then
 
price = args.AltValue:gsub(',','')
 
alt = true
 
end
 
-- Clean up price
 
price = tonumber(price,10) or 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
 
-- Table row
local ret = p._main{ name,
+
local ret = p._main(name,namenotes,quantity,quantitynotes,rarity,raritynotes,image,gemw)
altname,
 
namenotes,
 
quantity,
 
quantitynotes,
 
rarity,
 
raritynotes,
 
price,
 
alt,
 
image,
 
gemw,
 
hasgemw }
 
   
 
-- categories for mainspace
 
-- categories for mainspace
Line 85: Line 52:
 
local ns = mw.title.getCurrentTitle().nsText
 
local ns = mw.title.getCurrentTitle().nsText
 
if ns == '' then
 
if ns == '' then
cats = categories{name,quantity,rarity}
+
cats = categories(name,quantity,rarity)
 
end
 
end
 
return ret..cats
 
return ret..cats
 
end
 
end
   
function p._main(...)
+
function p._main(name,namenotes,quantity,quantitynotes,rarity,raritynotes,image,gemw)
  +
local rare_bg = raritybg[rarity:lower()] or '#FFFFFF'
local name,altname,namenotes,
 
  +
local rare_sort = raritysort[rarity:lower()] or '8'
quantity,quantitynotes,
 
  +
quantity = qty(quantity)
rarity,raritynotes,
 
price,alt,image,gemw,hasgemw = unpack(...)
 
local rare_bg, rare_txt, rare_sort = unpack(rarities[rarity:lower()] or {'#FFFFFF', '#000000', 8})
 
local total
 
quantity, total = qty(price,quantity)
 
 
if #quantitynotes > 3 then
 
quantity = quantity..quantitynotes
 
end
 
if #raritynotes > 3 then
 
rarity = rarity..raritynotes
 
end
 
 
 
-- Table row creation
 
-- Table row creation
local ret = mw.html.create('tr')
+
local ret = '\n|- style="text-align:center;"' ..
  +
'\n| class="hidenoimage" | [[' .. image .. ']]' ..
:css('text-align','center')
 
  +
'\n| style="text-align:left;" | [[' .. name .. ']] ' .. namenotes ..
:tag('td')
 
  +
'\n| ' .. quantity .. ' ' .. quantitynotes ..
:wikitext(image)
 
  +
'\n| style="background:' .. rare_bg .. ';"' ..
:done()
 
  +
-- display none is used to create a sort key to let
:tag('td')
 
  +
-- rarity sorting work properly
:css('text-align','left')
 
  +
' | <span style="display:none;">' .. rare_sort ..
:wikitext('[['..name..'|'..altname..']]' ..
 
  +
'; </span>' .. rarity .. ' ' .. raritynotes ..
(#namenotes > 3 and namenotes or ''))
 
  +
-- Redundant? Yes. But it keeps weird stuff from happening when there are spaces after the template
:done()
 
:tag('td')
+
'\n|-\n'
  +
return ret
:wikitext(quantity)
 
:done()
 
:tag('td')
 
:attr('data-sort-value',rare_sort)
 
:css({background = rare_bg, color = rare_txt})
 
:wikitext(rarity)
 
:done()
 
if gemw and hasgemw and not alt then
 
ret :tag('td')
 
:attr('title',commas(price)..' coins each')
 
:wikitext(total)
 
:done()
 
elseif gemw and not hasgemw then
 
ret :tag('td')
 
:tag('span')
 
:attr('title','Exchange page not found for "'..name..'". Double check that the exact item name and casing is used for the "name" parameter. Add "gemw=no" to this template if this item cannot be traded on the Grand Exchange or ignore this error and wait for the exchange page to be made.')
 
:css({ color = '#FF0000',
 
cursor = 'help',
 
['border-bottom'] = '1px dashed',
 
['font-weight'] = 'bold' })
 
:wikitext('Error')
 
:done()
 
:done()
 
elseif alt then
 
ret :tag('td')
 
:wikitext(total..ref{ name='DropsLineAltValueRef',
 
text='This item has a distinct value, even if it cannot be traded over the [[Grand Exchange]].'})
 
:done()
 
else
 
ret :tag('td')
 
:wikitext('Not sold')
 
:done()
 
end
 
return tostring(ret)
 
 
end
 
end
   
   
function qty(price,quantity)
+
function qty(quantity)
-- if no quantity is given, return unknown and the price
+
-- if no quantity is given, return unknown
 
if not quantity or quantity == 'unknown' then
 
if not quantity or quantity == 'unknown' then
return 'Unknown', price
+
return 'Unknown'
 
end
 
end
 
-- en dashes are the proper dash for number ranges
 
-- en dashes are the proper dash for number ranges
 
-- replace all hyphens and em dashes with en
 
-- replace all hyphens and em dashes with en
 
-- strip *all* whitespace
 
-- strip *all* whitespace
-- change '(noted)' to '$n' for parsing
+
-- change '(noted)' to 'n' for parsing
 
quantity = mw.ustring.gsub(quantity,'[-—]','–')
 
quantity = mw.ustring.gsub(quantity,'[-—]','–')
 
:gsub('%s','')
 
:gsub('%s','')
Line 169: Line 91:
 
-- split list into table
 
-- split list into table
 
local vals = mw.text.split(quantity,'[,;]')
 
local vals = mw.text.split(quantity,'[,;]')
  +
local ret = {}
-- 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
 
-- recreate the quantity string to ensure consistent formatting
local numstr = {}
+
local numstr = ''
 
for i, v in ipairs(vals) do
 
for i, v in ipairs(vals) do
 
local clean = v:gsub('$n','')
 
local clean = v:gsub('$n','')
-- if list element contains an en dash (indicating range)
+
-- if list element contain an en dash (indicating range)
 
-- Find the smaller/larger number (just in case)
 
-- Find the smaller/larger number (just in case)
 
-- Compare them to the current min/max
 
-- Compare them to the current min/max
 
-- put them in order with desired format
 
-- put them in order with desired format
 
if mw.ustring.find(v,'–') then
 
if mw.ustring.find(v,'–') then
  +
local noted = v:find('$n')
 
local splitvals = mw.text.split(clean,'–')
 
local splitvals = mw.text.split(clean,'–')
-- assume a is smaller, b is larger
 
 
local a = tonumber(splitvals[1])
 
local a = tonumber(splitvals[1])
 
local b = tonumber(splitvals[2])
 
local b = tonumber(splitvals[2])
  +
local smaller,larger
-- Just in case
 
 
if a > b then
 
if a > b then
a,b = b,a
+
smaller = b
  +
larger = a
  +
else
  +
smaller = a
  +
larger = b
 
end
 
end
  +
numstr = numstr .. commas._add(smaller) .. '–' .. commas._add(larger)
if a < low then
 
low = a
+
if noted then
  +
numstr = numstr .. ' (noted)'
 
end
 
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
 
else
 
local a = tonumber(clean)
 
local a = tonumber(clean)
if a < low then
+
if a then
low = a
+
numstr = numstr .. commas._add(a)
 
end
 
end
if a > high then
 
high = a
 
end
 
local addx = commas(a)
 
 
if v:find('$n') then
 
if v:find('$n') then
addx = addx..' (noted)'
+
numstr = numstr .. ' (noted)'
 
end
 
end
table.insert(numstr,addx)
 
 
end
 
end
  +
-- To prevent any possible confusion with formatted numbers
  +
-- elements should be separated with semicolons followed by a space
  +
numstr = numstr .. '; '
 
end
 
end
  +
-- removes the final separator, because it's redundant
-- Add a line break if there are too many elements
 
  +
numstr = numstr:sub(1,-3)
-- 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
 
   
  +
if numstr:find('%d') then
local qtys
 
  +
return numstr
 
if high == low then
 
qtys = { high = high }
 
 
else
 
else
  +
return 'Unknown'
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
 
 
-- Special function for coin images
 
function coins_image(q)
 
q = mw.text.split(q,'[,%-–]')
 
local max_q = 1
 
for _, v in ipairs(q) do
 
if (tonumber(v) or 0) > max_q then
 
max_q = tonumber(v)
 
end
 
end
 
-- From [[Module:Coins]] (mostly)
 
for _, j in ipairs( { 10000, 1000, 250, 100, 25, 5, 4, 3, 2 } ) do
 
if max_q >= j then
 
max_q = j
 
break
 
end
 
 
end
 
end
return 'Coins '..max_q..'.png'
 
 
end
 
end
   
 
-- adding categories to mainspace
 
-- adding categories to mainspace
function categories(...)
+
function categories(name,quantity,rarity)
local name,quantity,rarity = unpack(...)
 
 
local ret = ''
 
local ret = ''
 
name = name:lower()
 
name = name:lower()
 
quantity = quantity:lower()
 
quantity = quantity:lower()
if name:find('effigy') then
+
if name:find('clue scroll') then
ret = ret .. '[[Category:Effigy droppers]]'
 
elseif name:find('clue scroll') then
 
 
ret = ret .. '[[Category:Clue Drop Monsters]]'
 
ret = ret .. '[[Category:Clue Drop Monsters]]'
 
end
 
end
if not rarities[rarity:lower()] then
+
if not raritysort[rarity:lower()] then
 
ret = ret .. '[[Category:Needs drop rarity added]]'
 
ret = ret .. '[[Category:Needs drop rarity added]]'
 
end
 
end

Revision as of 01:28, 3 September 2015

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

-- <pre>
local p = {}

local yesno = require('Module:Yesno')
local params = require('Module:Paramtest')
local commas = require('Module:Addcommas')

local raritybg = {
	['always'] = '#AFEEEE',
	['common'] = '#56E156',
	['uncommon'] = '#FFED4C',
	['rare'] = '#FF863C',
	['very rare'] = '#FF6262',
	['random'] = '#FFA3FF',
	['varies'] = '#FFA3FF',
	['discontinued'] = '#DBFF4C'
}

local raritysort = {
	['always'] = '1',
	['common'] = '2',
	['uncommon'] = '3',
	['rare'] = '4',
	['very rare'] = '5',
	['varies'] = '6',
	['random'] = '6',
	['discontinued'] = '7'
}

function p.main(frame)
	local args = frame:getParent().args
	-- Params and defaults
	local name = params.ucflc( params.default_to(args.Name,'item') )
	local namenotes = args.Namenotes or args.namenotes or ''
	local quantity = params.default_to(args.Quantity,'Unknown')
	local quantitynotes = args.Quantitynotes or args.quantitynotes or ''
	local rarity = params.default_to(args.Rarity,'Unknown')
	local raritynotes = (args.Raritynotes or args.raritynotes) or ''
	rarity = params.ucflc(rarity)
	quantity = quantity:lower()
	local gemw = args.gemw or 'yes'
	gemw = yesno(gemw)
	-- Use 'File:<name>.png' if no image param
	-- User 'File:<image>' if image param; image param will include extension
	local image = 'File:' .. params.default_to(args.Image,name .. '.png')

	-- Table row
	local ret =  p._main(name,namenotes,quantity,quantitynotes,rarity,raritynotes,image,gemw)

	-- categories for mainspace
	local cats = ''
	local ns = mw.title.getCurrentTitle().nsText
	if ns == '' then
		cats = categories(name,quantity,rarity)
	end
	return ret..cats
end

function p._main(name,namenotes,quantity,quantitynotes,rarity,raritynotes,image,gemw)
	local rare_bg = raritybg[rarity:lower()] or '#FFFFFF'
	local rare_sort = raritysort[rarity:lower()] or '8'
	quantity = qty(quantity)
	-- Table row creation
	local ret = '\n|- style="text-align:center;"' .. 
		'\n| class="hidenoimage" | [[' .. image .. ']]' ..
		'\n| style="text-align:left;" | [[' .. name .. ']] ' .. namenotes ..
		'\n| ' .. quantity .. ' ' .. quantitynotes ..
		'\n| style="background:' .. rare_bg .. ';"' ..
			-- display none is used to create a sort key to let
			-- rarity sorting work properly
			' | <span style="display:none;">' .. rare_sort ..
			'; </span>' .. rarity .. ' ' .. raritynotes ..
			-- Redundant? Yes. But it keeps weird stuff from happening when there are spaces after the template
			'\n|-\n'
	return ret
end


function qty(quantity)
	-- if no quantity is given, return unknown
	if not quantity or quantity == 'unknown' then
		return 'Unknown'
	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,'[,;]')
	local ret = {}
	-- 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 contain 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 noted = v:find('$n')
			local splitvals = mw.text.split(clean,'–')
			local a = tonumber(splitvals[1])
			local b = tonumber(splitvals[2])
			local smaller,larger
			if a > b then
				smaller = b
				larger = a
			else
				smaller = a
				larger = b
			end
			numstr = numstr .. commas._add(smaller) .. '–' .. commas._add(larger)
			if noted then
				numstr = numstr .. ' (noted)'
			end
		else
			local a = tonumber(clean)
			if a then
				numstr = numstr .. commas._add(a)
			end
			if v:find('$n') then
				numstr = numstr .. ' (noted)'
			end
		end
		-- To prevent any possible confusion with formatted numbers
		-- elements should be separated with semicolons followed by a space
		numstr = numstr .. '; '
	end
	-- removes the final separator, because it's redundant
	numstr = numstr:sub(1,-3)

	if numstr:find('%d') then
		return numstr
	else
		return 'Unknown'
	end
end

-- adding categories to mainspace
function categories(name,quantity,rarity)
	local ret = ''
	name = name:lower()
	quantity = quantity:lower()
	if name:find('clue scroll') then
		ret = ret .. '[[Category:Clue Drop Monsters]]'
	end
	if not raritysort[rarity:lower()] then
		ret = ret .. '[[Category:Needs drop rarity added]]'
	end
	if quantity:find('Unknown') then
		ret = ret .. '[[Category:Needs drop quantity added]]'
	end
	return ret
end

return p