Module:AchievementSystem: Difference between revisions

// via Wikitext Extension for VSCode
// via Wikitext Extension for VSCode
Line 102: Line 102:
--[[
--[[
Loads achievement data from MediaWiki:AchievementData.json
Loads achievement data from MediaWiki:AchievementData.json
Uses caching to avoid repeated loading within the same page render
DEVELOPMENT MODE: Bypassing all caching for debugging


@return table The achievement data structure or default empty structure on failure
@return table The achievement data structure or default empty structure on failure
Line 108: Line 108:
function Achievements.loadData()
function Achievements.loadData()
     local success, result = pcall(function()
     local success, result = pcall(function()
         -- First, check the in-module request cache
         -- BYPASSING ALL CACHING
         if dataCache then
         debugLog("CACHE BYPASS ENABLED: Loading directly from wiki page")
            debugLog("Using cached achievement data")
            return dataCache
        end
          
          
         -- Try to load from mw.loadData cache (parser cache)
         -- Load data directly from the wiki
         local cacheSuccess, cachedData = pcall(function()
        debugLog("Loading achievement data directly from " .. ACHIEVEMENT_DATA_PAGE)
            return mw.loadData('Module:AchievementData')
         local content = safeGetPageContent(ACHIEVEMENT_DATA_PAGE)
        end)
          
          
         -- If cached data was loaded successfully, use it
         -- Debug the raw JSON content
         if cacheSuccess and cachedData then
         if content then
             debugLog("Loaded achievement data from mw.loadData cache")
             debugLog("Loaded raw JSON (" .. #content .. " bytes): " .. content:sub(1, 100) .. "...")
            dataCache = cachedData
        else
             return dataCache
             debugLog("ERROR: Could not load content from " .. ACHIEVEMENT_DATA_PAGE)
         end
         end
          
          
        -- Load data directly from the wiki if cache is invalid
        debugLog("Loading achievement data directly from " .. ACHIEVEMENT_DATA_PAGE)
        local content = safeGetPageContent(ACHIEVEMENT_DATA_PAGE)
         local data = safeParseJSON(content)
         local data = safeParseJSON(content)
          
          
         -- If something went wrong, use default empty data
         -- If something went wrong, use default empty data
         if not data then
         if not data then
             debugLog("ERROR: Failed to load achievement data - using default empty structure")
             debugLog("ERROR: Failed to parse JSON data - using default empty structure")
             data = DEFAULT_DATA
             data = DEFAULT_DATA
         else
         else
             -- Count the achievement entries for debug purposes
             -- Count the achievement entries for debug purposes
             local count = 0
             local count = 0
             for k, _ in pairs(data.user_achievements or {}) do  
            debugLog("Achievement entries:")
                 count = count + 1  
             for k, v in pairs(data.user_achievements or {}) do  
                 count = count + 1
                debugLog("User/Page ID found: " .. k .. " with " .. #v .. " achievements")
             end
             end
             debugLog("Loaded achievement data with " .. count .. " achievement entries")
             debugLog("Loaded achievement data with " .. count .. " achievement entries")
         end
         end
          
          
         -- Update cache variables
         -- DON'T update cache variables
         dataCache = data
         -- dataCache = data
          
          
         return data
         return data
Line 228: Line 223:
             -- This is a page ID, ensure it's treated as a string for table lookup
             -- This is a page ID, ensure it's treated as a string for table lookup
             key = tostring(identifier)
             key = tostring(identifier)
             debugLog("Searching for Page ID: " .. key)
             debugLog("Searching for Page ID: " .. key .. " (type: " .. type(key) .. ")")
         end
         end
       
        -- Debug the user_achievements structure
        debugLog("All available user_achievements keys:")
        for k, _ in pairs(data.user_achievements or {}) do
            debugLog("  - " .. k .. " (type: " .. type(k) .. ")")
        end
       
        -- Check if our key exists directly
        local keyExists = data.user_achievements[key] ~= nil
        debugLog("Key '" .. key .. "' exists in user_achievements: " .. tostring(keyExists))
          
          
         local userAchievements = data.user_achievements[key]
         local userAchievements = data.user_achievements[key]
Line 235: Line 240:
         if not userAchievements or #userAchievements == 0 then  
         if not userAchievements or #userAchievements == 0 then  
             debugLog("No achievements found for identifier: " .. key)
             debugLog("No achievements found for identifier: " .. key)
             return nil  
              
            -- Fallback: try a numeric key if the string version didn't work
            if tonumber(key) then
                local numKey = tonumber(key)
                debugLog("Trying fallback with numeric key: " .. numKey)
                userAchievements = data.user_achievements[numKey]
               
                if userAchievements and #userAchievements > 0 then
                    debugLog("Found achievements using numeric key!")
                    key = numKey
                else
                    debugLog("No achievements found using numeric key either")
                    return nil
                end
            else
                return nil
            end
         end
         end