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 = | 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 | ||
local jsonText | |||
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 | -- Get raw content from the wiki page | ||
return | local contentSuccess, content = pcall(function() | ||
return pageTitle:getContent() | |||
end) | end) | ||
if | if contentSuccess and content and content ~= "" then | ||
debugLog("Successfully retrieved raw content, length: " .. #content) | |||
-- 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(" | debugLog("Failed to get content: " .. tostring(content or 'unknown error')) | ||
return DEFAULT_DATA | |||
end | end | ||
end | end | ||
-- | -- Try different JSON decode approaches | ||
if jsonText and mw.text and mw.text.jsonDecode then | |||
-- First try WITHOUT PRESERVE_KEYS flag (standard approach) | |||
local jsonDecodeSuccess, jsonData = pcall(function() | |||
return mw.text.jsonDecode(jsonText) | |||
end) | |||
if jsonDecodeSuccess and jsonData then | |||
debugLog("Successfully decoded content with standard jsonDecode") | |||
if | return jsonData | ||
debugLog(" | |||
end | end | ||
-- | -- If that failed, try with JSON_TRY_FIXING flag | ||
jsonDecodeSuccess, jsonData = pcall(function() | |||
return mw.text.jsonDecode(jsonText, mw.text.JSON_TRY_FIXING) | |||
end) | |||
if jsonDecodeSuccess and jsonData then | |||
debugLog("Successfully decoded content with JSON_TRY_FIXING") | |||
return jsonData | |||
end | end | ||
debugLog("All JSON decode approaches failed") | |||
else | else | ||
debugLog(" | debugLog("No JSON text available or mw.text.jsonDecode not available") | ||
end | end | ||