Module:Requirement

From Fallen London Wiki

Documentation for this module may be created at Module:Requirement/doc

--[[ This module contains logic for checking whether a value of a quality 
matches a given lock/unlock requirement.  Intended primarily for use in 
World Lock/Unlock templates, but can also be used in calculators, etc.
--]]

local p = {}

local str = require("Module:String")

local function _match(value,requirement)
	if value == '0' then
		return false
	end
	if requirement == '' or requirement == nil then
		return true
	end
	-- requirement is "exactly [number]"
	local exactvalue = string.match(requirement, '^exactly (%d+)$')
	if exactvalue then
		return value == exactvalue
	end
	-- requirement is a numerical range, inclusive ("[number] - [number]")
	local rangemin, rangemax = string.match(requirement, '^(%d+)%s*%-%s*(%d+)$')
	if rangemin and rangemax then
		return tonumber(rangemin) <= tonumber(value) and tonumber(value) <= tonumber(rangemax)
	end
	-- require is just "[number]", indicating a minimum
	local minvalue = string.match(requirement, '^%d+$')
	if minvalue then
		return tonumber(value) >= tonumber(minvalue)
	end
	-- escape any regex special characters in the value, since it will be interpreted as regex pattern
	local escapedvalue = str._escapePattern(value)
	-- requirement is a non-numerical string (possibly a list of options), see if value is somewhere in it
	local stringmatch = string.match(requirement, escapedvalue)
	if stringmatch then
		return true
	else
		return false
	end
end

function p.match(frame)
	local value = frame.args[1] or frame.args['value']
	local requirement = frame.args[2] or frame.args['requirement']
	
	return _match(value,requirement) or ''
end

return p