Module:ListDefeatScenarios

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

Module used to list Defeat Scenarios.

Usage

{{#invoke:ListDefeatScenarios|main}}

Manual List

{{#invoke:ListDefeatScenarios|list|1|2|3|4|..}}

The arguments after list (1, 2, 3, 4...) are the IDs of the scenarios to list.


local p = {}

function p.main(frame)
    if frame == mw.getCurrentFrame() then
		args = require('Module:ProcessArgs').merge(true)
	else
		frame = mw.getCurrentFrame()
    end

    -- cargo query
    local fields = '_pageName, id, name, timeChange, respawnLocation'
    local cargoArgs = { orderBy = '_pageName', limit = 5000 }
    local data = mw.ext.cargo.query('DefeatScenarios', fields, cargoArgs)

    if #data < 1 then
        return '<b>No results found!</b>'
    end
    
    -- build table output 
    local html = mw.html.create()
	buildoutput(data, html, frame)
    return html
end

-- duplicate scenarios
-- these scenarios are just duplicates of existing IDs, but with very trivial differences
function getDuplicates()
    -- key: the scene which is a duplicate
    -- value: the original scene it was cloned from
    local duplicates = {}
    -- clones of "Beaten and bloody, but alive"
    duplicates["75"] = 1
    duplicates["76"] = 1
    duplicates["90"] = 1
    duplicates["11"] = 1
    duplicates["12"] = 1
    duplicates["17"] = 1
    duplicates["63"] = 1
    duplicates["14"] = 1
    duplicates["15"] = 1
    duplicates["13"] = 1
    duplicates["70"] = 1
    duplicates["18"] = 1
    duplicates["16"] = 1
    duplicates["42"] = 1
    duplicates["46"] = 1
    duplicates["43"] = 1
    duplicates["48"] = 1
    duplicates["44"] = 1
    duplicates["45"] = 1
    duplicates["47"] = 1
    duplicates["36"] = 1
    duplicates["38"] = 1
    duplicates["33"] = 1
    duplicates["34"] = 1
    duplicates["35"] = 1
    duplicates["37"] = 1
    duplicates["41"] = 1
    duplicates["30"] = 1
    duplicates["24"] = 1
    duplicates["29"] = 1
    duplicates["27"] = 1
    duplicates["28"] = 1
    duplicates["26"] = 1
    duplicates["132"] = 1
    duplicates["148"] = 1
    duplicates["146"] = 1
    duplicates["129"] = 1
    duplicates["115"] = 1
    duplicates["131"] = 1
    duplicates["128"] = 1
    duplicates["121"] = 1
    duplicates["124"] = 1
    duplicates["143"] = 1
    duplicates["123"] = 1
    duplicates["117"] = 1
    duplicates["120"] = 1
    duplicates["119"] = 1
    duplicates["145"] = 1
    duplicates["126"] = 1
    duplicates["114"] = 1
    duplicates["127"] = 1
    duplicates["122"] = 1
    duplicates["147"] = 1
    duplicates["118"] = 1
    duplicates["130"] = 1
    duplicates["144"] = 1
    duplicates["133"] = 1
    duplicates["125"] = 1
    duplicates["116"] = 1
    -- clones of Gep
    duplicates["102"] = 2
    -- clones of "Nursed to health at the Levant Inn"
    duplicates["317"] = 39
    -- clones of "Captured by Kazite bandits"
    duplicates["88"] = 84
    duplicates["89"] = 84
    return duplicates
end

function p.list(frame)
    if frame == mw.getCurrentFrame() then
		args = require('Module:ProcessArgs').merge(true)
	else
		frame = mw.getCurrentFrame()
    end

    local cargoWhere = ''
    local checkedIDs = {}
    local duplicates = getDuplicates()
    
    for i,arg in ipairs(args) do
    	local idString = arg
    	
    	-- if arg begins with "_", remove it
    	if (string.sub(idString, 1, 1) == '_') then
    		idString = string.sub(idString, 2)
		end
		idString = string.sub(idString, 1, 5)
		
		-- check if it ends with "_", if so remove it
		while (string.find(idString, "_") ~= nil) do
			idString = string.sub(idString, 1, string.len(idString) - 1)
		end
    	
    	local id = tonumber(idString)
    	if not (id == nil or id == '') then
    		
    		if (duplicates[tostring(id)]) then
    			id = duplicates[tostring(id)]
			end
    		
    		if not checkedIDs[id] then
    			checkedIDs[id] = true
    			if cargoWhere ~= '' then cargoWhere = cargoWhere .. ' OR ' end
        		cargoWhere = cargoWhere .. 'id="' .. id .. '"'
        		
        		-- store in cargo table
        		frame:callParserFunction{ 
					name='#cargo_store', 
					args = {
						'_table=DefeatScenarioSources',
						id = id,
					}
				}
        	end	
		end
    end

    -- basic cargo query
    local fields = '_pageName, id, name, timeChange, respawnLocation'
    local cargoArgs = { orderBy = '_pageName', where = cargoWhere, limit = 5000 }
    local data = mw.ext.cargo.query('DefeatScenarios', fields, cargoArgs)

    if #data < 1 then
        return 'No results!'
    end

    -- build table output 
    local html = mw.html.create()
    buildoutput(data, html, frame)
    return html
end

function buildoutput(data, html, frame) 
    local table = html:tag('table'):addClass('wikitable sortable')
    local header = table:tag('tr')
    header:tag('th'):wikitext('Scenario')
    header:tag('th'):wikitext('Time Progression')
    header:tag('th'):wikitext('Respawn Locations')

    for _,v in ipairs(data) do
        local tr = table:tag('tr')
        -- name
        name = '[[' .. v._pageName .. '|' .. v.name .. ']]'
        tr:tag('td'):wikitext(frame:preprocess(name))
        -- time
        tr:tag('td'):wikitext(v.timeChange)
        -- locations
        tr:tag('td'):wikitext(frame:preprocess(v.respawnLocation))
    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)
    for _,v in ipairs(table1) do
        if contains(table2, v) then
            return true
        end
    end
    return false
end

function contains(table, value)
    for _,v in ipairs(table) do
        if v == value then
            return true
        end
    end
    return false
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