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

    -- basic cargo query

    local fields = '_pageName, name, tags, slots, items, effects'
    local cargoArgs = { orderBy = '_pageName', groupBy = 'name' }
    local data = mw.ext.cargo.query('EnchantRecipes', fields, cargoArgs)

    -- actual search
    local results = search(data, args)

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

    -- build table output 
    local html = mw.html.create()
    html:wikitext(frame:preprocess('{{hatnote|<i>DLC: {{The Soroboreans}} [[The Soroboreans]]</i>}}<b>{{PAGENAME}}</b> is compatible with the following [[Enchantments]]:'))

    local table = html:tag('table'):addClass('wikitable sortable')

    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('td'):cssText('text-align:center'):wikitext(frame:preprocess('[[' .. v._pageName .. '|' .. v.name  .. ']]'))
        tr:tag('td'):wikitext(frame:preprocess(v.effects))
    end
    
    return html
end

-- build list of compatible enchants (too complex for cargo query)
function search(data, args)
    local itemTags = split(args.tags, ',')
    local results = {}
    local i = 1
    for _,enchant in ipairs(data) do        
        local isCompatible = false
    
        -- check for explicit item requirements
        if notempty(enchant.items) then            
            local name = args.name or mw.title.getCurrentTitle().text
            local items = split(enchant.items, ',')
            if contains(items, name) then
                isCompatible = true
            end
        end
    
        -- check slot and tags
        if not isCompatible and notempty(enchant.slots) then
            local slots = split(enchant.slots, ',')
            if hasmatch(slots, itemTags) then
                if notempty(enchant.tags) then
                    local enchTags = split(enchant.tags, ',')
                    isCompatible = hasmatch(enchTags, itemTags)
                else
                    isCompatible = true
                end            
            end
        end
    
        -- actually add it to the matches list
        if isCompatible then
            results[i] = enchant
            i = i + 1
        end
    end
    return results
end

----------------------------------------------------------------
--------------------------- HELPERS ----------------------------

function notempty(string)
    return string ~= nil and string ~= ''
end

function hasmatch(table1, table2)
    local flag = false
    for _,v in ipairs(table1) do
        if contains(table2, v) then
            flag = true
            break
        end
    end
    return flag
end

function contains(table, value)
    local flag = false
    for _,v in ipairs(table) do
        if v == value then
            flag = true
            break
        end
    end
    return flag
end

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

return p
Advertisement