Module:Commodity

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 = 'Ship' else commodity.type = 'Ground vehicle' end 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, [ 'Type' ] = commodity.type,

[ 'Shop' ] = shop.name, [ 'Location' ] = shop.position, }   if commodity.rental_price_days.duration_1 ~= nil then object[ '1 day rental price' ] = common.formatNum( commodity.rental_price_days.duration_1, 0 ) .. ' aUEC' object[ '3 days rental price' ] = common.formatNum( commodity.rental_price_days.duration_3, 0 ) .. ' aUEC' object[ '7 days rental price' ] = common.formatNum( commodity.rental_price_days.duration_7, 0 ) .. ' aUEC' object[ '30 days rental price' ] = common.formatNum( commodity.rental_price_days.duration_30, 0 ) .. ' aUEC' else object[ '1 day rental price' ] = common.formatNum( commodity.rental_price_days[ 1 ], 0 ) .. ' aUEC' object[ '3 days rental price' ] = common.formatNum( commodity.rental_price_days[ 3 ], 0 ) .. ' aUEC' object[ '7 days rental price' ] = common.formatNum( commodity.rental_price_days[ 7 ], 0 ) .. ' aUEC' object[ '30 days rental price' ] = 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,        [ 'Base price' ] = common.formatNum( commodity.base_price or nil, nil ),        [ 'Price' ] = common.formatNum( commodity.price_calculated or nil, nil ),        [ 'Minimum price' ] = common.formatNum( commodity.price_range.min or nil, nil ),        [ 'Maximum price' ] = common.formatNum( commodity.price_range.max or nil, nil ),

[ 'Price offset' ] = common.formatNum( commodity.base_price_offset or nil, nil ), [ 'Discount' ] = common.formatNum( commodity.max_discount or nil, nil ), [ 'Premium' ] = common.formatNum( commodity.max_premium or nil, nil ), [ 'Stock' ] = common.formatNum( commodity.inventory or nil, nil ), [ 'Maximum stock' ] = common.formatNum( commodity.max_inventory or nil, nil ), [ 'Restock rate' ] = common.formatNum( commodity.refresh_rate or nil, nil ),

[ 'Buyable' ] = commodity.buyable, [ 'Sellable' ] = commodity.sellable, [ 'Rentable' ] = type( commodity.rental_price_days ) == 'table', [ 'Type' ] = commodity.type,

[ 'Shop' ] = shop.name, [ 'Location' ] = shop.position, } ) 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 ' ' .. math.floor( offset ) .. '% ' 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

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

setmetatable( instance, metatable )

return instance end

return Commodity