Module:Disassemble

-- -- TODO: clean up legacy "material [#]" code from often, sometimes, rarely local edit_mod = require('Module:Edit button') local edit = edit_mod('Unknown') local onmain_mod = require('Module:Mainonly').on_main local onmain = onmain_mod local hc = require('Module:Paramtest').has_content local disdata = mw.loadData('Module:Disassemble/data')

local p = {} local junkpast75 = { [75] = 4.2, [76] = 3.8, [77] = 3.4, [78] = 3.0, [79] = 2.7,	[80] = 2.3, [81] = 2.0, [82] = 1.7, [83] = 1.4, [85] = 1.0,	[86] = 0.8, [87] = 0.6, [88] = 0.4, [89] = 0.3 }

local materials = { ['base'] = 'Base parts', ['blade'] = 'Blade parts', ['clear'] = 'Clear parts', ['connector'] = 'Connector parts', ['cover'] = 'Cover parts', ['crafted'] = 'Crafted parts', ['crystal'] = 'Crystal parts', ['delicate'] = 'Delicate parts', ['deflecting'] = 'Deflecting parts', ['flexible'] = 'Flexible parts', ['head'] = 'Head parts', ['magic'] = 'Magic parts', ['metallic'] = 'Metallic parts', ['organic'] = 'Organic parts', ['padded'] = 'Padded parts', ['plated'] = 'Plated parts', ['simple'] = 'Simple parts', ['smooth'] = 'Smooth parts', ['spiked'] = 'Spiked parts', ['spiritual'] = 'Spiritual parts', ['stave'] = 'Stave parts', ['tensile'] = 'Tensile parts', ['dextrous'] = 'Dextrous components', ['direct'] = 'Direct components', ['enhancing'] = 'Enhancing components', ['ethereal'] = 'Ethereal components', ['evasive'] = 'Evasive components', ['healthy'] = 'Healthy components', ['heavy'] = 'Heavy components', ['imbued'] = 'Imbued components', ['light'] = 'Light components', ['living'] = 'Living components', ['pious'] = 'Pious components', ['powerful'] = 'Powerful components', ['precious'] = 'Precious components', ['precise'] = 'Precise components', ['protective'] = 'Protective components', ['refined'] = 'Refined components', ['sharp'] = 'Sharp components', ['strong'] = 'Strong components', ['stunning'] = 'Stunning components', ['subtle'] = 'Subtle components', ['swift'] = 'Swift components', ['variable'] = 'Variable components', ['ancient'] = 'Ancient components', ['armadyl'] = 'Armadyl components', ['ascended'] = 'Ascended components', ['bandos'] = 'Bandos components', ['brassican'] = 'Brassican components', ['corporeal'] = 'Corporeal components', ['culinary'] = 'Culinary components', ['dragonfire'] = 'Dragonfire components', ['explosive'] = 'Explosive components', ['fortunate'] = 'Fortunate components', ['fungal'] = 'Fungal components', ['harnessed'] = 'Harnessed components', ['knightly'] = 'Knightly components', ['noxious'] = 'Noxious components', ['oceanic'] = 'Oceanic components', ['pestiferous'] = 'Pestiferous components', ['refined'] = 'Refined components', ['resilient'] = 'Resilient components', ['rumbling'] = 'Rumbling components', ['saradomin'] = 'Saradomin components', ['seren'] = 'Seren components', ['shifting'] = 'Shifting components', ['silent'] = 'Silent components', ['undead'] = 'Undead components', ['zamorak'] = 'Zamorak components', ['zaros'] = 'Zaros components', }

function get_mat(m) local str = string.gsub(string.gsub(string.lower(m), ' parts?', ), ' components?', ) if materials[str] then return materials[str], true else return (m or ''), false end end

function get_mat_cats(parts) local cats = {} local mat, yn 	for _,v in ipairs(parts) do		mat, yn = get_mat(v.n)		if yn then table.insert(cats,string.format('',mat)) end end return table.concat(cats,'') end

