Module:SpawnGroupContainerTableEntry

From ARK Wiki
Jump to navigation Jump to search
Template-info.png Documentation

local p = {}

local CellThickBorder = 'style="border-left: 3px solid var(--theme-border-color)"'
local CellThickBorderT = 'style="border-top: 3px solid var(--theme-border-color)"'
local CellThickBorderLT = 'style="border-top: 3px solid var(--theme-border-color);border-left: 3px solid var(--theme-border-color)"'

local AnyInsect = 'Any Insect'
local InsectSpecies = {'Aberrant Meganeura', 'Aberrant Trilobite', 'Ammonite', 'Diseased Leech', 'Eurypterid',
					   'Giant Bee', 'Giant Queen Bee', 'Glowbug', 'Lamprey', 'Leech', 'Meganeura', 'Oil Jug Bug',
					   'Titanomyrma Drone', 'Titanomyrma Soldier', 'Trilobite', 'Water Jug Bug'}


local function trim( s )
    return (s:gsub( '^[\t\r\n\f ]+', '' ):gsub( '[\t\r\n\f ]+$', '' ))
end


function p.main(frame)
	local args = frame.args
	local name = args.name
	
	-- collections
	local groups = {}
	local limits = {}
	
	-- load-time state refs
	local currentGroup = nil
	
	-- load data
	for _, param in ipairs(args) do
		param = trim(param)
		
		local delimpos = string.find(param, ': ', 1, true)
		if delimpos == nil and currentGroup == nil then
			error('possible creature entry without a prior group declaration')
		end
		
		if delimpos == nil then
			-- op: append creature to current group
			-- format: {creature name}, {chance#1}%, {chance#2}%, ..., {chance#N}%
			-- find position of the delimiter
			local values = mw.text.split(param, ', ', true)
			if #values <= 1 then
				error('creature entry does not follow the format of "{creature name}, {chance}%..."')
			end
			
			local result = { creature = trim(values[1]), guaranteedCount = 0 }
			for index, text in ipairs(values) do
				if index ~= 1 then
					text = trim(text)
					result[#result+1] = text
					if text == '100%' then
						result.guaranteedCount = result.guaranteedCount + 1
					end
				end
			end
			currentGroup[#currentGroup+1] = result
		else
			local keyword = string.sub(param, 1, delimpos-1)
			local value = trim(string.sub(param, delimpos+2, #param))
			
			if keyword == 'group' then
				currentGroup = { weight = value }
				groups[#groups+1] = currentGroup
			elseif keyword == 'limit' then
			else
				error('unknown parameter type "' .. keyword .. '"')
			end
		end
	end
	
	-- generate output
	local rowCount = 0
	local out = {
		'|-',
		"[RESERVED]",
	}
	
	for index, group in ipairs(groups) do
		if index ~= 1 then
			out[#out+1] = '|-'
		end
		
		out[#out+1] = '|rowspan=' .. #group .. ' '..(index==1 and CellThickBorderLT or CellThickBorder)..'|' .. group.weight
		for jndex, member in ipairs(group) do
			if jndex ~= 1 then
				out[#out+1] = '|-'
			end
			
			local caption = '[['..member.creature..']]'
			if member.creature == AnyInsect then
				caption = '{{HoverNote|' .. AnyInsect .. '|' .. table.concat(InsectSpecies, ', ') .. '}}'
			end
			
			if #member > 1 then
				if #member == member.guaranteedCount then
					caption = caption .. ' (' .. member.guaranteedCount..')'
				else
					caption = caption .. ' (' .. member.guaranteedCount..'-'..#member..')'
				end
			end
		
			out[#out+1] = (index==1 and jndex==1 and '|' .. CellThickBorderT .. '|' or '|') .. caption
			out[#out+1] = (index==1 and jndex==1 and '|' .. CellThickBorderT .. '|' or '|')..table.concat(member, ' / ')
			rowCount = rowCount+1
		end
	end
	
	out[2] = "|" .. CellThickBorderT .. " rowspan=" .. rowCount .. "|'''" .. name .. "'''"
	return table.concat(out, '\n')
end

return p