Module:VersionMessageBox

From Against the Storm Official Wiki
Revision as of 18:00, 15 October 2024 by Aeredor (talk | contribs) (updated to externalize the view; makes this logic simpler)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

--- @module VersionMessageBox
local VersionMessageBox = {}



--region Dependencies

local VIEW_TEMPLATE = "Version/view"

--endregion



--region Private constants

local PATTERN_CAPTURE_MAJOR_VERSION = "(%d+%.%d+)"

--endregion



--region Private methods

---
--- Extracts just the major version parts of the numbers and returns whether
--- the content is out of date.
---
---@param versionWhenLastUpdated string the version it was last updated
---@param currentVersion string the current version
---@return boolean true if the last updated is before the current major version
local function isOutOfDate(versionWhenLastUpdated, currentVersion)

	local lastUpdated = tonumber(versionWhenLastUpdated:match(PATTERN_CAPTURE_MAJOR_VERSION) or 0)
	local current = tonumber(currentVersion:match(PATTERN_CAPTURE_MAJOR_VERSION) or 0)

	return current > lastUpdated
end

--endregion



--region Public methods

---main
--- Checks whether a message box is necessary. If the page is outdated, sends the information to the view template.
---
---@param frame table the template's calling context
---@return string wiki markup for a message box, or a blank string if it's up-to-date
function VersionMessageBox.main(frame)

	local lastUpdated = frame.args.lastupdated
	local currentVersion = frame.args.currentversion

	-- validate that there are version values to use
	if not lastUpdated or "" == lastUpdated then
		error("You must specify the version with which you updated the page. Please see the template documentation for how to use the parameters")
	end
	if not currentVersion or "" == currentVersion then
		error("The current version was not retrieved correctly. Please reach out on the community discord for assistance")
	end

	if isOutOfDate(lastUpdated, currentVersion) then
		return frame:expandTemplate{
			title = VIEW_TEMPLATE,
			args = {
				["lastupdated"] = lastUpdated,
			}
		}
	else
		-- the page is up to date, nothing else needed
		return ""
	end
end

--endregion

return VersionMessageBox