Module:Sandbox: Difference between revisions

test
test
Line 1: Line 1:
local p = {}
local p = {}
local heroes_data = mw.loadJsonData("Data:HeroData.json")
local items_data = mw.loadJsonData("Data:ItemData.json")


-- {{#invoke:Sandbox|get_hero_key|hero name}}
-- {{#invoke:Sandbox|get_item_key|item name}}
function p.get_hero_key(frame)
function p.get_item_key(frame)
     local hero_name_input = frame.args[1] -- Get the hero name from the template argument
     local item_name_input = frame.args[1] -- Get the item name from the template argument
     if not hero_name_input then
     if not item_name_input then
         return "Error: Hero name not specified."
         return "Error: Item name not specified."
     end
     end


     hero_name_input = hero_name_input:lower() -- For case-independent search
     item_name_input = item_name_input:lower() -- For case-independent search


     -- Go through all the characters and look for a name match.
     -- Go through all the items and look for a name match.
     for hero_key, hero_data in pairs(heroes_data) do
     for item_key, item_data in pairs(items_data) do
         if hero_data["Name"] then
         if item_data and item_data["Name"] then
             local current_hero_name = hero_data["Name"]:lower()
             local current_item_name = item_data["Name"]:lower()
             if current_hero_name == hero_name_input then
             if current_item_name == item_name_input then
                 return hero_key -- Return the key if the name matches
                 return item_key -- Return the key if the name matches
             end
             end
         end
         end
     end
     end


     return "Hero not found." -- If nothing is found
     return "Item not found." -- If nothing is found
end
end


return p
return p