function plink(mat) local str = get_mat(mat) return string.format(' %s', str, str, str) end

function p.main(frame) local args = frame:getParent.args

local common = {} local rare = {} local uncommon = {} local special = {} local cats = {''}

local _discat = string.lower(args.category or '') _discat = disdata[_discat] if _discat then -- if an item has a category, it will be given these defaults args.often = _discat.often args.sometimes = _discat.sometimes args.rarely = _discat.rarely args.compqty = args.compqty or _discat.compqty args.itemqty = args.itemqty or _discat.itemqty or 1 args.level = args.level or 1 args.x10 = args.x10 or _discat.x10 end

local level = tonumber(args.level) if level then -- junk if tonumber(string.gsub(args.junk or ,'%D',),10) then -- do nothing -- don't fix integer junk else if level < 75 then args.junk = 100 - level * 1.1 elseif level >= 75 and level < 90 then args.junk = junkpast75[level] elseif level >= 90 then args.junk = 0.0 end -- fix integer junk if tonumber(args.junk) % 1 == 0 then args.junk = args.junk..'.0' end end

-- xp		if tonumber(string.gsub(args.xp or ,'%D',),10) then -- do nothing else local x10 = string.lower(tostring(args.x10)) if x10 == 'yes' or x10 == 'true' then x10 = 10 else x10 = 1 end args.xp = math.max(math.floor(level * 0.03 * x10 * 1000 + 0.009)/1000,0.1) args.xp = math.floor(args.xp * 10 + 0.05)/10 end end local specialchance = args.specialchance or '' if string.lower(specialchance) == 'no' then specialchance = false else specialchance = true end

local junk = args.junk if hc(junk) then junk = string.gsub(junk, '%%', '') -- remove % if present if not string.find(junk, '%d+%.%d') then table.insert(cats, '') end if tonumber(junk) == 0 then table.insert(cats, '') end

junk = junk .. '%'	else junk = edit table.insert(cats, '') end

local _common = mw.text.split(args.often or '','%s*,%s') local _uncommon = mw.text.split(args.sometimes or '','%s*,%s') local _rare = mw.text.split(args.rarely or '','%s*,%s')

for _, v in ipairs(_common) do		v = string.gsub(v, '%[%[(.+)%]%]', '%1') local _,_,n,l = string.find(v,'(.+) ?%[(%d+)%]') if not n then n = v		end

if v:find('%S') then table.insert(common,{ n = n, l = l }) end end

for _, v in ipairs(_uncommon) do		v = string.gsub(v, '%[%[(.+)%]%]', '%1') local _,_,n,l = string.find(v,'(.+) ?%[(%d+)%]') if not n then n = v		end if not tonumber(l) then l = 1 end if v:find('%S') then table.insert(uncommon,{ n = n, l = l }) end end for _, v in ipairs(_rare) do		v = string.gsub(v, '%[%[(.+)%]%]', '%1') local _,_,n,l = string.find(v,'(.+) ?%[(%d+)%]') if not n then n = v		end if not tonumber(l) then l = 1 end if v:find('%S') then table.insert(rare,{ n = n, l = l }) end end local _special = mw.text.split(args.special or '','%s*,%s')

for _, v in ipairs(_special) do		v = string.gsub(v, '%[%[(.+)%]%]', '%1') local _,_,n,l = string.find(v,'(.+) ?%[(%d+)%]') if not n then n,l = v		end if not tonumber(l) then l = 1 end if v:find('%S') then table.insert(special,{ n = n, l = l }) end end

