Dead by Daylight Wiki
Advertisement

local p = {}
local utils = require("Module:Utils")
local data = require("Module:Datatable" .. utils.lang())
local mathOps = require("Module:MathOps")
local str = require("Module:Strings")
local frame = mw.getCurrentFrame()

strings = {
	noRealm = "The currently defined Realm is not yet stored in the 'Realms Table' in the [[Module:Datatable|Datatable Module]]",
	unknownRealmImage = "UnknownRealmImage",
	realms = "Lokacje",
	orString = "lub",
	isOneOf = "jest jedną z",
	isTheOnly = "jest jedynym",
	wasMap = "było",
	beforeRetired = "zanim zostało wycofane",
	--oneMapHeaderOrder = "#1# #2# #3# #4# #5# #6# #7# #8# #9# #10# #11# #12# #13# #14# #15# #16# #17# #18# #19# #20#", --if changed order is needed
	--multipleMapsHeaderOrder = "#1# #2# #3# #4# #5# #6# #7# #8# #9# #10# #11# #12# #13# #14# #15# #16# #17# #18# #19# #20#", --if changed order is needed
	--retiredMapHeaderOrder = "#1# #2# #3# #4# #5# #6# #7# #8# #9# #10# #11# #12# #13# #14# #15# #16# #17# #18# #19# #20#", --if changed order is needed
	map = "Mapa",
	maps = "Mapy",
	inThe = "w",
	realm = "Lokacja",
	realmInfo = "Powiązana Lokalizacja",
	wipName = "WIP/File Name",
	areaTiles = "Powierzchnia Płyt (sqP, 8x8 m<sup>2</sup>)",
	areaMetres = "Powierzchnia Mapy (m<sup>2</sup>)",
	areaTotalTiles = "Pełna Powierzchnia Płyt (sqP)",
	areaTotalMetres = "Pełna Powierzchnia Mapy (m<sup>2</sup>)",
	release = "Aktualizacja Publikacji",
	patch = "Aktualizacja",
	landmarkSound = "Dźwięk Punktu Orientacyjnego",
	realmIntro = "Motyw Początkowy",
	layout = "Układ Mapy",
	layouts = "Układ Map",
	
	codeName = "Code Name",
	killer = "Powiązany Zabójca",
	palette = "Paleta Kolorów",
	location = "Lokalizacja",
	dlc = "Powiązane DLC",
	mapCount = "Liczba Map",
	releaseCount = "Aktualizacje Publikacji",
	releaseChapter = "Powiązany Rozdział",
	chapter = "ROZDZIAŁ",
	
	retiredMap = bclr("red", "WYCOFANE")
}

function getRealmByName(name)
	for _, realm in ipairs(data.realms) do
		if realm.name == name then return realm end
	end
end

--Function to convert string "true/false" to actual boolean values
function resolveBoolParam(param)
	if type(param) ~= "string" then
		return utils.bool(param.args[1])
	end
end

--Function returning an average size across all maps stored in table maps.
--If convertToMetres is set to true then result will be returned in square metres
function p.getAverageMapSize(convertToMetres)
	convertToMetres = resolveBoolParam(convertToMetres)--utils.bool(convertToMetres)
	local result = 0
    local sum = 0
	local i = 1
    	
    while maps[i] do --going through all maps
    	local mapTiles = maps[i].ASTiles
    	if(type(mapTiles) ~= "table") then 
			sum = sum + mapTiles
		else --in case mapTiles are described in multiple layers
			local j = 1
			while mapTiles[j] do
				sum = sum + mapTiles[j][1] --mapTiles are expected to be first parameter
				j = j + 1
			end
		end
		i = i + 1
	end
	
	if i > 1 then i = i - 1 end --as the index starts at 1 we must decrease the counter as the loop ends with incrementation of counter/index even after last map looped
	sum = sum / i --making average
	if convertToMetres then result = p.toSMetres(sum) else result = sum end
	return mathOps.round(result)
