Anonymous

Module:Items: Difference between revisions

From Melvor Idle
1,440 bytes removed ,  27 February 2022
getItemByID/getItem/getItems: Prevent cloning of items in an attempt to conserve memory; getSpecialAttackTable: Remove unused function
(Undo revision 50703 by Auron956 (talk))
Tag: Undo
(getItemByID/getItem/getItems: Prevent cloning of items in an attempt to conserve memory; getSpecialAttackTable: Remove unused function)
Line 68: Line 68:


function p.getItemByID(ID)
function p.getItemByID(ID)
local result = Shared.clone(ItemData.Items[ID + 1])
return ItemData.Items[ID + 1]
if result ~= nil then
result.id = ID
end
return result
end
end


function p.getItem(name)
function p.getItem(name)
local result = nil
name = string.gsub(name, "%%27", "'")
name = string.gsub(name, "%%27", "'")
name = string.gsub(name, "'", "'")
name = string.gsub(name, "'", "'")
for i, item in pairs(ItemData.Items) do
for i, item in ipairs(ItemData.Items) do
local itemName = string.gsub(item.name, '#', '')
local itemName = string.gsub(item.name, '#', '')
if name == itemName then
if name == itemName then
result = Shared.clone(item)
return item
--Make sure every item has an id, and account for Lua being 1-index
result.id = i - 1
break
end
end
end
end
return result
return nil
end
end


function p.getItems(checkFunc)
function p.getItems(checkFunc)
local result = {}
local result = {}
for i, item in pairs(ItemData.Items) do
local itemCount = 0
for i, item in ipairs(ItemData.Items) do
if checkFunc(item) then
if checkFunc(item) then
local newItem = Shared.clone(item)
itemCount = itemCount + 1
newItem.id = i - 1
result[itemCount] = item
table.insert(result, newItem)
end
end
end
end
Line 250: Line 242:
local item = p.getItem(itemName)
local item = p.getItem(itemName)
if item == nil then
if item == nil then
return "ERROR: No item named "..ItemName.." exists in the data module[[Category:Pages with script errors]]"
return "ERROR: No item named "..itemName.." exists in the data module[[Category:Pages with script errors]]"
end
end
return p._getWeaponAttackType(item)
return p._getWeaponAttackType(item)
Line 425: Line 417:
end
end
table.insert(resultPart, '\r\n|}')
table.insert(resultPart, '\r\n|}')
return table.concat(resultPart)
end
function p.getSpecialAttackTable(frame)
local spAttTable = {}
for i, item in Shared.skpairs(ItemData.Items) do
if item.hasSpecialAttack then
for i, spAtt in ipairs(item.specialAttacks) do
if spAttTable[spAtt.id] == nil then spAttTable[spAtt.id] = {sortName=item.name, defn = spAtt, Icons = {}} end
table.insert(spAttTable[spAtt.id].Icons, Icons.Icon({item.name, type='item'}))
end
end
end
local resultPart = {}
table.insert(resultPart, '{|class="wikitable sortable stickyHeader"')
table.insert(resultPart, '\r\n|-class="headerRow-0"')
table.insert(resultPart, '\r\n!style="min-width:180px"|Weapon(s)!!Name!!Chance!!Effect')
for i, spAttData in Shared.skpairs(spAttTable) do
local spAtt = spAttData.defn
table.sort(spAttData.Icons, function(a, b) return a < b end)
table.insert(resultPart, '\r\n|-')
table.insert(resultPart, '\r\n|data-sort-value="'..spAttData.sortName..'"|'..table.concat(spAttData.Icons, '<br/>'))
table.insert(resultPart, '||'..spAtt.name..'||data-sort-value="'..spAtt.defaultChance..'"|'..spAtt.defaultChance..'%')
table.insert(resultPart, '||'..spAtt.description)
end
table.insert(resultPart, '\r\n|}')
return table.concat(resultPart)
return table.concat(resultPart)
end
end