Module:ListEnchants

From Outward Wiki
Jump to navigation Jump to search
Template-info.svg Documentation

Module used to list Enchantments. Will list any Template:Enchantment.


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, type, name, tags, slots, items, effects, compatibles'
    local cargoArgs = { orderBy = 'compatibles, _pageName', groupBy = 'name', where = 'DLC="' .. args.DLC .. '"' }
    local data = mw.ext.cargo.query('EnchantRecipes', fields, cargoArgs)

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

    -- the intended order for the outputted tables.
    -- we can iterate over this with ipairs to retain the sort.
    local order = {
        'Weapon',
        'Bow',
        'Lantern',
        'Chakram',
        'Dagger',
        'Lexicon',
        'Armor',
        'Chest',
        'Boots',
        'Helmet'
    }

    -- prepare sorted list holders (create empty tables corresponding to the type)
    local sorted = {}
    for _, v in ipairs(order) do
        sorted[v] = {}
    end

    -- sort recipes into their holder
    for _, v in ipairs(data) do
        local tbl = find(sorted, v.type)
        if tbl ~= nil then
            table.insert(tbl, v)
        end
    end

    -- build table output 
    local html = mw.html.create()
    -- iterate using the intended order
    for _, v in ipairs(order) do
    	if (#sorted[v] > 0) then
        	buildoutput(v, sorted[v], html, frame)
        end
    end

    return html
end

function buildoutput(title, data, html, frame)
    
    if title == 'Armor' or title == 'Weapon' then
        html:wikitext(frame:preprocess('<h2>' .. title .. ' Enchantments</h2>'))
    else
        html:wikitext(frame:preprocess('<h3>' .. title .. '</h3>'))
    end

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

    local header = table:tag('tr')
    header:tag('th'):cssText('min-width: 80px'):wikitext('Enchantment')
    header:tag('th'):cssText('min-width: 125px'):wikitext('Compatible')
    header:tag('th'):cssText('min-width: 300px'):wikitext('Effects')

    for _,v in ipairs(data) do
        local tr = table:tag('tr')
        tr:tag('td'):cssText('text-align:center'):wikitext(frame:preprocess('[[' .. v._pageName .. '|' .. v.name  .. ']]'))
        tr:tag('td'):cssText('max-width: 175px'):wikitext(frame:preprocess(v.compatibles))
        tr:tag('td'):cssText('min-width: 300px'):wikitext(frame:preprocess(v.effects))
    end

    return table
end




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

function find(tbl, val)
    for k, v in pairs(tbl) do
        if k == val then return v end
    end
    return nil
end

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