Module:AchievementSystem: Difference between revisions
// via Wikitext Extension for VSCode |
// via Wikitext Extension for VSCode |
||
| Line 23: | Line 23: | ||
end | end | ||
-- | -------------------------------------------------------------------------------- | ||
-- JSON Handling | |||
-------------------------------------------------------------------------------- | |||
local json | local json | ||
local jsonLoaded = pcall(function() | local jsonLoaded = pcall(function() | ||
| Line 47: | Line 49: | ||
end | end | ||
-- | -------------------------------------------------------------------------------- | ||
-- Configuration, Default Data, and Cache | |||
-------------------------------------------------------------------------------- | |||
local ACHIEVEMENT_DATA_PAGE = 'MediaWiki:AchievementData.json' | local ACHIEVEMENT_DATA_PAGE = 'MediaWiki:AchievementData.json' | ||
local dataCache = nil | local dataCache = nil | ||
local DEFAULT_DATA = { | local DEFAULT_DATA = { | ||
schema_version = 1, | schema_version = 1, | ||
| Line 63: | Line 65: | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
-- | -- (Optional) Testing config | ||
-- Removed forced dev-role injection so it won't override normal JSON lookups. | |||
-------------------------------------------------------------------------------- | |||
local TEST_CONFIG = { | |||
enabled = true, | |||
test_page_id = "18451", | |||
test_user = "direct-test-user", | |||
debug_messages = true, | |||
force_achievements = false, -- Disabled forcing achievements | |||
type_mapping = { | |||
["jedi"] = "ach1", | |||
["champion"] = "ach2", | |||
["sponsor"] = "ach3", | |||
["ach1"] = "ach1", | |||
["ach2"] = "ach2", | |||
["ach3"] = "ach3", | |||
["title-test"] = "dev-role", | |||
["dev-role"] = "dev-role" | |||
}, | |||
test_achievements = {"title-test", "ach1", "ach2", "ach3"} | |||
} | |||
local function isTestPage(pageId) | |||
return TEST_CONFIG.enabled and tostring(pageId) == TEST_CONFIG.test_page_id | |||
end | |||
-------------------------------------------------------------------------------- | |||
-- Load achievement data from the JSON page | |||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
function Achievements.loadData() | function Achievements.loadData() | ||
mw.log("JSON-DEBUG: Starting to load achievement data") | mw.log("JSON-DEBUG: Starting to load achievement data") | ||
-- Use the request-level cache if we already loaded data once | |||
if dataCache then | if dataCache then | ||
mw.log("JSON-DEBUG: Using request-level cached data") | mw.log("JSON-DEBUG: Using request-level cached data") | ||
| Line 74: | Line 104: | ||
local success, data = pcall(function() | local success, data = pcall(function() | ||
-- | -- Try parser cache first | ||
local loadDataSuccess, cachedData = pcall(function() | local loadDataSuccess, cachedData = pcall(function() | ||
return mw.loadData('Module:AchievementSystem') | return mw.loadData('Module:AchievementSystem') | ||
| Line 83: | Line 113: | ||
return cachedData | return cachedData | ||
else | else | ||
mw.log("JSON-DEBUG: mw.loadData failed or returned empty, | mw.log("JSON-DEBUG: mw.loadData failed or returned empty, proceeding to direct page load") | ||
end | end | ||
| Line 122: | Line 152: | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
-- | -- Check if a page/user has any achievements | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
function Achievements.hasAchievements(pageId) | function Achievements.hasAchievements(pageId) | ||
| Line 139: | Line 169: | ||
end | end | ||
-- Check for | -- Check for legacy "n123" style | ||
if key:match("^%d+$") then | if key:match("^%d+$") then | ||
local alt = "n" .. key | local alt = "n" .. key | ||
| Line 147: | Line 177: | ||
end | end | ||
-- We removed the forced "true" for test pages to avoid dev-role injection | |||
return false | return false | ||
end | end | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
-- | -- Get a user-friendly name for a given achievement type | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
function Achievements.getAchievementName(achievementType) | function Achievements.getAchievementName(achievementType) | ||
| Line 174: | Line 205: | ||
return typeData.name | return typeData.name | ||
else | else | ||
mw.log("ACHIEVEMENT-NAME-WARNING: '" .. typeData.id .. "' has no name") | mw.log("ACHIEVEMENT-NAME-WARNING: '" .. typeData.id .. "' has no name; using ID") | ||
return achievementType | return achievementType | ||
end | end | ||
| Line 180: | Line 211: | ||
end | end | ||
mw.log("ACHIEVEMENT-NAME-ERROR: No achievement found with type | mw.log("ACHIEVEMENT-NAME-ERROR: No achievement found with type '" .. achievementType .. "'; using ID fallback") | ||
return achievementType | return achievementType | ||
end | end | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
-- | -- Find the top-tier achievement for the user (lowest tier number) | ||
-- | -- Return the CSS class and the readable achievement name | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
function Achievements.getTitleClass(pageId) | function Achievements.getTitleClass(pageId) | ||
| Line 242: | Line 273: | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
-- Renders | -- Renders a simple "box" with the top-tier achievement for the user | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
function Achievements.renderAchievementBox(pageId) | function Achievements.renderAchievementBox(pageId) | ||
| Line 266: | Line 297: | ||
local highestTier = 999 | local highestTier = 999 | ||
local topAch = nil | local topAch = nil | ||
for _, achievement in ipairs(userAchievements) do | for _, achievement in ipairs(userAchievements) do | ||
local achType = achievement.type | local achType = achievement.type | ||
| Line 286: | Line 318: | ||
) | ) | ||
end | end | ||
return '' | return '' | ||
end | end | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
-- | -- Simple pass-through to track pages (for future expansions) | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
function Achievements.trackPage(pageId, pageName) | function Achievements.trackPage(pageId, pageName) | ||
| Line 297: | Line 330: | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
-- | -- Retrieve a specific achievement if present, by type | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
function Achievements.getSpecificAchievement(pageId, achievementType) | function Achievements.getSpecificAchievement(pageId, achievementType) | ||
| Line 326: | Line 359: | ||
end | end | ||
-- Removed the forced injection code for test pages to avoid dev-role overrides | |||
debugLog("ACHIEVEMENT-DEBUG: No match found for achievement type: " .. achievementType) | |||
return nil | return nil | ||
end | end | ||
return Achievements | return Achievements | ||