end

--Call a function with a string parameter that will be resolved afterwards
function p.getCountOfMaps()
	return utils.getCount("map")
end

function p.getCountOfRealms()
	return utils.getCount("realm")
end

--Returns the biggest map from the maps table. Size is measured in Square Tiles.
--returnName: [optional parameter], String -> Boolean, flag whether function should return Name of map or size value by default
function p.getBiggestMap(returnName)
	if returnName == nil then
		returnName = false
	elseif type(returnName) == "table" then
		returnName = utils.bool(returnName.args[1]) or false
	end
	local mapId = 0
	local result = 0
	local mapTiles
	local i = 1
	
	while maps[i] do
		mapTiles = maps[i].ASTiles
		if(type(mapTiles) ~= "table") then 
			if mapTiles > result then
				result = mapTiles
				mapId = i
			end
		else --in case mapTiles are described in multiple layers
			local j = 1
			local subTotal = 0
			
			while mapTiles[j] do
				subTotal = subTotal + mapTiles[j][1] --mapTiles are expected to be first parameter
				j = j + 1
			end
			
			if subTotal > result then
				result = subTotal
				mapId = i
			end
		end
		i = i + 1
	end
	
	if(returnName) then
		return maps[mapId].name
	end
	return result
end

--Returns the smallest map from the maps table. Size is measured in Square Tiles.
--returnName: [optional parameter], String -> Boolean, flag whether function should return Name of map or size value by default
function p.getSmallestMap(returnName)
	if returnName == nil then
		returnName = false
	elseif type(returnName) == "table" then
		returnName = utils.bool(returnName.args[1]) or false
	end
	local mapId
	local result = 0
	local mapTiles
	local i = 1
	
	if maps[i].ASTiles then --just a avoiding assigning magic constant so using first map as a starting point
		result = maps[i].ASTiles 
		mapId = 1
	end
	while maps[i] do
		mapTiles = maps[i].ASTiles
		if(type(mapTiles) ~= "table") then 
			if mapTiles < result and mapTiles > 0 then
				result = mapTiles
				mapId = i
			end
		else --in case mapTiles are described in multiple layers
			local j = 1
			local subTotal = 0
			
			for j, mapLayer in ipairs(mapTiles) do
				subTotal = subTotal + mapLayer[1] --mapTiles are expected to be first parameter
			end
			
			if subTotal < result and subTotal > 0 then
				result = subTotal
				mapId = i
			end
		end
		i = i + 1
	end
	
	if(returnName) then
		return maps[mapId].name
	end
	return result
end

--Returns Count of realms based on map. If the parameter is not passed function use Page name instead
function p.getCountOfRealmMap(map)
	if type(map) ~= "string" then
		map = map.args[1] or mw.title.getCurrentTitle().text
	end
	local realmId = getRealmIdByMapName(map)

	return p.getCountOfRealmMapByRealmId(realmId)
end

function p.getCountOfRealmMapByRealmId(realmId)
	local result = 0
	local currentMap
	local i = 1
	
	while maps[i] do
		currentMap = maps[i]
		if currentMap.realm == realmId then
			result = result + 1
		end
		i = i + 1
	end
	
	return result
end

function p.getCountOfRealmMapsByName(realm)
	if type(realm) ~= "string" then
		realm = realm.args[1] or mw.title.getCurrentTitle().text
	end
	local realmId = p.getRealmIdByRealm(realm)

	return p.getCountOfRealmMapByRealmId(realmId)
end

function p.getRealmIdByRealm(realm)
	if type(realm) ~= "string" then
		realm = realm.args[1] or mw.title.getCurrentTitle().text
	end
	
	spec = "^(.+) %(.+%)$" --page specification, ex.: "Silent Hill (Realm)"" >> "Silent Hill"
	if string.find(realm, spec) then
		realm = realm:match(spec)	
	end
	for i, realmItem in ipairs(data.realms) do
		if realm == realmItem.name then return realmItem.id end
	end
	return 0
