Module:AchievementSystem: Difference between revisions

// via Wikitext Extension for VSCode
// via Wikitext Extension for VSCode
Line 410: Line 410:
     if not userAchievements or #userAchievements == 0 then
     if not userAchievements or #userAchievements == 0 then
         return ''
         return ''
    end
   
    -- Build a lookup table for achievement type definitions
    local typeDefinitions = {}
    if data and data.achievement_types then
        for _, typeData in ipairs(data.achievement_types) do
            if typeData.id and typeData.name then
                typeDefinitions[typeData.id] = {
                    name = typeData.name,
                    tier = typeData.tier or 999
                }
            end
        end
     end
     end


    -- Look for the highest-tier achievement (lowest tier number)
     local highestTier = 999
     local highestTier = 999
     local topAch = nil
     local topAchType = nil


     for _, achievement in ipairs(userAchievements) do
     for _, achievement in ipairs(userAchievements) do
         local achType = achievement.type
         local achType = achievement.type
         for _, typeData in ipairs(data.achievement_types) do
         if typeDefinitions[achType] and typeDefinitions[achType].tier < highestTier then
            if typeData.id == achType then
            highestTier = typeDefinitions[achType].tier
                local tier = typeData.tier or 999
            topAchType = achType
                if tier < highestTier then
                    highestTier = tier
                    topAch = typeData
                end
            end
         end
         end
     end
     end


     if topAch then
    -- If we found an achievement, render it
     if topAchType and typeDefinitions[topAchType] then
        local achName = typeDefinitions[topAchType].name or topAchType
       
         return string.format(
         return string.format(
             '<div class="achievement-box-simple" data-achievement-type="%s">%s</div>',
             '<div class="achievement-box-simple" data-achievement-type="%s">%s</div>',
             topAch.id,
             topAchType,
             htmlEncode(topAch.name or topAch.id or "")
             htmlEncode(achName)
         )
         )
     end
     end
Line 465: Line 477:


     local key = tostring(pageId)
     local key = tostring(pageId)
    debugLog("DIRECT LOOKUP: Checking for '" .. achievementType .. "' in page ID '" .. key .. "'")
   
    -- List ALL achievement IDs in the JSON
    local availableIds = {}
    for userId, userAchs in pairs(data.user_achievements) do
        table.insert(availableIds, userId)
       
        -- List all achievement types for this user
        local typesForUser = {}
        for _, ach in ipairs(userAchs) do
            table.insert(typesForUser, tostring(ach.type))
        end
       
        debugLog("User ID '" .. userId .. "' has achievement types: " .. table.concat(typesForUser, ", "))
    end
    debugLog("ALL User IDs in JSON: " .. table.concat(availableIds, ", "))
   
    -- Get user achievements
     local userAchievements = data.user_achievements[key] or {}
     local userAchievements = data.user_achievements[key] or {}
      
      
Line 492: Line 486:
     end
     end
      
      
     -- Log the achievements we found
     -- Direct lookup for the requested achievement type
    if #userAchievements > 0 then
        debugLog("Found " .. #userAchievements .. " achievements for user " .. key)
        for i, ach in ipairs(userAchievements) do
            debugLog("  Achievement " .. i .. ": type=" .. (ach.type or "nil"))
        end
    else
        debugLog("No achievements found for user " .. key)
    end
 
    -- Special handling for test page ID 18451
    if key == "18451" and achievementType == "dev-role" then
        -- Extra logging to debug this specific case
        debugLog("CRITICAL CHECK: Looking for dev-role in test page 18451!")
       
        -- First check if the user exists
        if data.user_achievements["18451"] then
            debugLog("User ID 18451 exists in JSON with " ..
                    #data.user_achievements["18451"] .. " achievements")
                   
            -- Dump all achievements for this user
            for i, ach in ipairs(data.user_achievements["18451"]) do
                debugLog("  Achievement " .. i .. ": type=" .. (ach.type or "nil"))
            end
        else
            debugLog("WARNING: User ID 18451 doesn't exist in JSON!")
        end
    end
 
    -- Find achievement by type
     for _, achievement in ipairs(userAchievements) do
     for _, achievement in ipairs(userAchievements) do
        debugLog("Checking achievement type: " .. tostring(achievement.type) ..
                " against requested: " .. tostring(achievementType))
               
         if achievement.type == achievementType then
         if achievement.type == achievementType then
             debugLog("FOUND ACHIEVEMENT: " .. achievementType .. " for user " .. key)
             debugLog("FOUND ACHIEVEMENT: " .. achievementType .. " for user " .. key)
Line 533: Line 495:


     debugLog("ACHIEVEMENT-DEBUG: No match found for achievement type: " .. achievementType)
     debugLog("ACHIEVEMENT-DEBUG: No match found for achievement type: " .. achievementType)
    return nil
end
--------------------------------------------------------------------------------
-- Get achievement definition directly from JSON data
--------------------------------------------------------------------------------
function Achievements.getAchievementDefinition(achievementType)
    if not achievementType or achievementType == '' then
        debugLog("ACHIEVEMENT-DEF: Empty achievement type")
        return nil
    end
   
    local data = Achievements.loadData()
    if not data or not data.achievement_types then
        debugLog("ACHIEVEMENT-DEF: No achievement data loaded")
        return nil
    end
   
    -- Direct lookup in achievement_types array
    for _, typeData in ipairs(data.achievement_types) do
        if typeData.id == achievementType then
            debugLog("ACHIEVEMENT-DEF: Found definition for " .. achievementType)
            return typeData
        end
    end
   
    debugLog("ACHIEVEMENT-DEF: No definition found for " .. achievementType)
     return nil
     return nil
end
end


return Achievements
return Achievements