Outward Wiki
Advertisement

Lua module used by EffectSources template.


local p = {}

function p.sources(frame)
    local output = {}

    if frame == mw.getCurrentFrame() then
		args = require('Module:ProcessArgs').merge(true)
	else
		frame = mw.getCurrentFrame()
    end
    
    local _effect = args.name or mw.title.getCurrentTitle().text

    local fields = '_pageName, effect, type, buildup, chance, effectlevel'
    local args = { where = string.format('effect="%s"', _effect), orderBy = '_pageName' }
    local result = p.doQuery(_effect, fields, args)  

    if result then
        local html = mw.html.create()
        local hasPlayerInflicts = 0
        local hasPlayerReceives = 0
        local hasInflictsPlayer = 0
        local hasRequires = 0
        local hasRemoves = 0
        local hasBenefitsFrom = 0
        local hasEffectLevel = 0
        local hasPrevents = 0

        for k,v in ipairs(result) do
            if v.type == 'PlayerReceives' then
                hasPlayerReceives = 1 
            elseif v.type == 'PlayerInflicts' then
                hasPlayerInflicts = 1
            elseif v.type == 'Prevents' then
            	hasPrevents = 1
            elseif v.type == 'InflictsPlayer' then
                hasInflictsPlayer = 1
            elseif v.type == 'Requires' then
                hasRequires = 1
            elseif v.type == 'Removes' then
                hasRemoves = 1
            elseif v.type == 'BenefitsFrom' then
                hasBenefitsFrom = 1
            end
            if v.effectlevel == '5' then
                hasEffectLevel = 1
            elseif v.effectlevel == '4' then
                hasEffectLevel = 1
            elseif v.effectlevel == '3' then
                hasEffectLevel = 1
            elseif v.effectlevel == '2' then
                hasEffectLevel = 1
            elseif v.effectlevel == '1' then
                hasEffectLevel = 1
            end
        end

        -- list PlayerReceives
        if hasPlayerReceives == 1 then
            if hasEffectLevel == 0 then                
                html:tag('b'):wikitext('Obtained From:'):done()
                local list = html:tag('ul')   
                for k,v in ipairs(result) do
                    if v.type == 'PlayerReceives' then
                        local output = string.format("[[%s]]", v._pageName)
                        if not ( tonumber(v.chance) == nil ) then
                            output = string.format('%s (%s%% chance)', output, v.chance)
                        end
                        list:tag('li'):wikitext(output):done()
                    end
                end
                list:done()
            else
                local hasLevel5 = 0
                local hasLevel4 = 0
                local hasLevel3 = 0
                local hasLevel2 = 0
                local hasLevel1 = 0
                for k,v in ipairs(result) do
                    if v.effectlevel == '5' then
                        hasLevel5 = 1
                    elseif v.effectlevel == '4' then
                        hasLevel4 = 1
                    elseif v.effectlevel == '3' then
                        hasLevel3 = 1
                    elseif v.effectlevel == '2' then
                        hasLevel2 = 1
                    elseif v.effectlevel == '1' then
                        hasLevel1 = 1
                    end
                end
                if (hasLevel5 == 1) then
                    html:tag('b'):wikitext('Level 5:'):done()
                    local list = html:tag('ul')
                    for k, v in ipairs(result) do
                        if v.effectlevel == '5' then
                            list:tag('li'):wikitext(string.format("[[%s]]", v._pageName)):done()
                        end
                    end
                    list:done()
                end
                if (hasLevel4 == 1) then
                    html:tag('b'):wikitext('Level 4:'):done()
                    local list = html:tag('ul')
                    for k, v in ipairs(result) do
                        if v.effectlevel == '4' then
                            list:tag('li'):wikitext(string.format("[[%s]]", v._pageName)):done()
                        end
                    end
                    list:done()
                end
                if (hasLevel3 == 1) then
                    html:tag('b'):wikitext('Level 3:'):done()
                    local list = html:tag('ul')
                    for k, v in ipairs(result) do
                        if v.effectlevel == '3' then
                            list:tag('li'):wikitext(string.format("[[%s]]", v._pageName)):done()
                        end
                    end
                    list:done()
                end
                if (hasLevel2 == 1) then
                    html:tag('b'):wikitext('Level 2:'):done()
                    local list = html:tag('ul')
                    for k, v in ipairs(result) do
                        if v.effectlevel == '2' then
                            list:tag('li'):wikitext(string.format("[[%s]]", v._pageName)):done()
                        end
                    end
                    list:done()
                end
                if (hasLevel1 == 1) then
                    html:tag('b'):wikitext('Level 1:'):done()
                    local list = html:tag('ul')
                    for k, v in ipairs(result) do
                        if v.effectlevel == '1' then
                            list:tag('li'):wikitext(string.format("[[%s]]", v._pageName)):done()
                        end
                    end
                    list:done()
                end
            end
        end
        
        -- get Removes sources
        if hasRemoves == 1 then
            html:tag('b'):wikitext('Removed With:'):done()
            local list = html:tag('ul')   
            for k,v in ipairs(result) do
                if v.type == 'Removes' then
                    local output = string.format("[[%s]]", v._pageName)
                    list:tag('li'):wikitext(output):done()
                end
            end
            list:done()
        end
        
        -- get Prevents souces
        
        if hasPrevents == 1 then
            html:tag('b'):wikitext('Resisted By:'):done()
            local list = html:tag('ul')   
            for k,v in ipairs(result) do
                if v.type == 'Prevents' then
                    local output = '[[' .. v._pageName .. ']] (' .. v.buildup .. '%)'
                    list:tag('li'):wikitext(output):done()
                end
            end
            list:done()	
    	end
        
        -- get Playerinflicts sources
        if hasPlayerInflicts == 1 then
            html:tag('b'):wikitext('Inflicted With:'):done()
            local list = html:tag('ul')   
            for k,v in ipairs(result) do
                if v.type == 'PlayerInflicts' then
                    local output = string.format("[[%s]]", v._pageName)
                    if not ( tonumber(v.buildup) == nil ) then
                        output = string.format('%s (%s%% [[Effects#Buildup|buildup]])', output, v.buildup)
                    end
                    list:tag('li'):wikitext(output):done()
                end
            end
            list:done()
        end

        -- get Requires sources
        if hasRequires == 1 then
            html:tag('b'):wikitext('Required by Skills:'):done()
            local list = html:tag('ul')   
            for k,v in ipairs(result) do
                if v.type == 'Requires' then
                    local output = string.format("[[%s]]", v._pageName)
                    list:tag('li'):wikitext(output):done()
                end
            end
            list:done()
        end

        -- get BenefitsFrom sources
        if hasBenefitsFrom == 1 then
            html:tag('b'):wikitext(string.format('Skills which benefit from %s:', _effect)):done()
            local list = html:tag('ul')   
            for k,v in ipairs(result) do
                if v.type == 'BenefitsFrom' then
                    local output = string.format("[[%s]]", v._pageName)
                    list:tag('li'):wikitext(output):done()
                end
            end
            list:done()
        end
        
        -- get InflictsPlayer sources
        if hasInflictsPlayer == 1 then
            html:tag('b'):wikitext('Inflicted by Enemies:'):done()
            local list = html:tag('ul')   
            for k,v in ipairs(result) do
                if v.type == 'InflictsPlayer' then
                    local output = string.format("[[%s]]", v._pageName)
                    if not ( tonumber(v.buildup) == nil ) then
                        output = string.format('%s ([[Effects#Buildup|%s%%]])', output, v.buildup)
                    else
                        if not ( tonumber(v.chance) == nil ) then
                            output = string.format('%s (%s%% chance per hit)', output, v.chance)
                        end
                    end
                    list:tag('li'):wikitext(output):done()
                end
            end
            list:done()
        end

        return html
    else
        -- return mw.html.create():css('width: 50%'):wikitext('Auto-generated list: No sources found.'):done() 
    end
end


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

return p
Advertisement