Outward Wiki
Register
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

    -- get tags

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

    -- basic cargo query

    local fields = '_pageName, name, tags, slots, 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
    for _,enchant in ipairs(data) do
        
        local isCompatible = false
        -- check for explicit item requirements
        if enchant.items ~= nil and enchant.items ~= '' then
            local enchantItems = split(enchant.items, ',')
            for _,item in ipairs(enchantItems) do
                if _name == item then 
                    isCompatible = true 
                    break
                end
            end
        else
            isCompatible = true
        end

        if not isCompatible and notempty(enchant.slots) and hasmatch(enchant.slots, args.tags) then
            
            if notempty(enchant.tags) then
                isCompatible = hasmatch(enchant.tags, args.tags)
            else
                isCompatible = true
            end

            -- local hasAnySlot = false
            -- if enchant.slots ~= nil and enchant.slots ~= '' then
            --     local slots = split(enchant.slots, ',')
            --     for _,slot in ipairs(slots) do
            --         for _,tag in ipairs(tags) do
            --             if tag == slot then 
                            
            --                 break
            --             end
            --         end
            --         if hasAnySlot then break end
            --     end
            -- end
            -- if hasAnySlot then
            --     if enchant.tags ~= nil and enchant.tags ~= '' then
            --         local tags = split(enchant.tags, ',')
            --         for _,tag in ipairs(tags) do
            --             for _,ourTag in ipairs(tags) do
            --                 if ourTag == tag then 
            --                     isCompatible = true
            --                     break
            --                 end
            --             end
            --             if isCompatible then break end
            --         end
            --     else 
            --         isCompatible = true
            --     end
            -- end
        end

        -- actually add it to the matches list
        if isCompatible then
            results[i] = enchant
            i = i + 1
        end
    end

    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>}}<br><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 .. ']]'))
        tr:tag('td'):wikitext(frame:preprocess(v.effects))
    end
    
    return html
end

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

function hasmatch(table1, table2)
    local flag = false
    for _,x in ipairs(table1) do
        for _,y in ipairs(table2) do
            if (x == y) then
                flag = true
                break
            end
        end
        if flag then break end
    end
    return flag
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