Module:Spell cost table

--

local p = {}

local gep = require('Module:Exchange')._price local coins = require('Module:Coins')._amount

local combo_runes = { ['mist rune'] = { 'air rune', 'water rune' }, ['dust rune'] = { 'air rune', 'earth rune' }, ['mud rune'] = { 'water rune', 'earth rune' }, ['smoke rune'] = { 'air rune', 'fire rune' }, ['steam rune'] = { 'water rune', 'fire rune' }, ['lava rune'] = { 'earth rune', 'fire rune' } }

local staffs = { ['staff of air'] = { 'air rune' }, ['staff of water'] = { 'water rune' }, ['staff of earth'] = { 'earth rune' }, ['staff of fire'] = { 'fire rune' }, ['mud battlestaff'] = { 'water rune', 'earth rune' }, ['steam battlestaff'] = { 'water rune', 'fire rune' }, ['lava battlestaff'] = { 'earth rune', 'fire rune' }, ['smoke battlestaff'] = { 'air rune', 'fire rune' }, }

function p.main(frame) local args = frame:getParent.args local runes = {} for i=1,10 do       if not args['Rune'..i] then break end local rune = string.lower(args['Rune'..i])..' rune' local num = tonumber(args['Rune'..i..'num'] or 1) table.insert(runes,{rune,num}) end local no_staff = args.nostaff ~= nil return p._main(runes,no_staff) end

function p._main(runes,no_staff) local ret = mw.html.create('table') :addClass('wikitable') :tag('caption') :wikitext('Spell cost') :done :tag('tr') :tag('th') :wikitext('Input') :done :tag('th') :wikitext('Cost') :done :done :tag('tr') :tag('td') :wikitext(make_pics(runes)) :done :tag('td') :wikitext(total_price(runes)) :done :done local combos_used = {} local staffs_used = {} for i, v in pairs(combo_runes) do       local amtused = 0 local tblcopy = copytbl(runes) for _, w in ipairs(v) do           for j, x in ipairs(tblcopy) do                if w == x[1] then table.remove(tblcopy,j) if x[2] > amtused then amtused = x[2] end end end end if amtused > 0 then table.insert(tblcopy,{i, amtused}) table.insert(combos_used,tblcopy) end end if #combos_used > 0 then ret:tag('tr') :tag('th') :attr('colspan','2') :wikitext('Combo runes') :done :done for _, v in ipairs(combos_used) do           ret:tag('tr') :tag('td') :wikitext(make_pics(v)) :done :tag('td') :wikitext(total_price(v)) :done :done end end

for i, v in pairs(staffs) do       local isused = false local tblcopy = copytbl(runes) for _, w in ipairs(v) do           for j, x in ipairs(tblcopy) do                if w == x[1] then table.remove(tblcopy,j) isused = true end end end if isused then table.insert(tblcopy,{i, 0}) table.insert(staffs_used,tblcopy) end end

if #staffs_used > 0 and not no_staff then ret:tag('tr') :tag('th') :attr('colspan','2') :wikitext('Staves') :done :done for _, v in ipairs(staffs_used) do           ret:tag('tr') :tag('td') :wikitext(make_pics(v)) :done :tag('td') :wikitext(total_price(v)) :done :done end end return ret end

function copytbl(tbl) local ret = {} for _, v in ipairs(tbl) do       table.insert(ret,v) end return ret end

function make_pics(runes) local ret = {} for _, v in ipairs(runes) do       if v[1] > 0 then table.insert(ret,..v[2]..) end table.insert(ret,'') end return table.concat(ret) end

function total_price(runes) local ret = 0 for _, v in ipairs(runes) do       if v[2] > 0 then ret = ret + gep(v[1]) * v[2] end end return coins(ret) end

return p