Modul:Commodity

Aus Star Citizen Wiki
Modul Dokumentation[view][edit][history][purge]
Diese Dokumentation wird von Modul:Commodity/doc aus übernommen. Änderungen können auf der Diskussionsseite vorgeschlagen werden.
Function list
L 10 — round
L 20 — fixVehicleName
L 41 — fixType
L 73 — addRentalSubObject
L 109 — addBuySubObject
L 146 — methodtable.addShop
L 172 — methodtable.addShopData
L 190 — Commodity.formatOffset
L 205 — Commodity.calcPrice
L 241 — Commodity.calcPricePP
L 260 — Commodity.getPrice
L 276 — Commodity.new
✔ Alle Tests bestanden.
Unit tests
Name Expected Actual
testCalcPriceFloat
testCalcPriceInt
testCalcPricePPFloat
testCalcPricePPInt
testFormatOffset
testFormatOffsetSpan

Modulinfo

Dieses Modul ist ein Grundmodul für Handelswaren aller Art.

Öffentliche Methoden

Das Modul stellt folgende öffentliche Methoden bereit:

  • addShop( shop )
    • Speichert alle Items eines Shops als Subobjekte in der aktuellen Seite. Daten müssen aus der Star Citizen Wiki API kommen.
  • addShopData( data )
    • Speichert alle Shops eines Items ins Semantic Wiki. Ruft automatisch addShop auf. Daten müssen aus der Star Citizen Wiki API kommen.
  • Commodity.formatOffset( offset )
    • Formartiert ein Offset
  • Commodity.calcPrice( price, offset, power )
    • Berechnet einen finalen Preis anhand Basispreis, Offset. Power entspricht der Anzahl von Nachkommastellen.
  • Commodity.calcPricePP( price, percentage, isPremium, power )
    • Berechnet einen Preis Plus, oder Minus einer bestimmten Prozentzahl
  • Commodity.getPrice( name )
    • Gibt den günstigsten Preis für ein Item zurück

Tests

✔ Alle Tests bestanden.

Unit tests
Name Expected Actual
testCalcPriceFloat
testCalcPriceInt
testCalcPricePPFloat
testCalcPricePPInt
testFormatOffset
testFormatOffsetSpan

local Commodity = {}

local metatable = {}
local methodtable = {}
local common = require('Module:Common')

metatable.__index = methodtable


local function round( number, decimals )
    local power = 10^decimals
    return math.floor( number * power ) / power
end


--- Removes the Manufacturer Prefix from vehicle entries
---
--- @param commodity table
--- @return table
local function fixVehicleName( commodity )
    if commodity == nil or commodity.type == nil then
        return commodity
    end

    if ( commodity.type == 'Vehicle' or commodity.type == 'GroundVehicle' ) and commodity.name ~= nil and commodity.fixed == nil then
        local exploded = mw.text.split( commodity.name, ' ', true )
        table.remove( exploded, 1 )

        commodity.name = mw.text.trim( table.concat( exploded, ' ' ) )
        commodity.fixed = true
    end

    return commodity
end


--- Updates the type for vehicles
---
--- @param commodity table
--- @return table
local function fixType( commodity )
    if commodity == nil or commodity.type == nil then
        return commodity
    end

    if commodity.type == 'Vehicle' or commodity.type == 'GroundVehicle' then
        if commodity.sub_type == 'Vehicle_Spaceship' then
            commodity.type = {
            	'Raumschiff@de',
            	'Ship@en',
            }
        else
            commodity.type = {
            	'Fahrzeug@de',
            	'Vehicle@en',
            }
        end
    else
    	commodity.type = {
    		commodity.type .. '@en',
    		commodity.type .. '@de',
    	}
    end

    return commodity
end


--- Adds a rental subobejct, if the commodity can be rented
---
--- @param commodity table
--- @param shop table
local function addRentalSubObject( commodity, shop )
    if commodity.rentable == false or type( commodity.rental_price_days ) ~= 'table' then
        return
    end
    
    local object = {
        [ 'Name' ] = commodity.name,
        [ 'UUID' ] = commodity.uuid,
        [ 'Typ' ] = commodity.type,

        [ 'Händler' ] = shop.name,
        [ 'Ort' ] = shop.position,

        [ 'Spielversion' ] = commodity.version,
    }
    
    if commodity.rental_price_days.duration_1 ~= nil then
        object[ '1 Tag' ] = common.formatNum( commodity.rental_price_days.duration_1, 0 ) .. ' aUEC'
        object[ '3 Tage' ] = common.formatNum( commodity.rental_price_days.duration_3, 0 ) .. ' aUEC'
        object[ '7 Tage' ] = common.formatNum( commodity.rental_price_days.duration_7, 0 ) .. ' aUEC'
        object[ '30 Tage' ] = common.formatNum( commodity.rental_price_days.duration_30, 0 ) .. ' aUEC'
	else
        object[ '1 Tag' ] = common.formatNum( commodity.rental_price_days[ 1 ], 0 ) .. ' aUEC'
        object[ '3 Tage' ] = common.formatNum( commodity.rental_price_days[ 3 ], 0 ) .. ' aUEC'
        object[ '7 Tage' ] = common.formatNum( commodity.rental_price_days[ 7 ], 0 ) .. ' aUEC'
        object[ '30 Tage' ] = common.formatNum( commodity.rental_price_days[ 30 ], 0 ) .. ' aUEC'
	end

    mw.smw.subobject( object )
