Outward Wiki
Advertisement
Template-info Documentation

Module invoked by Template:UsedInEnchantments.


local p = {}

function p.main(frame)        
    if frame == mw.getCurrentFrame() then
		args = require('Module:ProcessArgs').merge(true)
	else
		frame = mw.getCurrentFrame()
    end
    
    -- get some implicit args

    local _name = args.name or mw.title.getCurrentTitle().text
    local isWeapon = (args.type == 'Weapon' or args.type == 'Bow')

    -- get tags

    local tags = {}
    if args.tags ~= nil then
        tags = split(args.tags, ',')
    end

    -- basic cargo query

    local fields = '_pageName, name, slots, isany, tags, items, effects'
    local args = { 
        orderBy = '_pageName' 
    }
    local data = mw.ext.cargo.query('Enchantments', fields, args)

    -- build list of compatible enchants (too complex for cargo query)
    local results = {}
    local i = 1
    local debug = ''
    for _,enchant in ipairs(data) do
        
        -- see if our slot is compatible
        local isSlot = false
        local enchantSlots = split(enchant.slots, ',')
        for _,slot in ipairs(enchantSlots) do
            if isWeapon and slot == 'Weapon' then
                isSlot = true
            elseif args.type == slot then
                isSlot = true
            end
        end
        if isSlot then
            local metTags = true
            local metItems = true
            -- check tags / items (unless it's an "Any" recipe)
            if enchant.isany == nil or enchant.isany == '' then
                if enchant.tags ~= nil then
                    local enchantTags = split(enchant.tags, ',')
                    local metTags = false
                    for _,tag in ipairs(enchantTags) do
                        for _,ourTag in ipairs(tags) do
                            if ourTag == tag then 
                                metTags = true 
                                break
                            end
                        end
                        if metTags then break end
                    end
                end
                if enchant.items ~= nil then
                    local enchantItems = split(enchant.items, ',')
                    local metItems = false
                    for _,item in ipairs(enchantItems) do
                        if _name == item then 
                            metItems = true 
                            break
                        end
                    end
                end
            end
            if metTags and metItems then
                -- actually add it to the matches list
                results[i] = enchant
                i = i + 1
            end
        end
    end

    if #results < 1 then
        return '<b>No results found!</b>'
    end

    -- build table output 
    local html = mw.html.create()    
    local table = html:tag('table')

    local header = table:tag('tr')
    header:tag('th'):wikitext('Enchantment')
    header:tag('th'):wikitext('Effects')

    for _,v in ipairs(results) do
        local tr = table:tag('tr')
        tr:tag(frame:preprocess('[[' .. v._pageName .. ']]'))
        tr:tag(frame:preprocess(v.effects))
    end
    
    return html
end

function split(inputstr, sep)
    if sep == nil then
        sep = "%s"
    end
    local t={}
    for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
        table.insert(t, str)
    end
    return t
end

return p
Advertisement