Modul:ArmorTable

Aus Steel Beasts Wiki
Version vom 17. Mai 2023, 20:20 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:ArmorTable/Doku erstellt werden

local ArmorTable = {}

local common = require( 'Module:Common' )

local metatable = {}
local methodtable = {}

metatable.__index = methodtable


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

    -- name from args or current page
    -- local subType = self.frameArgs[ 'Typ' ] or 'Raumschiff'

    local query = {
        '[[Ist Basisversion::wahr]][[Kategorie:Körperpanzerung]][[Set::+]]',
        '?#-=page',
        '?Set#-=set',
        '?Schadensreduktion#-=dmg_resist',
        '?Temperaturresistenz=temp_resist',
        '?Rüstungsklasse#-=class', '+lang=' .. common.getLocaleForPage(),
        '?Rüstungstyp#-=type', '+lang=' .. common.getLocaleForPage(),
        '?SP=sp',
        '?Hersteller#-=manufacturer',
        '?Spielversion#-=version',
        '?-Name.Basispreis=price',
        'mainlabel=name',
        'sort=name,Rüstungsklasse,Set',
        'order=asc,asc,asc',
        'limit=500',
    }

    local data = mw.smw.ask( query )

    if data == nil or data[ 1 ] == nil then
        return nil
    end
	
	mw.logObject(data[ 1 ] )
    
    self.smwData = data

    return self.smwData
end


--- Sorts the table by Position -> Shop
---
--- @return table
function methodtable.sort( self )
    local sorted = {}

    for _, row in pairs( self.smwData ) do
        if row.type ~= nil and row.set ~= nil then
            if type( sorted[ row.type ] ) ~= 'table' then
                sorted[ row.type ] = {}
            end

            if type( sorted[ row.type ][ row.set ] ) ~= 'table' then
                sorted[ row.type ][ row.set ] = {}
            end

            table.insert( sorted[ row.type ][ row.set ], row )
        end
    end

    return sorted
end


--- Creates the buy table
---
--- @param armor table
--- @return string
function methodtable.createTable(self, items)
    if #items == 0 then
        return ''
    end

    local currentRow = 1

    local table = mw.html.create( 'table' )
                    :attr( 'class', 'wikitable broadtable sortable' )
                    :attr( 'style', 'width: 100%')
                    :tag( 'tr' )
                        :tag( 'th' )
                            :wikitext( 'Name' )
                            :done()
                        :tag( 'th' )
                            :wikitext( 'Schadensreduktion' )
                            :done()
                        :tag( 'th' )
                            :wikitext( 'Temperaturresistenz' )
                            :done()
                        :tag( 'th' )
                            :wikitext( 'Rüstungsklasse' )
                            :done()
                        :tag( 'th' )
                            :wikitext( 'Preis' )
                            :done()
                        :tag( 'th' )
                            :wikitext( 'Hersteller' )
                            :done()
                        :tag( 'th' )
                            :wikitext( 'Spielversion' )
                            :done()
                        :done()
                    :done()

    for _, armor in pairs(items) do
        local trClass = 'row-odd'
        if currentRow % 2 == 0 then
            trClass = 'row-even'
        end
        currentRow = currentRow + 1

		local price = '-'
		if type( armor.price ) == 'table' then
			price = armor.price[ 1 ]
		end
		
		local tempResist = armor.temp_resist
		if type( tempResist ) == 'table' then
			tempResist = (armor.temp_resist[1] or '-') .. ' / ' .. (armor.temp_resist[2] or '-')
		end

        table:tag( 'tr' )
             :addClass( trClass )
                 :tag( 'td' )
                     :wikitext( string.format( '[[%s]]', armor.page ) )
                     :done()
                 :tag( 'td' )
                    :wikitext( ( ( armor.dmg_resist or 0 ) * 100 ) .. '%' )
                    :done()
                 :tag( 'td' )
                    :wikitext( tempResist )
                    :done()
                 :tag( 'td' )
                    :wikitext( armor.class )
                    :done()
                 :tag( 'td' )
                    :wikitext( price )
                    :done()
                 :tag( 'td' )
                	:wikitext( string.format( '[[%s]]', armor.manufacturer ) )
                    :done()
                 :tag( 'td' )
                     :wikitext( armor.version )
                    :done()
             :done()
    end

    return table:allDone()
end


--- Outputs the table
---
--- @return string
function methodtable.make( self )
    local data = self:getSmwData()

    if data == nil then
        return '<p class="hatnote">Konnte keine semantischen Daten laden.</p>'
    end

    local sorted = self:sort()
    local tabber = self.args[ 'output_tabber' ] ~= nil
    local out = ''

    for class, sets in common.spairs( sorted ) do
        out = string.format( "%s\n=== [[%s]] ===\n", out, class)
        
        local content = ''

        for set, items in common.spairs(sets) do
        	local format = "%s\n==== [[%s]] ====\n%s"

            if tabber then
            	format = "%s|-|%s=\n%s"
            end

			content = string.format( format, content, set, tostring( self:createTable(items) ) )
        end

		if tabber then
			out = out .. "\n" .. mw.getCurrentFrame():extensionTag( 'tabber', mw.text.trim( content ) )
		else
			out = out .."\n" .. content
		end
    end

    return mw.text.trim( out )
end


--- New Instance
--- @return table ArmorTable
function ArmorTable.new( self, frameArgs )
    local instance = {
        args = frameArgs,
    }

    setmetatable( instance, metatable )

    return instance
end

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

    local instance = ArmorTable:new( args )

    return instance:make()
end


return ArmorTable