Module:HeroData/nav: Difference between revisions

LVL (talk | contribs)
Fix alphabetical sorting for heroes starting with "The" (e.g., The Doorman).
LVL (talk | contribs)
Improved previous change to hero sorting. Now uses the wiki's intended _sort key data structure
Line 38: Line 38:
     for hero_key, hero_data in pairs(heroes_data) do
     for hero_key, hero_data in pairs(heroes_data) do
         if hero_data["Name"] ~= nil then
         if hero_data["Name"] ~= nil then
            -- if herolabs is requested, only needs to have herolabs=true
            -- otherwise, needs to be not disabled, in development, and herolabs is nil or false
             local this_hero_status = hero_release_module.get_hero_release_status(hero_key)
             local this_hero_status = hero_release_module.get_hero_release_status(hero_key)
              
              
Line 52: Line 50:
                 end
                 end
                  
                  
                 -- ===== Create a sort key and store an object instead of a string =====
                 -- ===== FINAL, CORRECT LOGIC START =====
                 local hero_name = lang_module.get_string(hero_key, nil, 'en')
                 local hero_name = lang_module.get_string(hero_key, nil, 'en')
                 local sort_key = hero_name
                 local sort_key_to_find = hero_key .. '_sort'
                local potential_sort_key = lang_module.get_string(sort_key_to_find, nil, 'en')
                  
                  
                 -- If the name starts with "The ", make the sort key the rest of the string
                local sort_key
                 if string.sub(sort_key, 1, 4) == "The " then
                 -- string.find returns nil if the substring is not found.
                     sort_key = string.sub(sort_key, 5) -- "Doorman"
                -- This is our check to see if the translation module failed and returned the key itself.
                 if string.find(potential_sort_key, sort_key_to_find, 1, true) then
                     -- Failure case: The returned string contains the key we looked for. Fall back to the display name.
                    sort_key = hero_name
                else
                    -- Success case: The returned string is a valid translation (like "Doorman"). Use it.
                    sort_key = potential_sort_key
                 end
                 end


Line 67: Line 72:
                  
                  
                 table.insert(heroes, {sort_key = sort_key, wikitext = hero_wikitext})
                 table.insert(heroes, {sort_key = sort_key, wikitext = hero_wikitext})
                -- ===== FINAL, CORRECT LOGIC END =====
             end
             end
         end
         end
     end
     end
          
          
     -- ===== Use a custom sort function =====
     -- Use a custom sort function
     table.sort(heroes, function(a, b)
     table.sort(heroes, function(a, b)
         return a.sort_key < b.sort_key
         return a.sort_key < b.sort_key
Line 78: Line 84:
     -- Add each item to output
     -- Add each item to output
     local ret = ''
     local ret = ''
    -- ===== Access the wikitext property of the hero object =====
     for index, hero_object in ipairs(heroes) do
     for index, hero_object in ipairs(heroes) do
         ret = ret .. '{{Hero card2' .. hero_object.wikitext
         ret = ret .. '{{Hero card2' .. hero_object.wikitext