Module:FindId

From Idlescape Wiki
Revision as of 08:47, 1 June 2024 by HASH (talk | contribs) (Finds id of a item, monster, ... from id list data modules)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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)
    return name:gsub("%s", "", 1):reverse():gsub("%s", "", 1):reverse():gsub("%s+", "_")
end

local function findId(hName, type)
    local IdList = p.loadData(type)
    for key, value in pairs(IdList) do
        if key == hName then
            return value
        else
            return "id not found"
        end
    end
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 type = args[2]
    local id

    id = findId(hName, type)

    return id
end

return p