Module:Required for completing

-- -- essentially the inverse of Module:Questreq local quests = mw.loadData('Module:Questreq/data') local p = {} local yesno = require('Module:Yesno')

function _sortFunc(a,b) local _a, _b _a = tostring(a):lower _b = tostring(b):lower return _a < _b end

function getQuests(q) local qs = {} q = tostring(q) q = q:lower for k,v in pairs(quests) do		for i,l in ipairs(v) do			local _l = l			_l = _l:lower if q == _l then table.insert(qs, k)				break end end end if #qs == 0 then return nil end table.sort(qs, _sortFunc) return qs end

function p._getQuests(q, getNested) --[=[	-- preserves nesting structure -- saving here in case we want it, but not used function nest(_q) local nqs = getQuests(_q) local r = nil if nqs ~= nil then r = nqs for i,v in ipairs(nqs) do				table.insert(nqs, nest(v)) end end return r	end --]=]	-- does not preserve structure local nestedQuests = {} function nest(_q) local nqs = getQuests(_q) local r = nil if nqs ~= nil then r = nqs for i,v in ipairs(nqs) do				if not nestedQuests[v] then nestedQuests[v] = true nest(v) end end end return r	end local qs = getQuests(q) if qs == nil then return nil end if getNested then nest(q) -- unmark the direct requirements for i,v in ipairs(qs) do			nestedQuests[v] = nil end local _nested = {} for k,v in pairs(nestedQuests) do			table.insert(_nested, k)		end table.sort(_nested, _sortFunc) nestedQuests = _nested else nestedQuests = nil end return qs, nestedQuests end

function p._quests(args) local q = args[1] or mw.title.getCurrentTitle.text if not quests[q] then return string.format('"%s" is not a recognised quest name.', q)	end local qs, indirqs = p._getQuests(q, not yesno(args.noindirect)) local ret = {} if qs == nil or #qs == 0 then table.insert(ret, string.format('%s is not currently required for any quests.', q)) else table.insert(ret, string.format('%s is directly required for the following quests:', q)) for i,v in ipairs(qs) do			local repl, str str = '' v, repl = string.gsub(v, '^Full:', '') if repl > 0 then str = 'Full completion of ' end -- doubt this will ever happen but here's support for it anyway v, repl = string.gsub(v, '^Started:', '') if repl > 0 then str = 'Starting ' end table.insert(ret, string.format('* %s%s', str, v)) end if indirqs and #indirqs > 0 then table.insert(ret, '') table.insert(ret, ' It is therefore an indirect requirement for the following quests: ') for i,v in ipairs(indirqs) do				local repl, str str = '' v, repl = string.gsub(v, '^Full:', '') if repl > 0 then str = 'Full completion of ' end -- doubt this will ever happen but here's support for it anyway v, repl = string.gsub(v, '^Started:', '') if repl > 0 then str = 'Starting ' end table.insert(ret, string.format('* %s%s', str, v)) end table.insert(ret, ' ') end end return table.concat(ret, '\n') end

function p.quests(frame) return p._quests(frame:getParent.args) end

return p --