Module:Items: Difference between revisions

Amend logic for obtaining skill level requirements, implements _getItemLevelReqs()
mNo edit summary
(Amend logic for obtaining skill level requirements, implements _getItemLevelReqs())
Line 12: Line 12:
local Icons = require('Module:Icons')
local Icons = require('Module:Icons')
local Num = require('Module:Number')
local Num = require('Module:Number')
-- Used by p._getItemLevelReqs()
local LevelReqCache = {}


p.EasterEggs = {'Amulet of Calculated Promotion', 'Clue Chasers Insignia', '8', 'Lemon', 'Easter Egg',
p.EasterEggs = {'Amulet of Calculated Promotion', 'Clue Chasers Insignia', '8', 'Lemon', 'Easter Egg',
Line 79: Line 82:
return slot
return slot
end
end
end
end
-- Given an item, returns a table containing the item's level requirements (if any)
-- The table structure is as follows:
-- { ['normal'] = { SkillID = SkillLevel, ... }, ['abyssal'] = { SkillID = SkillLevel, ... } }
-- If optional parameter 'condense' is specified as true, any normal requirements are excluded
-- from the output if there should exist an abyssal level requirement for the same skill
function p._getItemLevelReqs(item, condense)
local condenseResult = (type(condense) == 'boolean' and condense) or false
local reqTypeMap = {
['SkillLevel'] = 'normal',
['AbyssalLevel'] = 'abyssal'
}
local levelReqs = LevelReqCache[item.id]
if levelReqs == nil then
-- Item not in cache - obtain level requirements data
if item.equipRequirements == nil then
return nil
end
levelReqs = { ['normal'] = {}, ['abyssal'] = {} }
for _, requirement in ipairs(item.equipRequirements) do
local reqTypeKey = reqTypeMap[requirement.type]
if reqTypeKey ~= nil then
-- Requirement is a level requirement
local reqSkillID, reqLevel = requirement.skillID, requirement.level
local reqCurrentLevel = levelReqs[reqTypeKey][reqSkillID]
if reqCurrentLevel == nil or reqLevel > reqCurrentLevel then
levelReqs[reqTypeKey][reqSkillID] = reqLevel
end
end
end
-- Cache level requirements data for any subsequent lookups
LevelReqCache[item.id] = levelReqs
end
if not condenseResult then
-- Return requirements as is
return levelReqs
else
-- Exclude any normal level requirements should an abyssal requirement exist for
-- the same skill
local levelReqsCondensed = { ['normal'] = {}, ['abyssal'] = levelReqs.abyssal }
for skillID, reqLevel in pairs(levelReqs.normal) do
if levelReqs.abyssal[skillID] == nil then
levelReqsCondensed.normal[skillID] = reqLevel
end
end
return levelReqsCondensed
end
end
end
end
Line 97: Line 149:
result = false
result = false
end
end
elseif string.find(StatName, '^(.+)LevelRequired$') ~= nil and item.equipRequirements ~= nil then
elseif (
local skillName = Shared.titleCase(string.match(StatName, '^(.+)LevelRequired$'))
string.find(StatName, '^(.+)LevelRequired$') ~= nil
if skillName ~= nil then
or string.find(StatName, '^(.+)AbyssalLevel$') ~= nil
local skillID = Constants.getSkillID(skillName)
) and item.equipRequirements ~= nil then
if skillID ~= nil then
local levelType = 'normal'
for i, requirement in ipairs(item.equipRequirements) do
local skillName = string.match(StatName, '^(.+)LevelRequired$')
if requirement.type == "SkillLevel" and requirement.skillID == skillID then
if skillName == nil then
result = requirement.level
levelType = 'abyssal'
break
skillName = string.match(StatName, '^(.+)AbyssalLevel$')
end
end
end
end
end
elseif string.find(StatName, '^(.+)AbyssalLevel$') ~= nil and item.equipRequirements ~= nil then
local skillName = Shared.titleCase(string.match(StatName, '^(.+)AbyssalLevel$'))
if skillName ~= nil then
if skillName ~= nil then
local skillID = Constants.getSkillID(skillName)
local skillID = Constants.getSkillID(Shared.titleCase(skillName))
if skillID ~= nil then
if skillID ~= nil then
for i, requirement in ipairs(item.equipRequirements) do
local levelReqs = p._getItemLevelReqs(item)
if requirement.type == 'AbyssalLevel' and requirement.skillID == skillID then
result = levelReqs ~= nil and levelReqs[levelType][skillID]
result = requirement.level
break
end
end
end
end
end
end
Line 286: Line 329:
function p._hasLevelRequirements(item)
function p._hasLevelRequirements(item)
--Function true if an item has at least one level requirement to equip
--Function true if an item has at least one level requirement to equip
if item.equipRequirements ~= nil then
local levelReqs = p._getItemLevelReqs(item)
for idx, requirement in ipairs(item.equipRequirements) do
if levelReqs ~= nil then
if requirement.type == 'SkillLevel' and requirement.level > 1 then
for levelType, reqs in pairs(levelReqs) do
if not Shared.tableIsEmpty(reqs) then
return true
return true
end
end