Difference between revisions of "Module:FindId"

From Idlescape Wiki
Jump to navigation Jump to search
(Finds id of a item, monster, ... from id list data modules)
 
(Fixed bugs)
Line 21: Line 21:
  
 
local function hyphenateName(name)
 
local function hyphenateName(name)
     return name:gsub("%s", "", 1):reverse():gsub("%s", "", 1):reverse():gsub("%s+", "_")
+
    local lName = name:lower()
 +
     return lName:gsub('^%s*(.-)%s*$', '%1'):gsub("%s+", "_")
 
end
 
end
  
local function findId(hName, type)
+
local function findId(hName, idType)
     local IdList = p.loadData(type)
+
     local idList = p.loadData(idType)
     for key, value in pairs(IdList) do
+
     for key, value in pairs(idList) do
         if key == hName then
+
         if hName == key then
 
             return value
 
             return value
        else
 
            return "id not found"
 
 
         end
 
         end
 
     end
 
     end
 +
    return "id not found"
 
end
 
end
  
Line 42: Line 42:
 
function p._findId(args)
 
function p._findId(args)
 
     local hName = hyphenateName(args[1])
 
     local hName = hyphenateName(args[1])
     local type = args[2]
+
     local idType = args[2]
 
     local id
 
     local id
  
     id = findId(hName, type)
+
     id = findId(hName, idType)
  
 
     return id
 
     return id

Revision as of 09:36, 1 June 2024


local p = {}

local idListNames = {
	ability = 'Module:Ability_ids/data',
    enchantment = 'Module:Enchantment_ids/data',
    item = 'Module:Item_ids/data',
    location = 'Module:location_ids/data',
    monster = 'Module:Monster_ids/data',
}

local loadedIdLists = {}

function p.loadData(listType)
	local listName = idListNames[listType]
	if loadedIdLists[listName] == nil then
		loadedIdLists[listName] = mw.loadData(listName)
	end

	return loadedIdLists[listName]
end

local function hyphenateName(name)
    local lName = name:lower()
    return lName:gsub('^%s*(.-)%s*$', '%1'):gsub("%s+", "_")
end

local function findId(hName, idType)
    local idList = p.loadData(idType)
    for key, value in pairs(idList) do
        if hName == key then
            return value
        end
    end
    return "id not found"
end

function p.findId(frame)
    local args = frame:getParent().args
	return p._item(args)
end

function p._findId(args)
    local hName = hyphenateName(args[1])
    local idType = args[2]
    local id

    id = findId(hName, idType)

    return id
end

return p