Content deleted Content added
Alistair3149 (talk | contribs) No edit summary |
Alistair3149 (talk | contribs) No edit summary |
||
(33 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
require( 'strict' ) |
require( 'strict' ) |
||
require( 'Module:Mw.html extension' ) |
require( 'Module:Mw.html extension' ) |
||
local ItemPorts = {} |
local ItemPorts = {} |
||
Line 8: | Line 8: | ||
metatable.__index = methodtable |
metatable.__index = methodtable |
||
local MODULE_NAME = 'Module:ItemPorts' |
|||
local config = mw.loadJsonData( MODULE_NAME .. '/config.json' ) |
|||
local i18n = require( 'Module:i18n' ):new() |
|||
local TNT = require( 'Module:Translate' ):new() |
|||
--- Wrapper function for Module:i18n.translate |
|||
--- |
|||
--- @param key string The translation key |
|||
--- @return string If the key was not found, the key is returned |
|||
local function t( key ) |
|||
return i18n:translate( key ) |
|||
end |
|||
--- Wrapper function for Module:Translate.translate |
|||
--- |
|||
--- @param key string The translation key |
|||
--- @param addSuffix boolean|nil Adds a language suffix if config.smw_multilingual_text is true |
|||
--- @return string If the key was not found in the .tab page, the key is returned |
|||
local function translate( key, addSuffix, ... ) |
|||
return TNT:translate( MODULE_NAME .. '/i18n.json', config, key, addSuffix, { ... } ) or key |
|||
end |
|||
--- Utility function to format item size for display |
|||
--- TODO: Perhaps this should go into a common module |
|||
--- |
|||
--- @param size any Size(s) to be formatted |
|||
--- @return string |
|||
local function formatItemSize( size ) |
|||
-- Range |
|||
if type( size ) == 'table' then |
|||
if size[ 1 ] == size[ 2 ] or not size[ 1 ] or not size[ 2 ] then |
|||
return formatItemSize( size[ 1 ] or size[ 2 ] ) |
|||
end |
|||
return string.format( 'S%d–%d', size[ 1 ], size[ 2 ] ) |
|||
-- Numerial size |
|||
elseif type( size ) == 'number' then |
|||
return string.format( 'S%d', size ) |
|||
end |
|||
-- String size (e.g. M, XL) |
|||
return size |
|||
end |
|||
Line 15: | Line 60: | ||
--- @return table |
--- @return table |
||
local function makeSmwQueryObject( page ) |
local function makeSmwQueryObject( page ) |
||
local ignores = config.blocklist_itemport_name or {} |
|||
return { |
|||
mw.ustring.format( |
|||
local itemPortName = t( 'SMW_ItemPortName' ) |
|||
'[[-Has subobject::' .. page .. ']][[%s::+]]', |
|||
local query = { |
|||
'Item port name' |
|||
string.format( '?%s#-=name', itemPortName ), |
|||
), |
|||
string.format( '?%s#-=display_name', t( 'SMW_ItemPortDisplayName' ) ), |
|||
string.format( '?%s#-=min_size', t( 'SMW_ItemPortMinimumSize' ) ), |
|||
string.format( '?%s#-=max_size', t( 'SMW_ItemPortMaximumSize' ) ), |
|||
string.format( '?%s#-=equipped_name', t( 'SMW_EquippedItemName' ) ), |
|||
string.format( '?%s#-=equipped_size', t( 'SMW_EquippedItemSize' ) ), |
|||
string.format( '?%s#-=equipped_uuid', t( 'SMW_EquippedItemUUID' ) ), |
|||
mw.ustring.format( |
|||
string.format( 'sort=%s', itemPortName ), |
|||
'order=asc', |
|||
'Item port maximum size' |
|||
), |
|||
'order=asc,desc', |
|||
'limit=1000' |
'limit=1000' |
||
} |
} |
||
table.insert( query, 1, string.format( |
|||
'[[-Has subobject::' .. page .. ']][[%s::+]]', |
|||
itemPortName |
|||
) ) |
|||
for _, portName in ipairs( ignores ) do |
|||
table.insert( query, 2, string.format( |
|||
'[[%s::!' .. portName .. ']]', |
|||
itemPortName |
|||
) ) |
|||
end |
|||
--mw.logObject( query, '[ItemPorts] Query SMW with the following parameters' ) |
|||
return query |
|||
end |
end |
||
Line 39: | Line 97: | ||
--- @return table|nil |
--- @return table|nil |
||
function methodtable.getSmwData( self, page ) |
function methodtable.getSmwData( self, page ) |
||
--mw.logObject( self.smwData, 'cachedSmwData' ) |
|||
-- Cache multiple calls |
|||
-- Cache multiple calls |
|||
if self.smwData ~= nil then |
if self.smwData ~= nil then |
||
return self.smwData |
return self.smwData |
||
end |
end |
||
local smwData = mw.smw.ask( makeSmwQueryObject( page ) ) |
|||
if smwData == nil or smwData[ 1 ] == nil then |
if smwData == nil or smwData[ 1 ] == nil then |
||
Line 50: | Line 109: | ||
end |
end |
||
--mw.logObject( smwData, 'getSmwData' ) |
|||
self.smwData = smwData |
self.smwData = smwData |
||
return self.smwData |
return self.smwData |
||
end |
end |
||
--- Generates wikitext needed for the template |
--- Generates wikitext needed for the template |
||
--- @return string |
--- @return string |
||
function methodtable.out( self ) |
function methodtable.out( self ) |
||
local smwData = self:getSmwData( self.page ) |
|||
if smwData == nil then |
|||
local msg = string.format( t( 'message_error_no_itemports_found' ), self.page ) |
|||
return 'Error SMW data not found' |
|||
return require( 'Module:Hatnote' )._hatnote( msg, { icon = 'WikimediaUI-Error.svg' } ) |
|||
end |
|||
end |
|||
local containerHtml = mw.html.create( 'div' ):addClass( 'template-itemPorts' ) |
local containerHtml = mw.html.create( 'div' ):addClass( 'template-itemPorts' ) |
||
for _, port in ipairs( smwData ) do |
|||
local title, subtitle |
|||
-- Use display_name if it is different, so that we use the same key as the game localization |
|||
if port.min_size == port.max_size then |
|||
if port.display_name and port.display_name ~= port.name and translate( 'itemPort_' .. port.display_name ) ~= 'itemPort_' .. port.display_name then |
|||
size_text = mw.ustring.format( 'S%d', port.min_size ) |
|||
subtitle = translate( 'itemPort_' .. port.display_name ) |
|||
else |
|||
elseif port.name then |
|||
size_text = mw.ustring.format( 'S%d–%d', port.min_size, port.max_size ) |
|||
subtitle = translate( 'itemPort_' .. port.name ) |
|||
end |
|||
end |
|||
if port.equipped_name ~= nil then |
|||
-- FIXME: Add i18n for N/A |
|||
title = mw.ustring.format( '[[%s]]', port.equipped_name ) |
|||
local size_primary, size_secondary |
|||
else |
|||
local port_size = formatItemSize( { port.min_size, port.max_size } ) |
|||
title = 'No item equipped' |
|||
local equipped_size = formatItemSize( port.equipped_size ) |
|||
end |
|||
if not equipped_size then |
|||
local portHtml = mw.html.create( 'div' ):addClass( 'template-itemPort' ) |
|||
size_primary = port_size or 'N/A' |
|||
portHtml:tag( 'div' ) |
|||
elseif equipped_size == port_size then |
|||
size_primary = equipped_size |
|||
else |
|||
size_primary = equipped_size |
|||
size_secondary = port_size |
|||
end |
|||
if port.equipped_name ~= nil then |
|||
if port.equipped_name == '<= PLACEHOLDER =>' then |
|||
-- TODO: Display more specific name by getting the type of the item |
|||
title = translate( 'item_placeholder' ) |
|||
else |
|||
title = string.format( '[[%s]]', port.equipped_name ) |
|||
end |
|||
else |
|||
title = translate( 'msg_no_item_equipped' ) |
|||
end |
|||
local portHtml = mw.html.create( 'div' ):addClass( 'template-itemPort' ) |
|||
-- Size |
|||
portHtml:tag( 'div' ) |
|||
:addClass( 'template-itemPort-port' ) |
:addClass( 'template-itemPort-port' ) |
||
:IF( size_secondary ) |
|||
:tag( 'div' ) |
|||
:tag( 'div' ) |
|||
:addClass( 'template-itemPort-subtitle' ) |
|||
:wikitext( size_secondary ) |
|||
portHtml:tag( 'div' ) |
|||
:done() |
|||
:END() |
|||
:IF( size_primary ) |
|||
:tag( 'div' ) |
|||
:addClass( 'template-itemPort-title' ) |
|||
:wikitext( size_primary ) |
|||
:done() |
|||
:END() |
|||
portHtml:tag( 'div' ) |
|||
:addClass( 'template-itemPort-item' ) |
:addClass( 'template-itemPort-item' ) |
||
:addClassIf( port.equipped_name, 'template-itemPort-item--hasItem' ) |
:addClassIf( port.equipped_name, 'template-itemPort-item--hasItem' ) |
||
:tag( 'div' ) |
|||
:addClass( 'template-itemPort-subtitle' ) |
|||
:wikitext( subtitle ) |
|||
:done() |
|||
:tag( 'div' ) |
|||
:addClass( 'template-itemPort-title' ) |
|||
:wikitext( title ) |
|||
portHtml:node( containerHtml ) |
|||
end |
|||
return tostring( containerHtml ) .. mw.getCurrentFrame():extensionTag{ |
|||
name = 'templatestyles', args = { src = 'Module:ItemPorts/styles.css' } |
|||
} |
|||
end |
|||
containerHtml:node( portHtml ) |
|||
end |
|||
return mw.getCurrentFrame():extensionTag { |
|||
name = 'templatestyles', args = { src = MODULE_NAME .. '/styles.css' } |
|||
} .. tostring( containerHtml ) |
|||
end |
|||
--- New Instance |
--- New Instance |
||
Line 121: | Line 210: | ||
return instance |
return instance |
||
end |
end |
||
--- Parser call for generating the table |
--- Parser call for generating the table |
||
function ItemPorts.outputTable( frame ) |
function ItemPorts.outputTable( frame ) |
||
local args = require( 'Module:Arguments' ).getArgs( frame ) |
local args = require( 'Module:Arguments' ).getArgs( frame ) |
||
local page = args[ 1 ] or mw.title.getCurrentTitle(). |
local page = args[ 1 ] or mw.title.getCurrentTitle().text |
||
local instance = ItemPorts:new( page ) |
local instance = ItemPorts:new( page ) |
||
Line 134: | Line 222: | ||
end |
end |
||
--- For debugging use |
|||
--- |
|||
--- @param page string page name on the wiki |
|||
--- @return string |
|||
function ItemPorts.test( page ) |
|||
local instance = ItemPorts:new( page ) |
|||
local out = instance:out() |
|||
return out |
|||
end |
|||
return ItemPorts |
return ItemPorts |
Latest revision as of 19:20, 6 November 2024
This documentation is transcluded from Module:ItemPorts/doc. Changes can be proposed in the talk page.
Module:ItemPorts is shared across the Star Citizen Wikis.
This module is shared across the Star Citizen Wikis. Any changes should also be relayed to the GitHub repository.
Module:ItemPorts loads configuration from Module:ItemPorts/config.json.
This module can be configurated from the config.json subpage.
Module:ItemPorts loads messages from Module:ItemPorts/i18n.json.
This module is designed to be language-neutral. All of the messages are saved in the i18n.json subpage.
This module is unused.
This module is neither invoked by a template nor required/loaded by another module. If this is in error, make sure to add
{{Documentation}}
/{{No documentation}}
to the calling template's or parent's module documentation.Module:ItemPorts requires 6 modules.
Function list |
---|
require( 'strict' )
require( 'Module:Mw.html extension' )
local ItemPorts = {}
local metatable = {}
local methodtable = {}
metatable.__index = methodtable
local MODULE_NAME = 'Module:ItemPorts'
local config = mw.loadJsonData( MODULE_NAME .. '/config.json' )
local i18n = require( 'Module:i18n' ):new()
local TNT = require( 'Module:Translate' ):new()
--- Wrapper function for Module:i18n.translate
---
--- @param key string The translation key
--- @return string If the key was not found, the key is returned
local function t( key )
return i18n:translate( key )
end
--- Wrapper function for Module:Translate.translate
---
--- @param key string The translation key
--- @param addSuffix boolean|nil Adds a language suffix if config.smw_multilingual_text is true
--- @return string If the key was not found in the .tab page, the key is returned
local function translate( key, addSuffix, ... )
return TNT:translate( MODULE_NAME .. '/i18n.json', config, key, addSuffix, { ... } ) or key
end
--- Utility function to format item size for display
--- TODO: Perhaps this should go into a common module
---
--- @param size any Size(s) to be formatted
--- @return string
local function formatItemSize( size )
-- Range
if type( size ) == 'table' then
if size[ 1 ] == size[ 2 ] or not size[ 1 ] or not size[ 2 ] then
return formatItemSize( size[ 1 ] or size[ 2 ] )
end
return string.format( 'S%d–%d', size[ 1 ], size[ 2 ] )
-- Numerial size
elseif type( size ) == 'number' then
return string.format( 'S%d', size )
end
-- String size (e.g. M, XL)
return size
end
--- Creates the object that is used to query the SMW store
---
--- @param page string the item page containing data
--- @return table
local function makeSmwQueryObject( page )
local ignores = config.blocklist_itemport_name or {}
local itemPortName = t( 'SMW_ItemPortName' )
local query = {
string.format( '?%s#-=name', itemPortName ),
string.format( '?%s#-=display_name', t( 'SMW_ItemPortDisplayName' ) ),
string.format( '?%s#-=min_size', t( 'SMW_ItemPortMinimumSize' ) ),
string.format( '?%s#-=max_size', t( 'SMW_ItemPortMaximumSize' ) ),
string.format( '?%s#-=equipped_name', t( 'SMW_EquippedItemName' ) ),
string.format( '?%s#-=equipped_size', t( 'SMW_EquippedItemSize' ) ),
string.format( '?%s#-=equipped_uuid', t( 'SMW_EquippedItemUUID' ) ),
string.format( 'sort=%s', itemPortName ),
'order=asc',
'limit=1000'
}
table.insert( query, 1, string.format(
'[[-Has subobject::' .. page .. ']][[%s::+]]',
itemPortName
) )
for _, portName in ipairs( ignores ) do
table.insert( query, 2, string.format(
'[[%s::!' .. portName .. ']]',
itemPortName
) )
end
--mw.logObject( query, '[ItemPorts] Query SMW with the following parameters' )
return query
end
--- Queries the SMW Store
--- @return table|nil
function methodtable.getSmwData( self, page )
--mw.logObject( self.smwData, 'cachedSmwData' )
-- Cache multiple calls
if self.smwData ~= nil then
return self.smwData
end
local smwData = mw.smw.ask( makeSmwQueryObject( page ) )
if smwData == nil or smwData[ 1 ] == nil then
return nil
end
--mw.logObject( smwData, 'getSmwData' )
self.smwData = smwData
return self.smwData
end
--- Generates wikitext needed for the template
--- @return string
function methodtable.out( self )
local smwData = self:getSmwData( self.page )
if smwData == nil then
local msg = string.format( t( 'message_error_no_itemports_found' ), self.page )
return require( 'Module:Hatnote' )._hatnote( msg, { icon = 'WikimediaUI-Error.svg' } )
end
local containerHtml = mw.html.create( 'div' ):addClass( 'template-itemPorts' )
for _, port in ipairs( smwData ) do
local title, subtitle
-- Use display_name if it is different, so that we use the same key as the game localization
if port.display_name and port.display_name ~= port.name and translate( 'itemPort_' .. port.display_name ) ~= 'itemPort_' .. port.display_name then
subtitle = translate( 'itemPort_' .. port.display_name )
elseif port.name then
subtitle = translate( 'itemPort_' .. port.name )
end
-- FIXME: Add i18n for N/A
local size_primary, size_secondary
local port_size = formatItemSize( { port.min_size, port.max_size } )
local equipped_size = formatItemSize( port.equipped_size )
if not equipped_size then
size_primary = port_size or 'N/A'
elseif equipped_size == port_size then
size_primary = equipped_size
else
size_primary = equipped_size
size_secondary = port_size
end
if port.equipped_name ~= nil then
if port.equipped_name == '<= PLACEHOLDER =>' then
-- TODO: Display more specific name by getting the type of the item
title = translate( 'item_placeholder' )
else
title = string.format( '[[%s]]', port.equipped_name )
end
else
title = translate( 'msg_no_item_equipped' )
end
local portHtml = mw.html.create( 'div' ):addClass( 'template-itemPort' )
-- Size
portHtml:tag( 'div' )
:addClass( 'template-itemPort-port' )
:IF( size_secondary )
:tag( 'div' )
:addClass( 'template-itemPort-subtitle' )
:wikitext( size_secondary )
:done()
:END()
:IF( size_primary )
:tag( 'div' )
:addClass( 'template-itemPort-title' )
:wikitext( size_primary )
:done()
:END()
portHtml:tag( 'div' )
:addClass( 'template-itemPort-item' )
:addClassIf( port.equipped_name, 'template-itemPort-item--hasItem' )
:tag( 'div' )
:addClass( 'template-itemPort-subtitle' )
:wikitext( subtitle )
:done()
:tag( 'div' )
:addClass( 'template-itemPort-title' )
:wikitext( title )
containerHtml:node( portHtml )
end
return mw.getCurrentFrame():extensionTag {
name = 'templatestyles', args = { src = MODULE_NAME .. '/styles.css' }
} .. tostring( containerHtml )
end
--- New Instance
---
--- @return table ItemPorts
function ItemPorts.new( self, page )
local instance = {
page = page or nil
}
setmetatable( instance, metatable )
return instance
end
--- Parser call for generating the table
function ItemPorts.outputTable( frame )
local args = require( 'Module:Arguments' ).getArgs( frame )
local page = args[ 1 ] or mw.title.getCurrentTitle().text
local instance = ItemPorts:new( page )
local out = instance:out()
return out
end
--- For debugging use
---
--- @param page string page name on the wiki
--- @return string
function ItemPorts.test( page )
local instance = ItemPorts:new( page )
local out = instance:out()
return out
end
return ItemPorts