Jump to content

Module:ElementAchievementHeader: Difference between revisions

// via Wikitext Extension for VSCode
// via Wikitext Extension for VSCode
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
--[[  
--[[
* Module:ElementAchievementHeader
* Name: ElementAchievementHeader
* Renders the achievement header for person templates
* Author: Mark W. Datysgeld
* Displays title achievements with special styling
* Description: Element module that renders achievement headers for Person templates and handles all title-type achievements
*
* Notes: Creates blocks for Blueprint framework; loads achievement data using AchievementSystem; filters for title-type achievements; renders with animated backgrounds using two-layer approach for readability; includes phantom row when no achievements found; protected execution with error handling
* Integrates with Blueprint template system.
]]
]]


local p = {}
local p = {}
Line 34: Line 33:
             end
             end
              
              
             -- CRITICAL: We must pre-load the JSON data with the frame before getting any achievements
             -- CRITICAL: WE MUST PRE-LOAD THE JSON DATA WITH THE FRAME BEFORE GETTING ANY ACHIEVEMENTS
            -- This is how LuaTemplatePerson.lua ensures the JSON is loaded correctly
             pcall(function()
             pcall(function()
                 Achievements.loadData(frame)
                 Achievements.loadData(frame)
Line 42: Line 40:
              
              
             -- Now get the title achievement
             -- Now get the title achievement
             local achievementId, displayName, achievementId2 = Achievements.getTitleAchievement(pageId, frame)
             local titleAchievement = Achievements.getTitleAchievement(pageId, frame)
              
              
             -- Include more comprehensive debug information
             -- Minimal debug comment only visible in source
            local pageName = mw.title.getCurrentTitle().fullText or "[Unknown]"
           
            -- Get user achievements directly for debugging
            local userAchievements = {}
            pcall(function() userAchievements = Achievements.getUserAchievements(pageId) or {} end)
            local achievementCount = #userAchievements
           
            -- Format the debug output with more details
             local debugInfo = string.format(
             local debugInfo = string.format(
                 "<!-- Achievement Debug: Page='%s', ID='%s', Found=%d achievements, "..
                 "<!-- Achievement Header: pageId=%s -->",
                "Title Data: ID='%s', Name='%s', ID2='%s' -->",
                 pageId or "nil"
                pageName,
                 pageId or "nil",
                achievementCount,
                achievementId or "nil",
                displayName or "nil",
                achievementId2 or "nil"
             )
             )
              
              
             -- Always create a row for debugging purposes
             -- Always create a row for debugging purposes
             if achievementId ~= "" and displayName ~= "" and achievementId2 ~= "" then
             if titleAchievement then
                 -- Achievement found - create populated row with achievement data
                -- Get category link from the centralized function
                local categoryLink = Achievements.getCategoryLinks({ { type = titleAchievement.id } }, frame)
 
                 -- Achievement found: create populated row with achievement data
                 return string.format(
                 return string.format(
                     '|-\n! colspan="2" class="achievement-header %s" data-achievement-id="%s" data-achievement-name="%s" | %s%s',
                     '|-\n! colspan="2" class="achievement-header %s" data-achievement-id="%s" data-achievement-name="%s" | ' ..
                     achievementId, achievementId, displayName, displayName, debugInfo
                    '<div class="achievement-foreground-layer">%s</div>%s%s',
                     titleAchievement.id, titleAchievement.id, titleAchievement.name, titleAchievement.name, debugInfo, categoryLink
                 )
                 )
             else
             else
                 -- No achievement found - create phantom row with directly visible debug info
                 -- No achievement found: create empty phantom row
                local visibleDebugInfo = string.format(
                    "DEBUG: Page ID=%s, Found %d achievements, Title achievement=%s",
                    tostring(pageId),
                    achievementCount,
                    achievementId or "none"
                )
               
                 return string.format(
                 return string.format(
                     '|-\n! colspan="2" class="achievement-header-phantom" style="font-size:85%%; color:#777;" | %s %s',
                     '|-\n! colspan="2" class="achievement-header-phantom" | ' ..
                    visibleDebugInfo,
                    '<div class="achievement-foreground-layer"></div>%s',
                     debugInfo
                     debugInfo
                 )
                 )

Latest revision as of 02:58, 25 August 2025

Documentation for this module may be created at Module:ElementAchievementHeader/doc

--[[
* Name: ElementAchievementHeader
* Author: Mark W. Datysgeld
* Description: Element module that renders achievement headers for Person templates and handles all title-type achievements
* Notes: Creates blocks for Blueprint framework; loads achievement data using AchievementSystem; filters for title-type achievements; renders with animated backgrounds using two-layer approach for readability; includes phantom row when no achievements found; protected execution with error handling
]]

local p = {}

p.elementName = "achievementHeader"

-- Load required modules
local ErrorHandling = require('Module:ErrorHandling')
local Achievements = require('Module:AchievementSystem')

-- Default configuration
p.defaultConfig = {
    headerClass = "achievement-header"
}

-- Create block function
function p.createBlock()
    return function(template, args)
        -- Protected execution wrapper
        local function execute()
            local frame = mw.getCurrentFrame()
            
            -- Get the current page ID
            local pageId = mw.title.getCurrentTitle().id
            if not pageId then
                -- Include debug comment when no page ID available
                return "<!-- Achievement Header: No page ID available -->"
            end
            
            -- CRITICAL: WE MUST PRE-LOAD THE JSON DATA WITH THE FRAME BEFORE GETTING ANY ACHIEVEMENTS
            pcall(function()
                Achievements.loadData(frame)
                Achievements.loadTypes(frame)
            end)
            
            -- Now get the title achievement
            local titleAchievement = Achievements.getTitleAchievement(pageId, frame)
            
            -- Minimal debug comment only visible in source
            local debugInfo = string.format(
                "<!-- Achievement Header: pageId=%s -->",
                pageId or "nil"
            )
            
            -- Always create a row for debugging purposes
            if titleAchievement then
                -- Get category link from the centralized function
                local categoryLink = Achievements.getCategoryLinks({ { type = titleAchievement.id } }, frame)

                -- Achievement found: create populated row with achievement data
                return string.format(
                    '|-\n! colspan="2" class="achievement-header %s" data-achievement-id="%s" data-achievement-name="%s" | ' ..
                    '<div class="achievement-foreground-layer">%s</div>%s%s',
                    titleAchievement.id, titleAchievement.id, titleAchievement.name, titleAchievement.name, debugInfo, categoryLink
                )
            else
                -- No achievement found: create empty phantom row
                return string.format(
                    '|-\n! colspan="2" class="achievement-header-phantom" | ' ..
                    '<div class="achievement-foreground-layer"></div>%s',
                    debugInfo
                )
            end
        end
        
        -- Wrap with error handling
        if template._errorContext then
            return ErrorHandling.protect(
                template._errorContext,
                "ElementBlock_achievementHeader",
                execute,
                ""
            )
        else
            local ok, result = pcall(execute)
            return ok and result or "<!-- Achievement Header: Protected execution failed -->"
        end
    end
end

return p