Jump to content

Module:ElementAchievementHeader: Difference between revisions

// via Wikitext Extension for VSCode
// via Wikitext Extension for VSCode
 
(11 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
              
              
             -- Use getTitleAchievement function that specifically checks for type="title"
             -- CRITICAL: WE MUST PRE-LOAD THE JSON DATA WITH THE FRAME BEFORE GETTING ANY ACHIEVEMENTS
             -- IMPORTANT: The function returns achievementId, displayName, achievementId
             pcall(function()
            -- We need to directly use these return values correctly
                Achievements.loadData(frame)
            local achievementId, displayName, achievementId2 = Achievements.getTitleAchievement(pageId, frame)
                Achievements.loadTypes(frame)
            end)
              
              
             -- Include debug information
             -- Now get the title achievement
            local titleAchievement = Achievements.getTitleAchievement(pageId, frame)
           
            -- Minimal debug comment only visible in source
             local debugInfo = string.format(
             local debugInfo = string.format(
                 "<!-- Achievement Debug: cssClass='%s', displayName='%s', achievementId='%s', pageId='%s' -->",
                 "<!-- Achievement Header: pageId=%s -->",
                cssClass or "nil",
                displayName or "nil",
                achievementId or "nil",
                 pageId or "nil"
                 pageId or "nil"
             )
             )
              
              
             -- If title achievement found, create a populated row (exact match with original)
             -- Always create a row for debugging purposes
             if cssClass ~= "" and displayName ~= "" and achievementId ~= "" then
             if titleAchievement then
                 -- Exactly matching original HTML structure from LuaTemplatePerson.lua
                 -- 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
                 -- Return only debug info without creating an actual row
                 -- No achievement found: create empty phantom row
                 return debugInfo
                 return string.format(
                    '|-\n! colspan="2" class="achievement-header-phantom" | ' ..
                    '<div class="achievement-foreground-layer"></div>%s',
                    debugInfo
                )
             end
             end
         end
         end

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