Module:LootTable/sandbox

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 == 'yes' local showQuality = args.showQuality == 'yes' local setRolls = args.setRolls or 1 local overviewNote = args.overviewNote or nil local splitUnique = args.splitUnique == 'yes' local collapseCommonLoot = args.collapseCommonLoot == 'yes' local cssClasses = args.class or '' local difficulty = args.difficulty or nil if difficulty ~= nil then local DIFFICULTY_CLASSES = { ['Alpha'] = 'red', ['Beta'] = 'yellow', ['Gamma'] = 'green', } 	cssClasses = 'loottable-color-' .. DIFFICULTY_CLASSES[difficulty] .. ' ' .. cssClasses end

-- aggregator local allUniqueItems = {} local allItems = {} local allLootSets = {} -- reusable variables local weight, minQty, maxQty, minQu, maxQu, isEntryUnique = 1, 1, 1, 1, 1, false -- 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]) weight, minQty, maxQty, minQu, maxQu = 1, 1, 1, 1, 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) elseif pName == 'quantity' then minQty, maxQty = _parseRange(pValue) elseif pName == 'quality' then minQu, maxQu = _parseRange(pValue) else return 'set #' .. setIndex .. ': unrecognized parameter "' .. pName .. '"' end end end -- initialize set info currentSet = { ["name"] = name, ["weight"] = weight, ["minQty"] = minQty, ["maxQty"] = maxQty, ["minQu"] = minQu, ["maxQu"] = maxQu, ["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]) weight, minQty, maxQty, minQu, maxQu, isEntryUnique = 1, 1, 1, 1, 1, false for pIndex, param in ipairs(params) do   			if pIndex > 2 and #param > 0 then param = mw.text.trim(param) if param == 'unique' then isEntryUnique = true else -- handle key:value pairs local pPair = mw.text.split(param, ':', true) local pName = mw.text.trim(pPair[1]) local pValue = mw.text.trim(pPair[2]) if pName == 'weight' then weight = tonumber(pValue) elseif pName == 'quantity' then minQty, maxQty = _parseRange(pValue) elseif pName == 'quality' then minQu, maxQu = _parseRange(pValue) else return 'entry #' .. setIndex .. ': unrecognized parameter "' .. pName .. '"' end end end end currentEntry = { ["name"] = name, ["weight"] = weight, ["minQty"] = minQty, ["maxQty"] = maxQty, ["minQu"] = minQu, ["maxQu"] = maxQu, ["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(currentEntry["items"], {weight, item}) -- insert item to total collections if isEntryUnique then table.insert(allUniqueItems, item) else table.insert(allItems, item) end else return 'invalid param or wrong context at ' .. setIndex .. ' (' .. opname .. ')' end end end -- flush set if currentSet ~= nil then table.insert(allLootSets, currentSet) end

--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) allUniqueItems = _removeDuplicates(allUniqueItems) table.sort(allItems) table.sort(allUniqueItems)

local overview = '' local detailed = {} -- generate overview -- split unique items from general pool if splitUnique then overview = '\n; Unique to this mission\n\n; Common\n' end -- add a note if overviewNote ~= nil then overview = overview .. overviewNote .. '\n' end -- make a collapsible if collapseCommonLoot then overview = overview .. ' ' end -- actual list overview = overview .. '' -- end the collapsible if collapseCommonLoot then overview = overview .. ' ' end

return ' \n' .. (caption ~= nil and '; ' .. caption .. '\n' or '') .. f:preprocess(' \nOverview=' .. overview .. '\n'                     .. '|-|Detailed=' .. table.concat(detailed, '\n') .. ' ') .. ' ' end

return p