Module:Starmap

From IxWiki
Jump to navigation Jump to search

Documentation for this module may be created at Module:Starmap/doc

-- Code bundled using https://github.com/ari-party/scribunto-bundler
-- Find module source code at https://github.com/ari-party/sct-module-starmap
local _bundler_load, _bundler_register = (function(superRequire)
    local loadingPlaceholder = { [{}] = true }

    local register
    local modules = {}

    local load
    local loaded = {}

    ---@param name string
    ---@param body function
    register = function(name, body)
        if not modules[name] then
            modules[name] = body
        end
    end

    ---@param name string
    ---@return any
    load = function(name)
        local loadedModule = loaded[name]

        if loadedModule then
            if loadedModule == loadingPlaceholder then
                return nil
            end
        else
            if not modules[name] then
                if not superRequire then
                    local identifier = type(name) == 'string' and '\"' .. name .. '\"' or tostring(name)
                    error('Tried to require ' .. identifier .. ', but no such module has been registered')
                else
                    return superRequire(name)
                end
            end

            loaded[name] = loadingPlaceholder
            loadedModule = modules[name]()
            loaded[name] = loadedModule
        end

        return loadedModule
    end

    return load, register
end)(require)

_bundler_register('bundler_main', function()
    ---@alias system { affiliation: table, aggregated_danger: number, aggregated_economy: number, aggregated_population: number, code: string, description: string, id: number, name: string, status: string, type: string }
---@alias object { affiliation: table, age: number, code: string, designation: string, id: number, sensor_danger: number, sensor_economy: number, sensor_population: number, size: number, star_system: table, star_system_id: number, subtype: table, type: string }

local Starmap = {
    children = _bundler_load("AZZl-DxcrE2its1ypJz3S"),
    findStructure = _bundler_load("uGA4i9cQUnC3ylf2vzpw_"),
    inSystem = _bundler_load("ix59yBhWwBwiUMIgUHzQq"),
    link = _bundler_load("PSyvLb1OB2t-JITdzI03w"),
    pathTo = _bundler_load("_NUSjZDV6f1_8IdB93Syz"),
    systemObjects = _bundler_load("qpZX-mPvFyuBP10k0UDA2")
} 

local t = _bundler_load("LCAb9XMf4ha8d-dgPCrQl")
local stringUtil = _bundler_load("AqogeVwVGxx49t3UAXbdx")

---@param frame table https://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual#Frame_object
function Starmap.main( frame )
    local args = frame:getParent().args

    local structure = Starmap.findStructure(
        args[ 2 ] or 'object',
        stringUtil.trim( args[ 1 ] )
    )

    if structure then
        local location = structure.code
        local system = nil
        if structure.star_system then system = structure.star_system.code end

        return Starmap.link( location, system )
    end

    return ''
end

function Starmap.test( type, name )
    local targetObject = Starmap.findStructure( type, name )

    if targetObject then mw.log( Starmap.pathTo( targetObject ) ) end
end

return Starmap

end)

_bundler_register('AZZl-DxcrE2its1ypJz3S', function()
    local data = mw.loadJsonData( 'Module:Starmap/starmap.json' )

--- Get the objects of a system
---@param id number The structure id
---@param isSystem boolean If the id is a system's
---@deprecated
return function ( id, isSystem )
    if id == nil then return nil end

    local objects = data.objects
    local children = {}

    local targetKey = 'parent_id'
    if isSystem == true then targetKey = 'star_system_id' end

    for _, object in ipairs( objects ) do
        if object[ targetKey ] == id then
            table.insert( children, object )
        end
    end

    return children
end

end)

_bundler_register('uGA4i9cQUnC3ylf2vzpw_', function()
    local data = mw.loadJsonData( 'Module:Starmap/starmap.json' )
local pluralize = _bundler_load("jVwjQViJY4dQ7bO5UekHp")

--- Look for a structure in starmap
--- A structure can be an astronomical anomaly
---@param structureType string The type of structure (system/object)
---@param structureIdentifier string | number The name/code/designation/id of the structure in Star Citizen
---@return nil | system | object
return function ( structureType, structureIdentifier )
    local structures = data[ pluralize( structureType ) ]
    if structures == nil then return nil end -- Invalid type

    if type( structureIdentifier ) == 'string' then
        structureIdentifier = mw.ustring.lower( structureIdentifier )
    end

    for _, structure in ipairs( structures ) do
        if
            mw.ustring.lower( structure.name or '' ) == structureIdentifier or
            mw.ustring.lower( structure.designation or '' ) == structureIdentifier or
            mw.ustring.lower( structure.code ) == structureIdentifier or
            structure.id == structureIdentifier
        then
            return structure
        end
    end

    return nil -- Not found
end

end)

_bundler_register('jVwjQViJY4dQ7bO5UekHp', function()
    local common = require( 'Module:Common' )

---@param str string
---@return string
return function ( str )
    return common.pluralize( str )
end

end)

