Module:LootTable

local p = {}

function _removeDuplicates(t) -- borrowed from: https://www.mediawiki.org/w/index.php?title=Module:TableTools local ret, exists = {}, {} for i, v in ipairs(t) do		if not exists[v] then ret[#ret + 1] = v			exists[v] = true end end return ret end

function _parseRange(s) local v = mw.text.split(s, '..', true) if #v == 2 then return tonumber(v[1]), tonumber(v[2]) end v = tonumber(s) return v, v end

function p.loottable( f ) local dlclink = require('Module:DLCLink').link local args = f:getParent.args

local caption = args.name or nil local nonRepeating = args.nonRepeating or false local showQuality = args.showQuality or false local setRolls = args.setRolls or 1

-- aggregator local allItems = {} local allLootSets = {} -- start reading loot entry sets local currentSet = nil local currentEntry = nil local setWeightSum = 0 for setIndex, v in ipairs(args) do    if setIndex >= 1 and #v > 0 then local params = mw.text.split(v, ', ', true) local opname = mw.text.trim(params[1]) if opname == 'begin set' then -- flush previous set if currentSet ~= nil then table.insert(allLootSets, currentSet) end -- parse set info local name = mw.text.trim(params[2]) local weight, minQty, maxQty = 1, 1, 1 for paramIndex, p in ipairs(params) do   			if paramIndex > 2 and #p > 0 then --local name, value = mw.text.split(p, ':', true) --name, value = mw.text.trim(name), mw.text.trim(value) --if name == 'weight' then --	weight = tonumber(value) --elseif name == 'quantity' then --	minQty, maxQty = _parse_range(value) --else --	return 'set #' .. setIndex .. ': unrecognized parameter "' .. name .. '"' --end end end -- initialize set info currentSet = { ["name"] = name, ["weight"] = weight, ["minQty"] = minQty, ["maxQty"] = maxQty, ["entryWeightSum"] = 0, ["entries"] = {}, }   		-- push to total list setWeightSum = setWeightSum + weight table.insert(allLootSets, currentSet) elseif opname == 'entry' and currentSet ~= nil then -- parse entry info local name = mw.text.trim(params[2]) local weight = 1 for paramIndex, paramValue in ipairs(params) do   			if paramIndex > 2 and #paramValue > 0 then local pPair = mw.text.split(paramValue, ':', true) local pName = mw.text.trim(pPair[1]) local pValue = mw.text.trim(pPair[2]) if pName == 'weight' then weight = tonumber(pValue) end end end currentEntry = { ["name"] = name, ["weight"] = weight, ["items"] = {}, }   		-- push this entry onto the set currentSet["entryWeightSum"] = currentSet["entryWeightSum"] + weight table.insert(currentSet["entries"], currentEntry) elseif #params >= 2 and tonumber(opname) ~= nil and currentEntry ~= nil then -- insert item local weight = tonumber(opname) local item = mw.text.trim(params[2]) table.insert(allItems, item) table.insert(currentEntry["items"], {weight, item}) else return 'invalid param or wrong context at ' .. setIndex .. ' (' .. opname .. ')' end end end -- flush set if currentSet ~= nil then table.insert(allLootSets, currentSet) end

local output = {} --for _, item in ipairs(itemList) do --  local fileName = string.gsub(string.gsub(item[1], '[:/]', ' '), '+', '-')

-- table.insert(output, ''..(item[2] ~= nil and item[2]..quantitySign or '')..' '..dlclink(item[1],noDlcIcon)..'') --end

allItems = _removeDuplicates(allItems) table.sort(allItems)

return '\n' .. (caption ~= nil and '; ' .. caption .. '\n' or '') .. f:preprocess(' \nOverview=\n'                     .. ' ') .. ' ' end

return p