Module:Loot

Documentation for this module may be created at Module:Loot/doc

local p = {}
local data = mw.loadData("Module:Data/Loot")
local enemyData = mw.loadData("Module:Data/Enemies")

function p.createSourceLootTable(frame)
	local loot = data[frame.args.name]
	local enemy = enemyData[frame.args.enemy]

	-- table header
	local str = mw.html.create('')
		:wikitext('{| class="table table-dark" style="width:auto"'):newline()
		
	-- check if enemy parameter is not empty
	if enemy ~= nil then
		loot = data[enemy.lootTable]
	end

	if loot.rolls ~= 1 then
		str:wikitext('! Item !! Chance per roll !! Chance for 1 or more'):newline()
	else
		str:wikitext('! Item !! Chance'):newline()
	end
	
	if enemy ~= nil then
		if enemy.chestToSpawn ~= nil or type(enemy.guaranteedDrops) == "table" then
			str:wikitext('|- class=subheader'):newline()
				:wikitext('! colspan=3 | Guaranteed'):newline()
				
			if enemy.chestToSpawn ~= nil then
				str:wikitext('|-'):newline()
					:wikitext('| colspan=3 | ' .. frame:expandTemplate{title = 'Icon', args = {enemy.chestToSpawn}}):newline()
			end
			if type(enemy.guaranteedDrops) == "table" then
				for objectKey, objectValue in pairs(enemy.guaranteedDrops) do
					local amount = ''
					if objectValue.amount ~= 1 then
						if type(objectValue.amount) == "table" then
							amount = objectValue.amount[1] .. "-" .. objectValue.amount[2];
						else
							amount = objectValue.amount
						end
					end
					
					str:wikitext('|-'):newline()
						:wikitext('| colspan=3 | ' .. frame:expandTemplate{title = 'Icon', args = {objectValue.name, amount}}):newline()
				end
			end
		end
	end
	
	-- add 1 roll of guaranteed loot if it exist
	if loot.guaranteedLoot[1] ~= nil then
		str:wikitext('|- class=subheader'):newline()
			:wikitext('! colspan=3 | 1 roll'):newline()
			
		for objectKey, objectValue in pairs(loot.guaranteedLoot) do
			local amount = ''
			if objectValue.amount ~= 1 then
				if type(objectValue.amount) == "table" then
					amount = objectValue.amount[1] .. "-" .. objectValue.amount[2];
				else
					amount = objectValue.amount
				end
			end
			
			str:wikitext('|-'):newline()
				:wikitext(objectValue.itemName == "Nothing" and '| Nothing' or '| ' .. frame:expandTemplate{title = 'Icon', args = {objectValue.itemName, amount}}):newline()
				:wikitext('| colspan=2 | ' .. objectValue.dropChance):newline()
		end
	end

	-- add multiroll header if it rolls more than 1
	if loot.rolls ~= 1 then
		local rolls  = ''
		
		if loot.rolls[2] ~= nil then
			if loot.guaranteedLoot[1] ~= nil then
				rolls = (loot.rolls[1] - 1) .. "-" .. (loot.rolls[2] - 1);
			else
				rolls = loot.rolls[1] .. "-" .. loot.rolls[2];
			end
		else
			if loot.guaranteedLoot[1] ~= nil then
				rolls = loot.rolls - 1;
			else
				rolls = loot.rolls;
			end
		end
		
		str:wikitext('|- class=subheader'):newline()
			:wikitext('! colspan=3 | ' .. rolls .. ' rolls'):newline()
	end

	-- add regular loot table
	for objectKey, objectValue in pairs(loot.loot) do
		local amount = ''
		if objectValue.amount ~= 1 then
			if type(objectValue.amount) == "table" then
				amount = objectValue.amount[1] .. "-" .. objectValue.amount[2];
			else
				amount = objectValue.amount
			end
		end
		
		str:wikitext('|-'):newline()
			:wikitext(objectValue.itemName == "Nothing" and '| Nothing' or '| ' .. frame:expandTemplate{title = 'Icon', args = {objectValue.itemName, amount}}):newline()
			:wikitext('| ' .. objectValue.dropChance):newline()
			
		if loot.rolls ~= 1 then
			str:wikitext('| ' .. objectValue.dropChanceAtLeastOne):newline()
		end
	end
	
	str:wikitext('|}')
	
	return str
end

return p
Cookies help us deliver our services. By using our services, you agree to our use of cookies.