Module:Disassembly

--

local p = {} local infobox = require('Module:Infobox') local onmain = require('Module:Mainonly').on_main local disdata = mw.loadData('Module:Disassemble/data') local materials = mw.loadData('Module:Disassemble/mats') local tooltips = require('Module:Tooltip') local yesno = require('Module:Yesno')

-- no more ? marks maybe? function basic_tooltip(arg) return string.format(' ? ',arg or '') end

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 p.main(frame) local args = frame:getParent.args

local ret = infobox.new(args)

ret:defineParams{ { name = 'category', func = getCategory, dupes = true }, { name = 'catnames', func = { name = getCategoryNames, params = { 'category' } }, dupes = true }, { name = 'often_mats', func = { name = getMats, params = { 'category', 'often', 'often' }, flag = { 'd', 'd', 'r' } } }, { name = 'sometimes_mats', func = { name = getMats, params = { 'category', 'sometimes', 'sometimes' }, flag = { 'd', 'd', 'r' } } }, { name = 'rarely_mats', func = { name = getMats, params = { 'category', 'rarely', 'rarely' }, flag = { 'd', 'd', 'r' } } }, { name = 'level', func = 'numbers', dupes = true }, { name = 'x10', func = { name = getX10, params = { 'category', 'x10' } } }, { name = 'xp', func = { name = getXP, params = { 'level', 'x10' } } }, { name = 'junkraw', func = { name = getRawJunk, params = { 'level' } } }, { name = 'junk', func = { name = getJunk, params = { 'junkraw' } } }, { name = 'compqty', func = { name = getCompQty, params = { 'category', 'compqty' } } }, { name = 'itemqty', func = { name = getItemQty, params = { 'category', 'itemqty' } } }, { name = 'special_mats', func = { name = getSpecialMats, params = { 'special' } } }, { name = 'specialchance', func = { name = yesno, params = { 'specialchance', '???' }, flag = { 'd', 'r' } }, dupes = true }, }

ret:defaultOnly{ 'often_mats', 'sometimes_mats', 'rarely_mats', 'special_mats' } ret:setMaxButtons('8')

ret:create ret:cleanParams

ret:attr({ id = 'DisassemblyT' }) ret:float('left') ret:css({ width = '300px' }) ret:defineName('Disassembly')

ret	:addRow{ { tag = 'th', content = 'Disassembly XP', css = { ['text-align'] = 'left' }, title = 'Experience received for disassembling' }, { tag = 'argd', content = 'xp', css = { ['text-align'] = 'right' } } }		:addRow{ { tag = 'th', content = 'Item quantity required', css = { ['text-align'] = 'left' }, title = 'Amount disassembled per action' }, { tag = 'argd', content = 'itemqty', css = { ['text-align'] = 'right' } } }		:addRow{ { tag = 'th', content = 'Material count', css = { ['text-align'] = 'left' }, title = 'The amount of materials received normally (not including specials); shown in chat window' }, { tag = 'argd', content = 'compqty', css = { ['text-align'] = 'right' } } }		:addRow{ { tag = 'th', content = 'Base junk chance', css = { ['text-align'] = 'left' }, title = 'Base chance of receiving junk' }, { tag = 'argd', content = 'junk', css = { ['text-align'] = 'right' } } }		:addRow{ { tag = 'th', content = 'Materials received', colspan = '2', css = { background = '#E5E5E5' } }, }

local function matRow(builder, matname) builder :tag('td') :attr('colspan','2') :wikitext(string.format(' %s',matname,matname,matname)) :done :done end

local spec = ret:param('special_mats', 'd') local schance = ret:param('specialchance','d') local oftn = ret:param('often_mats', 'd') local smts = ret:param('sometimes_mats', 'd') local rrly = ret:param('rarely_mats', 'd')

if spec[1] then local specchance if schance == true then specchance = '100%' elseif schance == false then specchance = 'not 100%' else specchance = 'Unknown chance' end

