Advertisement
gaz_lloyd

Untitled

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