Module:AchievementSystem: Difference between revisions

// via Wikitext Extension for VSCode
// via Wikitext Extension for VSCode
Line 1: Line 1:
-- Module:AchievementSystem
-- Module:AchievementSystem
-- Loads data from MediaWiki:AchievementData.json
-- Loads data from MediaWiki:AchievementData.json and MediaWiki:AchievementList.json
-- AchievementList.json contains achievement type definitions
-- AchievementData.json contains user achievement assignments
-- All achievement styling is defined in CSS/Templates.css, not in the JSON. This module only assigns CSS classes based on achievement IDs in the format:
-- All achievement styling is defined in CSS/Templates.css, not in the JSON. This module only assigns CSS classes based on achievement IDs in the format:
-- .person-template .template-title.achievement-{id}::after {}
-- .person-template .template-title.achievement-{id}::after {}
Line 84: Line 86:


local DEFAULT_DATA = {
local DEFAULT_DATA = {
     schema_version = 1,
     schema_version = 2,
     last_updated = os.date('!%Y-%m-%dT%H:%M:%SZ'),
     last_updated = os.date('!%Y-%m-%dT%H:%M:%SZ'),
     achievement_types = {},
     achievement_types = {},
Line 92: Line 94:
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- Load achievement types from the JSON page
-- Load achievement types from the JSON page
-- @param frame - The Scribunto frame object for preprocessing
-- @return Array of achievement type definitions
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
function Achievements.loadTypes(frame)
function Achievements.loadTypes(frame)
Line 220: Line 224:
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- Load achievement data from the JSON page
-- Load achievement data from the JSON page
-- @param frame - The Scribunto frame object for preprocessing
-- @return Table containing the full achievement data
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
function Achievements.loadData(frame)
function Achievements.loadData(frame)
Line 312: Line 318:
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- Get user achievements
-- Get user achievements
-- @param pageId - The page ID to get achievements for
-- @return Array of achievement objects for the specified page
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
local userAchievementsCache = {}
function Achievements.getUserAchievements(pageId)
function Achievements.getUserAchievements(pageId)
     if not pageId or pageId == '' then
     if not pageId or pageId == '' then
         return {}
         return {}
    end
   
    -- Check cache first
    local cacheKey = tostring(pageId)
    if userAchievementsCache[cacheKey] then
        return userAchievementsCache[cacheKey]
     end
     end


Line 323: Line 339:
     end
     end


     local key = tostring(pageId)
     local key = cacheKey
     local userEntry = data.user_achievements[key]
     local userEntry = data.user_achievements[key]
      
      
     -- If found with string key, return achievements
     -- If found with string key, return achievements
     if userEntry and userEntry.achievements then
     if userEntry and userEntry.achievements then
         return ensureArray(userEntry.achievements)
         local achievements = ensureArray(userEntry.achievements)
        userAchievementsCache[cacheKey] = achievements
        return achievements
     end
     end
      
      
Line 336: Line 354:
         userEntry = data.user_achievements[numKey]
         userEntry = data.user_achievements[numKey]
         if userEntry and userEntry.achievements then
         if userEntry and userEntry.achievements then
             return ensureArray(userEntry.achievements)
             local achievements = ensureArray(userEntry.achievements)
            userAchievementsCache[cacheKey] = achievements
            return achievements
         end
         end
     end
     end
      
      
    -- Cache empty result to avoid repeated lookups
    userAchievementsCache[cacheKey] = {}
     return {}
     return {}
end
end
Line 345: Line 367:
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- Check if a page/user has any achievements
-- Check if a page/user has any achievements
-- @param pageId - The page ID to check
-- @return Boolean indicating if the page has any achievements
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
function Achievements.hasAchievements(pageId)
function Achievements.hasAchievements(pageId)
Line 357: Line 381:
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- Get a user-friendly name for a given achievement type
-- Get a user-friendly name for a given achievement type
-- @param achievementType - The achievement type ID
-- @param frame - The Scribunto frame object for preprocessing
-- @return String containing the user-friendly name
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
function Achievements.getAchievementName(achievementType, frame)
function Achievements.getAchievementName(achievementType, frame)
Line 382: Line 409:
-- Find the top-tier Title achievement for the user (lowest tier number)
-- Find the top-tier Title achievement for the user (lowest tier number)
-- Return the CSS class and the readable achievement name
-- Return the CSS class and the readable achievement name
-- @param pageId - The page ID to get the title achievement for
-- @param frame - The Scribunto frame object for preprocessing
-- @return CSS class, display name
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
function Achievements.getTitleClass(pageId, frame)
function Achievements.getTitleClass(pageId, frame)
Line 423: Line 453:
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- Renders a box with the top-tier achievement for the user
-- Renders a box with the top-tier achievement for the user
-- @param pageId - The page ID to render the achievement box for
-- @param frame - The Scribunto frame object for preprocessing
-- @return HTML string containing the achievement box
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
function Achievements.renderAchievementBox(pageId, frame)
function Achievements.renderAchievementBox(pageId, frame)
Line 475: Line 508:
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- Get page name for a given page ID
-- Get page name for a given page ID
-- @param pageId - The page ID to get the name for
-- @return String containing the page name
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
function Achievements.getPageName(pageId)
function Achievements.getPageName(pageId)
Line 508: Line 543:
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- Simple pass-through to track pages (for future expansions)
-- Simple pass-through to track pages (for future expansions)
-- @param pageId - The page ID to track
-- @param pageName - The page name
-- @return Boolean indicating success
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
function Achievements.trackPage(pageId, pageName) -- REVIEW
function Achievements.trackPage(pageId, pageName) -- REVIEW
Line 516: Line 554:
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- Retrieve a specific achievement if present, by type
-- Retrieve a specific achievement if present, by type
-- @param pageId - The page ID to get the achievement for
-- @param achievementType - The achievement type ID to look for
-- @return Achievement object or nil if not found
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
function Achievements.getSpecificAchievement(pageId, achievementType)
function Achievements.getSpecificAchievement(pageId, achievementType)
Line 536: Line 577:
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- Get achievement definition directly from JSON data
-- Get achievement definition directly from JSON data
-- @param achievementType - The achievement type ID to get the definition for
-- @param frame - The Scribunto frame object for preprocessing
-- @return Achievement type definition or nil if not found
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
function Achievements.getAchievementDefinition(achievementType, frame)
function Achievements.getAchievementDefinition(achievementType, frame)
Line 558: Line 602:
-- This specifically looks for achievements with type="title"
-- This specifically looks for achievements with type="title"
-- Return the CSS class, readable achievement name, and achievement ID (or empty strings if none found)
-- Return the CSS class, readable achievement name, and achievement ID (or empty strings if none found)
-- @param pageId - The page ID to get the title achievement for
-- @param frame - The Scribunto frame object for preprocessing
-- @return achievementId, displayName, achievementId
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
function Achievements.getTitleAchievement(pageId, frame)
function Achievements.getTitleAchievement(pageId, frame)