Module:AchievementSystem: Difference between revisions
No edit summary |
// via Wikitext Extension for VSCode |
||
| Line 134: | Line 134: | ||
Checks if a user has any achievements | Checks if a user has any achievements | ||
@param | @param identifier string|number The page ID or username to check | ||
@return boolean True if the user has any achievements, false otherwise | @return boolean True if the user has any achievements, false otherwise | ||
]] | ]] | ||
function Achievements.hasAchievements( | function Achievements.hasAchievements(identifier) | ||
local success, result = pcall(function() | local success, result = pcall(function() | ||
if not | if not identifier or identifier == '' then return false end | ||
local data = Achievements.loadData() | local data = Achievements.loadData() | ||
| Line 151: | Line 146: | ||
end | end | ||
local userAchievements = data.user_achievements[ | -- Handle both page IDs and usernames for backward compatibility | ||
local key = identifier | |||
if type(identifier) == 'string' and not tonumber(identifier) then | |||
-- This is a username, normalize it | |||
if not identifier:match('^User:') then | |||
key = 'User:' .. identifier | |||
end | |||
else | |||
-- This is a page ID, ensure it's treated as a string for table lookup | |||
key = tostring(identifier) | |||
end | |||
local userAchievements = data.user_achievements[key] | |||
return userAchievements and #userAchievements > 0 | return userAchievements and #userAchievements > 0 | ||
| Line 167: | Line 174: | ||
Gets the highest tier achievement for a user | Gets the highest tier achievement for a user | ||
@param | @param identifier string|number The page ID or username to check | ||
@return table|nil The achievement type data or nil if user has no achievements | @return table|nil The achievement type data or nil if user has no achievements | ||
]] | ]] | ||
function Achievements.getHighestAchievement( | function Achievements.getHighestAchievement(identifier) | ||
local success, result = pcall(function() | local success, result = pcall(function() | ||
if not | if not identifier or identifier == '' then return nil end | ||
local data = Achievements.loadData() | local data = Achievements.loadData() | ||
| Line 184: | Line 186: | ||
end | end | ||
local userAchievements = data.user_achievements[ | -- Handle both page IDs and usernames for backward compatibility | ||
local key = identifier | |||
if type(identifier) == 'string' and not tonumber(identifier) then | |||
-- This is a username, normalize it | |||
if not identifier:match('^User:') then | |||
key = 'User:' .. identifier | |||
end | |||
else | |||
-- This is a page ID, ensure it's treated as a string for table lookup | |||
key = tostring(identifier) | |||
end | |||
local userAchievements = data.user_achievements[key] | |||
if not userAchievements or #userAchievements == 0 then return nil end | if not userAchievements or #userAchievements == 0 then return nil end | ||
| Line 216: | Line 230: | ||
Gets the CSS class for the highest achievement to be applied to the template title | Gets the CSS class for the highest achievement to be applied to the template title | ||
@param | @param identifier string|number The page ID or username to check | ||
@return string The CSS class name or empty string if no achievement | @return string The CSS class name or empty string if no achievement | ||
]] | ]] | ||
function Achievements.getTitleClass( | function Achievements.getTitleClass(identifier) | ||
local success, result = pcall(function() | local success, result = pcall(function() | ||
local achievement = Achievements.getHighestAchievement( | local achievement = Achievements.getHighestAchievement(identifier) | ||
if not achievement or not achievement.id then return '' end | if not achievement or not achievement.id then return '' end | ||
| Line 238: | Line 252: | ||
Gets all achievements for a user, formatted for display | Gets all achievements for a user, formatted for display | ||
@param | @param identifier string|number The page ID or username to check | ||
@return table Array of achievement data objects for display | @return table Array of achievement data objects for display | ||
]] | ]] | ||
function Achievements.getUserAchievements( | function Achievements.getUserAchievements(identifier) | ||
local success, result = pcall(function() | local success, result = pcall(function() | ||
if not | if not identifier or identifier == '' then return {} end | ||
local data = Achievements.loadData() | local data = Achievements.loadData() | ||
| Line 255: | Line 264: | ||
end | end | ||
local userAchievements = data.user_achievements[ | -- Handle both page IDs and usernames for backward compatibility | ||
local key = identifier | |||
if type(identifier) == 'string' and not tonumber(identifier) then | |||
-- This is a username, normalize it | |||
if not identifier:match('^User:') then | |||
key = 'User:' .. identifier | |||
end | |||
else | |||
-- This is a page ID, ensure it's treated as a string for table lookup | |||
key = tostring(identifier) | |||
end | |||
local userAchievements = data.user_achievements[key] or {} | |||
local results = {} | local results = {} | ||
| Line 304: | Line 325: | ||
Renders HTML for an achievement box to display in templates | Renders HTML for an achievement box to display in templates | ||
@param | @param identifier string|number The page ID or username to render achievements for | ||
@return string HTML for the achievement box or empty string if no achievements | @return string HTML for the achievement box or empty string if no achievements | ||
]] | ]] | ||
function Achievements.renderAchievementBox( | function Achievements.renderAchievementBox(identifier) | ||
local success, result = pcall(function() | local success, result = pcall(function() | ||
local achievements = Achievements.getUserAchievements( | local achievements = Achievements.getUserAchievements(identifier) | ||
if not achievements or #achievements == 0 then return '' end | if not achievements or #achievements == 0 then return '' end | ||
| Line 344: | Line 365: | ||
for now we rely on the cache version mechanism for invalidation | for now we rely on the cache version mechanism for invalidation | ||
@param pageName string The page name | @param pageId number|string The page ID to track | ||
@param pageName string The page name (for reference) | |||
@return boolean Always returns true (for future expansion) | @return boolean Always returns true (for future expansion) | ||
]] | ]] | ||
function Achievements.trackPage(pageName) | function Achievements.trackPage(pageId, pageName) | ||
-- This function is designed to be safe by default | -- This function is designed to be safe by default | ||
return true | return true | ||