Diese Dokumentation wird von Modul:Common/Api/doc aus übernommen. Änderungen können auf der Diskussionsseite vorgeschlagen werden.
Dieses Modul wird nicht verwendet.
Dieses Modul wird weder von einer Vorlage aufgerufen noch von einem anderen Modul benötigt/geladen. Wenn dies ein Fehler ist, stelle bitte sicher, dass
{{Dokumentation}}
/{{Keine Dokumentation}}
auf der Dokumentationsseite der Vorlage oder des Moduls eingebunden ist.Function list |
---|
L 7 — structureValid L 24 — common.getManufacturerName L 55 — common.getVolume L 72 — common.getFormattedCarryingCapacity L 83 — common.setBaseObject L 101 — common.checkResponseStructure L 123 — common.mapTranslation L 133 — add L 159 — common.getVersionInfoRef L 175 — common.makeAccessSafe L 176 — set L 184 — iterSet |
Unit tests | |||
---|---|---|---|
Name | Expected | Actual | |
✔ | testBooleanToTextFalseish | ||
✔ | testBooleanToTextFalseishCustomText | ||
✔ | testBooleanToTextTrueish | ||
✔ | testExtractPrefixNumerical | ||
✔ | testExtractPrefixNumericalEmpty | ||
✔ | testFormatNum | ||
✔ | testFormatNumberString | ||
✔ | testFormatNumberStringFail | ||
✔ | testRemoveTypeSuffix | ||
✔ | testRemoveTypeSuffixNoMatch | ||
✔ | testToNumberNum | ||
✔ | testToNumberString | ||
✔ | testToNumberStringFail |
Enthält allgemeine Funktionen für die Arbeit mit API Daten.
local common = {}
--- Checks if the provided data structure is valid
--- @param table data
--- @param string key
local function structureValid( data, key )
key = key or nil
if type( data ) ~= 'table' then
return false
end
if key ~= nil then
return data[key] ~= nil
end
return true
end
--- @param table data
--- @return string|nil
function common.getManufacturerName( data )
if not structureValid( data, 'manufacturer' ) then
return nil
end
local manufacturerName = data.manufacturer_description or data.manufacturer.name or nil
if manufacturerName ~= nil then
manufacturerName = mw.ustring.gsub( manufacturerName, '%[PH%]', '' )
manufacturerName = mw.ustring.gsub( manufacturerName, 'PH ', '' )
end
if manufacturerName == '@LOC_PLACEHOLDER' or manufacturerName == '@LOC PLACEHOLDER' or manufacturerName == 'Unknown Manufacturer' then
manufacturerName = 'Unbekannter Hersteller'
end
if manufacturerName == 'Virgil' then
manufacturerName = 'Virgil Ltd'
end
if manufacturerName == 'Vanduul Components - Generic' then
manufacturerName = 'Vanduul'
end
return manufacturerName
end
--- Returns the volume of an item converted to µSCU
--- @param table data
--- @return string|nil
function common.getVolume( data )
if not structureValid( data, 'dimension' ) then
return nil
end
local volume = data.dimension.volume or nil
if volume ~= nil then
volume = volume * (10^6)
end
return volume
end
--- Returns carrying capacity of an item converted to µSCU
--- @param table data
--- @return string|nil
function common.getFormattedCarryingCapacity( data )
if not structureValid( data, 'inventory' ) then
return nil
end
return data.inventory.scu_converted .. data.inventory.unit
end
--- Sets Base Information data on the smw set object
--- @param table data
function common.setBaseObject( data, setObj )
if not structureValid( data, 'base_version' ) then
setObj['Ist Basisversion'] = 1
else
setObj[ 'Ist Basisversion' ] = 0
setObj[ 'Basisversion UUID' ] = data.base_version.uuid
setObj[ 'Basisversion' ] = data.base_version.name
end
return setObj
end
--- Checks if Api Request was successful and if the Response is valid
--- @param response table
--- @param errorOnData boolean
--- @param errorOnData boolean
--- @return boolean
function common.checkResponseStructure( response, errorOnStatus, errorOnData )
if response[ 'status_code' ] ~= nil and response[ 'status_code' ] ~= 200 then
if errorOnStatus == nil or errorOnStatus == true then
error( 'API Anfrage resultierte in Fehlercode ' .. response[ 'status_code' ] .. '(' .. response[ 'message' ] .. ')', 0 )
end
return false
end
if response[ 'data' ] == nil then
if errorOnData == nil or errorOnData == true then
error( 'API Daten entalten kein "data" Feld', 0 )
end
return false
end
return true
end
--- Maps an array from the api to monolingual text format
--- @param input table - Form of { "de_DE" = "...", "en_EN" = "..." } or table of tables
--- @param ucFirst boolean - Whether to uppercase the first character
--- @return table
function common.mapTranslation( input, ucFirst )
input = input or {}
ucFirst = ucFirst or false
if type( input ) ~= 'table' then
return input
end
local out = {}
local function add( msg, locale )
if ucFirst then
msg = mw.language.getContentLanguage():ucfirst( msg )
end
table.insert( out, msg .. '@' .. string.sub( locale, 1, 2 ) )
end
for locale, translation in pairs( input ) do
if type( translation ) == 'table' then
for locale, msg in pairs( translation ) do
add( msg, locale )
end
elseif type( translation ) == 'string' then
add( translation, locale )
end
end
return out
end
--- Formats a game version to a ref link
--- @param version string - Version string
--- @param lang string - Target language de or en
--- @return table
function common.getVersionInfoRef( version, lang )
if version == nil or type( version ) ~= 'string' or #version == 0 then
return ''
end
return mw.getCurrentFrame():extensionTag{
name = 'ref',
content = require( 'Module:Translate' ):new().formatInLanguage( lang, 'Module:Common/i18n.json', 'msg_version_info', version, '[https://github.com/StarCitizenWiki/scunpacked-data GitHub]' )
}
end
--- Sets the table to return nil for unknown keys instead of erroring out
--- For deep nested tables use apiData:get( 'table1.table2.table3' ) etc.
--- @param apiData table - The json decoded data from the api
--- @return table
function common.makeAccessSafe( apiData )
local function set( data )
setmetatable( data, {
__index = function(self, key)
return nil
end
} )
end
local function iterSet( data )
set( data )
for _, v in pairs( data ) do
if type( v ) == 'table' then
iterSet( v )
end
end
end
iterSet( apiData )
apiData.get = function( self, key )
local parts = mw.text.split( key, '.', true )
local val = self
for _, part in ipairs( parts ) do
local success, conv = pcall( tonumber, part, 10 )
if success and conv ~= nil then
part = conv
end
if val[ part ] == nil then
return nil
end
val = val[ part ]
end
if val == nil or ( type( val ) == 'table' and #val == 0 ) or type( val ) == 'function' then
return nil
end
return val
end
return apiData
end
return common