end


--- Adds a buy subobejct, if the commodity can be bought
---
--- @param commodity table
--- @param shop table
local function addBuySubObject( commodity, shop )
    if commodity.buyable == false then
        return
    end

    mw.smw.subobject( {
        [ 'UUID' ] = commodity.uuid,
        [ 'Name' ] = commodity.name,
        [ 'Basispreis' ] = common.formatNum( commodity.base_price or nil, nil ),
        [ 'Preis' ] = common.formatNum( commodity.price_calculated or nil, nil ),
        [ 'Minimalpreis' ] = common.formatNum( commodity.price_range.min or nil, nil ),
        [ 'Maximalpreis' ] = common.formatNum( commodity.price_range.max or nil, nil ),

        [ 'Preisoffset' ] = common.formatNum( commodity.base_price_offset or nil, nil ),
        [ 'Rabatt' ] = common.formatNum( commodity.max_discount or nil, nil ),
        [ 'Premium' ] = common.formatNum( commodity.max_premium or nil, nil ),
        [ 'Bestand' ] = common.formatNum( commodity.inventory or nil, nil ),
        [ 'Maximalbestand' ] = common.formatNum( commodity.max_inventory or nil, nil ),
        [ 'Wiederauffüllungsrate' ] = common.formatNum( commodity.refresh_rate or nil, nil ),

        [ 'Kaufbar' ] = commodity.buyable,
        [ 'Verkaufbar' ] = commodity.sellable,
        [ 'Mietbar' ] = type( commodity.rental_price_days ) == 'table',
        [ 'Typ' ] = commodity.type,

        [ 'Händler' ] = shop.name,
        [ 'Ort' ] = shop.position,

        [ 'Spielversion' ] = commodity.version,
    } )
end


--- Adds all available commodities from a shop to the page
---
--- @param shop table
--- @return void
function methodtable.addShop( self, shop )
    if type( shop ) ~= 'table' or shop.items == nil then
        return
    end
    
    local data = {}
    if type( shop.items.data ) == 'table' then
    	data = shop.items.data
    elseif type( shop.items ) == 'table' then
    	data = shop.items
	end

    for _, commodity in pairs( data ) do
        commodity = fixVehicleName( commodity )
        commodity = fixType( commodity )

        addBuySubObject( commodity, shop )
        addRentalSubObject( commodity, shop )
    end
end


--- Adds all available shop data to the page
---
--- @param data table
--- @return void
function methodtable.addShopData( self, data )
    if type( data ) ~= 'table' or data.shops == nil or type( data.shops ) ~= 'table' then
        return
    end

    local shopData = {}
    if type( data.shops.data ) == 'table' then
    	shopData = data.shops.data
    elseif type( data.shops ) == 'table' then
    	shopData = data.shops
	end

    for _, shop in pairs( shopData ) do
        self:addShop( shop )
    end
end


function Commodity.formatOffset( offset )
    if offset == nil or offset == 0 or offset == '0' then
        return '0%'
    end

    return '<span title="' .. offset .. '%">' .. math.floor( offset ) .. '%</span>'
end


--- Calculates the price based on the items offset
--- price * ( 1 + ( offset / 100 ) )
---
--- @param price string|number base price
--- @param offset string|number|nil offset
--- @return string
function Commodity.calcPrice( price, offset, power )
	power = power or 0
	offset = common.toNumber( offset, false )

	if type( price ) == 'table' then
		price = price[ 1 ]
	end

	local priceNum = common.toNumber( price, false )

    if offset == nil or offset == false or offset == 0 or offset == -100 then
        return mw.getContentLanguage():formatNum( priceNum )
    end

    if priceNum == false then
        -- Failsafe
        return price
    end

    priceNum = round( priceNum * ( 1 + ( offset / 100 ) ), power )
    local formatted = mw.getContentLanguage():formatNum( priceNum )

    if formatted == nil then
        return priceNum
    end

    return formatted
end


--- Calculates a price plus or minus a given percentage
---
--- @param price string|number price
--- @param range string|number|nil range call as whole percent (e.g. 25 for 25% etc.)
--- @param isPremium boolean True to add percentage
--- @return number
function Commodity.calcPricePP( price, percentage, isPremium, power )
	power = power or 0
	local priceNum = common.toNumber( price, false )
	percentage = common.toNumber( percentage, false )

    if percentage == false or priceNum == false then
    	return price
    end

	if isPremium == true then
		percentage = percentage * -1
	end

	return round( priceNum * ( 1 - ( percentage / 100 ) ), power )
end


--- @param name string
--- @return table
function Commodity.getPrice( name )
    local query = {
        '[[Name::' .. name .. ']][[Kaufbar::1]]',
        '?Preis#-p0=price',
        'mainlabel=-',
        'limit=1',
        'order=asc',
        'sort=Preis',
    }

    return mw.smw.ask( query )
end


--- New Instance
--- Library entrance
function Commodity.new( self )
    local instance = {}

    setmetatable( instance, metatable )

    return instance
end


return Commodity
Cookies helfen uns bei der Bereitstellung dieses Wikis. Durch die Nutzung des Star Citizen Wiki erklärst du dich damit einverstanden, dass wir Cookies speichern.