_bundler_register('ix59yBhWwBwiUMIgUHzQq', function()
    local config = mw.loadJsonData( 'Module:Starmap/config.json' )
local t = _bundler_load("LCAb9XMf4ha8d-dgPCrQl")
local stringUtil = _bundler_load("AqogeVwVGxx49t3UAXbdx")
local findStructure = _bundler_load("uGA4i9cQUnC3ylf2vzpw_")

---@param system system
---@return string
return function ( system )
    local linkContent = config.link_overwrites[ system.code ] or
        stringUtil.removeParentheses( system.name ) .. ' system'

    return mw.ustring.format( t( 'in_system' ), '[[' .. linkContent .. ']]' )
end

end)

_bundler_register('LCAb9XMf4ha8d-dgPCrQl', function()
    local TNT = require( 'Module:Translate' ):new()
local config = mw.loadJsonData( 'Module:Starmap/config.json' )

local lang
if config.module_lang then
    lang = mw.getLanguage( config.module_lang )
else
    lang = mw.getContentLanguage()
end
local langCode = lang:getCode()

--- Wrapper function for Module:Translate.translate
---@param key string The translation key
---@param addSuffix? boolean Adds a language suffix if config.smw_multilingual_text is true
---@return string value If the key was not found in the .tab page, the key is returned
return function ( key, addSuffix, ... )
    return TNT:translate( 'Module:Starmap/i18n.json', config, key, addSuffix, { ... } ) or key
end

end)

_bundler_register('AqogeVwVGxx49t3UAXbdx', function()
    local Common = require( 'Module:Common' )

local StringUtil = {
    removeParentheses = Common.removeParentheses,
    trim = Common.trim
}

return StringUtil

end)

_bundler_register('PSyvLb1OB2t-JITdzI03w', function()
    local config = mw.loadJsonData( 'Module:Starmap/config.json' )

--- Create a starmap link from arguments
---@param locationName string Location param
---@param systemName? string System param, only added if the `location` argument is present
return function ( locationName, systemName )
    local str = config.starmap .. '?'

    if locationName then str = str .. 'location=' .. locationName end
    if locationName and systemName then str = str .. '&' .. 'system=' .. systemName end

    return str
end

end)

_bundler_register('_NUSjZDV6f1_8IdB93Syz', function()
    local config = mw.loadJsonData( 'Module:Starmap/config.json' )
local t = _bundler_load("LCAb9XMf4ha8d-dgPCrQl")
local tableUtil = _bundler_load("MmF8hN027XB_7Jv2VUvTw")
local stringUtil = _bundler_load("AqogeVwVGxx49t3UAXbdx")
local findStructure = _bundler_load("uGA4i9cQUnC3ylf2vzpw_")
local inSystem = _bundler_load("ix59yBhWwBwiUMIgUHzQq")

--- E.g.: [[Planet]], orbits [[Star]], in [[System]]
---@param target table Target structure
---@return string?
return function ( target )
    local links = {}

    local function processStructure( structure )
        if not structure then return end
        local parent = structure.parent or structure.star_system
        if not parent then return end

        local parentType = 'object'
        if tableUtil.contains( config.systems, parent.type ) then parentType = 'system' end

        parent = findStructure( parentType, parent.code )

        if parentType == 'system' then
            ---@type system
            ---@diagnostic disable-next-line: assign-type-mismatch
            parent = parent

            table.insert( links, inSystem( parent ) )
        else
            ---@type object
            ---@diagnostic disable-next-line: assign-type-mismatch
            parent = parent

            if parent.type == 'STAR' then
                local designation = stringUtil.removeParentheses( parent.designation )

                local linkContent = config.link_overwrites[ parent.code ] or
                    designation .. ' (star)|' .. designation .. ' star'

                table.insert( links, mw.ustring.format( t( 'orbits_star' ), '[[' .. linkContent .. ']]' ) )
            else
                local linkContent = config.link_overwrites[ parent.code ] or
                    -- LuaLS doesn't see the field??
                    ---@diagnostic disable-next-line: undefined-field
                    stringUtil.removeParentheses( parent.name or parent.designation )

                table.insert( links, '[[' .. linkContent .. ']]' )
            end

            processStructure( parent )
        end
    end

    processStructure( target )

    local sentence = table.concat( links, ', ' )
    sentence = mw.ustring.gsub( sentence, '^%l', mw.ustring.upper )
    return sentence
end

end)

_bundler_register('MmF8hN027XB_7Jv2VUvTw', function()
    local TableUtil = {}

--- Value is in table
---@param array table<any>
---@param target any
---@return boolean
function TableUtil.contains( array, target )
    for _, value in ipairs( array ) do
        if value == target then
            return true
        end
    end

    return false
end

return TableUtil

end)

_bundler_register('qpZX-mPvFyuBP10k0UDA2', function()
    local data = mw.loadJsonData( 'Module:Starmap/starmap.json' )
local findStructure = _bundler_load("uGA4i9cQUnC3ylf2vzpw_")

--- Get the objects of a system
---@param systemName string The type of structure (system/object)
---@deprecated
return function ( systemName )
    local system = findStructure( 'system', systemName )
    if system == nil then return nil end -- System doesn't exist

    systemName = mw.ustring.lower( systemName )

    local objects = data.objects
    local systemObjects = {}

    for _,
    ---@type object
    object in ipairs( objects ) do
        if object.star_system_id == system.id then
            table.insert( systemObjects, object )
        end
    end

    return systemObjects
end

end)

return _bundler_load('bundler_main')