end

--Returns Realm ID based on map name passed as a parameter
function getRealmIdByMapName(map)
	local i = 1
	local currentMap
	
	while maps[i] do
		currentMap = maps[i]
		if currentMap.name == map then
			return currentMap.realm
		end
		i = i + 1
	end
	return 0
end

function p.toSMetres(number)
	return number * 64	
end

function p.toSTiles(number)
	return number / 64	
end

function p.getAltNameByMap(map)
	local result
	local mapId
	
	if type(map) ~= "string" then
		map = map.args[1] or mw.title.getCurrentTitle().text
	end
	
	mapId = getMapIndexByMapName(map)
	
	if maps[mapId].altName == maps[mapId].name or maps[mapId].altName == nil then --if Map name is the same as its main name or doesn't have it at all return empty string
		result = ""
	else
		result = maps[mapId].altName
	end
	
	return result
end

function getMapIndexByMapName(map)
	for _, currentMap in ipairs(maps) do
		if currentMap.name == map then
			return currentMap.id
		end
	end
	return 0
end

function getMapByMapName(map)
	for _, currentMap in ipairs(maps) do
		if currentMap.name == map then
			return currentMap
		end
	end
	return nil
end

function p.displayRealmIfExist(mapName)
	if type(mapName) ~= types.string then
		mapName = mapName.args[1] or mw.title.getCurrentTitle().text
	end
	local altName = p.getAltNameByMap(mapName)
	local result = cstr.empty
	
	if altName ~= cstr.empty then
		result = strings.orString .. space .. i(quotes(altName)) .. space
	end
	
	return result
end

--------------------------------------------------------------------------------------------------------

--id: [optional parameter], id of map from table maps
function p.getMapName(id)
	if type(id) == "table" and type(id.args[1]) ~= "nil" then
		id = tonumber(id.args[1])
	elseif type(id) == "table" then
		id = getMapIndexByMapName(mw.title.getCurrentTitle().text)
	end

	return 	maps[id].name
end

