Module:AchievementSystem: Difference between revisions

// via Wikitext Extension for VSCode
Tag: Manual revert
// via Wikitext Extension for VSCode
Line 523: Line 523:
     debugLog("ACHIEVEMENT-DEF: No definition found for " .. achievementType)
     debugLog("ACHIEVEMENT-DEF: No definition found for " .. achievementType)
     return nil
     return nil
end
--------------------------------------------------------------------------------
-- Find and return title achievement for the user if one exists
-- This specifically looks for achievements with type="title"
-- Return the CSS class, readable achievement name, and achievement ID (or empty strings if none found)
--------------------------------------------------------------------------------
function Achievements.getTitleAchievement(pageId)
    if not pageId or pageId == '' then
        debugLog("Empty page ID provided to getTitleAchievement")
        return '', '', ''
    end
    local data = Achievements.loadData()
    if not data or not data.user_achievements then
        debugLog("No achievement data available in getTitleAchievement")
        return '', '', ''
    end
    local key = tostring(pageId)
    debugLog("Looking up title achievements for ID: " .. key)
   
    -- Try to fetch achievements for this pageId
    local userAchievements = data.user_achievements[key] or {}
   
    -- If no achievements found under normal ID, try alternative format
    if #userAchievements == 0 and key:match("^%d+$") then
        local altKey = "n" .. key
        debugLog("No achievements under ID '" .. key .. "', trying alternative ID: '" .. altKey .. "'")
        userAchievements = data.user_achievements[altKey] or {}
    end
    -- Log found achievements for debugging
    if #userAchievements > 0 then
        debugLog("Found " .. #userAchievements .. " achievements for user " .. key)
    else
        debugLog("No achievements found for user " .. key)
        return '', '', ''
    end
    -- Build a table of achievement definitions for quick lookup
    local typeDefinitions = {}
    for _, typeData in ipairs(data.achievement_types) do
        typeDefinitions[typeData.id] = typeData
    end
    -- Find title achievements only
    local highestTier = 999
    local titleAchievement = nil
    for _, achievement in ipairs(userAchievements) do
        local achType = achievement.type
        if achType then
            local typeData = typeDefinitions[achType]
            if typeData then
                -- Only consider achievements with type="title"
                if typeData.type == "title" then
                    debugLog("Found title achievement: " .. achType)
                    local tier = typeData.tier or 999
                    if tier < highestTier then
                        highestTier = tier
                        titleAchievement = typeData
                        debugLog("New highest tier title achievement: " .. typeData.id)
                    end
                end
            else
                debugLog("No type definition found for achievement: " .. achType)
            end
        else
            debugLog("Achievement missing type property")
        end
    end
    if not titleAchievement or not titleAchievement.id then
        debugLog("No valid title achievement found for user " .. key)
        return '', '', ''
    end
    local cssClass = "achievement-" .. titleAchievement.id
    local displayName = titleAchievement.name or titleAchievement.id or "Award"
    local achievementId = titleAchievement.id
   
    debugLog("Using title achievement: " .. cssClass .. " with name: " .. displayName)
   
    return cssClass, displayName, achievementId
end
end


return Achievements
return Achievements