Module:AchievementSystem: Difference between revisions

// via Wikitext Extension for VSCode
// via Wikitext Extension for VSCode
Line 200: Line 200:
     local key = tostring(pageId)
     local key = tostring(pageId)
      
      
     -- Try string key first
     -- Try string key first with new structure
     local userAchievements = data.user_achievements[key] or {}
     local userEntry = data.user_achievements[key]
     if #userAchievements > 0 then
     if userEntry then
        return ensureArray(userAchievements)
        -- Check if it's the new structure with achievements array
        if userEntry.achievements then
            return ensureArray(userEntry.achievements)
        -- Check if it's the old structure (direct array)
        elseif type(userEntry) == "table" and #userEntry > 0 then
            return ensureArray(userEntry)
        end
     end
     end
      
      
Line 209: Line 215:
     local numKey = tonumber(key)
     local numKey = tonumber(key)
     if numKey and data.user_achievements[numKey] then
     if numKey and data.user_achievements[numKey] then
         return ensureArray(data.user_achievements[numKey])
         local userEntry = data.user_achievements[numKey]
        -- Check if it's the new structure with achievements array
        if userEntry.achievements then
            return ensureArray(userEntry.achievements)
        -- Check if it's the old structure (direct array)
        elseif type(userEntry) == "table" and #userEntry > 0 then
            return ensureArray(userEntry)
        end
     end
     end
      
      
Line 215: Line 228:
     if key:match("^%d+$") then
     if key:match("^%d+$") then
         local alt = "n" .. key
         local alt = "n" .. key
         if data.user_achievements[alt] and #data.user_achievements[alt] > 0 then
         local userEntry = data.user_achievements[alt]
            return ensureArray(data.user_achievements[alt])
        if userEntry then
            -- Check if it's the new structure with achievements array
            if userEntry.achievements then
                return ensureArray(userEntry.achievements)
            -- Check if it's the old structure (direct array)
            elseif type(userEntry) == "table" and #userEntry > 0 then
                return ensureArray(userEntry)
            end
         end
         end
     end
     end
      
      
     -- Try string comparison as last resort
     -- Try string comparison as last resort
     for userId, achievements in pairs(data.user_achievements) do
     for userId, userEntry in pairs(data.user_achievements) do
         if tostring(userId) == key then
         if tostring(userId) == key then
             return ensureArray(achievements)
             -- Check if it's the new structure with achievements array
            if userEntry.achievements then
                return ensureArray(userEntry.achievements)
            -- Check if it's the old structure (direct array)
            elseif type(userEntry) == "table" and #userEntry > 0 then
                return ensureArray(userEntry)
            end
         end
         end
     end
     end
Line 362: Line 388:
     end
     end


    return ''
end
--------------------------------------------------------------------------------
-- Get page name for a given page ID
--------------------------------------------------------------------------------
function Achievements.getPageName(pageId)
    if not pageId or pageId == '' then
        return ''
    end
   
    local data = Achievements.loadData()
    if not data or not data.user_achievements then
        return ''
    end
   
    local key = tostring(pageId)
    local userEntry = data.user_achievements[key]
   
    -- Check if it's the new structure with page_name at top level
    if userEntry and userEntry.page_name then
        return userEntry.page_name
    end
   
    -- Try numeric key if string key didn't work
    local numKey = tonumber(key)
    if numKey and data.user_achievements[numKey] and data.user_achievements[numKey].page_name then
        return data.user_achievements[numKey].page_name
    end
   
    -- Try legacy "n123" style
    if key:match("^%d+$") then
        local alt = "n" .. key
        if data.user_achievements[alt] and data.user_achievements[alt].page_name then
            return data.user_achievements[alt].page_name
        end
    end
   
    -- Try old structure where page_name might be in the first achievement
    local achievements = Achievements.getUserAchievements(pageId)
    if #achievements > 0 and achievements[1].page_name then
        return achievements[1].page_name
    end
   
     return ''
     return ''
end
end
Line 369: Line 439:
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
function Achievements.trackPage(pageId, pageName)
function Achievements.trackPage(pageId, pageName)
    -- In the future, this could update the page_name in the JSON data
     return true
     return true
end
end