Module:Container loot

From The Cycle: Frontier Wiki
Revision as of 21:15, 31 May 2023 by VeryGreatFrog (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

This module spits out the loot of a specific container. If it has multiple tiers the output will be tabbed. The output is not sorted - this is not possible.

Parameters

Must always call the main function. The second argument must be name of a container that is found in [[Module:Item spawns/containerData.json]

Example

{{#invoke:Container loot|main|Ammo Box}}

local p = {} --p stands for package

p.items = mw.loadJsonData( "Module:Item infobox/itemData.json" )
p.lootPools = mw.loadJsonData( "Module:Item spawns/lootPoolData.json" )
p.containers = mw.loadJsonData( "Module:Item spawns/containerData.json" )

local args = mw.getCurrentFrame().args

for k, v in pairs(p.containers) do
    if args[1] == k then
        p.container = k
        p.spawns = v
        break
    end
end


p.output = ''
p.tables = {}
function p.main ( frame ) 
	p.output = p.output ..  frame:expandTemplate{title = 'Notice', args = { "The table describes the typical loot of the container. Some containers of this type might have different or unique loot in them. Those containers are not shown in this list. <br>The spawn chances are an ''approximation'' and should ''not'' be taken as exact values." } }
	local isTabbed = false
	if tablelength(p.spawns) > 1 then 
		isTabbed = true
	end
	
	for k, v in pairs(p.spawns) do
		local spawns = p.lootPools[v]
		
		local spawnOutput = "<table class=\"wikitable sortable\"><caption>Items in this container</caption><tr><th>Item</th><th>Max spawn chance</th></tr>"
		
		local s = spawns['items']
		local data = {}
		for c, d in pairs(s) do
			
			local number = d['chance']
			number =  string.format("%03d", number)
			table.insert(data, number .. "$" .. c)
		end
		
		table.sort(data, function(a,b) return a > b end)
		local output2 = ''
		for c, d in ipairs(data) do
			local name = mw.text.split(d, '$', true)[2]
			local number = s[name]['chance']
			-- get the name of the item, using Icon link if possible
			if p.items[name] ~= nil then
				local n = p.items[name]['inGameName']
				n = string.gsub(n, 'ammo', 'Ammo')
				name =  frame:expandTemplate{ title = 'Icon link', args = { n } }
			elseif name == 'TOOL_MineralScanner_01' then
				name =  frame:expandTemplate{ title = 'Icon link', args = { 'Scanner' } }
			elseif name == 'TOOL_Mining_01' then
				name =  frame:expandTemplate{ title = 'Icon link', args = { 'Heavy Mining Tool' } }
			else
				name = '[[' .. name .. ']]'
			end
			
			-- add it to our output
			spawnOutput = spawnOutput .. "<tr><td>" .. name .. "</td><td>" .. round(number, 4) .. "% </td></tr>"
		end
		spawnOutput = spawnOutput .. '</table>'
		p.tables[k] = spawnOutput
	end
	
	if isTabbed then
		local text = ''

		for k, v in pairs(p.spawns) do
			text = text .. '|-|Tier ' .. tonumber(p.lootPools[v]["tier"]) .. '='
			text = text .. p.tables[k]
		end
		return p.output .. frame:callParserFunction('#tag:tabber', text) 
	else
		return p.output .. p.tables[1]
	end
end

function tablelength(T)
  local count = 0
  for _ in pairs(T) do count = count + 1 end
  return count
end

-- thanks http://lua-users.org/wiki/SimpleRound
function round(num, numDecimalPlaces)
  local mult = 10^(numDecimalPlaces or 0)
  return math.floor(num * mult + 0.5) / mult
end

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