Modul:VehicleNavplate

Aus Steel Beasts Wiki
Version vom 3. September 2023, 08:02 Uhr von Dr.Thodt (Diskussion | Beiträge) (1 Version importiert)
(Unterschied) ← Nächstältere Version | Aktuelle Version (Unterschied) | Nächstjüngere Version → (Unterschied)

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

local VehicleNavplate = {}

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, category )
    -- Cache multiple calls
    if self.smwData ~= nil then
        return self.smwData
    end

    category = category or ''

    local askData = {
        '?#-=page',
        '?Hersteller#-=manufacturer',
        sort = 'Hersteller',
        order = 'asc',
        limit = 250,
    }

    local query = ''

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

	query = query .. [[:+]]
	table.insert( askData, 1, query )

    local data = mw.smw.ask( askData )

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

	mw.logObject( data )

    self.smwData = data

    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 )
    local data = self:getSmwData( self.plateType )

    if data == nil then
        return '<p class="hatnote">Konnte keine semantischen Daten laden.</p>'
    end
    
    local grouped = self:group( data, 'manufacturer' )
    
    local args = {
		title = self.args[ 2 ] or 'Raumschiffe',
		subtitle = 'Übersicht'
	}
	
	local i = 1
	for manufacturer, vehicles in common.spairs( grouped ) do
		args[ 'label' .. i ] = string.format( '[[Datei:Comm-Link 18427 Logo %s.png|25px|link=]] %s', manufacturer, manufacturer )
		args[ 'list' .. i ] = table.concat( vehicles )
		i = i + 1
	end

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


--- New Instance
--- @return table VehicleNavplate
function VehicleNavplate.new( self, frameArgs )
    local plateType = frameArgs[ 1 ] or 'Raumschiff'

    local instance = {
        args = frameArgs,
        plateType = plateType,
    }

    setmetatable( instance, metatable )

    return instance
end


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

    local instance = VehicleNavplate:new( args )

    return instance:make()
end


return VehicleNavplate