Module:AchievementSystem: Difference between revisions

// via Wikitext Extension for VSCode
Tag: Manual revert
// via Wikitext Extension for VSCode
Line 10: Line 10:


-- Debug configuration
-- Debug configuration
local DEBUG_MODE = false
local DEBUG_MODE = true
local function debugLog(message)
local function debugLog(message)
     if not DEBUG_MODE then return end
     if not DEBUG_MODE then return end
Line 135: Line 135:
-- Load achievement data from the JSON page
-- Load achievement data from the JSON page
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
function Achievements.loadData()
function Achievements.loadData(frame)
     debugLog("Starting to load achievement data")
     debugLog("Starting to load achievement data")


Line 145: Line 145:


     local success, data = pcall(function()
     local success, data = pcall(function()
         -- Try using mw.loadJsonData first (preferred method)
         -- Get the JSON content using frame:preprocess if available
        if mw.loadJsonData then
        local jsonText
            debugLog("Attempting to use mw.loadJsonData for " .. ACHIEVEMENT_DATA_PAGE)
        if frame then
            debugLog("Using frame:preprocess to get JSON content")
            jsonText = frame:preprocess('{{MediaWiki:AchievementData.json}}')
        else
            -- Fallback to direct content loading if frame is not available
            debugLog("No frame available, falling back to direct content loading")
           
            -- Try using mw.loadJsonData first (preferred method)
            if mw.loadJsonData then
                debugLog("Attempting to use mw.loadJsonData for " .. ACHIEVEMENT_DATA_PAGE)
               
                local loadJsonSuccess, jsonData = pcall(function()
                    return mw.loadJsonData(ACHIEVEMENT_DATA_PAGE)
                end)
               
                if loadJsonSuccess and jsonData and type(jsonData) == 'table' then
                    debugLog("Successfully loaded data with mw.loadJsonData")
                    return jsonData
                else
                    debugLog("mw.loadJsonData failed: " .. tostring(jsonData or 'unknown error'))
                end
            end
           
            -- Direct content loading approach as fallback
            local pageTitle = mw.title.new(ACHIEVEMENT_DATA_PAGE)
            if not pageTitle or not pageTitle.exists then
                debugLog(ACHIEVEMENT_DATA_PAGE .. " does not exist")
                return DEFAULT_DATA
            end
              
              
             local loadJsonSuccess, jsonData = pcall(function()
            -- Get raw content from the wiki page
                 return mw.loadJsonData(ACHIEVEMENT_DATA_PAGE)
             local contentSuccess, content = pcall(function()
                 return pageTitle:getContent()
             end)
             end)
              
              
             if loadJsonSuccess and jsonData and type(jsonData) == 'table' then
             if contentSuccess and content and content ~= "" then
                debugLog("Successfully loaded data with mw.loadJsonData")
                debugLog("Successfully retrieved raw content, length: " .. #content)
                 return jsonData
               
                -- Remove any BOM or leading whitespace that might cause issues
                content = content:gsub("^%s+", "")
                if content:byte(1) == 239 and content:byte(2) == 187 and content:byte(3) == 191 then
                    debugLog("Removing UTF-8 BOM from content")
                    content = content:sub(4)
                end
               
                 jsonText = content
             else
             else
                 debugLog("mw.loadJsonData failed: " .. tostring(jsonData or 'unknown error'))
                 debugLog("Failed to get content: " .. tostring(content or 'unknown error'))
                return DEFAULT_DATA
             end
             end
        else
            debugLog("mw.loadJsonData not available, falling back to direct content loading")
         end
         end
          
          
         -- Direct content loading approach as fallback
         -- Try different JSON decode approaches
         local pageTitle = mw.title.new(ACHIEVEMENT_DATA_PAGE)
         if jsonText and mw.text and mw.text.jsonDecode then
        if not pageTitle or not pageTitle.exists then
             -- First try WITHOUT PRESERVE_KEYS flag (standard approach)
             debugLog(ACHIEVEMENT_DATA_PAGE .. " does not exist")
             local jsonDecodeSuccess, jsonData = pcall(function()
             return DEFAULT_DATA
                return mw.text.jsonDecode(jsonText)
        end
            end)
       
        -- Get raw content from the wiki page
        local contentSuccess, content = pcall(function()
            return pageTitle:getContent()
        end)
       
        if contentSuccess and content and content ~= "" then
            debugLog("Successfully retrieved raw content, length: " .. #content)
              
              
            -- Remove any BOM or leading whitespace that might cause issues
             if jsonDecodeSuccess and jsonData then
            content = content:gsub("^%s+", "")
                 debugLog("Successfully decoded content with standard jsonDecode")
             if content:byte(1) == 239 and content:byte(2) == 187 and content:byte(3) == 191 then
                 return jsonData
                 debugLog("Removing UTF-8 BOM from content")
                 content = content:sub(4)
             end
             end
              
              
             -- Use mw.text.jsonDecode for parsing WITHOUT PRESERVE_KEYS flag
             -- If that failed, try with JSON_TRY_FIXING flag
             if mw.text and mw.text.jsonDecode then
             jsonDecodeSuccess, jsonData = pcall(function()
                local jsonDecodeSuccess, jsonData = pcall(function()
                return mw.text.jsonDecode(jsonText, mw.text.JSON_TRY_FIXING)
                    return mw.text.jsonDecode(content)
            end)
                end)
           
               
            if jsonDecodeSuccess and jsonData then
                if jsonDecodeSuccess and jsonData then
                debugLog("Successfully decoded content with JSON_TRY_FIXING")
                    debugLog("Successfully decoded content with mw.text.jsonDecode")
                return jsonData
                    return jsonData
                else
                    debugLog("mw.text.jsonDecode failed: " .. tostring(jsonData or 'unknown error'))
                end
            else
                debugLog("mw.text.jsonDecode not available")
             end
             end
           
            debugLog("All JSON decode approaches failed")
         else
         else
             debugLog("Failed to get content: " .. tostring(content or 'unknown error'))
             debugLog("No JSON text available or mw.text.jsonDecode not available")
         end
         end