Module:Disassemble

-- 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 materials = mw.loadData('Module:Disassemble/mats') local tooltips = require('Module:Tooltip')

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, [84] = 1.2,	[85] = 1.0, [86] = 0.8, [87] = 0.6, [88] = 0.4, [89] = 0.3 }

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

function get_mat_cats(parts) local cats = {} local mat, yn 	for _, v in ipairs(parts) do		local matpass = v		if type(matpass) == 'table' then matpass = v.n		end mat, yn = get_mat(matpass) 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.potion = args.potion or _discat.potion or false args.x10 = args.x10 or _discat.x10 if _discat.cat then table.insert(cats,string.format('',_discat.cat)) end elseif string.lower(args.category or '') ~= 'no' then table.insert(cats,'') 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 args.junk = math.floor((args.junk) * 1000 + 0.009)/1000 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 else level = 1 table.insert(cats, '') end

local junk = args.junk local junknum = -1 if hc(junk) then junk = string.gsub(junk, '%%', '') -- remove % if present if tonumber(junk) == 0 then table.insert(cats, '') end junknum = junk if onmain then junk = string.format('Junk chance::%s%%',junk) else junk = junk..'%' end else junk = edit 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, '[%]%[]','')

if v:find('%S') then table.insert(common,v) end end

for _, v in ipairs(_uncommon) do		v = string.gsub(v, '[%]%[]','')

if v:find('%S') then table.insert(uncommon,v) end end

for _, v in ipairs(_rare) do		v = string.gsub(v, '[%]%[]','')

if v:find('%S') then table.insert(rare,v) end end

local _special = mw.text.split(args.special or '','%s*,%s') local hasspec = false 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 = '?' end n = mw.text.trim(n) if v:find('%S') then table.insert(special,{ n = n, l = l }) hasspec = true if l == '?' then table.insert(cats, '') end end end

local specialchance = string.lower(args.specialchance or '') if specialchance == 'no' then specialchance = false elseif specialchance == 'yes' then specialchance = true elseif hasspec then specialchance = '?' table.insert(cats, '') end

local xp = tonumber(args.xp) if not xp then xp = edit 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 end local potions = string.lower(tostring(args.potion)) if potions == 'yes' or potions == 'true' then potions = true else potions = false end if tonumber(args.calcvalue) then 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,junknum,xp,junk,iqty,cqty,potions) end

function p._main(cats,special,specialchance,common,uncommon,rare,junknum,xp,junk,iqty,cqty,potions) local ret = mw.html.create('div') -- invariable ID for linking :attr('id','DisassemblyT') :wikitext('This item can be disassembled with the Invention skill.') :wikitext(cats)

local qtynotes = '' if potions then qtynotes = ' [?] ' end local tooltip_span = '' local tooltip_div = nil if (tonumber(junknum) or -1) > 0 then local junkreduction = { {34, 0.99}, {49, 0.97}, {64, 0.95}, {69, 0.93}, {78, 0.91}, {83, 0.88}, {91, 0.86}, {95, 0.83}, {105, 0.8}		}		local junktstr = 'Your actual junk chance depends on your junk chance reduction researched. See the table below for all values, and Junk for more information.' local junkttable = mw.html.create('table') junkttable	:addClass('wikitable') :css({						['text-align'] = 'right',						margin = '0 auto',					}) :tag('tr') :tag('th') :wikitext('Reduction') :done :tag('th') :wikitext('') :done :tag('th') :wikitext('Junk chance') :done :done :tag('tr') :tag('td') :wikitext('None') :done :tag('td') :wikitext(1) :done :tag('td') :wikitext(junk) :done :done for i,v in ipairs(junkreduction) do			junkttable	:tag('tr') :tag('td') :wikitext(i) :done :tag('td') :wikitext(v[1]) :done :tag('td') :wikitext(math.ceil(junknum * v[2] * 10)/10 .. '%') :done :done end tooltip_span = tostring(tooltips._span({'junkchance' .. junknum})) .. ' '		tooltip_div = tooltips._div({			name = 'junkchance' .. junknum,			content = junktstr .. '\n' .. tostring(junkttable),		}) end

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(tooltip_span .. 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..qtynotes) :done :done :tag('tr') :tag('th') :attr('colspan','2') :css('background','#e5e5e5') :wikitext('Possible materials') :done :done local specnote = ''

if type(specialchance) == 'string' then specnote = ' (Unknown chance)' elseif 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)) :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)) :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)) :done :done end end

ret:node(tooltip_div)

return ret end

return p