Module:HeroDataArrays: Difference between revisions

From Deadlock Wiki
Jump to navigation Jump to search
mNo edit summary
mNo edit summary
Line 24: Line 24:
if string.find(key, ">") ~= nil then -- If key is actually a path of keys,
if string.find(key, ">") ~= nil then -- If key is actually a path of keys,
local node = heroData          -- starting with the biggest node heroData,
local node = heroData          -- starting with the biggest node heroData,
for k in string.gmatch(key, "([^>]+)") do -- iterate over each key,
for k in string.gmatch(key, "([^>]+)") do -- iterate over each key, separating by ">",
_node = node[k] -- progressing down each node in the path,
if node[k] ~= nil then -- trying the key as a string e.g. "1",
if _node == nil then -- trying the numeric index if the string
node = node[k]
_node = node[tonumber(k)] -- index fails.
else -- else using the key as numeric,
end
node = node[tonumber(k)]
node = _node
end -- drilling down the JSON node after node
end
end
out[key] = node
out[key] = node -- until outputting the final node
else
else
out[key] = heroData[key]
out[key] = heroData[key]

Revision as of 02:21, 19 October 2024

Overview

This module consists of one generic function, heroDataArray, and a series of functions that create specific wikitables using the output from heroDataArray.


local p = {}

local allHeroData = mw.loadJsonData("Data:HeroData.json")
local inGameHeroData = {}

-- Just get heroes that are playable
for i, heroData in pairs(allHeroData) do
	if heroData["IsDisabled"] == false and heroData["InDevelopment"] == false then
		table.insert(inGameHeroData, heroData)
	end
end

-- heroDataArray takes as argument a table of any length containing keys or 
-- paths of keys in the HeroData JSON, e.g. {Name, MaxHealth, LevelScaling>Health}.
-- A path of keys should be specified as such, with the ">" character separating
-- subsequent keys.
local heroDataArray = function(args)
	local outData = {}
	
	for i, heroData in ipairs(inGameHeroData) do -- Iterate over each hero
		local out = {}
		for j, key in pairs(args) do
			if string.find(key, ">") ~= nil then			-- If key is actually a path of keys,
				local node = heroData           			-- starting with the biggest node heroData,
				for k in string.gmatch(key, "([^>]+)") do	-- iterate over each key, separating by ">",
					if node[k] ~= nil then					-- trying the key as a string e.g. "1",
						node = node[k]						
					else									-- else using the key as numeric,
						node = node[tonumber(k)]
					end										-- drilling down the JSON node after node
				end
				out[key] = node								-- until outputting the final node
			else
				out[key] = heroData[key]
			end
		end
		outData[heroData["Name"]] = out
	end
	return(outData)
end

p.heroHealthTable = function(frame)
	heroHealthData = heroDataArray({"Name", "MaxHealth", "LevelScaling>Health"})

	z = "{| class=\"wikitable sortable mw-collapsible\" style=\"text-align:center\" \n"..
		"|+ Hero Base Health Stats \n"..
		"! Hero !! Starting !! Added per PI !! At Max PI \n"
	
	for i,a in pairs(heroHealthData) do
		z = z..
			"|- \n"..
			"| style=\"text-align:left;\" | "..frame:expandTemplate{title="PageRef", args={a["Name"], ['type']="Hero"}}..
				" || "..a["MaxHealth"].." || +"..a["LevelScaling>Health"].." || "..
				tostring(tonumber(a["MaxHealth"])+tonumber(a["LevelScaling>Health"])*14).." \n"
	end
	
	z = z.."|}"
	
	return z
end

return p