Module:Scavenge

local p = {} function p.gui( f ) local function getTemplateArgs( str ) local result = {} for v in mw.text.gsplit(str, "+", true) do                   local pair = mw.text.split(v, "=", true) if table.getn(pair) == 2 then result[pair[1]] = pair[2] else table.insert(result, v)                   end end return result end local args = f.args or f       if f == mw.getCurrentFrame and args[1] == nil then args = f:getParent.args else f = mw.getCurrentFrame end mw.logObject(args) local tool = args[1] or '' local block = args[2] or '' local output = {} local total = 0

-- Iterate through the Scavenge inventory slots and gathers valid cells. -- Format should be a Grid Cell template with the weight = number for i=3,38 do               local drop = {} if args[i] ~= nil then drop.cell = getTemplateArgs(args[i]) drop.weight = tonumber(drop.cell.weight) -- mw.logObject(drop) if drop.cell ~= nil and drop.weight ~= nil then table.insert(output, drop) total = total + drop.weight end end end

local result = {tool, block} for i,drop in ipairs(output) do               local cell = drop.cell cell.weight = nil cell.desc = "&7" .. (drop.weight/total*100) .. "% Chance" local title = cell[1] if not (cell[1] == "O" or cell[1] == "Gc") then title = "Gc" else -- Note: Slow operation but table is small enough table.remove(cell, 1) -- Shift down args to support the templates end local gc = f:expandTemplate{title = title, args = cell} table.insert(result, gc) end

local grid = f:expandTemplate{title = "Cg/Scavenge", args = result} mw.log(grid) return grid end

return p