if (#special + #common + #uncommon + #rare) < 1 then table.insert(cats, '') end

local xp = tonumber(args.xp) if not xp then xp = edit table.insert(cats, '') else -- fix integer xp		if tonumber(xp) % 1 == 0 then xp = xp..'.0' end end

local iqty = tonumber(args.itemqty) or 1

local cqty = tonumber(args.compqty) if not cqty then cqty = edit table.insert(cats, '') end

if onmain then table.insert(cats, get_mat_cats(common)) table.insert(cats, get_mat_cats(uncommon)) table.insert(cats, get_mat_cats(rare)) table.insert(cats, get_mat_cats(special)) cats = table.concat(cats) else cats = '' end

return p._main(cats,special,specialchance,common,uncommon,rare,xp,junk,iqty,cqty) end

function p._main(cats,special,specialchance,common,uncommon,rare,xp,junk,iqty,cqty) local ret = mw.html.create('div') :wikitext('This item can be disassembled with the Invention skill.') :wikitext(cats)

local _t = ret:tag('table') :addClass('wikitable') :css('width','300px') :tag('tr') :tag('th') :attr('title', 'Experience received for disassembling') :wikitext('Disassembly XP ') :tag('sup') :css({							color = '#15f',							['font-style'] = 'italic',							cursor = 'help',							['border-bottom'] = 'dotted 1px black'							}) :wikitext('?') :done :done :tag('td') :css('text-align','right') :wikitext(xp) :done :done :tag('tr') :tag('th') :attr('title', 'Amount disassembled per action') :wikitext('Item quantity required ') :tag('sup') :css({							color = '#15f',							['font-style'] = 'italic',							cursor = 'help',							['border-bottom'] = 'dotted 1px black'							}) :wikitext('?') :done :done :tag('td') :css('text-align','right') :wikitext(iqty) :done :done :tag('tr') :tag('th') :attr('title', 'Base chance of receiving junk') :wikitext('Base junk chance ') :tag('sup') :css({							color = '#15f',							['font-style'] = 'italic',							cursor = 'help',							['border-bottom'] = 'dotted 1px black'							}) :wikitext('?') :done :done :tag('td') :css('text-align','right') :wikitext(junk) :done :done :tag('tr') :tag('th') :attr('title', 'The amount of materials received normally (not including specials), shown in chat window') :wikitext('Total materials received ') :tag('sup') :css({							color = '#15f',							['font-style'] = 'italic',							cursor = 'help',							['border-bottom'] = 'dotted 1px black'							}) :wikitext('?') :done :done :tag('td') :css('text-align','right') :wikitext(cqty) :done :done :tag('tr') :tag('th') :attr('colspan','2') :done :done :tag('tr') :tag('th') :attr('colspan','2') :css('background','#e5e5e5') :wikitext('Possible materials') :done :done local specnote = ''

if specialchance then specnote = '(100%)' else specnote = '(not 100%)' end

if #special > 0 then _t:tag('tr') :tag('th') :attr('colspan', '2') :attr('title', 'The amount of special materials received (if any are received); note that the amount of special materials received is independent of the total materials listed above') :wikitext('Special '..specnote) :tag('sup') :css({							color = '#15f',							['font-style'] = 'italic',							cursor = 'help',							['border-bottom'] = 'dotted 1px black'							}) :wikitext('?') :done :done :done for _, v in ipairs(special) do			_t:tag('tr') :tag('td') :attr('colspan', '2') :wikitext(v.l .. ' × ' .. plink(v.n)) :done :done end end -- common

if #common > 0 then _t:tag('tr') :tag('th') :attr('colspan', '2') :wikitext('Often') :done :done for _, v in ipairs(common) do			_t:tag('tr') :tag('td') :attr('colspan', '2') :wikitext(plink(v.n)) :done :done end end

-- uncommon if #uncommon > 0 then _t:tag('tr') :tag('th') :attr('colspan', '2') :wikitext('Sometimes') :done :done for _, v in ipairs(uncommon) do			_t:tag('tr') :tag('td') :attr('colspan', '2') :wikitext(plink(v.n)) :done :done end end

-- rare if #rare > 0 then _t:tag('tr') :tag('th') :attr('colspan', '2') :wikitext('Rarely') :done :done for _, v in ipairs(rare) do			_t:tag('tr') :tag('td') :attr('colspan', '2') :wikitext(plink(v.n)) :done :done end end

return ret end

return p