Modul:ManufacturerNavplate

Aus Steel Beasts Wiki
Version vom 5. Juli 2023, 14:14 Uhr von sc>FoXFTW
(Unterschied) ← Nächstältere Version | Aktuelle Version (Unterschied) | Nächstjüngere Version → (Unterschied)

Die Dokumentation für dieses Modul kann unter Modul:ManufacturerNavplate/Doku erstellt werden

local ManufacturerNavplate = {}

local metatable = {}
local methodtable = {}

metatable.__index = methodtable


local navplate = require( 'Module:Navplate' )
local common = require( 'Module:Common' )


--- Queries the SMW Store
--- @return table
function methodtable.getSmwData( self )
    -- Cache multiple calls
    if self.smwData ~= nil then
        return self.smwData
    end

    manufacturer = self.manufacturer

    local askData = {
        '?#-=page',
        '?Name#-=name',
        '?Typ#-=type',
        '+lang=de',
        '?Rüstungsklasse#-=armortype',
        '+lang=de',
        '?Fahrzeugtyp#-=vehicletype',
        '+lang=de',
        '?Waffentyp#-=weapontype',
        '+lang=de',
        '?Ist Basisversion=base_version',
        order = 'asc',
        limit = 250,
    }

    local query = ''

    if string.sub( manufacturer, 1, 2 ) == '[[' then
    	query = manufacturer
    else
    	query = '[[Hersteller::' .. manufacturer .. '|+depth=0]]'
    end

	query = query .. '[[Kategorie:Universum]]' -- Ignore subobjects
	table.insert( askData, 1, query )

    local data = mw.smw.ask( askData )

    if data == nil or data[ 1 ] == nil then
        return nil
    end

    local presort = {}
    for _, row in pairs( data ) do
    	if type( row['armortype'] ) == 'string' then
    		row['type'] = row['armortype']
    	end

    	if type( row['weapontype'] ) == 'string' then
    		row['type'] = row['weapontype']
    	end
    	
    	if row.vehicletype ~= nil then
    		-- TODO: Localize and fix ground vehicles
    		row['type'] = 'Raumschiff'
		end

		if row.base_version == nil or row.base_version == true then
			table.insert( presort, row )
		end
    end

	mw.logObject(presort)

    self.smwData = presort

    return self.smwData
end


--- Sorts the table by Manufacturer
---
--- @param data table SMW data - Requires a 'page' key on each row
--- @param groupKey string Key on objects to group them under, e.g. manufacturer
--- @return table
function methodtable.group( self, data, groupKey )
    local grouped = {}

    if type( data ) ~= 'table' or type( groupKey ) ~= 'string' then
        return grouped
    end

    for _, row in pairs( data ) do
        if row[ groupKey ] ~= nil then
            if type( grouped[ row[ groupKey ] ] ) ~= 'table' then
                grouped[ row[ groupKey ] ] = {}
            end
            
            local name = common.removeTypeSuffix( row.page, self.plateType )
            if row.name ~= nil then
            	name = row.name
        	end

            table.insert(
            	grouped[ row[ groupKey ] ],
            	string.format(
            		'[[%s|%s]]',
            		row.page,
            		name
        		)
        	)
        end
    end

	mw.logObject( grouped )

    return grouped
end



--- Outputs the table
---
--- @return string
function methodtable.make( self )
    if self.manufacturer == nil or self.manufacturer == '' then
        return ''
    end

    local data = self:getSmwData( self.manufacturer )

    if data == nil then
        return ''
    end

    local grouped = self:group( data, 'type' )
    
    local args = {
		title = self.manufacturer,
		subtitle = 'Produkte von'
	}

	local i = 1
	for manufacturer, items in common.spairs( grouped ) do
		args[ 'label' .. i ] = string.format( '%s', manufacturer )
		args[ 'list' .. i ] = table.concat( items )
		i = i + 1
	end

    return navplate.navplateTemplate({
    	args = args
    })
end


--- New Instance
--- @return table ManufacturerNavplate
function ManufacturerNavplate.new( self, frameArgs )
    local manufacturer = frameArgs[ 1 ] or frameArgs[ 'Hersteller' ] or frameArgs[ 'Manufacturer' ]
	local title = mw.title.getCurrentTitle()

    local instance = {
        args = frameArgs,
        manufacturer = manufacturer,
        plateType = 'Hersteller',
        title = title,
    }

    setmetatable( instance, metatable )

    return instance
end


--- Entry for module calls
---
--- @return string
function ManufacturerNavplate.fromArgs( frame )
    local args = require( 'Module:Arguments ').getArgs( frame )

    local instance = ManufacturerNavplate:new( args )

    return instance:make()
end


return ManufacturerNavplate