Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Please sign up or log in to edit the wiki.

Module:Navplate/Manufacturers: Difference between revisions

From the Star Citizen Wiki, the fidelity™ encyclopedia
Content deleted Content added
No edit summary
No edit summary
Line 59: Line 59:
local navplateData = {}
local navplateData = {}


-- Lua tables has no concept of order, need to iterate it manually
for _, navplateRow in common.spairs( template[ 'content' ] ) do
local i = 1
for _, navplateRow in pairs( template[ 'content' ] ) do
local label = navplateRow[ 'label' ]
local label = navplateRow[ 'label' ]
for _, result in pairs( data ) do
for _, result in pairs( data ) do

-- Match category
-- Match category
local categories = result[ 'Category' ]
local categories = result[ 'Category' ]
Line 67: Line 70:
for _, category in pairs( categories ) do
for _, category in pairs( categories ) do
if category == navplateRow[ 'conditions' ] then
if category == navplateRow[ 'conditions' ] then
if navplateData[ label ] == nil then
-- Create row if it does not exist already
navplateData[ label ] = {}
if navplateData[ i ] == nil then
navplateData[ i ] = {
label = label,
pages = {}
}
end
end
table.insert( navplateData[ label ], result[ 'page' ] )
table.insert( navplateData[ i ][ 'pages' ], result[ 'page' ] )
end
end
end
end
end
end
end

if navplateData[ i ] ~= nil then
i = i + 1
end
end
end
end
Line 93: Line 104:
local navplateData = self:buildNavplateData( data )
local navplateData = self:buildNavplateData( data )
mw.logObject( navplateData )
mw.logObject( navplateData )

if navplateData ~= nil then
if navplateData ~= nil then
for i, row in ipairs( navplateData ) do
local i = 1
args[ 'label' .. i ] = row[ 'label' ]
args[ 'list' .. i ] = table.concat( row[ 'pages' ] )
end
end
end
end
end



Revision as of 00:21, 6 February 2024

Module documentation[view][edit][history][purge]
This documentation is transcluded from Module:Navplate/Manufacturers/doc. Changes can be proposed in the talk page.

-- This is barebone but the idea is to make a generic module that
-- build any navplate from template defined from data.json
--
-- TODO:
-- - Make functions generic
-- - Support more SMW inline query conditions other than category
-- - Implement i18n
-- - Support group headers
-- - Support icon

local NavplateManufacturers = {}

local metatable = {}
local methodtable = {}

metatable.__index = methodtable

local common = require( 'Module:Common' )
local navplate = require( 'Module:Navplate' )
local template = mw.loadJsonData( 'Module:Navplate/Manufacturers/data.json' );

--- Queries the SMW Store
--- @return table
function methodtable.getSmwData( self, category )
	-- Cache multiple calls
    if self.smwData ~= nil then
        return self.smwData
    end

	local askData = {
		'[[:+]]',
		'[[' .. self.category .. ']]',
		'?#-=page',
		'?Category#'
	}

	local data = mw.smw.ask( askData )

	if data == nil or data[ 1 ] == nil then
        return nil
    end
	
	--mw.logObject( data )

	-- Init self.smwData
	if self.smwData == nil then
		self.smwData = {}
	end

	self.smwData = data

	return self.smwData
end

--- Build a table of data that represents the navplate from SMW data based on the template
---
--- @return table
function methodtable.buildNavplateData( self, data )
	local navplateData = {}

	-- Lua tables has no concept of order, need to iterate it manually
	local i = 1
	for _, navplateRow in pairs( template[ 'content' ] ) do
		local label = navplateRow[ 'label' ]
		for _, result in pairs( data ) do

			-- Match category
			local categories = result[ 'Category' ]
			if categories ~= nil and type( categories ) == 'table' then
				for _, category in pairs( categories ) do
					if category == navplateRow[ 'conditions' ] then
						-- Create row if it does not exist already
						if navplateData[ i ] == nil then
							navplateData[ i ] = {
								label = label,
								pages = {}
							}
						end
						table.insert( navplateData[ i ][ 'pages' ], result[ 'page' ] )
					end
				end
			end
		end

		if navplateData[ i ] ~= nil then
			i = i + 1
		end
	end

	return navplateData
end

--- Outputs the table
---
--- @return string
function methodtable.make( self )
	local args = {
		subtitle = 'Products of',
		title = 'Manufacturer'
	}

	local data = self:getSmwData( self.category )
	if data ~= nil then
		local navplateData = self:buildNavplateData( data )
		mw.logObject( navplateData )

		if navplateData ~= nil then
			for i, row in ipairs( navplateData ) do
				args[ 'label' .. i ] = row[ 'label' ]
				args[ 'list' .. i ] = table.concat( row[ 'pages' ] )
			end
		end
	end

	-- mw.logObject( args )

    return navplate.navplateTemplate({
		args = args
    })
end

--- New Instance
---
--- @return table NavplateManufacturers
function NavplateManufacturers.new( self, frameArgs )
    local instance = {
		category = 'Category:Greycat Industrial'
	}
    setmetatable( instance, metatable )
    return instance
end


--- "Main" entry point
---
--- @param frame table Invocation frame
--- @return string
function NavplateManufacturers.main( frame )
    local instance = NavplateManufacturers:new()

    return instance:make()
end


return NavplateManufacturers