Dead by Daylight Wiki
mNo edit summary
mNo edit summary
Line 295: Line 295:
 
function p.getMapSizeByMapInSMetres(map)
 
function p.getMapSizeByMapInSMetres(map)
 
return p.getMapSizeByMap(map, true)
 
return p.getMapSizeByMap(map, true)
 
--if type(map) ~= "string" then
 
-- map = map.args[1] or mw.title.getCurrentTitle().text
 
--end
 
--
 
--return p.toSMetres(maps[getMapIdByMapName(map)].ASTiles)
 
 
end
 
end
   
Line 400: Line 394:
 
local realmAbbr = realms[realmId][2]
 
local realmAbbr = realms[realmId][2]
 
local mapAltName = ""
 
local mapAltName = ""
  +
local imgIndex = isListedInImagesByMapId(mapId)
if type(maps[mapId].altName) ~= "nil" then
 
 
  +
if(imgIndex ~= 0) then
  +
mapAltName = mapImages[imgIndex].image
 
elseif type(maps[mapId].altName) ~= "nil" then
 
mapAltName = utils.resolveFileName(maps[mapId].altName)
 
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
 
else --Not the happies name of variable but it works as this else branch is only for rare cases

Revision as of 21:37, 16 April 2020


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

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 getCount("map")
end

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

function getCount(subject)
	local list
	
	--if you have another list just add it into a list then call appropriate function
	if		subject == "map"	then list = maps
	elseif	subject == "realm"	then list = realms
	else return 0
	end
	
	local i = 0
	while list[i + 1] do i = i + 1 end

	return i
end

--Returns the biggest map from the maps table. Size is measured in Square Tiles.
function p.getBiggestMap()
	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 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 end
		end
		i = i + 1
	end
	
	return result
end

--Returns the smallest map from the maps table. Size is measured in Square Tiles.
function p.getSmallestMap()
	local result = 0
	local mapTiles
	local i = 1
	
	if maps[i].ASTiles then result = maps[i].ASTiles end --just a avoiding assigning magic constant so using first map as a starting point
	while maps[i] do
		mapTiles = maps[i].ASTiles
		if(type(mapTiles) ~= "table") then 
			if mapTiles < result then result = mapTiles 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 end
		end
		i = i + 1
	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
	local i = 1
	
	while realms[i] do
		if realm == realms[i][1] then return i end
		i = i + 1
	end
	
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

--Returns Realm name based on passed map name. If the function is called without parameter the function will use Page name as a map name
function p.getRealmNameByMap(map)
	if type(map) ~= "string" then
		map = map.args[1] or mw.title.getCurrentTitle().text
	end
	
	return getRealmNameById(getRealmIdByMapName(map))
end

