Module:SpawnGroupContainerTableEntry

De ARK Wiki
Aller à la navigation Aller à la recherche
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 = 'Tous les insectes'
local InsectSpecies = {'Méganeura irradiée', 'Trilobite irradié', 'Ammonite', 'Sangsue porteuse de maladie', 'Euryptéride',
					   'Abeille géante', 'Reine des abeilles géantes', 'Luciole', 'Lamproie', 'Sangsue', 'Méganeura', 'Insecte jarre à pétrole',
					   'Ouvrière Titanomyrma', 'Légionnaire Titanomyrma', 'Trilobite', 'Insecte jarre à eau'}


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('entrée de créature possiblement sans déclaration préalable de groupe')
		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('l\'entrée de la créature ne respecte pas le format "{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