Module:Quest

From Path of Exile Wiki
Revision as of 13:17, 3 January 2017 by >TheFrz
Jump to navigation Jump to search
Module documentation[view] [edit] [history] [purge]


Lua logo

This module depends on the following other modules:

This module implements {{Quest}} and {{Quest list}}

local getArgs = require('Module:Arguments').getArgs
local util = require('Module:Util')

local p = {}

-----

local args_format_boss = function(args, key)
    local boss_key = 'boss' .. key
    local link_key = boss_key .. '_link'
    local image_key = boss_key .. '_image'
    if args[boss_key] == nil then
        error('Multiple bosses, but ' .. boss_key .. ' is empty')
    end
    args[link_key] = args[link_key] or args[boss_key]
    args[image_key] = args[image_key] or args[boss_key] .. '.png'
end

-----

local display_format_boss = function(args, key)
    local boss_key = 'boss' .. key
    local v = {}
    -- Format the link
    v[#v + 1] = '[['
    v[#v + 1] = args[boss_key .. '_link']
    v[#v + 1] = '|'
    v[#v + 1] = args[boss_key]
    v[#v + 1] = ']]'
    v[#v + 1] = '<br>'
    -- Format the image
    v[#v + 1] = '[[File:'
    v[#v + 1] = args[boss_key .. '_image']
    v[#v + 1] = '|alt='
    v[#v + 1] = args[boss_key]
    v[#v + 1] = ']]'

    return table.concat(v)
end

-----

local display_quest_add_row = function(tbl, head, content)
    if content == nil then
        content = ''
    end
    tbl
        :tag('tr')
            :tag('th')
                :attr('scope', 'row')
                :wikitext(head)
                :done()
            :tag('td')
                :wikitext(content)
                :done()
end

-----

------------------------------------------------------------------------------------------------------
-- Template: Quest

p.quest = function(frame)
    local args = getArgs(frame, {parentFirst = true})
    local g_frame = util.misc.get_frame(frame)

    -- test args

    --
    -- Args
    --

    if args.name == nil or string.len(args.name) == 0 then
        error('Name is required')
    end

    args.icon = args.icon or args.name .. ' quest icon.png'
    if args.boss then
        local n = tonumber(args.boss)
        -- The number of bosses were given
        if n then
            args.boss = n
            for i=1, n do
                args_format_boss(args, tostring(i))
            end
        else
            args_format_boss(args, '')
        end
    end
    args.key_item = args.key_item
    args.required = util.cast.boolean(args.required)
    args.start = args.start
    args.objective = args.objective
    args.completion = args.completion

    --
    -- Formatting
    --

    local tbl = mw.html.create('table')
    tbl
        :attr('class', 'quest-table')
        :tag('tr')
            :tag('td')
                :attr('class', 'quest-table-iconbox')
                :attr('colspan', 2)
                :wikitext(string.format('%s<br>[[File:%s|alt=%s]]', args.name, args.icon, args.icon))

    local rtext = ''
    if args.required then
        rtext = 'Yes'
    else
        rtext = 'No'
    end
    display_quest_add_row(tbl, 'Required ', rtext)

    for _, v in pairs({'Start', 'Objective', 'Completion'}) do
        display_quest_add_row(tbl, v, args[string.lower(v)])
    end

    if type(args.boss) == 'number' then
        local v = {}
        for i=1, args.boss do
            v[#v + 1] = display_format_boss(args, tostring(i))
            v[#v + 1] = '<br>'
        end
        display_quest_add_row(tbl, 'Boss ', table.concat(v))
    elseif type(args.boss) == 'string' then
        display_quest_add_row(tbl, 'Boss ', display_format_boss(args, ''))
    end

    if args.key_item then
        display_quest_add_row(tbl, 'Key Item ', args.key_item)
    end

    return tostring(tbl)
end

-----

return p