Advertisement
gaz_lloyd

Untitled

Sep 2nd, 2015
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.83 KB | None | 0 0
  1.  
  2. -- <pre>
  3. local p = {}
  4.  
  5. local params = require('Module:Paramtest')
  6. local lang = mw.language.getContentLanguage()
  7. local commas = function (n) return lang:formatNum(n) end
  8.  
  9. local geprice = require('Module:Exchange')._price
  10. local ref = require('Module:Reftag')
  11.  
  12. --bg, txt, sort
  13. local rarities = {
  14.     always = {'#AFEEEE', '#000000', 1},
  15.     common = {'#56E156', '#000000', 2},
  16.     uncommon = {'#FFED4C', '#000000', 3},
  17.     rare = {'#FF863C', '#000000', 4},
  18.     ['very rare'] = {'#FF6262', '#000000', 5},
  19.     random = {'#FFA3FF', '#000000', 6},
  20.     varies = {'#FFA3FF', '#000000', 6},
  21.     discontinued = {'#DBFF4C', '#000000', 7}
  22. }
  23.  
  24. function p.main(frame)
  25.     local args = frame:getParent().args
  26.     -- Params and defaults
  27.     local name,namenotes,
  28.         quantity,quantitynotes,
  29.         rarity,raritynotes = params.defaults{
  30.                     {args.Name,'Item'},
  31.                     {args.Namenotes,''},
  32.                     {args.Quantity,'unknown'},
  33.                     {args.Quantitynotes,''},
  34.                     {args.Rarity,'unknown'},
  35.                     {args.Raritynotes,''}
  36.                 }
  37.     local altname = params.default_to(args.Alt,name)
  38.     local gemwname = params.default_to(args.gemwname,name)
  39.     local raritynotes = args.Raritynotes or ''
  40.     rarity = params.ucflc(rarity)
  41.     quantity = quantity:lower()
  42.     local gemw = string.lower(args.gemw or 'yes') == 'yes'
  43.     local price
  44.     local alt = false
  45.     -- Test for existence of an exchange page
  46.     local hasgemw
  47.     if gemw then
  48.         hasgemw, price = pcall(geprice,gemwname)
  49.     elseif args.AltValue then
  50.         price = args.AltValue:gsub(',','')
  51.         alt = true
  52.     end
  53.     -- Clean up price
  54.     price = tonumber(price,10) or false
  55.  
  56.     -- Use 'File:<name>.png' if no image param
  57.     -- Use 'File:<image>' if image param; image param will include extension
  58.     -- Special catch for coins
  59.     local image,image_n
  60.     if name:lower() == 'coins' then
  61.         image_n = coins_image(quantity)
  62.     else
  63.         image_n = params.default_to(args.Image, name .. '.png')
  64.     end
  65.     if image_n:lower() == 'no' or params.is_empty(args.Name) then
  66.         image = ''
  67.     else
  68.         image = '[[File:' .. image_n .. '|link=' .. name .. ']]'
  69.     end
  70.     -- Table row
  71.     local ret =  p._main{ name,
  72.             altname,
  73.             namenotes,
  74.             quantity,
  75.             quantitynotes,
  76.             rarity,
  77.             raritynotes,
  78.             price,
  79.             alt,
  80.             image,
  81.             gemw,
  82.             hasgemw }
  83.  
  84.     -- categories for mainspace
  85.     local cats = ''
  86.     local ns = mw.title.getCurrentTitle().nsText
  87.     if ns == '' then
  88.         cats = categories{name,quantity,rarity}
  89.     end
  90.     return ret..cats
  91. end
  92.  
  93. function p._main(...)
  94.     local name,altname,namenotes,
  95.         quantity,quantitynotes,
  96.         rarity,raritynotes,
  97.         price,alt,image,gemw,hasgemw = unpack(...)
  98.     local rare_bg, rare_txt, rare_sort = unpack(rarities[rarity:lower()] or {'#FFFFFF', '#000000', 8})
  99.     local total
  100.     quantity, total = qty(price,quantity)
  101.  
  102.     if #quantitynotes > 3 then
  103.         quantity = quantity..quantitynotes
  104.     end
  105.     if #raritynotes > 3 then
  106.         rarity = rarity..raritynotes
  107.     end
  108.  
  109.     -- Table row creation
  110.     local ret = '|- style="text-align:center;"' ..
  111.             '\n| ' .. image ..
  112.             '\n| style="text-align:left;" | [[' .. name .. ' | ' .. altname .. ']]' ..
  113.             (#namenotes > 3 and namenotes or '') ..
  114.             '\n| ' .. quantity ..
  115.             '\n| data-sort-value ="' .. rare_sort .. '" style="background:' .. rare_bg .. '; color:' .. rare_txt .. ';" | ' .. rarity
  116.            
  117.     if gemw and hasgemw and not alt then
  118.         ret = ret .. '\n| title="' .. commas(price) .. ' coins each" | ' .. total
  119.     elseif gemw and not hasgemw then
  120.         ret = ret .. '\n| 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.' ..
  121.         ' style="color:#FF0000; cursor:help; border-bottom:1px dashed; font-weight:bold;" | Error'
  122.     elseif alt then
  123.         ret = ret .. '\n| ' .. total..ref{ name='DropsLineAltValueRef',
  124.                     text='This item has a distinct value, even if it cannot be traded over the [[Grand Exchange]].'}
  125.     else
  126.         ret = ret .. '\n| Not sold'
  127.     end
  128.     return ret
  129. end
  130.  
  131.  
  132. function qty(price,quantity)
  133.     -- if no quantity is given, return unknown and the price
  134.     if not quantity or quantity == 'unknown' then
  135.         return 'Unknown', price
  136.     end
  137.     -- en dashes are the proper dash for number ranges
  138.     -- replace all hyphens and em dashes with en
  139.     -- strip *all* whitespace
  140.     -- change '(noted)' to '$n' for parsing
  141.     quantity = mw.ustring.gsub(quantity,'[-—]','–')
  142.         :gsub('%s','')
  143.         :gsub('%(noted%)','$n')
  144.     -- split list into table
  145.     local vals = mw.text.split(quantity,'[,;]')
  146.     -- All prices ranges will be a range
  147.     -- e.g. if items valued at 100 coins are dropped in quantities of 1, 3, 5
  148.     -- the price will be 100–500 rather than 100; 300; 500
  149.     -- If low and high vars are the same in the end, only 1 price is displayed
  150.     local low = 2147483648
  151.     local high = 0
  152.     -- recreate the quantity string to ensure consistent formatting
  153.     local numstr = {}
  154.     for i, v in ipairs(vals) do
  155.         local clean = v:gsub('$n','')
  156.         -- if list element contains an en dash (indicating range)
  157.         -- Find the smaller/larger number (just in case)
  158.         -- Compare them to the current min/max
  159.         -- put them in order with desired format
  160.         if mw.ustring.find(v,'–') then
  161.             local splitvals = mw.text.split(clean,'–')
  162.             -- assume a is smaller, b is larger
  163.             local a = tonumber(splitvals[1])
  164.             local b = tonumber(splitvals[2])
  165.             -- Just in case
  166.             if a > b then
  167.                 a,b = b,a
  168.             end
  169.             if a < low then
  170.                 low = a
  171.             end
  172.             if b > high then
  173.                 high = b
  174.             end
  175.             local addx = commas(a)..'–'..commas(b)
  176.             if v:find('$n') then
  177.                 addx = addx..' (noted)'
  178.             end
  179.             table.insert(numstr,addx)
  180.         else
  181.             local a = tonumber(clean)
  182.             if a < low then
  183.                 low = a
  184.             end
  185.             if a > high then
  186.                 high = a
  187.             end
  188.             local addx = commas(a)
  189.             if v:find('$n') then
  190.                 addx = addx..' (noted)'
  191.             end
  192.             table.insert(numstr,addx)
  193.         end
  194.     end
  195.     -- Add a line break if there are too many elements
  196.     -- To keep the tables thin
  197.     if #numstr > 11 then
  198.         local mid = math.floor(#numstr/2)
  199.         numstr[mid] = '<br/>'..numstr[mid]
  200.     end
  201.     -- To prevent any possible confusion with formatted numbers
  202.     -- elements should be separated with semicolons followed by a space
  203.     numstr = table.concat(numstr,'; ')
  204.     -- If no numbers are found in the string, return unknown
  205.     if not numstr:find('%d') then
  206.         return 'Unknown', price
  207.     end
  208.  
  209.     local qtys
  210.  
  211.     if high == low then
  212.         qtys = { high = high }
  213.     else
  214.         qtys = { low = low, high = high }
  215.     end
  216.  
  217.     local priceret = get_price(price,qtys)
  218.     return numstr, priceret
  219. end
  220.  
  221. -- function to parse the quantity ranges and give a price range
  222. -- also returns the desired format
  223. function get_price(price,quantity)
  224.     local ttl
  225.     if not price then
  226.         ttl = 'Not sold'
  227.     elseif not quantity.low then
  228.         ttl = price * quantity.high
  229.         ttl = commas(ttl)
  230.     else
  231.         local lower = price * quantity.low
  232.         local higher = price * quantity.high
  233.         ttl = commas(lower) .. '–' .. commas(higher)
  234.     end
  235.     return ttl
  236. end
  237.  
  238. -- Special function for coin images
  239. function coins_image(q)
  240.     q = mw.text.split(q,'[,%-–]')
  241.     local max_q = 1
  242.     for _, v in ipairs(q) do
  243.         if (tonumber(v) or 0) > max_q then
  244.             max_q = tonumber(v)
  245.         end
  246.     end
  247.     -- From [[Module:Coins]] (mostly)
  248.     for _, j in ipairs( { 10000, 1000, 250, 100, 25, 5, 4, 3, 2 } ) do
  249.         if max_q >= j then
  250.             max_q = j
  251.             break
  252.         end
  253.     end
  254.     return 'Coins '..max_q..'.png'
  255. end
  256.  
  257. -- adding categories to mainspace
  258. function categories(...)
  259.     local name,quantity,rarity = unpack(...)
  260.     local ret = ''
  261.     name = name:lower()
  262.     quantity = quantity:lower()
  263.     if name:find('effigy') then
  264.         ret = ret .. '[[Category:Effigy droppers]]'
  265.     elseif name:find('clue scroll') then
  266.         ret = ret .. '[[Category:Clue Drop Monsters]]'
  267.     end
  268.     if not rarities[rarity:lower()] then
  269.         ret = ret .. '[[Category:Needs drop rarity added]]'
  270.     end
  271.     if quantity:find('Unknown') then
  272.         ret = ret .. '[[Category:Needs drop quantity added]]'
  273.     end
  274.     return ret
  275. end
  276.  
  277. return p
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement