Module:Questreq

-- -- Part of Template:Questreq's implementation -- For information on how to update this page, please see Module:Questreq -- --

local p = {}

-- Load data from quest list local quests = mw.loadData('Module:Questreq/data')

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

function p._main(quest,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('questreq') :css('background','none') :tag('tr') :tag('th') :css('text-align','left') :wikitext('Quests:') :done :done :tag('tr') :tag('td') :css('padding-left','25px') :tag('ul') :node( list_reqs(quest,1,limit) ) :done :done :done end

-- -- Recursive list function -- Level determines how deep the indentation is -- Replaces 'Started:' modifier -- If the quest just listed was found in the big list and the limit for level is not reached -- the quest's requirements will be listed as a sublist 1 level deeper -- function list_reqs(quest,level,limit) local req_name, sub_req_list if quest then -- Look for quest in the list local subreqs = quests[quest] if subreqs then req_name = tidy_link(quest) -- 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 = quest 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 quests needed, as JSON, for further processing function p.JSON_reqs(quest,level,limit) local req_name, sub_req_list local ret = {} if quest then -- Look for quest in the list local subreqs = quests[quest] 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[quest] = true end

return ret end

-- -- Function to tidy quest names into links -- Any parenthetical (e.g. '(quest)') will be removed from the text except for numbered quests where they are actually part of the name in-game, but remain in the link -- Returns a link -- function tidy_link(name) if name then local alt = name:match('(.*)%(.*%)') or name name = string.format('%s',name,alt) end return name end

return p