Difference between revisions of "Module:FindId"

From Idlescape Wiki
Jump to navigation Jump to search
m (Now works with names containing "-", e.g. 'Notes on Acid - II)
m (Remove '.' from names)
 
Line 2: Line 2:
  
 
local idListNames = {
 
local idListNames = {
ability = 'Module:Ability_ids/data',
+
  ability = 'Module:Ability_ids/data',
    enchantment = 'Module:Enchantment_ids/data',
+
  enchantment = 'Module:Enchantment_ids/data',
    item = 'Module:Item_ids/data',
+
  item = 'Module:Item_ids/data',
    location = 'Module:location_ids/data',
+
  location = 'Module:location_ids/data',
    monster = 'Module:Monster_ids/data',
+
  monster = 'Module:Monster_ids/data'
 
}
 
}
  
Line 12: Line 12:
  
 
function p.loadData(listType)
 
function p.loadData(listType)
local listName = idListNames[listType]
+
  local listName = idListNames[listType]
if loadedIdLists[listName] == nil then
+
  if loadedIdLists[listName] == nil then
loadedIdLists[listName] = mw.loadData(listName)
+
    loadedIdLists[listName] = mw.loadData(listName)
end
+
  end
  
return loadedIdLists[listName]
+
  return loadedIdLists[listName]
 
end
 
end
  
 
local function hyphenateName(name)
 
local function hyphenateName(name)
    local lName = name:lower()
+
  local lName = name:lower()
    return lName:gsub('^%s*(.-)%s*$', '%1'):gsub("'", ""):gsub("%-%s"or"%-", ""):gsub("%s+", "_"):gsub("['%-]", "")
+
  return lName:gsub('^%s*(.-)%s*$', '%1'):gsub("'", ""):gsub("%-%s" or "%-", ""):gsub("%s+", "_"):gsub("['%-]", ""):gsub("%.", "")
 
end
 
end
  
 
local function findId(hName, idType)
 
local function findId(hName, idType)
    local idList = p.loadData(idType)
+
  local idList = p.loadData(idType)
    local id = idList[hName]
+
  local id = idList[hName]
    if id then
+
  if id then
        return id
+
    return id
    end
+
  end
    return "id not found"
+
  return "id not found"
 
end
 
end
  
 
function p.findId(frame)
 
function p.findId(frame)
    local args = frame:getParent().args
+
  local args = frame:getParent().args
return p._item(args)
+
  return p._item(args)
 
end
 
end
  
 
function p._findId(args)
 
function p._findId(args)
    local hName = hyphenateName(args[1])
+
  local hName = hyphenateName(args[1])
    local idType = args[2]
+
  local idType = args[2]
    local id
+
  local id
  
    id = findId(hName, idType)
+
  id = findId(hName, idType)
  
    return id
+
  return id
 
end
 
end
  
 
return p
 
return p

Latest revision as of 02:23, 15 December 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("'", ""):gsub("%-%s" or "%-", ""):gsub("%s+", "_"):gsub("['%-]", ""):gsub("%.", "")
end

local function findId(hName, idType)
  local idList = p.loadData(idType)
  local id = idList[hName]
  if id then
    return id
  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