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 = | 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 | ||
-- | -- Debug logging disabled - do not use mw.log | ||
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 | ||
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 | ||
return "No achievements found for page ID " .. pageId | return "No achievements found for page ID " .. pageId | ||
end | end | ||
-- | -- 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 | ||
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 | ||
table.insert(output, "[" .. i .. "] " .. achType .. " (WARNING: No definition found)") | |||
end | end | ||
end | end | ||
return | return table.concat(output, "\n") | ||
end | end | ||
| Line 584: | Line 583: | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
function Achievements.diagnoseJsonLoading() | function Achievements.diagnoseJsonLoading() | ||
local output = {} | |||
table.insert(output, "Starting JSON diagnostics") | |||
-- Check MediaWiki capabilities | -- Check MediaWiki capabilities | ||
if not mw.loadJsonData then | if not mw.loadJsonData then | ||
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 | ||
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 | ||
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 | ||
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 | return "Incorrect content model: " .. pageTitle.contentModel | ||
end | end | ||
| Line 616: | Line 616: | ||
if not loadJsonSuccess then | 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 | -- 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 | ||
table.insert(output, "CRITICAL ERROR - Content appears to be HTML, not JSON!") | |||
elseif not content:match("^%s*{") then | elseif not content:match("^%s*{") then | ||
table.insert(output, "ERROR - Content does not start with {") | |||
end | end | ||
| Line 637: | Line 637: | ||
if not jsonDecodeSuccess then | if not jsonDecodeSuccess then | ||
table.insert(output, "ERROR - JSON syntax error in content") | |||
end | end | ||
else | else | ||
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 | ||
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 | ||
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 | ||
table.insert(output, "ERROR - Missing user_achievements object") | |||
return "Missing user_achievements in JSON" | return "Missing user_achievements in JSON" | ||
end | end | ||
-- Success | -- Success | ||
table.insert(output, "JSON loading successful") | |||
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 | ||
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" | ||