Module:GetDataValue

From HoloCure Wiki
Jump to navigation Jump to search
Information.svg
This template's documentation can be found at Template:GetDataValue.

-- Loosely based on Apex Legends Wiki's Module:Symbol.
-- As of writing, my total experience with Lua is whopping 5 hours,
-- so if you know Lua and your eyes are bleeding by the end of the page,
-- I kindly ask you to make it better.
local p = {}
local getArgs = require('Module:Arguments').getArgs

function p.main(frame)
	local args = getArgs(frame, {
		wrappers = {
			'Template:GetDataValue'
		}
	})
	return p.getValue(args)
end

function p.getValue(args)
	local name = args[1]
	local stat = args[2]
	local level = tonumber(args[3])
	
	-- swap table if arg[4] is defined
	local tableMod = 'Module:WeaponData'
	if args[4] then
		if args[4]:lower() == 'special' then
			tableMod = 'Module:SpecialData'
		elseif args[4]:lower() == 'skill' then
			tableMod = 'Module:SkillData'
		end
	end
	local data = mw.loadData(tableMod)
	
	--error checks
	assert(name, "No name input given.")
	assert(stat, "No stat input given.")
	assert(data[name], "Could not find \'" .. name .. "\' in " ..  tableMod .. ". Ensure that the name and data module are correct and the target has an existing entry there.")
	
	-- copy table with levels, to get its size
	local weapon = {}
	for k,v in pairs(data[name]) do
		weapon[k] = v
	end
	
	-- check that level is valid and set it to min/max if it isn't
	if not level then
		level = 1
		
	elseif (level < 1) then
		level = 1
		
	elseif (level > #weapon) then
		level = #weapon
	end
	
	-- this iterates down from given level, interrupting when a value is found in the table
	local value
	for i = level, 1, -1 do

		if weapon[i][stat] then
			value = weapon[i][stat]
			break
		end
		
	end

	return value
end

return p