Module:AchievementSystem: Difference between revisions
// via Wikitext Extension for VSCode |
// via Wikitext Extension for VSCode |
||
| Line 410: | Line 410: | ||
if not userAchievements or #userAchievements == 0 then | if not userAchievements or #userAchievements == 0 then | ||
return '' | return '' | ||
end | |||
-- Build a lookup table for achievement type definitions | |||
local typeDefinitions = {} | |||
if data and data.achievement_types then | |||
for _, typeData in ipairs(data.achievement_types) do | |||
if typeData.id and typeData.name then | |||
typeDefinitions[typeData.id] = { | |||
name = typeData.name, | |||
tier = typeData.tier or 999 | |||
} | |||
end | |||
end | |||
end | end | ||
-- Look for the highest-tier achievement (lowest tier number) | |||
local highestTier = 999 | local highestTier = 999 | ||
local | local topAchType = nil | ||
for _, achievement in ipairs(userAchievements) do | for _, achievement in ipairs(userAchievements) do | ||
local achType = achievement.type | local achType = achievement.type | ||
if typeDefinitions[achType] and typeDefinitions[achType].tier < highestTier then | |||
highestTier = typeDefinitions[achType].tier | |||
topAchType = achType | |||
end | end | ||
end | end | ||
if | -- If we found an achievement, render it | ||
if topAchType and typeDefinitions[topAchType] then | |||
local achName = typeDefinitions[topAchType].name or topAchType | |||
return string.format( | return string.format( | ||
'<div class="achievement-box-simple" data-achievement-type="%s">%s</div>', | '<div class="achievement-box-simple" data-achievement-type="%s">%s</div>', | ||
topAchType, | |||
htmlEncode( | htmlEncode(achName) | ||
) | ) | ||
end | end | ||
| Line 465: | Line 477: | ||
local key = tostring(pageId) | local key = tostring(pageId) | ||
local userAchievements = data.user_achievements[key] or {} | local userAchievements = data.user_achievements[key] or {} | ||
| Line 492: | Line 486: | ||
end | end | ||
-- | -- Direct lookup for the requested achievement type | ||
for _, achievement in ipairs(userAchievements) do | for _, achievement in ipairs(userAchievements) do | ||
if achievement.type == achievementType then | if achievement.type == achievementType then | ||
debugLog("FOUND ACHIEVEMENT: " .. achievementType .. " for user " .. key) | debugLog("FOUND ACHIEVEMENT: " .. achievementType .. " for user " .. key) | ||
| Line 533: | Line 495: | ||
debugLog("ACHIEVEMENT-DEBUG: No match found for achievement type: " .. achievementType) | debugLog("ACHIEVEMENT-DEBUG: No match found for achievement type: " .. achievementType) | ||
return nil | |||
end | |||
-------------------------------------------------------------------------------- | |||
-- Get achievement definition directly from JSON data | |||
-------------------------------------------------------------------------------- | |||
function Achievements.getAchievementDefinition(achievementType) | |||
if not achievementType or achievementType == '' then | |||
debugLog("ACHIEVEMENT-DEF: Empty achievement type") | |||
return nil | |||
end | |||
local data = Achievements.loadData() | |||
if not data or not data.achievement_types then | |||
debugLog("ACHIEVEMENT-DEF: No achievement data loaded") | |||
return nil | |||
end | |||
-- Direct lookup in achievement_types array | |||
for _, typeData in ipairs(data.achievement_types) do | |||
if typeData.id == achievementType then | |||
debugLog("ACHIEVEMENT-DEF: Found definition for " .. achievementType) | |||
return typeData | |||
end | |||
end | |||
debugLog("ACHIEVEMENT-DEF: No definition found for " .. achievementType) | |||
return nil | return nil | ||
end | end | ||
return Achievements | return Achievements | ||