Module:Patch

From Sea of Thieves Wiki
Jump to navigation Jump to search

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

local p = {}
function p.main(frame)
    local args = require('Dev:Arguments').getArgs(frame)
    local raisebadargissue = false -- For error handling later.
    local badpatchverissue = false -- For flagging pages with invalid patches.
    
	-- Cargo Query
	local a = mw.ext.cargo.query( "Versions", "_pageName,version,name,release_date", { where = 'version="' .. args[1] .. '"', ["no html"] = true } )
	a = a[1] or { }
	
	local html = mw.html.create( "ul" )
		:tag("li")

		-- Version + Date
		if a.version then
			html = html
				:tag("b")
				:wikitext( '[[' .. a.version .. ']] (' .. a.release_date .. ')' )
				:done()
				:tag("ul")
				
			-- Detail notes sub-bullets
			for i=2,25 do -- #args should get the array length of args to prevent unncessecary loops.
				if args[i] ~= nil then
					local inc_arg = args[i]
					local arg_isvalid = false
					if type(inc_arg) == 'string' then -- If the argument is a string, it's valid.
						arg_isvalid = true
					elseif type(inc_arg) == 'number' then -- If the argument is a number, cast to string to make valid.
						inc_arg = tostring(inc_arg)
						arg_isvalid = true
					elseif type(inc_arg) == 'boolean' then -- If the argument is a boolean, then a little logic can convert it to a string easily.
						if inc_arg == true then
							inc_arg = 'true'
						else
							inc_arg = 'false'
						end
						arg_isvalid = true
					end -- If one of the remaining types gets in, then something is very wrong and that line should be skipped and probably flagged.
					if arg_isvalid then -- If the arg is valid then do the following.
						-- First, we separate the string by newline characters into an array via a gmatch.
						local arg_nsplit = {}
						for j in inc_arg:gmatch("([^\n]*)\n?") do
							if type(j) == 'string' then
							if #j ~= 0 then -- Don't do anything if the string is completely empty. 
							-- Now, we use pattern matches to find the first alphanumeric character.
							local strstart, _ = string.find(j, '[^%*^%s]') -- Locates the first character that's not a whitespace or a * in the string.
							local asteriskdepth = 0 -- Predefine asterisk depth as 0.
							if strstart ~= nil then
								if strstart > 1 then -- If strstart = 1 then there is no asterisks to worry about in the string at all.
									local strslicefront = string.sub(j, 1, strstart - 1) -- We need to subtract 1 here otherwise it includes the first part of the string. This fetches a substring that is everything up to the first character.
									_, asteriskdepth = string.gsub(strslicefront, "%*", "") -- Returns the number of asterisks in the substring.
								end
								local strsliceback = string.sub(j, strstart, #j) -- This will return all of the test we want to actually display in the li
								if asteriskdepth > 0 then
									for k=1, asteriskdepth do
										html = html
										:tag('ul')
									end
								end
								if #strsliceback > 0 then -- One last check to make sure this part of the string isn't empty.
									html = html
									:tag('li')
									:wikitext( strsliceback )
									:done()
								end
								if asteriskdepth > 0 then
									for k=1, asteriskdepth do
										html = html
										:done()
									end
								end
							end
						end
						end
					end
					else
						raisebadargissue = true
					end
				end
			end
		else
			badpatchverissue = true
			html = html
			:tag('span')
			:css('color', 'red')
			:wikitext('Error: Patch version not found! An issue exists with the inputted patch name, please check that an existing version has been used.')
		end
	html = html:allDone()
	strout = tostring(html)
	if raisebadargissue then
		strout = strout .. '[[Category:Patches with arguments causing undefined behavior]]' -- Any page in this category needs to be looked at *thoroughly* because this shouldn't happen. Yes, this includes nil because it means that the argument somehow became nil after it was checked for nil the first time.
	elseif badpatchverissue then
		strout = strout .. '[[Category:Patches with invalid version arguments]]' -- Flagging pages where a input patch version doesn't exist. Either due to user error, or the patch's page not being created yet.
	end
	return strout
end
return p