Module:Enemies

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

A Lua module used on the Enemies page.


local p = {}

function p.enemies(frame)
	args = require('Module:ProcessArgs').merge(true)
 
    type = args.type or ""
    dlc = args.DLC or ""

    local fields = { '_pageName', 'DLC', 'name', 'image', 'class', 'type', 'locations', 'health', 'healthRegen', 'impactResistance', 'protection', 'physResist', 'ethResist', 'decayResist', 'lightResist', 'frostResist', 'fireResist', 'StatusImmunity', 'BaseDamage', 'BaseImpact', 'inflicts' }
    
    local cargoWhere = ""

    if (type ~= "") then
        cargoWhere = string.format('type="%s"', type)
    end
    if (dlc ~= "") then
        if (cargoWhere ~= "") then
            cargoWhere = cargoWhere .. ' AND '
        end
        cargoWhere = cargoWhere .. 'DLC="' .. dlc .. '"'
    end

    local cargoArgs = { limit = 5000, where = cargoWhere }

    local result = p.doQuery(table.concat(fields,','), cargoArgs)  
    
    if not result then
        return mw.html.create():css('width: 50%'):wikitext('Auto-generated list: No enemies found.'):done() 
    else
        local table = mw.html.create('table'):addClass('wikitable sortable')

        -- Headers
        th = table:tag('tr')

        th:tag('th'):wikitext('Image')
        th:tag('th'):wikitext('Name')
        th:tag('th'):wikitext('Class')
        th:tag('th'):wikitext(frame:preprocess('{{health}} Health'))
        th:tag('th'):wikitext(frame:preprocess('{{health}} Regen'))
        th:tag('th'):wikitext(frame:preprocess('{{phys}}'))
        th:tag('th'):wikitext(frame:preprocess('{{ethereal}}'))
        th:tag('th'):wikitext(frame:preprocess('{{decay}}'))
        th:tag('th'):wikitext(frame:preprocess('{{lightning}}'))
        th:tag('th'):wikitext(frame:preprocess('{{frost}}'))
        th:tag('th'):wikitext(frame:preprocess('{{fire}}'))
        th:tag('th'):wikitext(frame:preprocess('{{impact}} Res.'))
        th:tag('th'):wikitext('Protection')
        th:tag('th'):wikitext('Status Immunity')
        th:tag('th'):wikitext('Base Damage')
        th:tag('th'):wikitext('Inflicts')

        -- Enemy rows
        styles = { 
            red = { color = "#df756f", align = 'center'},
            green = { color = "#aedc99", align = 'center'},
            none = { color = "#555555", align = 'center'}
        }
        
        for k,row in ipairs(result) do
            tr = table:tag('tr')

            tr:tag('td'):wikitext(frame:preprocess(string.format('[[File:%s|70x120px|link=%s]]',row.image,row._pageName)))
            local nameCell = string.format('[[%s]]',row._pageName)
            if (row.DLC ~= nil and row.DLC ~= '') then
            	nameCell = nameCell .. ' {{' .. row.DLC .. '}}'	
            end
            tr:tag('td'):wikitext(frame:preprocess(nameCell))
            tr:tag('td'):wikitext(frame:preprocess(row.class))
            
            tr:tag('td'):wikitext(frame:preprocess(row.health .. ' {{health}}')):attr('data-sort-value', tonumber(row.health) or 0)
            p.formatNumberCell(tr, row.healthRegen, '/s', frame, false)
            
            p.formatNumberCell(tr, row.physResist, '{{phys}}', frame, true)
            p.formatNumberCell(tr, row.ethResist, '{{ethereal}}', frame, true)
            p.formatNumberCell(tr, row.decayResist, '{{decay}}', frame, true)
            p.formatNumberCell(tr, row.lightResist, '{{lightning}}', frame, true)
            p.formatNumberCell(tr, row.frostResist, '{{frost}}', frame, true)
            p.formatNumberCell(tr, row.fireResist, '{{fire}}', frame, true)
            
            p.formatNumberCell(tr, row.impactResistance, '{{impact}}', frame, true)

            if (row.protection ~= nil and row.protection ~= '') then
                tr:tag('td'):wikitext(frame:preprocess(row.protection .. ' {{protection}}'))
                			:cssText('white-space:nowrap')
                	        :attr('data-sort-value', tonumber(row.protection) or 0)
            else
                tr:tag('td'):wikitext('-'):attr('data-sort-value', 0)
            end
                
            tr:tag('td'):wikitext(frame:preprocess(row.StatusImmunity))
            tr:tag('td'):wikitext(frame:preprocess(row.BaseDamage))
            tr:tag('td'):wikitext(frame:preprocess(row.inflicts))            
        end

        return table
    end
end

function p.formatNumberCell(tr, text, icon, frame, percent)
    local td = tr:tag('td')
    local value = tonumber(text) or 0
    local style = ''
    if (value > 0) then style = 'green' elseif (value < 0) then style = 'red' else style = 'none' end
    local text = value
    if percent then text = text .. '%' end
    td:css(styles[style])
    	:cssText('white-space:nowrap')
    	:wikitext(frame:preprocess(text .. ' ' .. icon))
    	:attr('data-sort-value', tonumber(value) or 0)
end

function p.doQuery(fields, args)
    local result = mw.ext.cargo.query('Enemies', fields, args)
	if not next(result) then
		return nil
	else
		return result
    end    
end

return p