Module:Missionreq

Revision as of 23:58, 12 June 2020 by Banri (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Module documentation
This documentation is transcluded from Module:Missionreq/doc. [edit] [purge]
Module:Missionreq's function main is invoked by Template:Missionreq.
Module:Missionreq loads data from Module:Missionreq/data.

Please see Module:Missionreq/data for how and where to add new missions to this module.


-- 
-- Part of [[Template:Missionreq]]'s implementation
-- For information on how to update this page, please see [[Module talk:Missionreq]]
-- <nowiki>
--

local p = {}

-- Load data from mission list
local missions = mw.loadData('Module:Missionreq/data')

-- Main function
function p.main(frame)
	local args = frame:getParent().args
	local mission = args[1]
	local limit = tonumber(args[2]) or 9
	local closed = args[3] ~= 'open'
	return p._main(mission,limit,closed)
end

function p._main(mission,limit,closed)
    
    local closedCss = ''
    if closed then
        closedCss = 'mw-collapsed'
    else
        closedCss = 'mw-open'
    end
    
	return mw.html.create('table')
			:addClass('mw-collapsible')
			:addClass(closedCss)
			:addClass('missionreq')
			:css('background','none')
		:tag('tr')
			:tag('th')
			:css('text-align','left')
			:wikitext('Missions:')
			:done()
		:done()
		:tag('tr')
			:tag('td')
				:css('padding-left','25px')
				:tag('ul')
					:node( list_reqs(mission,1,limit) )
				:done()
			:done()
		:done()
end

--
-- Recursive list function
-- Level determines how deep the indentation is
-- Replaces 'Started:' modifier
-- If the mission just listed was found in the big list and the limit for level is not reached
-- the mission's requirements will be listed as a sublist 1 level deeper
--
function list_reqs(mission,level,limit)
	local req_name, sub_req_list
	if mission then
		local started
		-- Look for the 'Started:' modifier and replace it
		-- If found, set boolean to true
		if mission:find('Started:') then
			mission = mission:gsub('Started:%s*','')
			started = true
		end
		-- Look for mission in the list
		local subreqs = missions[mission]
		if subreqs then
			if started then
				req_name = 'Started ' .. tidy_link(mission)
			else
				req_name = tidy_link(mission)
			end
			-- For every requirement, call this function again
			-- Handled the same, but 1 level deeper
			-- If limit is reached, denote extra requirements exist with ellipses
            if subreqs[1] then
                if level <= limit then
					sub_req_list = mw.html.create('ul')
                    for i, q in ipairs(subreqs) do
						local sub_list = list_reqs(q,level+1,limit)
						sub_req_list:node(sub_list)
					end
					sub_req_list:done()
                else
                    req_name = req_name .. '…'
                end
            end
		else
		-- If the requirement can't be found in the big list
		-- Paste it as is and skip any attempt to make a sublist
			req_name =  mission
		end
	end

	local ret = mw.html.create('li')
				:wikitext(req_name)

	if sub_req_list then
		ret:node(sub_req_list)
	end

	return ret:done()
end

-- Function to simply get a list of missions needed, as JSON, for further processing
function p.JSON_reqs(mission,level,limit)
	local req_name, sub_req_list
	local ret = {}
	
	if mission then
		local started
		-- Look for the 'Started:' modifier and replace it
		-- If found, set boolean to true
		if mission:find('Started:') then
			mission = mission:gsub('Started:%s*','')
			started = true --currently treated exactly the same as without
		end
		-- Look for mission in the list
		local subreqs = missions[mission]
		if subreqs then
			-- For every requirement, call this function again
			-- Handled the same, but 1 level deeper
			-- If limit is reached, skip
            if subreqs[1] then
                if level <= limit then
					sub_req_list = mw.html.create('ul')
                    for i, q in ipairs(subreqs) do
						local sub_list = p.JSON_reqs(q,level+1,limit)
						
						for k,v in pairs(sub_list) do ret[k] = v end
					end
                else
                    -- terminate recursion
                end
            end
		else
			-- If the requirement can't be found in the big list, don't recurse
		end
		ret[mission] = true
	end

	return ret
end

--
-- Function to tidy mission names into links
-- Any parenthetical (e.g. '(mission)') will be removed from the text, but remain in the link
-- 'Recipe for Disaster/' will be replaced in the RfD submissions, so that only the submission name appears as text
-- Returns a link
-- The 'Full:' modifier is removed
--
function tidy_link(name)
	if name then
		if name:find('Full:') then
			name = name:sub(6)
		end
		local alt = name:match('(.*)%(.*%)') or name

		if name:find('Recipe for Disaster%/') then
			alt = name:gsub('Recipe for Disaster%/','')
		end

		name = string.format('[[%s|%s]]',name,alt)
	end
	return name
end

return p