Module:InstanceLootTable

From Warcraft Wiki
Jump to navigation Jump to search

This is the main module that handles the table generation. For the data such as loot, see the following instead:


local getArgs = require('Module:Arguments').getArgs
local p = {}

function p.GenerateTable(frame)
	local args = getArgs(frame, {wrappers = 'Template:InstanceLootTable'})
	
	if args.tier ~= nil and args.instance ~= nil then
		--Load instance data for the selected tier
		local data = mw.loadData('Module:InstanceLootTable/data/'..args.tier)
		
		if data[args.instance] ~= null then	
			local tableData = {}
			local isFirst = true
			
			--Looping through all the encounters in the selected instance to get all loot items
			for k,v in pairs(data[args.instance]) do
				--Check if we just fetching a single boss
				if args.boss ~= nil then
					--Displaying just a single boss
					if args.boss == k then
						--Loop through all items to generate rows
						local firstItem = true;
						for item, difficulties in pairs(v) do
							--Get Difficulty data for item
							local instanceLootArgs = {}
							instanceLootArgs['item'] = item
							local difficultyFound = false
							local lastDifficultyInserted = null
							for k, difficulty in ipairs(difficulties) do
								if difficulty ~= "n" then
									difficultyFound = true
									lastDifficultyInserted = difficulty
									instanceLootArgs[difficulty] = 1
								end
							end
							
							--If the item has no difficulty, then we should not display the () in Versions/InstanceLoot.
							if difficultyFound == false then
								instanceLootArgs['nodifficulty'] = 1
							end
							
							--Track the last difficulty inserted so we can check whether to add a middot or not after each difficulty
							if lastDifficultyInserted ~= null then
								instanceLootArgs['lastdifficulty'] = lastDifficultyInserted
							end
							
							--All rows
							table.insert(tableData, frame:callParserFunction("#using:"..item,{
								"InstanceLootTable/list",
								["item"] = frame:expandTemplate{ title = 'Versions/InstanceLoot', args = instanceLootArgs },
							}))		
						end
					end
				else
					--Displaying all encounters for instance
					
					--Add row header
					if isFirst then
						isFirst = false
						table.insert(tableData, frame:expandTemplate{ title = 'i-note', args = {data.lastUpdate}} .. "\n{| class='darktable sortable zebra plainlinks' align=center\n|-\n! Encounter !! Loot\n")
					end
					
					--Calculate the rowspan for the encounter column by counting number of items
					local count = 0
					for i in pairs(v) do
						count = count + 1
					end
					
					--Loop through all items to generate rows
					local firstItem = true;
					local items = "";
					for item, difficulties in pairs(v) do
						--Get Difficulty data for item
						local instanceLootArgs = {}
						instanceLootArgs['item'] = item
						for k, difficulty in ipairs(difficulties) do
							if difficulty ~= "n" then
								instanceLootArgs[difficulty] = 1
							end
						end
						
						if firstItem then
							--Row including the encounter column
							table.insert(tableData, frame:callParserFunction("#using:"..item,{
								"InstanceLootTable/row",
								["item"] = frame:expandTemplate{ title = 'Versions/InstanceLoot', args = instanceLootArgs },
								["encounterRowspan"] = count,
								["encounter"] = k
							}))
							firstItem = false;
						else
							--Rest of rows not including the encounter column
							table.insert(tableData, frame:callParserFunction("#using:"..item,{
								"InstanceLootTable/row",
								["item"] = frame:expandTemplate{ title = 'Versions/InstanceLoot', args = instanceLootArgs },
							}))		
						end
					end
				end
			end
		
			if args.boss == null then
				table.insert(tableData, "\n|-\n|}")
			end
			
			return table.concat(tableData)
			--return mw.text.jsonEncode(table.concat(tableData))
		else
			return args.instance .. " does not exist in [[Module:InstanceLootTable/data]]"	
		end
	else
		if args.tier == null then
			return " tier must be specified"
		elseif args.instance == null then
			return " instance must be specified"
		end
	end
end
return p