Module:AchievementSystem: Difference between revisions

// via Wikitext Extension for VSCode
// via Wikitext Extension for VSCode
Line 10: Line 10:


-- Debug configuration
-- Debug configuration
local DEBUG_MODE = true
local DEBUG_MODE = false
local function debugLog(message)
local function debugLog(message)
     if not DEBUG_MODE then return end
     if not DEBUG_MODE then return end
     -- Only use mw.log for console visibility
     -- Debug logging disabled - do not use mw.log
    mw.log("ACHIEVEMENT: " .. message)
end
end


Line 495: Line 494:
function Achievements.debugBadgesForPage(pageId)
function Achievements.debugBadgesForPage(pageId)
     if not pageId or pageId == '' then
     if not pageId or pageId == '' then
        mw.log("ACHIEVEMENT-BADGES: No page ID provided")
         return "ERROR: No page ID provided"
         return "ERROR: No page ID provided"
     end
     end
Line 501: Line 499:
     local userAchievements = Achievements.getUserAchievements(pageId)
     local userAchievements = Achievements.getUserAchievements(pageId)
     if #userAchievements == 0 then
     if #userAchievements == 0 then
        mw.log("ACHIEVEMENT-BADGES: No achievements found for page ID " .. pageId)
         return "No achievements found for page ID " .. pageId
         return "No achievements found for page ID " .. pageId
     end
     end
    mw.log("ACHIEVEMENT-BADGES: Found " .. #userAchievements .. " achievements for page ID " .. pageId)
      
      
     -- Log each achievement to console
     -- Build output string instead of logging
    local output = {}
    table.insert(output, "Found " .. #userAchievements .. " achievements for page ID " .. pageId)
   
    -- Add each achievement to output
     for i, achievement in ipairs(userAchievements) do
     for i, achievement in ipairs(userAchievements) do
         local achType = achievement.type or "nil"
         local achType = achievement.type or "nil"
Line 513: Line 512:
          
          
         if typeDef then
         if typeDef then
             mw.log("ACHIEVEMENT-BADGES: [" .. i .. "] " .. achType ..  
             table.insert(output, "[" .. i .. "] " .. achType ..  
                   " (Name: " .. (typeDef.name or "unnamed") ..  
                   " (Name: " .. (typeDef.name or "unnamed") ..  
                   ", Type: " .. (typeDef.type or "unspecified") ..  
                   ", Type: " .. (typeDef.type or "unspecified") ..  
                   ", Tier: " .. (typeDef.tier or "none") .. ")")
                   ", Tier: " .. (typeDef.tier or "none") .. ")")
         else
         else
             mw.log("ACHIEVEMENT-BADGES: [" .. i .. "] " .. achType .. " (WARNING: No definition found)")
             table.insert(output, "[" .. i .. "] " .. achType .. " (WARNING: No definition found)")
         end
         end
     end
     end
      
      
     return "Found " .. #userAchievements .. " achievements for page ID " .. pageId
     return table.concat(output, "\n")
end
end


Line 584: Line 583:
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
function Achievements.diagnoseJsonLoading()
function Achievements.diagnoseJsonLoading()
     mw.log("ACHIEVEMENT-DIAG: Starting JSON diagnostics")
     local output = {}
    table.insert(output, "Starting JSON diagnostics")
      
      
     -- Check MediaWiki capabilities
     -- Check MediaWiki capabilities
     if not mw.loadJsonData then
     if not mw.loadJsonData then
         mw.log("ACHIEVEMENT-DIAG: ERROR - mw.loadJsonData not available!")
         table.insert(output, "ERROR - mw.loadJsonData not available!")
     end
     end
      
      
     if not (mw.text and mw.text.jsonDecode) then
     if not (mw.text and mw.text.jsonDecode) then
         mw.log("ACHIEVEMENT-DIAG: CRITICAL ERROR - mw.text.jsonDecode not available!")
         table.insert(output, "CRITICAL ERROR - mw.text.jsonDecode not available!")
         return "JSON decoding not available"
         return "JSON decoding not available"
     end
     end
Line 599: Line 599:
     local pageTitle = mw.title.new(ACHIEVEMENT_DATA_PAGE)
     local pageTitle = mw.title.new(ACHIEVEMENT_DATA_PAGE)
     if not pageTitle or not pageTitle.exists then
     if not pageTitle or not pageTitle.exists then
         mw.log("ACHIEVEMENT-DIAG: ERROR - " .. ACHIEVEMENT_DATA_PAGE .. " does not exist")
         table.insert(output, "ERROR - " .. ACHIEVEMENT_DATA_PAGE .. " does not exist")
         return "JSON data page does not exist"
         return "JSON data page does not exist"
     end
     end
Line 605: Line 605:
     -- Check content model
     -- Check content model
     if pageTitle.contentModel and pageTitle.contentModel ~= "json" then
     if pageTitle.contentModel and pageTitle.contentModel ~= "json" then
         mw.log("ACHIEVEMENT-DIAG: ERROR - Incorrect content model: " .. pageTitle.contentModel)
         table.insert(output, "ERROR - Incorrect content model: " .. pageTitle.contentModel)
         mw.log("ACHIEVEMENT-DIAG: Page must be set to 'json' content model")
         table.insert(output, "Page must be set to 'json' content model")
         return "Incorrect content model: " .. pageTitle.contentModel
         return "Incorrect content model: " .. pageTitle.contentModel
     end
     end
Line 616: Line 616:
      
      
     if not loadJsonSuccess then
     if not loadJsonSuccess then
         mw.log("ACHIEVEMENT-DIAG: ERROR - Failed to load JSON: " .. tostring(loadJsonResult or "unknown error"))
         table.insert(output, "ERROR - Failed to load JSON: " .. tostring(loadJsonResult or "unknown error"))
          
          
         -- Try to get raw content for further diagnosis
         -- Try to get raw content for further diagnosis
Line 626: Line 626:
             -- Check for common issues
             -- Check for common issues
             if content:match("^<!DOCTYPE") or content:match("^<[Hh][Tt][Mm][Ll]") then
             if content:match("^<!DOCTYPE") or content:match("^<[Hh][Tt][Mm][Ll]") then
                 mw.log("ACHIEVEMENT-DIAG: CRITICAL ERROR - Content appears to be HTML, not JSON!")
                 table.insert(output, "CRITICAL ERROR - Content appears to be HTML, not JSON!")
             elseif not content:match("^%s*{") then
             elseif not content:match("^%s*{") then
                 mw.log("ACHIEVEMENT-DIAG: ERROR - Content does not start with {")
                 table.insert(output, "ERROR - Content does not start with {")
             end
             end
              
              
Line 637: Line 637:
              
              
             if not jsonDecodeSuccess then
             if not jsonDecodeSuccess then
                 mw.log("ACHIEVEMENT-DIAG: ERROR - JSON syntax error in content")
                 table.insert(output, "ERROR - JSON syntax error in content")
             end
             end
         else
         else
             mw.log("ACHIEVEMENT-DIAG: ERROR - Could not read page content")
             table.insert(output, "ERROR - Could not read page content")
         end
         end
          
          
Line 648: Line 648:
     -- Verify data structure
     -- Verify data structure
     if type(loadJsonResult) ~= "table" then
     if type(loadJsonResult) ~= "table" then
         mw.log("ACHIEVEMENT-DIAG: ERROR - Loaded data is not a table")
         table.insert(output, "ERROR - Loaded data is not a table")
         return "Invalid JSON structure"
         return "Invalid JSON structure"
     end
     end
      
      
     if not loadJsonResult.achievement_types then
     if not loadJsonResult.achievement_types then
         mw.log("ACHIEVEMENT-DIAG: ERROR - Missing achievement_types array")
         table.insert(output, "ERROR - Missing achievement_types array")
         return "Missing achievement_types in JSON"
         return "Missing achievement_types in JSON"
     end
     end
      
      
     if not loadJsonResult.user_achievements then
     if not loadJsonResult.user_achievements then
         mw.log("ACHIEVEMENT-DIAG: ERROR - Missing user_achievements object")
         table.insert(output, "ERROR - Missing user_achievements object")
         return "Missing user_achievements in JSON"
         return "Missing user_achievements in JSON"
     end
     end
      
      
     -- Success
     -- Success
     mw.log("ACHIEVEMENT-DIAG: JSON loading successful")
     table.insert(output, "JSON loading successful")
     mw.log("ACHIEVEMENT-DIAG: Found " .. #loadJsonResult.achievement_types .. " achievement types")
     table.insert(output, "Found " .. #loadJsonResult.achievement_types .. " achievement types")
      
      
     local userCount = 0
     local userCount = 0
Line 670: Line 670:
         userCount = userCount + 1
         userCount = userCount + 1
     end
     end
     mw.log("ACHIEVEMENT-DIAG: Found achievements for " .. userCount .. " users")
     table.insert(output, "Found achievements for " .. userCount .. " users")
   
    -- For critical issues, print to terminal
    if #output > 0 then
        print("ACHIEVEMENT DIAGNOSTICS: " .. table.concat(output, " | "))
    end
      
      
     return "JSON diagnostics complete - all checks passed"
     return "JSON diagnostics complete - all checks passed"