Module:HeroDataArrays

Revision as of 16:20, 18 October 2024 by SerpentofSet (talk | contribs) (Health table hopefully works?)

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,
					_node = node[k]							-- progressing down each node in the path,
					if _node == nil then					-- trying the numeric index if the string
						_node = node[tonumber(k)]			-- index fails.
					end
					node = _node
				end
				out[key] = node
			else
				out[key] = heroData[key]
			end
		end
		outData[heroData["Name"]] = out
	end
	return(outData)
end

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

	z = 
[[{| class="wikitable sortable"
|+ Hero Base Health Stats
! Hero !! Starting !! Added per PI !! At Max PI
]]
	
	for i,a in pairs(heroHealthData) do
		z=z..
[[|-
| {{PageRef|]]..a["Name"]..[[|type=Hero}} || ]]..a["MaxHealth"]..[[ || ]]..a["LevelScaling>Health"]..[[ || a billion
]]
	end
	
	z = z.."|}"
	
	return z
end

return p