function p.getMapSizeByMap(map, convertToMetres) --TODO as the TealmsInfo template is planned to redisgn this part it will be needed to rewrite this function
	if type(map) ~= "string" then
		map = map.args[1] or mw.title.getCurrentTitle().text
	end
	local index = getMapIndexByMapName(map)
	local value
	local areas = ""
	
	local asTiles = maps[index].ASTiles
	if(type(asTiles) == "table") then
		for i, area in ipairs(asTiles) do
			if area[1] > 0 then
				if convertToMetres then value = utils.commaFormat(p.toSMetres(area[1])) else value = area[1] end
				areas = areas .. value .. space .. brackets(area[2]) .. ((#asTiles < i and pg) or cstr.empty)
			end
		end
		return areas
	else
		if convertToMetres then return p.toSMetres(asTiles) end
		return asTiles
	end
	
	return areas
end

function p.getMapSizeByMapInSMetres(map)
	return p.getMapSizeByMap(map, true)
end

function p.getMinHooksByMap(map)
	if type(map) ~= "string" then
		map = map.args[1] or mw.title.getCurrentTitle().text
	end
	
	return 	maps[getMapIndexByMapName(map)].minHooks
end

--returns a table with Outline name(s)
function p.getOutlineGrid(map)
	map = utils.resolveParameter(map)
	local result = {}
	local mapId = getMapIndexByMapName(map)
	local i = 1
	local imgIndex = isListedInImagesByMapId(mapId)

	if type(maps[mapId].ASTiles) == "table" then
		while maps[mapId].ASTiles[i] do
			if(imgIndex ~= 0) then
				result[i] =  mapImages[imgIndex].outline[i] .. ".png"	
			else
				result[i] = p.resolveOutlineNameFromGrid(mapId, i) .. ".png"
			end
			i = i + 1
		end
	else
		if(imgIndex ~= 0) then
			result[i] = mapImages[imgIndex].outline .. ".png"
		else
			result[i] = p.resolveOutlineName(mapId) .. ".png"
		end
	end

	return result
end

function isListedInImagesByMapId(mapId)
	local i = 1
	while mapImages[i] do
		if(mapImages[i].id == mapId) then
			return i
		end
		i = i + 1
	end
	return 0
end

function p.resolveOutlineNameFromGrid(mapId, index)
	local outlineMapName = utils.resolveFileName(maps[mapId].techName or maps[mapId].name)
	local outlineSubName = utils.resolveFileName(maps[mapId].ASTiles[index].techName or maps[mapId].ASTiles[index][2])
	
	return outlineMapName .. "Outline_" .. outlineSubName
end

function p.resolveOutlineName(mapId) --These two functions should be merged --TODO
	local outlineMapName = utils.resolveFileName(maps[mapId].techName or maps[mapId].name)
	
	return outlineMapName .. "Outline"
end

function p.resolveImageNameByMap(map)
	if type(map) ~= "string" then
		map = map.args[1] or mw.title.getCurrentTitle().text
	end
	local realmId = getRealmIdByMapName(map)
	local mapId = getMapIndexByMapName(map)
	local realmAbbr = data.realms[realmId].abbr
	local mapAltName = ""
	local imgIndex = isListedInImagesByMapId(mapId)
	
	if(imgIndex ~= 0) then
		return mapImages[imgIndex].image .. ".png"
	elseif type(maps[mapId].altName) ~= "nil" then
		mapAltName = utils.resolveFileName(maps[mapId].altName)
	else --Not the happies name of variable but it works as this else branch is only for rare cases
		mapAltName = utils.resolveFileName(maps[mapId].name)
	end
	
	return "IconMap " .. realmAbbr .. " " .. mapAltName .. ".png"
end

function getThemeByIndex(mapIndex)
	local i = 1
	local mapId = maps[mapIndex].id
	
	while themes[i] do
		if themes[i].mapId == mapId then
			return themes[i].landmarksound .. ".ogg"
		end
		i = i + 1
	end
	return nil
end

function getRealmByMap(map)
	for _, realm in ipairs(data.realms) do
		if realm.id == map.realm then return realm end
	end
end

function p.assembleRealmsInfo(map)
	if type(map) ~= "string" then
		map = map.args[1] or mw.title.getCurrentTitle().text
	end
	local mapObject = getMapByMapName(map)
	local realm = getRealmByMap(mapObject)
	local i = 1
	local mapIndex = getMapIndexByMapName(map)
	local paramTable = {}
	local outlines = p.getOutlineGrid(map)

	paramTable["altmap"] = p.getAltNameByMap(map)
	paramTable["image"] = p.resolveImageNameByMap(map)
	paramTable["area"] = p.getMapSizeByMap(map)
	paramTable["area2"] = p.getMapSizeByMapInSMetres(map)
	if(type(maps[mapIndex].ASTiles) == "table") then
		local mapTiles = maps[mapIndex].ASTiles
		local areaTotal = 0
		i = 1
		while mapTiles[i] do
			areaTotal = areaTotal + mapTiles[i][1] --mapTiles are expected to be first parameter
			i = i + 1
		end
		paramTable["area3"] = areaTotal
		paramTable["area4"] = utils.commaFormat(p.toSMetres(areaTotal))
	end
	local theme = getThemeByIndex(mapIndex)
	if(type(theme) == "string") then
		paramTable["landmarkSound"] = theme
	end

	local result =
	'{| class = "infoboxtable"' .. nl ..
	ntl .. nl .. tl .. space .. 'class = "infoboxname bold center" colspan = 2 | "' .. mapObject.name .. '"' .. nl ..
	ntl .. nl .. tl .. space .. 'class = "center" colspan = 2' .. space .. tl .. file(paramTable.image .. tl .. '400px') .. nl ..
	((mapObject.altName and newTableLine(strings.wipName) .. mapObject.altName .. nl) or cstr.empty) ..
	newTableLine(strings.realmInfo) .. link(realm.name) .. nl ..
	((((type(paramTable.area) == "string" and paramTable.area ~= cstr.empty) or (type(paramTable.area) == "number" and paramTable.area > 0))
		and newTableLine(strings.areaTiles) .. paramTable.area .. nl) or cstr.empty) ..
	((((type(paramTable.area2) == "string" and paramTable.area2 ~= cstr.empty) or (type(paramTable.area2) == "number" and paramTable.area2 > 0))
		and newTableLine(strings.areaMetres) .. paramTable.area2 .. nl) or cstr.empty) ..
	((paramTable.area3 and paramTable.area3 > 0 and newTableLine(strings.areaTotalTiles) .. paramTable.area3 .. nl) or cstr.empty) ..
	((paramTable.area4 and paramTable.area4 ~= "0" and newTableLine(strings.areaTotalMetres) .. paramTable.area4 .. nl) or cstr.empty) ..
	((mapObject.release and newTableLine(strings.release) .. link(strings.patch .. space .. mapObject.release) .. nl) or cstr.empty) ..
	((paramTable.landmarkSound and
		ntl .. nl .. tl .. space .. 'class = "titleColumn center" colspan = 2' .. space .. tl .. strings.landmarkSound .. nl ..
		ntl .. nl .. tl .. space .. 'class = "valueColumn center soundColumn" colspan = 2' .. space .. tl .. paramTable.landmarkSound .. nl) or cstr.empty)
	local mapIntro = p.getMapIntro(realm)
	--mw.log(mapIntro)
	if mapIntro then
		result = result ..
		ntl .. nl .. tl .. space .. 'class = "titleColumn center" colspan = 2' .. space .. tl .. strings.realmIntro .. nl ..
		ntl .. nl .. tl .. space .. 'class = "valueColumn center soundColumn" colspan = 2' .. space .. tl .. mapIntro .. nl
	end
	if #outlines > 0 then
		result = result .. ntl .. nl .. tl .. space .. 'class = "titleColumn center" colspan = 2' .. space .. tl .. ((#outlines > 1 and strings.layouts) or strings.layout) .. nl
		for _, outline in ipairs(outlines) do
			result = result ..
				ntl .. nl .. tl .. space .. 'class = "center" colspan = 2' .. space .. tl ..  file(outline .. tl .. '300px') .. nl
		end
	end
	result = result .. '|}'
	
	--mw.log(result)
	return result
	
end
function newTableLine(title)
	return ntl .. nl .. tl .. space .. 'class = "titleColumn"' .. (title and tl .. title .. dtl) or cstr.empty
end

function p.getMapIntro(realm)
	local fileName = 'MapIntro ' .. (realm.techName or realm.name)
	local valid = utils.isValidFileName(fileName, cstr.ogg)
	return (valid and file(fileName .. dot .. cstr.ogg)) or valid
end

function p.assembleMapPageHeader(map)
	map = utils.resolveParameter(map)
	local result = p.assembleRealmsInfo(map)
	local mapObject = getMapByMapName(map)
	local realm = getRealmByMap(mapObject)
	local mapCount = p.getCountOfRealmMap(map)
	local stringList = {skip(b(map)), skip(p.displayRealmIfExist(map))}
	local order

	if mapObject.retired	then		table.addRange(stringList, skip(strings.wasMap), strings.map) order = strings.retiredMapHeaderOrder
	elseif mapCount > 1		then		table.addRange(stringList, skip(strings.isOneOf), bclr(2, mapCount), strings.maps) order = strings.multipleMapsHeaderOrder
	else								table.addRange(stringList, skip(strings.isTheOnly), strings.map) order = strings.oneMapHeaderOrder
	end

	table.addRange(
		stringList,
		strings.inThe,
		skip(((realm.possessive and realm.possessive .. space) or cstr.empty) .. link(realm.name)),
		strings.realm,
		(mapObject.retired and skip(strings.beforeRetired) or cstr.empty)
	)

	local headerString = utils.getDynamicString(stringList, order) .. dot
	
	dmp(stringList)
	return result .. headerString
end

function p.assembleRealmsArticle()
	result = cstr.empty
	
	for _, realm in ipairs(data.realms) do 
		result = result .. '=== ' .. link(realm.name) .. ' ===' .. nl .. p.assembleMapsForRealms(realm.name) .. dnl
	end
	
	--mw.log(result)
	return result
end

function p.assembleMapsForRealms(realm)
	local realmId = 0
	local result = cstr.empty
	local map
	local outlines
	if realm == nil then
		realmId = p.getRealmIdByRealm(mw.title.getCurrentTitle().text)
	elseif type(realm) == "table" then
		realmId = p.getRealmIdByRealm(realm.args[1] or mw.title.getCurrentTitle().text)
	elseif type(realm) == "string" then
		realmId = p.getRealmIdByRealm(realm)
	end
	if realmId == 0 then
		return 'Missing parameter "realm". Please add the parameter into the invocation.' .. dnl .. bclr("Example", "red") .. colon .. '{{#Invoke:Maps|assembleMapsForRealms|' .. bclr("name_of_realm", "orange") .. '}}' .. dnl
	end
	
	for _, map in ipairs(maps) do
		if(map.realm == realmId) then
			result = result .. 
				tl .. center(
					link(map.name) .. nl ..
					((map.retired and strings.retiredMap .. nl) or cstr.empty)) ..
				file(p.resolveImageNameByMap(map.name), "center", "frameless", "link=" .. map.name)
			
			outlines = p.getOutlineGrid(map.name)
			for _, outline in ipairs(outlines) do --loop for cases with multiple outlines
				result = result .. file(outline, "center", "frameless", "200px", "link=" .. map.name)
			end
			result = result .. nl --just for keeping the same formatting, functionally completely useless
		end
	end
	
	result = utils.wrapBasicTable(result)
	
	--mw.log(result)
	return result
end

function p.assembleSortedMapsTableBySize()
	local result = ""
	local center = "style=\"text-align:center\""
	utils.sortMapsByASTiles()
	local mapRates = p.getRatesOfMapTable()
	local mapRate
	local index
	local bgColor

	result = "{| class=\"wikitable\"\n|-" .. nl
	result = result .. "! Rank !! Map !! Size (sqT, 8x8 m<sup>2</sup>) !! Size (m<sup>2</sup>)\n"
	
	local i = 1
	while mapRates[i] do
		mapRate = mapRates[i]
		result = result .. "|- " .. center .. nl
		result = result .. "! "
		if mapRate.count > 1 then --if count of maps of such size is more than one
			result = result .. "rowspan=\"" .. mapRate.count .. "\"| "
		end
		result = result .. i .. nl --printing map(s) Rate
		globalIndex = getAbsolutePosition(mapRates, i)
		for index = globalIndex, globalIndex + mapRate.count - 1, 1 do --there must be -1 due to offset in lists as the globalIndex starts with at index of first map current Rate
			--mw.log("i: " .. i .. " | index: " .. index .. " | Map: ".. maps[index].name .. " |||| index == gobalIndex: " .. index .. " == " .. globalIndex .. " |||| mapRate.count > 1: " .. mapRate.count .. " > " .. 1)
			if(index > globalIndex) then --if the index is higher than starting position means if it's not the first iteration then do the trick
				result = result .. "|- " .. center .. nl
			end
			bgColor = data.realms[maps[index].realm].color --In map must be correct realmID otherwise the code fails
			--mw.log(bgColor)
			result = result .. "| style=\"background:#" .. bgColor .. ";color:" .. utils.resolveTextColorByBackground(bgColor) .. ";\" | " .. maps[index].name .. nl --TODO complete styles
			if(index == globalIndex and mapRate.count > 1) then
				result = result .. "| rowspan=\"" .. mapRate.count .. "\"| " .. mapRate.size .. nl
				result = result .. "| rowspan=\"" .. mapRate.count .. "\"| " .. utils.commaFormat(p.toSMetres(mapRate.size)) .. nl
			elseif (index == globalIndex) then
				result = result .. "| " .. mapRate.size .. nl
				result = result .. "| " .. utils.commaFormat(p.toSMetres(mapRate.size)) .. nl
			end
		end
		i = i + 1
	end
	
	result = result .. "|}"
	
	mw.log(result)
	return result
end

function p.getRatesOfMapTable()
	local i = 1
	local result = {}
	
	while maps[i] do --go through all maps
		local j = 1
		local rate = nil
		local size = maps[i].ASTiles
		if type(size) == "table" then
			size = utils.getSumOfASTiles(maps[i].ASTiles)
		end
		while result[j] do --everytime look for if there is already the size of map logged in result table
			if result[j].size == size then --if so then store it and skip the rest
				rate = result[j]
				break
			end
			j = j + 1
		end
		if rate == nil then --if such size wasn't found in rate table then create a new one, "j" doesn't have to be increased as it's already increased by last go-through of loop above
			result[j] = {}
			result[j].count = 1 --we do not store the size as the index in table will later serve as a rank (after we sort the table)
			result[j].size = size
		else
			result[j].count	= result[j].count + 1 -- "j" doesn't have to be decreased as the loop above breaked out before incrementation
		end
		i = i + 1

	end
	
	utils.sortMapRates(result) --sorting will cause that the index serve as an rate for table
	return result
end

--function returns an index reflecting a position in sorted maps table based on current index in mapRates table
function getAbsolutePosition(mapRates, currentIndex)
	local i = 1
	local result = 1
	while i < currentIndex do
		result = result + mapRates[i].count --sum count of map from every previous rate until you reach the current index
		i = i + 1
	end

	return result
end

function p.resolveRealmsTableMainPage()
	local result = ""
	local name
	local fileName

	result = result .. '<div class="fpbox" id="fpRealms" style="text-align: center;">'
	result = result .. '<div class="heading">' ..utils.IconLink(strings.realms) .. '</div>'
	result = result .. '<div class="fplinks">'
	for i, realm in ipairs(data.realms) do
		if not realm.skip then
			name = realm.name or strings.noRealm
			fileName = resolveRealmFileNameByDlc(realm, p.getLatestRealmId())
			
			result = result .. '<div class="fplink realmMainPageBox plainlinks image"><div class="box"><div class="row"><div class="mapCell cell">'
			result = result .. '<div class="image">' .. file(fileName, 'link=' .. (name .. (realm.multiName and space .. brackets(strings.realm) or cstr.empty))) .. '</div>'
			result = result .. '<div class="realmLink link">' .. link(name) .. '<hr class="shadow" /><div class = "mapLinks">'
			
			for j, map in ipairs(maps) do
				if map.realm == realm.id and not map.retired then
					result = result .. link(map.name)
				end
			end
			
			result = result .. '</div></div></div></div></div></div>'
		end
	end
	result = result .. '</div>'
	result = result .. '</div>'

	return result
end

function p.getLatestRealmId()
	result = 0;
	for _, realm in ipairs(data.realms) do
		if result < realm.id then result = realm.id end
	end
	return result
end

function resolveRealmFileNameByDlc(realm, latestId)
	local fileConst = "RealmKeyArt_" --RealmKeyArt 01.png
	local fileName
	
	fileName = fileConst .. string.format("%02d", realm.id)

	if realm.id == nil or (realm.id >= latestId and not utils.isValidFileName(fileName, "png")) then
		return 'UnknownDLC.png'
	else
		return fileName .. dot .. cstr.png
	end
end

function getRelevantDlcsByRealm(realm)
	if type(realm.dlc) == "number" then
		realm.dlc = {realm.dlc}
	end
	local result = {}
	
	for _, rDlc in ipairs(realm.dlc) do
		for _, dlc in ipairs(dlcs) do
			if dlc.id == rDlc then
				table.insert(result, dlc)
			end
		end
	end
	return result
end

function getKillersByRealm(realm)
	local filteredDlcs = (realm.dlc and getRelevantDlcsByRealm(realm)) or {}
	local result = {}
	for _, killer in ipairs(killers) do
		if killer.realm == realm.id then
			table.insert(result, killer)
		else
			for _, dlc in ipairs(filteredDlcs) do
				if killer.dlc == dlc.id then
					table.insert(result, killer)
				end
			end
		end
	end
	return result
end

function getKillerNameStrings(killers)
	local result = {}
	for i, killer in ipairs(killers) do
		table.insert(result, the(killer) .. utils.IconLink((killer.multiName and killer.realName) or killer.name, killer.shortName or killer.realName or killer.name, killer.name))
	end
	return result
end

function getPatchStrings(patches)
	local result = {}
	for i, patch in ipairs(patches) do
		table.insert(result, link(strings.patch .. space .. patch))
	end
	return result
end

function getDlcNames(filteredDlcs)
	local result = {}
	local counter = 0
	for _, dlc in ipairs(dlcs) do
		if dlc.category == 1 then
			counter = counter + 1
			for _, fDlc in ipairs(filteredDlcs) do
				if fDlc.id == dlc.id then
					table.insert(result, link(strings.chapter .. space .. counter .. colon .. space .. fDlc.name))
				end
			end
		end
	end
	return result
end

--codeName = "Code Name",
--killer = "Killer(s)",
--pallete = "Colour Palette",
--location = "Location",
--dlc = "DLC(s)",
--mapCount = "Map(s)",
--releaseCount = "Release(s)"

function p.resolveRealmInfobox(realmName)
	realmName = utils.resolveParameter(realmName)
	local realm = getRealmByName(realmName)
	local filteredDlcs = (realm.dlc and getRelevantDlcsByRealm(realm)) or nil

	local result =
	'{| class = "infoboxtable"' .. nl ..
	thLine('colspan = 2 class = "infoboxname"', realm.name) ..
	tLine('colspan = 2 class = "center"', file("RealmKeyArt" .. space .. string.format("%02d", realm.id) .. dot .. cstr.png .. tl .. "300px")) ..
	((realm.codeName and tLine('class = "titleColumn"', strings.codeName, realm.codeName)) or cstr.empty) ..
	tLine('class = "titleColumn"', strings.killer, join(getKillerNameStrings(getKillersByRealm(realm)), pg)) ..
	((realm.palette and tLine('class = "titleColumn"', strings.palette, realm.palette)) or cstr.empty) ..
	((realm.location and tLine('class = "titleColumn"', strings.location, realm.location)) or cstr.empty) ..
	((realm.dlc and tLine('class = "titleColumn"', strings.releaseChapter , join(getDlcNames(filteredDlcs), pg))) or cstr.empty) ..
	tLine('class = "titleColumn"', strings.mapCount, p.getCountOfRealmMapByRealmId(realm.id)) ..
	((realm.dlc and tLine('class = "titleColumn"', strings.releaseCount , join(getPatchStrings(table.get(filteredDlcs, "release")), pg))) or cstr.empty) ..
	((utils.isValidFileName('MapIntro ' .. (realm.techName or realm.name),cstr.ogg) and 
		tLine('class = "titleColumn center" colspan = 2', strings.realmIntro) ..
		tLine('class = "valueColumn center soundColumn" colspan = 2', file('MapIntro ' .. (realm.techName or realm.name) .. dot .. cstr.ogg))
	) or cstr.empty) ..
	'|}'
	
	mw.log(result)
	return result
end

return p
Advertisement