Module:AchievementSystem: Difference between revisions

// via Wikitext Extension for VSCode
// via Wikitext Extension for VSCode
Line 1: Line 1:
-- Module:AchievementSystem
-- Module:AchievementSystem
-- Implements the ICANNWiki Achievement System. It provides functions to: 1) Load and cache achievement data from MediaWiki:AchievementData.json; 2) Retrieve achievement information for users; 3) Render achievement displays for Person templates; 4) Track pages using achievements for cache invalidation.
-- Unified achievement module that combines data loading and achievement functionality.
-- This module: 1) Loads and caches achievement data from MediaWiki:AchievementData.json;
-- 2) Retrieves achievement information for users; 3) Renders achievement displays for
-- Person templates; 4) Tracks pages using achievements for cache invalidation.


local Achievements = {}
local Achievements = {}
Line 32: Line 35:
         decode = function() return nil end
         decode = function() return nil end
     }
     }
     mw.log('Warning: Module:JSON not available, achievement features will be limited')
     debugLog('WARNING: Module:JSON not available, achievement features will be limited')
end
end


Line 67: Line 70:
         local page = mw.title.new(pageName)
         local page = mw.title.new(pageName)
         if not page or not page.exists then
         if not page or not page.exists then
            debugLog("Page does not exist: " .. pageName)
             return nil
             return nil
         end
         end
Line 72: Line 76:
         local content = page:getContent()
         local content = page:getContent()
         if not content or content == '' then
         if not content or content == '' then
            debugLog("Page exists but content is empty: " .. pageName)
             return nil
             return nil
         end
         end
Line 79: Line 84:
      
      
     if not success then
     if not success then
         mw.log('Error getting page content: ' .. (result or 'unknown error'))
         debugLog('Error getting page content: ' .. (result or 'unknown error'))
         return nil
         return nil
     end
     end
Line 92: Line 97:
      
      
     local success, data = pcall(function() return json.decode(jsonString) end)
     local success, data = pcall(function() return json.decode(jsonString) end)
     if not success or not data then
     if not success then
         mw.log('Error parsing JSON: ' .. (data or 'unknown error'))
         debugLog('Error parsing JSON: ' .. (data or 'unknown error'))
         return nil
         return nil
     end
     end
Line 101: Line 106:


--[[
--[[
Loads achievement data from MediaWiki:AchievementData.json
Loads achievement data from MediaWiki:AchievementData.json with intelligent caching
DEVELOPMENT MODE: Bypassing all caching for debugging
This combines functionality from both the old AchievementData.lua and AchievementSystem.lua


@param forceFresh boolean If true, bypasses all caching and loads directly from wiki
@return table The achievement data structure or default empty structure on failure
@return table The achievement data structure or default empty structure on failure
]]
]]
function Achievements.loadData()
function Achievements.loadData(forceFresh)
     local success, result = pcall(function()
     local success, result = pcall(function()
         -- BYPASSING ALL CACHING
         -- Check if we can use the request-level cache
         debugLog("CACHE BYPASS ENABLED: Loading directly from wiki page")
         if dataCache and not forceFresh then
            debugLog("Using request-level cached achievement data")
            return dataCache
        end
       
        -- Check if we should try to load from parser cache using mw.loadData
        if not forceFresh then
            local loadDataSuccess, cachedData = pcall(function()
                -- Use mw.loadData to get cached data if available
                return mw.loadData('Module:AchievementData')
            end)
           
            if loadDataSuccess and cachedData then
                debugLog("Using mw.loadData cached achievement data")
                dataCache = cachedData -- Update request cache
                return cachedData
            else
                debugLog("No mw.loadData cached data available or error loading it")
            end
        else
            debugLog("CACHE BYPASS ENABLED: Loading directly from wiki page")
        end
          
          
         -- Load data directly from the wiki
         -- Load data directly from the wiki
Line 119: Line 146:
             debugLog("Loaded raw JSON (" .. #content .. " bytes): " .. content:sub(1, 100) .. "...")
             debugLog("Loaded raw JSON (" .. #content .. " bytes): " .. content:sub(1, 100) .. "...")
              
              
             -- Log full JSON content for complete debugging
             -- Detailed debug logging in development mode
             debugLog("FULL RAW JSON (first 500 chars): " .. content:sub(1, 500))
             if DEBUG_MODE then
            debugLog("FULL RAW JSON (last 500 chars): " .. content:sub(-500))
                debugLog("FULL RAW JSON (first 500 chars): " .. content:sub(1, 500))
                debugLog("FULL RAW JSON (last 500 chars): " .. content:sub(-500))
            end
         else
         else
             debugLog("ERROR: Could not load content from " .. ACHIEVEMENT_DATA_PAGE)
             debugLog("ERROR: Could not load content from " .. ACHIEVEMENT_DATA_PAGE)
Line 135: Line 164:
             -- Count the achievement entries for debug purposes
             -- Count the achievement entries for debug purposes
             local count = 0
             local count = 0
             debugLog("Achievement entries:")
             if DEBUG_MODE then
            for k, v in pairs(data.user_achievements or {}) do  
                debugLog("Achievement entries:")
                count = count + 1
                for k, v in pairs(data.user_achievements or {}) do  
                debugLog("User/Page ID found: " .. k .. " with " .. #v .. " achievements")
                    count = count + 1
                    debugLog("User/Page ID found: " .. k .. " with " .. #v .. " achievements")
                end
            else
                -- Just count in production mode
                for k, v in pairs(data.user_achievements or {}) do count = count + 1 end
             end
             end
             debugLog("Loaded achievement data with " .. count .. " achievement entries")
             debugLog("Loaded achievement data with " .. count .. " entries")
         end
         end
          
          
         -- DON'T update cache variables
         -- Update request cache so we don't need to reload within this page render
         -- dataCache = data
         dataCache = data
          
          
         return data
         return data