Module:AchievementSystem: Difference between revisions

No edit summary
// via Wikitext Extension for VSCode
Line 134: Line 134:
Checks if a user has any achievements
Checks if a user has any achievements


@param username string The username to check (with or without "User:" prefix)
@param identifier string|number The page ID or username to check
@return boolean True if the user has any achievements, false otherwise
@return boolean True if the user has any achievements, false otherwise
]]
]]
function Achievements.hasAchievements(username)
function Achievements.hasAchievements(identifier)
     local success, result = pcall(function()
     local success, result = pcall(function()
         if not username or username == '' then return false end
         if not identifier or identifier == '' then return false end
       
        -- Normalize username (ensure it has User: prefix)
        if not username:match('^User:') then
            username = 'User:' .. username
        end
          
          
         local data = Achievements.loadData()
         local data = Achievements.loadData()
Line 151: Line 146:
         end
         end
          
          
         local userAchievements = data.user_achievements[username]
        -- Handle both page IDs and usernames for backward compatibility
        local key = identifier
        if type(identifier) == 'string' and not tonumber(identifier) then
            -- This is a username, normalize it
            if not identifier:match('^User:') then
                key = 'User:' .. identifier
            end
        else
            -- This is a page ID, ensure it's treated as a string for table lookup
            key = tostring(identifier)
        end
       
         local userAchievements = data.user_achievements[key]
          
          
         return userAchievements and #userAchievements > 0
         return userAchievements and #userAchievements > 0
Line 167: Line 174:
Gets the highest tier achievement for a user
Gets the highest tier achievement for a user


@param username string The username to check (with or without "User:" prefix)
@param identifier string|number The page ID or username to check
@return table|nil The achievement type data or nil if user has no achievements
@return table|nil The achievement type data or nil if user has no achievements
]]
]]
function Achievements.getHighestAchievement(username)
function Achievements.getHighestAchievement(identifier)
     local success, result = pcall(function()
     local success, result = pcall(function()
         if not username or username == '' then return nil end
         if not identifier or identifier == '' then return nil end
       
        -- Normalize username (ensure it has User: prefix)
        if not username:match('^User:') then
            username = 'User:' .. username
        end
          
          
         local data = Achievements.loadData()
         local data = Achievements.loadData()
Line 184: Line 186:
         end
         end
          
          
         local userAchievements = data.user_achievements[username]
        -- Handle both page IDs and usernames for backward compatibility
        local key = identifier
        if type(identifier) == 'string' and not tonumber(identifier) then
            -- This is a username, normalize it
            if not identifier:match('^User:') then
                key = 'User:' .. identifier
            end
        else
            -- This is a page ID, ensure it's treated as a string for table lookup
            key = tostring(identifier)
        end
       
         local userAchievements = data.user_achievements[key]
          
          
         if not userAchievements or #userAchievements == 0 then return nil end
         if not userAchievements or #userAchievements == 0 then return nil end
Line 216: Line 230:
Gets the CSS class for the highest achievement to be applied to the template title
Gets the CSS class for the highest achievement to be applied to the template title


@param username string The username to check
@param identifier string|number The page ID or username to check
@return string The CSS class name or empty string if no achievement
@return string The CSS class name or empty string if no achievement
]]
]]
function Achievements.getTitleClass(username)
function Achievements.getTitleClass(identifier)
     local success, result = pcall(function()
     local success, result = pcall(function()
         local achievement = Achievements.getHighestAchievement(username)
         local achievement = Achievements.getHighestAchievement(identifier)
         if not achievement or not achievement.id then return '' end
         if not achievement or not achievement.id then return '' end
          
          
Line 238: Line 252:
Gets all achievements for a user, formatted for display
Gets all achievements for a user, formatted for display


@param username string The username to check
@param identifier string|number The page ID or username to check
@return table Array of achievement data objects for display
@return table Array of achievement data objects for display
]]
]]
function Achievements.getUserAchievements(username)
function Achievements.getUserAchievements(identifier)
     local success, result = pcall(function()
     local success, result = pcall(function()
         if not username or username == '' then return {} end
         if not identifier or identifier == '' then return {} end
       
        -- Normalize username (ensure it has User: prefix)
        if not username:match('^User:') then
            username = 'User:' .. username
        end
          
          
         local data = Achievements.loadData()
         local data = Achievements.loadData()
Line 255: Line 264:
         end
         end
          
          
         local userAchievements = data.user_achievements[username] or {}
        -- Handle both page IDs and usernames for backward compatibility
        local key = identifier
        if type(identifier) == 'string' and not tonumber(identifier) then
            -- This is a username, normalize it
            if not identifier:match('^User:') then
                key = 'User:' .. identifier
            end
        else
            -- This is a page ID, ensure it's treated as a string for table lookup
            key = tostring(identifier)
        end
       
         local userAchievements = data.user_achievements[key] or {}
         local results = {}
         local results = {}
          
          
Line 304: Line 325:
Renders HTML for an achievement box to display in templates
Renders HTML for an achievement box to display in templates


@param username string The username to render achievements for
@param identifier string|number The page ID or username to render achievements for
@return string HTML for the achievement box or empty string if no achievements
@return string HTML for the achievement box or empty string if no achievements
]]
]]
function Achievements.renderAchievementBox(username)
function Achievements.renderAchievementBox(identifier)
     local success, result = pcall(function()
     local success, result = pcall(function()
         local achievements = Achievements.getUserAchievements(username)
         local achievements = Achievements.getUserAchievements(identifier)
         if not achievements or #achievements == 0 then return '' end
         if not achievements or #achievements == 0 then return '' end
          
          
Line 344: Line 365:
       for now we rely on the cache version mechanism for invalidation
       for now we rely on the cache version mechanism for invalidation


@param pageName string The page name to track
@param pageId number|string The page ID to track
@param pageName string The page name (for reference)
@return boolean Always returns true (for future expansion)
@return boolean Always returns true (for future expansion)
]]
]]
function Achievements.trackPage(pageName)
function Achievements.trackPage(pageId, pageName)
     -- This function is designed to be safe by default
     -- This function is designed to be safe by default
     return true
     return true