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

Module:ItemVariants

From the Star Citizen Wiki, the fidelity™ encyclopedia
Revision as of 06:29, 16 March 2024 by Alistair3149 (talk | contribs)
Module documentation[view][edit][history][purge]
This documentation is transcluded from Module:ItemVariants/doc. Changes can be proposed in the talk page.

require( 'strict' )

local ItemVariants = {}

local metatable = {}
local methodtable = {}

metatable.__index = methodtable


-- TODO: Move this upstream to Common
local function tokenizeString( input )
    -- Split the input string into words (tokens)
    local tokens = {}
    for word in input:gmatch( "%S+" ) do
        table.insert( tokens, word )
    end
    return tokens
end


-- TODO: Move this upstream to Common
local function findCommonWords( str1, str2 )
    local tokens1 = tokenizeString( str1 )
    local tokens2 = tokenizeString( str2 )

    local commonWords = {}
    local wordSet2 = {}

    -- Create a set of words from the second string
    for _, word in ipairs( tokens2 ) do
        wordSet2[ word ] = true
    end

    -- Check if each word from the first string exists in the second string
    for _, word in ipairs( tokens1 ) do
        if wordSet2[ word ] then
            table.insert( commonWords, word )
        end
    end

    return commonWords
end


-- Extract baseItemName
local function getBaseItemName( smwData )
	local baseItemName = ''
	local commonWords = findCommonWords( smwData[1].name, smwData[2].name )
	for _, word in ipairs( commonWords ) do
	    baseItemName = baseItemName .. ' ' .. word
	end
	return mw.text.trim( baseItemName )
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 itemVariantName = 'Item variant name'
    return {
        mw.ustring.format(
            '[[-Has subobject::' .. page .. ']][[%s::+]]',
            itemVariantName
        ),
        mw.ustring.format( '?%s#-=name', itemVariantName ),
        mw.ustring.format( '?%s#-=uuid', 'Item variant UUID' ),
        'order=asc',
        'limit=100'
    }
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
        return 'some error message'
	end

	mw.logObject( smwData, 'getSMWData' )

	local baseItemName = getBaseItemName( smwData )

	for _, variant in ipairs( smwData ) do
		local variantName = variant.name

		if baseItemName ~= nil then
			variantName = mw.ustring.gsub( variantName, baseItemName, '' )
			variantName = mw.text.trim( variantName )
		end
		mw.log( variantName )
	end

	mw.logObject( baseItemName, 'baseItemName' )
end


--- New Instance
---
--- @return table ItemVariants
function ItemVariants.new( self, page )
    local instance = {
        page = page or nil
    }

    setmetatable( instance, metatable )

    return instance
end


--- Parser call for generating the table
function ItemVariants.outputTable( frame )
    local args = require( 'Module:Arguments' ).getArgs( frame )
    local page = args[ 1 ] or mw.title.getCurrentTitle().rootText

    local instance = ItemVariants:new( page )
    local out = instance:out()

    return out
end


--- For debugging use
---
--- @param page string page name on the wiki
--- @return string
function ItemVariants.test( page )
    local instance = ItemVariants:new( page )
    local out = instance:out()

    return out
end


return ItemVariants