--Returns Realm Name based on realm's ID
function getRealmNameById(id)
	return realms[id][1]
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 = getMapIdByMapName(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 getMapIdByMapName(map)
	local i = 1
	local currentMap
	
	while maps[i] do
		currentMap = maps[i]
		if currentMap.name == map then
			return i
		end
		i = i + 1
	end
	return 0
end

function p.displayRealmIfExist(map)
	if type(map) ~= "string" then
		map = map.args[1] or mw.title.getCurrentTitle().text
	end
	local altName = p.getAltNameByMap(map)
	local result = ""
	
	if altName ~= "" then
		result = "or ''\""..altName.."\"'' "
	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 = getMapIdByMapName(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 mapId = getMapIdByMapName(map)
	local value
	local areas = ""
	
	if(type(maps[mapId].ASTiles) == "table") then
		local i = 1
		while maps[mapId].ASTiles[i] do
			area = maps[mapId].ASTiles[i]
			if convertToMetres then value = p.toSMetres(area[1]) else value = area[1] end
			areas = areas .. value .. " (" .. area[2] .. ")"
			if(type(maps[mapId].ASTiles[i + 1]) ~= "nil") then
				--areas = areas .. " '''<nowiki>" .. frame:expandTemplate{title = '!'}  .. "</nowiki>''' "
				areas = areas .. " '''"..bar.."''' "
			end
			i = i + 1
		end
		return areas
	else
		if convertToMetres then return p.toSMetres(maps[mapId].ASTiles) end
		return maps[mapId].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[getMapIdByMapName(map)].minHooks
end

function p.getMaxHooksByMap(map)
	if type(map) ~= "string" then
		map = map.args[1] or mw.title.getCurrentTitle().text
	end
	
	if maps[getMapIdByMapName(map)].maxHooks == nil then
		return maps[getMapIdByMapName(map)].minHooks + 5 --default difference between count of min and max of hooks
	else
		return maps[getMapIdByMapName(map)].maxHooks
	end
end

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

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

function p.getOutlineGrid(map)
	if type(map) ~= "string" then
		map = map.args[1] or mw.title.getCurrentTitle().text
	end
	local result = {}
	local mapId = getMapIdByMapName(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].name)
	local outlineSubName = utils.resolveFileName(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].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 = getMapIdByMapName(map)
	local realmAbbr = realms[realmId][2]
	local mapAltName = ""
	local imgIndex = isListedInImagesByMapId(mapId)
	
	if(imgIndex ~= 0) then
		mapAltName = mapImages[imgIndex].image
	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 getMapReleaseById(mapId)
	return maps[mapId].release
end

function p.assembleRealmsInfo(map)
	if type(map) ~= "string" then
		map = map.args[1] or mw.title.getCurrentTitle().text
	end
	local i = 1
	local mapId = getMapIdByMapName(map)
	local paramTable = {}
	local outlines = p.getOutlineGrid(map)

	paramTable["map"] = p.getMapName(mapId)
	paramTable["altmap"] = p.getAltNameByMap(map)
	paramTable["image"] = p.resolveImageNameByMap(map)
	while outlines[i] do
		paramTable["outline"..i] = outlines[i]
		i = i + 1
	end
	paramTable["realm"] = "[[" .. p.getRealmNameByMap(map) .. "]]"
	paramTable["area"] = p.getMapSizeByMap(map)
	paramTable["area2"] = utils.commaFormat(p.getMapSizeByMapInSMetres(map))
	--TODO make compatible old design area 3 and 4
	paramTable["minhooks"] = p.getMinHooksByMap(map)
	paramTable["maxhooks"] = p.getMaxHooksByMap(map)
	paramTable["minpallets"] = p.getMinPalletsByMap(map)
	paramTable["maxpallets"] = p.getMaxPalletsByMap(map)
	if(type(getMapReleaseById(mapId)) ~= "nil") then
		paramTable["release"] = "[[Patch " .. getMapReleaseById(mapId) .. "]]"
	end

	mw.log(mw.dumpObject(paramTable))
	
	return frame:expandTemplate{title = 'RealmsInfo', args = paramTable} --string.char(10)
end

function p.assembleMapPageHeader(map)
	if type(map) ~= "string" then
		map = map.args[1] or mw.title.getCurrentTitle().text
	end
	local result
	local mapCount = p.getCountOfRealmMap(map)
	result = p.assembleRealmsInfo(map)
	result = result .. "\n\n'''" .. map .. "''' " .. p.displayRealmIfExist(map)
	if mapCount > 1 then
		result = result .. " is one of '''" .. frame:expandTemplate{title = "clr", args = {2, p.getCountOfRealmMap(map)} } .. "''' Maps"
	else
		result = result .. " is the only map"	
	end
	result = result .. " for the [[" .. p.getRealmNameByMap(map) .. "]] Realm."
--'''{{PAGENAME}}''' {{#Invoke:Maps|displayRealmIfExist}} is one of '''{{clr|2|{{#Invoke:Maps|getCountOfRealmMap}}}}''' Maps for the [[{{#Invoke:Maps|getRealmNameByMap}}]] Realm.

	return result
end

return p