ret:addRow{ { tag = 'th', content = string.format('Special (%s)',specchance), colspan = '2', 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' } }		for _, v in ipairs(spec) do			ret	:tag('tr') :tag('td') :attr('colspan','2') :wikitext(mw.ustring.format('%s × %s', v.q,v.n,v.n,v.n)							) :done :done end end

if oftn[1] then ret:addRow{ { tag = 'th', content = 'Often', colspan = '2' } }		for _, v in ipairs(oftn) do			matRow(ret:tag('tr'), v)		end end

if smts[1] then ret:addRow{ { tag = 'th', content = 'Sometimes', colspan = '2' } }		for _, v in ipairs(smts) do			matRow(ret:tag('tr'), v)		end end

if rrly[1] then ret:addRow{ { tag = 'th', content = 'Rarely', colspan = '2' } }		for _, v in ipairs(rrly) do			matRow(ret:tag('tr'), v)		end end

if onmain or true then local a1 = ret:param('all') local a2 = ret:categoryData ret:wikitext(addCategories(a1,a2)) end

ret:finish return ret:tostring end

-- param parsing function getCategory(arg) local _arg = string.lower(arg or '') if _arg == 'no' or _arg == 'custom' then return 'custom' elseif disdata[_arg] then -- nothing else _arg = nil end

return _arg end

function getCategoryNames(arg) local _arg = string.lower(arg or '') if _arg == 'custom' then return 'custom' elseif disdata[_arg] then _arg = disdata[_arg].cat else _arg = nil end

return _arg end

function getMats(_cat,arg,mattype) local mat_list

if _cat == 'custom' then mat_list = string.lower(arg or '') else local cat = disdata[_cat] or {} mat_list = cat[mattype] end

if not mat_list then return {} end

local ret = {}

for _, v in ipairs(mw.text.split(mat_list,',')) do		local _v = string.lower(v) :gsub('components?','') :gsub('parts?','')

_v = mw.text.trim(_v)

local _name = materials[_v] if _name then table.insert(ret,_name) end end

return ret end

function getSpecialMats(arg) local mat_list = string.lower(arg or '')

if not mat_list then return {} end

local ret = {}

for _, v in ipairs(mw.text.split(mat_list,',')) do		local _v = string.lower(v) :gsub('components?','') :gsub('parts?','')

_v = mw.text.trim(_v) local _m,_q = _v:match('(%w+)%s*%[?(%d*)%]?') local _name = materials[_v] if _name then table.insert(ret,{ n = _name, q = tonumber(_q) or '?' }) end end

return ret end

function getX10(_cat,x10) local xx10 if _cat == 'custom' then xx10 = string.lower(x10 or 'no') else local cat = disdata[_cat] or {} xx10 = cat.x10 end

return yesno(_cat,false) end

function getRawJunk(_l) local l = tonumber(_l)

if not l then return nil end

if l >= 90 then return 0 end

if l >= 75 then return junkpast75[l] end

local junk = 100 - 1.1 * l

junk = math.floor((junk) * 1000 + 0.009)/1000

return junk end

function getJunk(_j) local j = tonumber(_j)

if not j then return nil end

local ji,jd = math.floor(j), (j%1) * 10

return string.format('%s.%s%%',ji,jd) end

function getXP(_l,x10) local l = tonumber(_l)

if not l then l = 1 end

local mult = x10 and 10 or 1

local xp = math.max(math.floor(l * 0.03 * mult * 1000 + 0.009)/1000,0.1)

xp = math.floor(xp * 10 + 0.05)/10

if xp % 1 == 0 then xp = xp..'.0' end

return xp end

function getCompQty(_cat,qty) local compqty if _cat == 'custom' then compqty = string.lower(qty or 'no') else local cat = disdata[_cat] or {} compqty = cat.compqty end

return tonumber(compqty) end

function getItemQty(_cat,qty) local itemqty if _cat == 'custom' then itemqty = string.lower(qty or 'no') else local cat = disdata[_cat] or {} itemqty = cat.itemqty end

return tonumber(itemqty) or 1 end

function addCategories(args,cdata) local ret = {}

local spec_mats = {} local has_spec = false for _, v in ipairs(args.special_mats.d) do		table.insert(spec_mats,v.n)		has_spec = true end

for _, v in ipairs({				args.often_mats.d,				args.sometimes_mats.d,				args.rarely_mats.d,				spec_mats			}) do		for _, w in ipairs(v) do			table.insert(ret,string.format('Items that disassemble into %s',w)) end end

if args.catnames.d == 'custom' then -- nothing elseif infobox.isDefined(args.catnames.d) then table.insert(ret,string.format('Disassemble category/%s',args.catnames.d)) else table.insert(ret,'Missing disassembly category') end

local catswitches = args.catnames.switches if catswitches then for _, v in ipairs(catswitches) do			if infobox.isDefined(v) then if v == 'custom' then -- nothing else table.insert(ret,string.format('Disassemble category/%s',v)) end else table.insert(ret,'Missing disassembly category') end end end

if has_spec then if not cdata.specialchance.all_defined then table.insert(ret,'Missing special chance') end

for _, v in ipairs(spec_mats) do			if type(v.q) ~= 'number' then table.insert(ret,'Missing special material quantity') end end end

local cats = {} for _, v in ipairs(ret) do		table.insert(cats,string.format('',v)) end

return table.concat(cats) end return p