Modul:VehiclePriceTable: Unterschied zwischen den Versionen

Aus Steel Beasts Wiki
sc>FoXFTW
KKeine Bearbeitungszusammenfassung
 
K (1 Version importiert)
 
(kein Unterschied)

Aktuelle Version vom 3. September 2023, 08:02 Uhr

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

local VehiclePriceTable = {}

local metatable = {}
local methodtable = {}

metatable.__index = methodtable

local common = require( 'Module:Common' )
local log = require( 'Module:Log' )


--- 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

    if self.args[ 1 ] == 'rental' then
        query = {
            '[[1 Tag::+]][[Typ::Raumschiff||Typ::Fahrzeug]][[Ort::+]][[Ort::!Levski]]',
            '?#-=page',
            '?Name#-=name',
            '?1 Tag=price_1',
            '?1 Tag#-n=raw_1',
            '?3 Tage=price_3',
            '?3 Tage#-n=raw_3',
            '?7 Tage=price_7',
            '?7 Tage#-n=raw_7',
            '?30 Tage=price_30',
            '?30 Tage#-n=raw_30',
            '?Händler#-=vendor',
            '?Ort#-=position',
            '?Spielversion#-=version',
            'mainlabel=-',
            'sort=Name,Ort,Händler',
            'order=asc,asc,asc',
            'limit=250',
        }
    else
        query = {
            '[[Kaufbar::wahr]][[Typ::Raumschiff||Typ::Fahrzeug]][[Ort::+]][[Ort::!Levski]]',
            '?#-=page',
            '?Name#-=name',
            '?Preis=price',
            '?Preis#-n=price_raw',
            '?Händler#-=vendor',
            '?Ort#-=position',
            '?Spielversion#-=version',
            'mainlabel=-',
            'sort=Name,Ort,Händler',
            'order=asc,asc,asc',
            'limit=250',
        }
    end

    local data = mw.smw.ask( query )

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

    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.position ~= nil and row.vendor ~= nil then
	        if type( sorted[ row.position ] ) ~= 'table' then
	            sorted[ row.position ] = {}
	        end
	
	        if type( sorted[ row.position ][ row.vendor ] ) ~= 'table' then
	            sorted[ row.position ][ row.vendor ] = {}
	        end
	
	        table.insert( sorted[ row.position ][ row.vendor ], row )
		end
    end

    return sorted
end


--- Creates the buy table
---
--- @param vehicles table
--- @return string
function methodtable.createTable( self, vehicles )
    if #vehicles == 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( 'Preis' )
                            :done()
                        :tag( 'th' )
                            :wikitext( 'Spielversion' )
                            :done()
                        :done()
                    :done()

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

       table:tag( 'tr' )
            :addClass( trClass )
               :tag( 'td' )
                    :wikitext( string.format( '[[%s]]', vehicle.name ) )
                    :done()
               :tag( 'td' )
                    :attr( 'data-sort-value', vehicle.price_raw )
                    :wikitext( vehicle.price )
                    :done()
               :tag( 'td' )
                    :wikitext( vehicle.version )
                    :done()
            :done()
    end

    return table:allDone()
end


--- Creates the rental table
---
--- @param vehicles table
--- @return string
function methodtable.createRentalTable( self, vehicles )
    if #vehicles == 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( '1 Tag' )
                            :done()
                        :tag( 'th' )
                            :wikitext( '3 Tage' )
                            :done()
                        :tag( 'th' )
                            :wikitext( '7 Tage' )
                            :done()
                        :tag( 'th' )
                            :wikitext( '30 Tage' )
                            :done()
                        :tag( 'th' )
                            :wikitext( 'Spielversion' )
                            :done()
                        :done()
                    :done()

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

       table:tag( 'tr' )
            :addClass( trClass )
               :tag( 'td' )
                    :wikitext( string.format( '[[%s]]', vehicle.name ) )
                    :done()
               :tag( 'td' )
                    :attr( 'data-sort-value', vehicle.raw_1 )
                    :wikitext( vehicle.price_1 )
                    :done()
               :tag( 'td' )
               :attr( 'data-sort-value', vehicle.raw_3 )
               :wikitext( vehicle.price_3 )
                    :done()
               :tag( 'td' )
               :attr( 'data-sort-value', vehicle.raw_7 )
               :wikitext( vehicle.price_7 )
                    :done()
               :tag( 'td' )
               :attr( 'data-sort-value', vehicle.raw_30 )
               :wikitext( vehicle.price_30 )
                    :done()
               :tag( 'td' )
                    :wikitext( vehicle.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 log.info( 'Konnte keine semantischen Preisdaten laden.' )
    end

    local sorted = self:sort()
    local out = ''

    local rental = self.args[ 1 ] == 'rental'
    local tabber = self.args[ 'output_tabber' ] ~= nil

    for position, merchants in common.spairs( sorted ) do
    	local format = "=== [[%s]] ===\n"
    	if tabber then
			format = "|-|%s=\n"
    	end

        local outerContent = string.format( format, position )
        local innerContent = ''

        for merchant, vehicles in common.spairs( merchants ) do
        	local innerFormat = "%s\n==== [[%s]] ====\n%s"
        	local vehicleTable

            if rental == true then
            	vehicleTable = tostring( self:createRentalTable( vehicles ) )
            else
            	vehicleTable = tostring( self:createTable( vehicles ) )
            end

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

			innerContent = string.format( innerFormat, innerContent, merchant, vehicleTable )
        end

        if tabber then
        	innerContent = mw.getCurrentFrame():extensionTag( 'tabber', innerContent )
        end
		outerContent = string.format( "%s%s", outerContent, innerContent )
		
		out = string.format( "%s\n%s", out, outerContent )
    end

	if tabber then
		return mw.getCurrentFrame():extensionTag( 'tabber', mw.text.trim( out ) )
	end

    return mw.text.trim( out )
end


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

    setmetatable( instance, metatable )

    return instance
end

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

    local instance = VehiclePriceTable:new( args )

    return instance:make()
end


return VehiclePriceTable