Module:AchievementSystem: Difference between revisions

// via Wikitext Extension for VSCode
// via Wikitext Extension for VSCode
Line 614: Line 614:
     debugLog("Found title achievement: " .. achievementId .. " with name: " .. displayName)
     debugLog("Found title achievement: " .. achievementId .. " with name: " .. displayName)
     return achievementId, displayName, achievementId
     return achievementId, displayName, achievementId
end
--------------------------------------------------------------------------------
-- Simplified diagnostic function for JSON loading issues
--------------------------------------------------------------------------------
function Achievements.diagnoseJsonLoading()
    local output = {}
    table.insert(output, "Starting JSON diagnostics")
   
    -- Check MediaWiki capabilities
    if not mw.loadJsonData then
        table.insert(output, "ERROR - mw.loadJsonData not available!")
    end
   
    if not (mw.text and mw.text.jsonDecode) then
        table.insert(output, "CRITICAL ERROR - mw.text.jsonDecode not available!")
        return "JSON decoding not available"
    end
   
    -- Check page existence
    local pageTitle = mw.title.new(ACHIEVEMENT_DATA_PAGE)
    if not pageTitle or not pageTitle.exists then
        table.insert(output, "ERROR - " .. ACHIEVEMENT_DATA_PAGE .. " does not exist")
        return "JSON data page does not exist"
    end
   
    -- Check content model
    if pageTitle.contentModel and pageTitle.contentModel ~= "json" then
        table.insert(output, "ERROR - Incorrect content model: " .. pageTitle.contentModel)
        table.insert(output, "Page must be set to 'json' content model")
        return "Incorrect content model: " .. pageTitle.contentModel
    end
   
    -- Try to load JSON data
    local loadJsonSuccess, loadJsonResult = pcall(function()
        return mw.loadJsonData(ACHIEVEMENT_DATA_PAGE)
    end)
   
    if not loadJsonSuccess then
        table.insert(output, "ERROR - Failed to load JSON: " .. tostring(loadJsonResult or "unknown error"))
       
        -- Try to get raw content for further diagnosis
        local contentSuccess, content = pcall(function()
            return pageTitle:getContent()
        end)
       
        if contentSuccess and content and content ~= "" then
            -- Check for common issues
            if content:match("^<!DOCTYPE") or content:match("^<[Hh][Tt][Mm][Ll]") then
                table.insert(output, "CRITICAL ERROR - Content appears to be HTML, not JSON!")
            elseif not content:match("^%s*{") then
                table.insert(output, "ERROR - Content does not start with {")
            end
           
            -- Try direct JSON decoding as fallback
            local jsonDecodeSuccess, _ = pcall(function()
                return mw.text.jsonDecode(content)
            end)
           
            if not jsonDecodeSuccess then
                table.insert(output, "ERROR - JSON syntax error in content")
            end
        else
            table.insert(output, "ERROR - Could not read page content")
        end
       
        return "JSON loading failed"
    end
   
    -- Verify data structure
    if type(loadJsonResult) ~= "table" then
        table.insert(output, "ERROR - Loaded data is not a table")
        return "Invalid JSON structure"
    end
   
    if not loadJsonResult.achievement_types then
        table.insert(output, "ERROR - Missing achievement_types array")
        return "Missing achievement_types in JSON"
    end
   
    if not loadJsonResult.user_achievements then
        table.insert(output, "ERROR - Missing user_achievements object")
        return "Missing user_achievements in JSON"
    end
   
    -- Success
    table.insert(output, "JSON loading successful")
    table.insert(output, "Found " .. #loadJsonResult.achievement_types .. " achievement types")
   
    local userCount = 0
    for _, _ in pairs(loadJsonResult.user_achievements) do
        userCount = userCount + 1
    end
    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"
end
end


return Achievements
return Achievements